Merge pull request #5282 from a7i/work-suspend-validation

work suspension: webhook validation
This commit is contained in:
karmada-bot 2024-08-07 16:25:28 +08:00 committed by GitHub
commit d2adb3d9d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 77 additions and 5 deletions

View File

@ -43,6 +43,7 @@ func ValidatePropagationSpec(spec policyv1alpha1.PropagationSpec) field.ErrorLis
}
allErrs = append(allErrs, ValidateFailover(spec.Failover, field.NewPath("spec").Child("failover"))...)
allErrs = append(allErrs, validateResourceSelectorsIfPreemptionEnabled(spec, field.NewPath("spec").Child("resourceSelectors"))...)
allErrs = append(allErrs, validateSuspension(spec.Suspension, field.NewPath("spec").Child("suspension"))...)
return allErrs
}
@ -61,6 +62,21 @@ func validateResourceSelectorsIfPreemptionEnabled(spec policyv1alpha1.Propagatio
return allErrs
}
// validateSuspension validates no conflicts between dispatching and dispatchingOnClusters.
func validateSuspension(suspension *policyv1alpha1.Suspension, fldPath *field.Path) field.ErrorList {
if suspension == nil {
return nil
}
if (suspension.Dispatching != nil && *suspension.Dispatching) &&
(suspension.DispatchingOnClusters != nil && len(suspension.DispatchingOnClusters.ClusterNames) > 0) {
return field.ErrorList{
field.Invalid(fldPath.Child("suspension"), suspension, "suspension dispatching cannot co-exist with dispatchingOnClusters.clusterNames"),
}
}
return nil
}
// ValidatePlacement validates a placement before creation or update.
func ValidatePlacement(placement policyv1alpha1.Placement, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList

View File

@ -566,6 +566,62 @@ func TestValidatePropagationSpec(t *testing.T) {
},
expectedErr: "name cannot be empty if preemption is Always, the empty name may cause unexpected resources preemption",
},
{
name: "suspension dispatching cannot co-exist with dispatchingOnClusters",
spec: policyv1alpha1.PropagationSpec{
Suspension: &policyv1alpha1.Suspension{
Dispatching: ptr.To(true),
DispatchingOnClusters: &policyv1alpha1.SuspendClusters{
ClusterNames: []string{"cluster-name"},
},
},
},
expectedErr: "suspension dispatching cannot co-exist with dispatchingOnClusters.clusterNames",
},
{
name: "suspension dispatching with nil dispatchingOnClusters is valid",
spec: policyv1alpha1.PropagationSpec{
Suspension: &policyv1alpha1.Suspension{
Dispatching: ptr.To(true),
},
},
expectedErr: "",
},
{
name: "suspension dispatching with empty dispatching clusters is valid",
spec: policyv1alpha1.PropagationSpec{
Suspension: &policyv1alpha1.Suspension{
Dispatching: ptr.To(true),
DispatchingOnClusters: &policyv1alpha1.SuspendClusters{
ClusterNames: []string{},
},
},
},
expectedErr: "",
},
{
name: "dispatchingOnClusters.clusterNames without dispatching is valid",
spec: policyv1alpha1.PropagationSpec{
Suspension: &policyv1alpha1.Suspension{
DispatchingOnClusters: &policyv1alpha1.SuspendClusters{
ClusterNames: []string{"cluster-name"},
},
},
},
expectedErr: "",
},
{
name: "dispatchingOnClusters.clusterNames with dispatching false is valid",
spec: policyv1alpha1.PropagationSpec{
Suspension: &policyv1alpha1.Suspension{
Dispatching: ptr.To(false),
DispatchingOnClusters: &policyv1alpha1.SuspendClusters{
ClusterNames: []string{"cluster-name"},
},
},
},
expectedErr: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {