func ValidateAutoscalingPolicies()

in pkg/controller/common/autoscaling/validations.go [103:208]


func ValidateAutoscalingPolicies(
	autoscalingSpecPath SpecPathBuilder,
	autoscalingPolicies v1alpha1.AutoscalingPolicySpecs,
) field.ErrorList {
	var errs field.ErrorList
	policyNames := set.Make()
	mlPolicyCount := 0
	rolesSet := make([][]string, 0, len(autoscalingPolicies))
	for i, autoscalingSpec := range autoscalingPolicies {
		// The name field is mandatory.
		if len(autoscalingSpec.Name) == 0 {
			errs = append(errs, field.Required(autoscalingSpecPath(i, "name"), "name is mandatory"))
		} else {
			if policyNames.Has(autoscalingSpec.Name) {
				errs = append(
					errs,
					field.Invalid(autoscalingSpecPath(i, "name"), autoscalingSpec.Name, "policy is duplicated"),
				)
			}
			policyNames.Add(autoscalingSpec.Name)
		}

		// Validate the set of roles managed by this autoscaling policy.
		if len(autoscalingSpec.Roles) == 0 {
			errs = append(errs, field.Required(autoscalingSpecPath(i, "roles"), "roles field is mandatory and must not be empty"))
		} else {
			if containsStringSlice(rolesSet, autoscalingSpec.Roles) {
				// A set of roles must be unique across all the autoscaling policies.
				errs = append(
					errs,
					field.Invalid(
						autoscalingSpecPath(i, "name"),
						strings.Join(autoscalingSpec.Roles, ","),
						"roles set is duplicated"),
				)
			} else {
				rolesSet = append(rolesSet, autoscalingSpec.Roles)
			}
		}

		if stringsutil.StringInSlice(string(esv1.MLRole), autoscalingSpec.Roles) {
			mlPolicyCount++
		}

		if mlPolicyCount > 1 {
			errs = append(
				errs,
				field.Invalid(
					autoscalingSpecPath(i, "name"), strings.Join(autoscalingSpec.Roles, ","),
					"ML nodes must be in a dedicated NodeSet",
				),
			)
		}

		// Machine learning nodes must be in a dedicated tier.
		if stringsutil.StringInSlice(string(esv1.MLRole), autoscalingSpec.Roles) && len(ignoreRemoteClusterClientRole(autoscalingSpec.Roles)) > 1 {
			errs = append(
				errs,
				field.Invalid(
					autoscalingSpecPath(i, "name"), strings.Join(autoscalingSpec.Roles, ","),
					"ML nodes must be in a dedicated autoscaling policy"),
			)
		}

		if !(autoscalingSpec.NodeCountRange.Min >= 0) {
			errs = append(
				errs,
				field.Invalid(
					autoscalingSpecPath(i, "resources", "nodeCount", "min"),
					autoscalingSpec.NodeCountRange.Min,
					"min count must be equal or greater than 0",
				),
			)
		}

		if !(autoscalingSpec.NodeCountRange.Max > 0) {
			errs = append(
				errs,
				field.Invalid(
					autoscalingSpecPath(i, "resources", "nodeCount", "max"),
					autoscalingSpec.NodeCountRange.Max,
					"max count must be greater than 0"),
			)
		}

		if !(autoscalingSpec.NodeCountRange.Max >= autoscalingSpec.NodeCountRange.Min) {
			errs = append(
				errs,
				field.Invalid(autoscalingSpecPath(i, "resources", "nodeCount", "max"),
					autoscalingSpec.NodeCountRange.Max,
					"max node count must be an integer greater or equal than the min node count"),
			)
		}

		// Validate CPU
		errs = validateQuantities(errs, autoscalingSpecPath, autoscalingSpec.CPURange, i, "cpu", minCPU)

		// Validate Memory
		errs = validateQuantities(errs, autoscalingSpecPath, autoscalingSpec.MemoryRange, i, "memory", minMemory)

		// Validate storage
		errs = validateQuantities(errs, autoscalingSpecPath, autoscalingSpec.StorageRange, i, "storage", minStorage)
	}

	return errs
}