Merge pull request #5282 from a7i/work-suspend-validation
work suspension: webhook validation
This commit is contained in:
commit
d2adb3d9d3
|
@ -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, ValidateFailover(spec.Failover, field.NewPath("spec").Child("failover"))...)
|
||||||
allErrs = append(allErrs, validateResourceSelectorsIfPreemptionEnabled(spec, field.NewPath("spec").Child("resourceSelectors"))...)
|
allErrs = append(allErrs, validateResourceSelectorsIfPreemptionEnabled(spec, field.NewPath("spec").Child("resourceSelectors"))...)
|
||||||
|
allErrs = append(allErrs, validateSuspension(spec.Suspension, field.NewPath("spec").Child("suspension"))...)
|
||||||
return allErrs
|
return allErrs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,6 +62,21 @@ func validateResourceSelectorsIfPreemptionEnabled(spec policyv1alpha1.Propagatio
|
||||||
return allErrs
|
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.
|
// ValidatePlacement validates a placement before creation or update.
|
||||||
func ValidatePlacement(placement policyv1alpha1.Placement, fldPath *field.Path) field.ErrorList {
|
func ValidatePlacement(placement policyv1alpha1.Placement, fldPath *field.Path) field.ErrorList {
|
||||||
var allErrs field.ErrorList
|
var allErrs field.ErrorList
|
||||||
|
|
|
@ -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",
|
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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue