1
0
mirror of https://github.com/kubernetes-sigs/descheduler.git synced 2026-01-26 05:14:13 +01:00

Improves the validation logic in the plugins

Signed-off-by: googs1025 <googs1025@gmail.com>
This commit is contained in:
googs1025
2025-08-06 23:03:19 +08:00
parent f2211e1cef
commit 2cce141feb
17 changed files with 221 additions and 138 deletions

View File

@@ -21,25 +21,27 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
)
// ValidateRemovePodsViolatingNodeTaintsArgs validates RemovePodsViolatingNodeTaints arguments
func ValidateRemovePodsViolatingNodeTaintsArgs(obj runtime.Object) error {
args := obj.(*RemovePodsViolatingNodeTaintsArgs)
var allErrs []error
// At most one of include/exclude can be set
if args.Namespaces != nil && len(args.Namespaces.Include) > 0 && len(args.Namespaces.Exclude) > 0 {
return fmt.Errorf("only one of Include/Exclude namespaces can be set")
allErrs = append(allErrs, fmt.Errorf("only one of Include/Exclude namespaces can be set"))
}
if args.LabelSelector != nil {
if _, err := metav1.LabelSelectorAsSelector(args.LabelSelector); err != nil {
return fmt.Errorf("failed to get label selectors from strategy's params: %+v", err)
allErrs = append(allErrs, fmt.Errorf("failed to get label selectors from strategy's params: %+v", err))
}
}
if len(args.ExcludedTaints) > 0 && len(args.IncludedTaints) > 0 {
return fmt.Errorf("either includedTaints or excludedTaints can be set, but not both")
allErrs = append(allErrs, fmt.Errorf("either includedTaints or excludedTaints can be set, but not both"))
}
return nil
return utilerrors.NewAggregate(allErrs)
}

View File

@@ -1,6 +1,7 @@
package removepodsviolatingnodetaints
import (
"fmt"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -11,7 +12,7 @@ func TestValidateRemovePodsViolatingNodeTaintsArgs(t *testing.T) {
testCases := []struct {
description string
args *RemovePodsViolatingNodeTaintsArgs
expectError bool
errInfo error
}{
{
description: "valid namespace args, no errors",
@@ -20,7 +21,6 @@ func TestValidateRemovePodsViolatingNodeTaintsArgs(t *testing.T) {
Include: []string{"default"},
},
},
expectError: false,
},
{
description: "invalid namespaces args, expects error",
@@ -30,7 +30,7 @@ func TestValidateRemovePodsViolatingNodeTaintsArgs(t *testing.T) {
Exclude: []string{"kube-system"},
},
},
expectError: true,
errInfo: fmt.Errorf(`only one of Include/Exclude namespaces can be set`),
},
{
description: "valid label selector args, no errors",
@@ -39,7 +39,6 @@ func TestValidateRemovePodsViolatingNodeTaintsArgs(t *testing.T) {
MatchLabels: map[string]string{"role.kubernetes.io/node": ""},
},
},
expectError: false,
},
{
description: "invalid label selector args, expects errors",
@@ -52,14 +51,13 @@ func TestValidateRemovePodsViolatingNodeTaintsArgs(t *testing.T) {
},
},
},
expectError: true,
errInfo: fmt.Errorf(`failed to get label selectors from strategy's params: [key: Invalid value: "": name part must be non-empty; name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]'), values: Invalid value: []string(nil): for 'in', 'notin' operators, values set can't be empty]`),
},
{
description: "valid taint filters, no errors",
args: &RemovePodsViolatingNodeTaintsArgs{
ExcludedTaints: []string{"testTaint1=test1"},
},
expectError: false,
},
{
description: "invalid taint filters args, expects errors",
@@ -67,17 +65,19 @@ func TestValidateRemovePodsViolatingNodeTaintsArgs(t *testing.T) {
ExcludedTaints: []string{"do-not-evict"},
IncludedTaints: []string{"testTaint1=test1"},
},
expectError: true,
errInfo: fmt.Errorf(`either includedTaints or excludedTaints can be set, but not both`),
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
err := ValidateRemovePodsViolatingNodeTaintsArgs(tc.args)
hasError := err != nil
if tc.expectError != hasError {
t.Error("unexpected arg validation behavior")
for _, testCase := range testCases {
t.Run(testCase.description, func(t *testing.T) {
validateErr := ValidateRemovePodsViolatingNodeTaintsArgs(testCase.args)
if validateErr == nil || testCase.errInfo == nil {
if validateErr != testCase.errInfo {
t.Errorf("expected validity of plugin config: %q but got %q instead", testCase.errInfo, validateErr)
}
} else if validateErr.Error() != testCase.errInfo.Error() {
t.Errorf("expected validity of plugin config: %q but got %q instead", testCase.errInfo, validateErr)
}
})
}