Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exclusivity to the BuiltinHPARule #549

Merged
merged 1 commit into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/compute/v1alpha1/function_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ func (r *Function) ValidateCreate() error {
allErrs = append(allErrs, fieldErr)
}

fieldErr = validateBuiltinHPARules(r.Spec.Pod.BuiltinAutoscaler)
if fieldErr != nil {
allErrs = append(allErrs, fieldErr)
}

if len(allErrs) == 0 {
return nil
}
Expand Down
22 changes: 22 additions & 0 deletions api/compute/v1alpha1/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,25 @@ func validateMessaging(messaging *Messaging) *field.Error {
}
return nil
}

func validateBuiltinHPARules(rules []BuiltinHPARule) *field.Error {
isCPURuleExists := false
isMemoryRuleExists := false
for _, rule := range rules {
switch rule {
case AverageUtilizationCPUPercent20, AverageUtilizationCPUPercent50, AverageUtilizationCPUPercent80:
if isCPURuleExists {
return field.Invalid(field.NewPath("spec").Child("pod", "builtinAutoscaler"), rules,
"Duplicate CPU autoscaler metrics are set")
}
isCPURuleExists = true
case AverageUtilizationMemoryPercent20, AverageUtilizationMemoryPercent50, AverageUtilizationMemoryPercent80:
if isMemoryRuleExists {
return field.Invalid(field.NewPath("spec").Child("pod", "builtinAutoscaler"), rules,
"Duplicate Memory autoscaler metrics are set")
}
isMemoryRuleExists = true
}
}
return nil
}
2 changes: 1 addition & 1 deletion controllers/function_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func createFunction(function *v1alpha1.Function) {
if len(function.Spec.Pod.BuiltinAutoscaler) > 0 {
Expect(len(hpa.Spec.Metrics)).Should(Equal(len(function.Spec.Pod.BuiltinAutoscaler)))
for _, rule := range function.Spec.Pod.BuiltinAutoscaler {
autoscaler := spec.GetBuiltinAutoScaler(rule)
autoscaler, _ := spec.GetBuiltinAutoScaler(rule)
Expect(autoscaler).Should(Not(BeNil()))
Expect(hpa.Spec.Metrics).Should(ContainElement(autoscaler.Metrics()[0]))
}
Expand Down
28 changes: 19 additions & 9 deletions controllers/spec/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,22 @@ func NewHPARuleAverageUtilizationMemoryPercent(memoryPercentage int32) BuiltinAu
}
}

func GetBuiltinAutoScaler(builtinRule v1alpha1.BuiltinHPARule) BuiltinAutoScaler {
func GetBuiltinAutoScaler(builtinRule v1alpha1.BuiltinHPARule) (BuiltinAutoScaler, int) {
switch builtinRule {
case v1alpha1.AverageUtilizationCPUPercent80:
return NewHPARuleAverageUtilizationCPUPercent(80)
return NewHPARuleAverageUtilizationCPUPercent(80), cpuRuleIdx
case v1alpha1.AverageUtilizationCPUPercent50:
return NewHPARuleAverageUtilizationCPUPercent(50)
return NewHPARuleAverageUtilizationCPUPercent(50), cpuRuleIdx
case v1alpha1.AverageUtilizationCPUPercent20:
return NewHPARuleAverageUtilizationCPUPercent(20)
return NewHPARuleAverageUtilizationCPUPercent(20), cpuRuleIdx
case v1alpha1.AverageUtilizationMemoryPercent80:
return NewHPARuleAverageUtilizationMemoryPercent(80)
return NewHPARuleAverageUtilizationMemoryPercent(80), memoryRuleIdx
case v1alpha1.AverageUtilizationMemoryPercent50:
return NewHPARuleAverageUtilizationMemoryPercent(50)
return NewHPARuleAverageUtilizationMemoryPercent(50), memoryRuleIdx
case v1alpha1.AverageUtilizationMemoryPercent20:
return NewHPARuleAverageUtilizationMemoryPercent(20)
return NewHPARuleAverageUtilizationMemoryPercent(20), memoryRuleIdx
default:
return nil
return nil, 2
}
}

Expand All @@ -126,11 +126,21 @@ func makeDefaultHPA(objectMeta *metav1.ObjectMeta, minReplicas, maxReplicas int3
}
}

const (
cpuRuleIdx = iota
memoryRuleIdx
)

func MakeMetricsFromBuiltinHPARules(builtinRules []v1alpha1.BuiltinHPARule) []autov2beta2.MetricSpec {
isRuleExists := map[int]bool{}
metrics := []autov2beta2.MetricSpec{}
for _, r := range builtinRules {
s := GetBuiltinAutoScaler(r)
s, idx := GetBuiltinAutoScaler(r)
if s != nil {
if isRuleExists[idx] {
continue
}
isRuleExists[idx] = true
metrics = append(metrics, s.Metrics()...)
}
}
Expand Down