Skip to content

Commit

Permalink
cherry-pick: don't include terminated nodes in budget (#1735) (#1736)
Browse files Browse the repository at this point in the history
  • Loading branch information
njtran authored Oct 3, 2024
1 parent 65da714 commit 7b24b44
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pkg/controllers/disruption/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ func BuildDisruptionBudgets(ctx context.Context, cluster *state.Cluster, clk clo
continue
}

// Additionally, don't consider nodeclaims that have the terminating condition. A nodeclaim should have
// the Terminating condition only when the node is drained and cloudprovider.Delete() was successful
// on the underlying cloud provider machine.
if node.NodeClaim.StatusConditions().Get(v1.ConditionTypeInstanceTerminating).IsTrue() {
continue
}

nodePool := node.Labels()[v1.NodePoolLabelKey]
numNodes[nodePool]++

Expand Down
32 changes: 32 additions & 0 deletions pkg/controllers/disruption/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,38 @@ var _ = Describe("BuildDisruptionBudgetMapping", func() {
Expect(budgets[nodePool.Name][reason]).To(Equal(10))
}
})
It("should not consider nodes that have the terminating status condition as part of disruption count", func() {
nodePool.Spec.Disruption.Budgets = []v1.Budget{{Nodes: "100%"}}
ExpectApplied(ctx, env.Client, nodePool)
nodeClaim, node := test.NodeClaimAndNode(v1.NodeClaim{
ObjectMeta: metav1.ObjectMeta{
Finalizers: []string{"karpenter.sh/test-finalizer"},
Labels: map[string]string{
v1.NodePoolLabelKey: nodePool.Name,
corev1.LabelInstanceTypeStable: mostExpensiveInstance.Name,
v1.CapacityTypeLabelKey: mostExpensiveOffering.Requirements.Get(v1.CapacityTypeLabelKey).Any(),
corev1.LabelTopologyZone: mostExpensiveOffering.Requirements.Get(corev1.LabelTopologyZone).Any(),
},
},
Status: v1.NodeClaimStatus{
Allocatable: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceCPU: resource.MustParse("32"),
corev1.ResourcePods: resource.MustParse("100"),
},
},
})
nodeClaim.StatusConditions().SetTrue(v1.ConditionTypeInstanceTerminating)
ExpectApplied(ctx, env.Client, nodeClaim, node)
ExpectReconcileSucceeded(ctx, nodeStateController, client.ObjectKeyFromObject(node))
ExpectReconcileSucceeded(ctx, nodeClaimStateController, client.ObjectKeyFromObject(nodeClaim))

budgets, err := disruption.BuildDisruptionBudgets(ctx, cluster, fakeClock, env.Client, recorder)
Expect(err).To(Succeed())
// This should not bring in the terminating node.
for _, reason := range v1.WellKnownDisruptionReasons {
Expect(budgets[nodePool.Name][reason]).To(Equal(10))
}
})
It("should not return a negative disruption value", func() {
nodePool.Spec.Disruption.Budgets = []v1.Budget{{Nodes: "10%"}}
ExpectApplied(ctx, env.Client, nodePool)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/state/statenode.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,10 @@ func (in *StateNode) PodLimits() corev1.ResourceList {
func (in *StateNode) MarkedForDeletion() bool {
// The Node is marked for deletion if:
// 1. The Node has MarkedForDeletion set
// 2. The Node has a NodeClaim counterpart and is actively deleting
// 2. The Node has a NodeClaim counterpart and is actively deleting (or the nodeclaim is marked as terminating)
// 3. The Node has no NodeClaim counterpart and is actively deleting
return in.markedForDeletion ||
(in.NodeClaim != nil && !in.NodeClaim.DeletionTimestamp.IsZero()) ||
(in.NodeClaim != nil && (!in.NodeClaim.DeletionTimestamp.IsZero() || in.NodeClaim.StatusConditions().Get(v1.ConditionTypeInstanceTerminating).IsTrue())) ||
(in.Node != nil && in.NodeClaim == nil && !in.Node.DeletionTimestamp.IsZero())
}

Expand Down

0 comments on commit 7b24b44

Please sign in to comment.