From cfd97ba070a4927309c18f03e5a7ad5a8d846827 Mon Sep 17 00:00:00 2001 From: Wei Huang Date: Sat, 20 Jul 2019 23:12:32 -0700 Subject: [PATCH] Optimize logic in EvenPodsSpread API validation --- pkg/apis/core/validation/validation.go | 27 ++++++++------------------ 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/pkg/apis/core/validation/validation.go b/pkg/apis/core/validation/validation.go index a819a701b4e..b8203145de4 100644 --- a/pkg/apis/core/validation/validation.go +++ b/pkg/apis/core/validation/validation.go @@ -5703,16 +5703,10 @@ var ( supportedScheduleActions = sets.NewString(string(core.DoNotSchedule), string(core.ScheduleAnyway)) ) -type spreadConstraintPair struct { - topologyKey string - whenUnsatisfiable core.UnsatisfiableConstraintAction -} - // validateTopologySpreadConstraints validates given TopologySpreadConstraints. func validateTopologySpreadConstraints(constraints []core.TopologySpreadConstraint, fldPath *field.Path) field.ErrorList { allErrs := field.ErrorList{} - var existingConstraintPairs []spreadConstraintPair for i, constraint := range constraints { subFldPath := fldPath.Index(i) if err := ValidateMaxSkew(subFldPath.Child("maxSkew"), constraint.MaxSkew); err != nil { @@ -5725,14 +5719,8 @@ func validateTopologySpreadConstraints(constraints []core.TopologySpreadConstrai allErrs = append(allErrs, err) } // tuple {topologyKey, whenUnsatisfiable} denotes one kind of spread constraint - pair := spreadConstraintPair{ - topologyKey: constraint.TopologyKey, - whenUnsatisfiable: constraint.WhenUnsatisfiable, - } - if err := ValidateSpreadConstraintPair(subFldPath.Child("{topologyKey, whenUnsatisfiable}"), pair, existingConstraintPairs); err != nil { + if err := ValidateSpreadConstraintNotRepeat(subFldPath.Child("{topologyKey, whenUnsatisfiable}"), constraint, constraints[i+1:]); err != nil { allErrs = append(allErrs, err) - } else { - existingConstraintPairs = append(existingConstraintPairs, pair) } } @@ -5763,12 +5751,13 @@ func ValidateWhenUnsatisfiable(fldPath *field.Path, action core.UnsatisfiableCon return nil } -// ValidateSpreadConstraintPair tests that if `pair` exists in `existingConstraintPairs`. -func ValidateSpreadConstraintPair(fldPath *field.Path, pair spreadConstraintPair, existingConstraintPairs []spreadConstraintPair) *field.Error { - for _, existingPair := range existingConstraintPairs { - if pair.topologyKey == existingPair.topologyKey && - pair.whenUnsatisfiable == existingPair.whenUnsatisfiable { - return field.Duplicate(fldPath, pair) +// ValidateSpreadConstraintNotRepeat tests that if `constraint` duplicates with `existingConstraintPairs` +// on TopologyKey and WhenUnsatisfiable fields. +func ValidateSpreadConstraintNotRepeat(fldPath *field.Path, constraint core.TopologySpreadConstraint, restingConstraints []core.TopologySpreadConstraint) *field.Error { + for _, restingConstraint := range restingConstraints { + if constraint.TopologyKey == restingConstraint.TopologyKey && + constraint.WhenUnsatisfiable == restingConstraint.WhenUnsatisfiable { + return field.Duplicate(fldPath, fmt.Sprintf("{%v, %v}", constraint.TopologyKey, constraint.WhenUnsatisfiable)) } } return nil