-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert scale-down pdb check to drainability rule
- Loading branch information
Showing
4 changed files
with
209 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
cluster-autoscaler/simulator/drainability/rules/pdb/rule.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package pdb | ||
|
||
import ( | ||
"fmt" | ||
|
||
apiv1 "k8s.io/api/core/v1" | ||
"k8s.io/autoscaler/cluster-autoscaler/simulator/drainability" | ||
"k8s.io/autoscaler/cluster-autoscaler/utils/drain" | ||
) | ||
|
||
// Rule is a drainability rule on how to handle pods with pdbs. | ||
type Rule struct{} | ||
|
||
// New creates a new Rule. | ||
func New() *Rule { | ||
return &Rule{} | ||
} | ||
|
||
// Drainable decides how to handle pods with pdbs on node drain. | ||
func (Rule) Drainable(drainCtx *drainability.DrainContext, pod *apiv1.Pod) drainability.Status { | ||
for _, pdb := range drainCtx.RemainingPdbTracker.MatchingPdbs(pod) { | ||
if pdb.Status.DisruptionsAllowed < 1 { | ||
return drainability.NewBlockedStatus(drain.NotEnoughPdb, fmt.Errorf("not enough pod disruption budget to move %s/%s", pod.Namespace, pod.Name)) | ||
} | ||
} | ||
return drainability.NewUndefinedStatus() | ||
} |
155 changes: 155 additions & 0 deletions
155
cluster-autoscaler/simulator/drainability/rules/pdb/rule_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package pdb | ||
|
||
import ( | ||
"testing" | ||
|
||
apiv1 "k8s.io/api/core/v1" | ||
policyv1 "k8s.io/api/policy/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"k8s.io/autoscaler/cluster-autoscaler/core/scaledown/pdb" | ||
"k8s.io/autoscaler/cluster-autoscaler/simulator/drainability" | ||
"k8s.io/autoscaler/cluster-autoscaler/utils/drain" | ||
) | ||
|
||
func TestRule(t *testing.T) { | ||
one := intstr.FromInt(1) | ||
|
||
testCases := []struct { | ||
desc string | ||
pod *apiv1.Pod | ||
pdbs []*policyv1.PodDisruptionBudget | ||
wantOutcome drainability.OutcomeType | ||
wantReason drain.BlockingPodReason | ||
}{ | ||
{ | ||
desc: "no pdbs", | ||
pod: &apiv1.Pod{}, | ||
}, | ||
{ | ||
desc: "no matching pdbs", | ||
pod: &apiv1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "happy", | ||
Namespace: "good", | ||
Labels: map[string]string{ | ||
"label": "true", | ||
}, | ||
}, | ||
}, | ||
pdbs: []*policyv1.PodDisruptionBudget{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "bad", | ||
}, | ||
Spec: policyv1.PodDisruptionBudgetSpec{ | ||
MinAvailable: &one, | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"label": "true", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "good", | ||
}, | ||
Spec: policyv1.PodDisruptionBudgetSpec{ | ||
MinAvailable: &one, | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"label": "false", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
desc: "pdb prevents scale-down", | ||
pod: &apiv1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "sad", | ||
Namespace: "good", | ||
Labels: map[string]string{ | ||
"label": "true", | ||
}, | ||
}, | ||
}, | ||
pdbs: []*policyv1.PodDisruptionBudget{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "bad", | ||
}, | ||
Spec: policyv1.PodDisruptionBudgetSpec{ | ||
MinAvailable: &one, | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"label": "true", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "good", | ||
}, | ||
Spec: policyv1.PodDisruptionBudgetSpec{ | ||
MinAvailable: &one, | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"label": "true", | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "good", | ||
}, | ||
Spec: policyv1.PodDisruptionBudgetSpec{ | ||
MinAvailable: &one, | ||
Selector: &metav1.LabelSelector{ | ||
MatchLabels: map[string]string{ | ||
"label": "false", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
wantOutcome: drainability.BlockDrain, | ||
wantReason: drain.NotEnoughPdb, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
tracker := pdb.NewBasicRemainingPdbTracker() | ||
tracker.SetPdbs(tc.pdbs) | ||
drainCtx := &drainability.DrainContext{ | ||
RemainingPdbTracker: tracker, | ||
} | ||
|
||
got := New().Drainable(drainCtx, tc.pod) | ||
if got.Outcome != tc.wantOutcome || got.BlockingReason != tc.wantReason { | ||
t.Errorf("Rule.Drainable(%s) = (outcome: %v, reason: %v), want (outcome: %v, reason: %v)", tc.pod.Name, got.Outcome, got.BlockingReason, tc.wantOutcome, tc.wantReason) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters