Skip to content

Commit 76aaae9

Browse files
authored
feat: Add Global Default Termination Grace Period Variable (#2088)
1 parent af34958 commit 76aaae9

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

pkg/controllers/provisioning/scheduling/nodeclaimtemplate.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ import (
3030
"sigs.k8s.io/karpenter/pkg/scheduling"
3131
)
3232

33+
// DefaultTerminationGracePeriod is used as runtime defaulting for TerminationGracePeriod on the NodeClaim
34+
// This would be a mechanism to allow cloud providers to enforce a TerminationGracePeriod on all node
35+
// provisioned by Karpenter
36+
var DefaultTerminationGracePeriod *metav1.Duration = nil
37+
3338
// MaxInstanceTypes is a constant that restricts the number of instance types to be sent for launch. Note that this
3439
// is intentionally changed to var just to help in testing the code.
3540
var MaxInstanceTypes = 60
@@ -93,5 +98,9 @@ func (i *NodeClaimTemplate) ToNodeClaim() *v1.NodeClaim {
9398
Spec: i.Spec,
9499
}
95100
nc.Spec.Requirements = i.Requirements.NodeSelectorRequirements()
101+
if nc.Spec.TerminationGracePeriod == nil {
102+
nc.Spec.TerminationGracePeriod = DefaultTerminationGracePeriod
103+
}
104+
96105
return nc
97106
}

pkg/controllers/provisioning/suite_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ var _ = BeforeEach(func() {
9898
}
9999
fakeClock.SetTime(time.Now())
100100
state.PodSchedulingDecisionSeconds.Reset()
101+
pscheduling.DefaultTerminationGracePeriod = nil
101102
})
102103

103104
var _ = AfterSuite(func() {
@@ -220,6 +221,42 @@ var _ = Describe("Provisioning", func() {
220221
Expect(len(nodes.Items)).To(Equal(1))
221222
ExpectScheduled(ctx, env.Client, pod)
222223
})
224+
It("should expect nodeclaim terminationGracePeriod to be the global value when the nodepool terminationGracePeriod is not set", func() {
225+
nodePool := test.NodePool()
226+
pscheduling.DefaultTerminationGracePeriod = &metav1.Duration{Duration: 98 * time.Hour}
227+
ExpectApplied(ctx, env.Client, nodePool)
228+
pod := test.UnschedulablePod()
229+
ExpectProvisioned(ctx, env.Client, cluster, cloudProvider, prov, pod)
230+
ExpectScheduled(ctx, env.Client, pod)
231+
nodeClaims := &v1.NodeClaimList{}
232+
Expect(env.Client.List(ctx, nodeClaims)).To(Succeed())
233+
Expect(len(nodeClaims.Items)).To(Equal(1))
234+
Expect(nodeClaims.Items[0].Spec.TerminationGracePeriod.Duration).To(BeNumerically("==", 98*time.Hour))
235+
})
236+
It("should expect nodeclaim terminationGracePeriod to be the nil when the nodepool terminationGracePeriod and global terminationGracePeriod is not set", func() {
237+
nodePool := test.NodePool()
238+
ExpectApplied(ctx, env.Client, nodePool)
239+
pod := test.UnschedulablePod()
240+
ExpectProvisioned(ctx, env.Client, cluster, cloudProvider, prov, pod)
241+
ExpectScheduled(ctx, env.Client, pod)
242+
nodeClaims := &v1.NodeClaimList{}
243+
Expect(env.Client.List(ctx, nodeClaims)).To(Succeed())
244+
Expect(len(nodeClaims.Items)).To(Equal(1))
245+
Expect(nodeClaims.Items[0].Spec.TerminationGracePeriod).To(BeNil())
246+
})
247+
It("should respect terminationGracePeriod value set on the nodePool over global terminationGracePeriod", func() {
248+
nodePool := test.NodePool()
249+
nodePool.Spec.Template.Spec.TerminationGracePeriod = &metav1.Duration{Duration: 223 * time.Hour}
250+
pscheduling.DefaultTerminationGracePeriod = &metav1.Duration{Duration: 47 * time.Hour}
251+
ExpectApplied(ctx, env.Client, nodePool)
252+
pod := test.UnschedulablePod()
253+
ExpectProvisioned(ctx, env.Client, cluster, cloudProvider, prov, pod)
254+
ExpectScheduled(ctx, env.Client, pod)
255+
nodeClaims := &v1.NodeClaimList{}
256+
Expect(env.Client.List(ctx, nodeClaims)).To(Succeed())
257+
Expect(len(nodeClaims.Items)).To(Equal(1))
258+
Expect(nodeClaims.Items[0].Spec.TerminationGracePeriod.Duration).To(BeNumerically("==", 223*time.Hour))
259+
})
223260
It("should ignore NodePools that are deleting", func() {
224261
nodePool := test.NodePool()
225262
ExpectApplied(ctx, env.Client, nodePool)

0 commit comments

Comments
 (0)