Merge pull request #3829 from whitewindmills/preemption-validation

feat: validate resourceSelectors if Preemption is enabled
This commit is contained in:
karmada-bot 2023-07-24 19:30:36 +08:00 committed by GitHub
commit 0d50f598f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -26,6 +26,22 @@ func ValidatePropagationSpec(spec policyv1alpha1.PropagationSpec) field.ErrorLis
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("propagateDeps"), spec.PropagateDeps, "application failover is set, propagateDeps must be true"))
}
allErrs = append(allErrs, ValidateFailover(spec.Failover, field.NewPath("spec").Child("failover"))...)
allErrs = append(allErrs, validateResourceSelectorsIfPreemptionEnabled(spec, field.NewPath("spec").Child("resourceSelectors"))...)
return allErrs
}
// validateResourceSelectorsIfPreemptionEnabled validates ResourceSelectors if Preemption is Always.
func validateResourceSelectorsIfPreemptionEnabled(spec policyv1alpha1.PropagationSpec, fldPath *field.Path) field.ErrorList {
if spec.Preemption != policyv1alpha1.PreemptAlways {
return nil
}
var allErrs field.ErrorList
for index, resourceSelector := range spec.ResourceSelectors {
if len(resourceSelector.Name) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Index(index).Child("name"), resourceSelector.Name, "name can not be empty if preemption is Always, the empty name may cause unexpected resources preemption"))
}
}
return allErrs
}

View File

@ -536,6 +536,20 @@ func TestValidatePropagationSpec(t *testing.T) {
}},
expectedErr: "the cluster spread constraint must be enabled in one of the constraints in case of SpreadByField is enabled",
},
{
name: "resourceSelector name is empty when preemption is enabled",
spec: policyv1alpha1.PropagationSpec{
ResourceSelectors: []policyv1alpha1.ResourceSelector{
{
APIVersion: "v1",
Kind: "Pod",
Namespace: "default",
},
},
Preemption: policyv1alpha1.PreemptAlways,
},
expectedErr: "name can not be empty if preemption is Always, the empty name may cause unexpected resources preemption",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {