Merge pull request #80395 from Huang-Wei/cleanup-eps-validation
Optimize logic in EvenPodsSpread API validation
This commit is contained in:
		@@ -5698,16 +5698,10 @@ var (
 | 
				
			|||||||
	supportedScheduleActions = sets.NewString(string(core.DoNotSchedule), string(core.ScheduleAnyway))
 | 
						supportedScheduleActions = sets.NewString(string(core.DoNotSchedule), string(core.ScheduleAnyway))
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type spreadConstraintPair struct {
 | 
					 | 
				
			||||||
	topologyKey       string
 | 
					 | 
				
			||||||
	whenUnsatisfiable core.UnsatisfiableConstraintAction
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// validateTopologySpreadConstraints validates given TopologySpreadConstraints.
 | 
					// validateTopologySpreadConstraints validates given TopologySpreadConstraints.
 | 
				
			||||||
func validateTopologySpreadConstraints(constraints []core.TopologySpreadConstraint, fldPath *field.Path) field.ErrorList {
 | 
					func validateTopologySpreadConstraints(constraints []core.TopologySpreadConstraint, fldPath *field.Path) field.ErrorList {
 | 
				
			||||||
	allErrs := field.ErrorList{}
 | 
						allErrs := field.ErrorList{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var existingConstraintPairs []spreadConstraintPair
 | 
					 | 
				
			||||||
	for i, constraint := range constraints {
 | 
						for i, constraint := range constraints {
 | 
				
			||||||
		subFldPath := fldPath.Index(i)
 | 
							subFldPath := fldPath.Index(i)
 | 
				
			||||||
		if err := ValidateMaxSkew(subFldPath.Child("maxSkew"), constraint.MaxSkew); err != nil {
 | 
							if err := ValidateMaxSkew(subFldPath.Child("maxSkew"), constraint.MaxSkew); err != nil {
 | 
				
			||||||
@@ -5720,14 +5714,8 @@ func validateTopologySpreadConstraints(constraints []core.TopologySpreadConstrai
 | 
				
			|||||||
			allErrs = append(allErrs, err)
 | 
								allErrs = append(allErrs, err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		// tuple {topologyKey, whenUnsatisfiable} denotes one kind of spread constraint
 | 
							// tuple {topologyKey, whenUnsatisfiable} denotes one kind of spread constraint
 | 
				
			||||||
		pair := spreadConstraintPair{
 | 
							if err := ValidateSpreadConstraintNotRepeat(subFldPath.Child("{topologyKey, whenUnsatisfiable}"), constraint, constraints[i+1:]); err != nil {
 | 
				
			||||||
			topologyKey:       constraint.TopologyKey,
 | 
					 | 
				
			||||||
			whenUnsatisfiable: constraint.WhenUnsatisfiable,
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if err := ValidateSpreadConstraintPair(subFldPath.Child("{topologyKey, whenUnsatisfiable}"), pair, existingConstraintPairs); err != nil {
 | 
					 | 
				
			||||||
			allErrs = append(allErrs, err)
 | 
								allErrs = append(allErrs, err)
 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			existingConstraintPairs = append(existingConstraintPairs, pair)
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -5758,12 +5746,13 @@ func ValidateWhenUnsatisfiable(fldPath *field.Path, action core.UnsatisfiableCon
 | 
				
			|||||||
	return nil
 | 
						return nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// ValidateSpreadConstraintPair tests that if `pair` exists in `existingConstraintPairs`.
 | 
					// ValidateSpreadConstraintNotRepeat tests that if `constraint` duplicates with `existingConstraintPairs`
 | 
				
			||||||
func ValidateSpreadConstraintPair(fldPath *field.Path, pair spreadConstraintPair, existingConstraintPairs []spreadConstraintPair) *field.Error {
 | 
					// on TopologyKey and WhenUnsatisfiable fields.
 | 
				
			||||||
	for _, existingPair := range existingConstraintPairs {
 | 
					func ValidateSpreadConstraintNotRepeat(fldPath *field.Path, constraint core.TopologySpreadConstraint, restingConstraints []core.TopologySpreadConstraint) *field.Error {
 | 
				
			||||||
		if pair.topologyKey == existingPair.topologyKey &&
 | 
						for _, restingConstraint := range restingConstraints {
 | 
				
			||||||
			pair.whenUnsatisfiable == existingPair.whenUnsatisfiable {
 | 
							if constraint.TopologyKey == restingConstraint.TopologyKey &&
 | 
				
			||||||
			return field.Duplicate(fldPath, pair)
 | 
								constraint.WhenUnsatisfiable == restingConstraint.WhenUnsatisfiable {
 | 
				
			||||||
 | 
								return field.Duplicate(fldPath, fmt.Sprintf("{%v, %v}", constraint.TopologyKey, constraint.WhenUnsatisfiable))
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user