Skip to content

Commit

Permalink
chore: minor refactor to reduce copy/paste code in ScaledObject webho…
Browse files Browse the repository at this point in the history
…ok (#5403)

Signed-off-by: Jan Wozniak <wozniak.jan@gmail.com>
  • Loading branch information
wozniakjan authored Jan 29, 2024
1 parent f439f17 commit cd57dd9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ New deprecation(s):

### Other

- **General**: Minor refactor to reduce copy/paste code in ScaledObject webhook ([#5397](https://github.com/kedacore/keda/issues/5397))
- **General**: TODO ([#XXX](https://github.com/kedacore/keda/issues/XXX))

## v2.13.0
Expand Down
21 changes: 6 additions & 15 deletions apis/keda/v1alpha1/scaledobject_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,22 +317,13 @@ func verifyCPUMemoryScalers(incomingSo *ScaledObject, action string, dryRun bool
continue
}

if trigger.Type == cpuString {
// Fail if neither pod's container spec has a CPU limit specified, nor a default CPU limit is
if trigger.Type == cpuString || trigger.Type == memoryString {
// Fail if neither pod's container spec has particular resource limit specified, nor a default limit is
// specified in LimitRange in the same namespace as the deployment
if !isWorkloadResourceSet(container.Resources, corev1.ResourceCPU) &&
!isContainerResourceLimitSet(context.Background(), incomingSo.Namespace, corev1.ResourceCPU) {
err := fmt.Errorf("the scaledobject has a cpu trigger but the container %s doesn't have the cpu request defined", container.Name)
scaledobjectlog.Error(err, "validation error")
metricscollector.RecordScaledObjectValidatingErrors(incomingSo.Namespace, action, "missing-requests")
return err
}
} else if trigger.Type == memoryString {
// Fail if neither pod's container spec has a memory limit specified, nor a default memory limit is
// specified in LimitRange in the same namespace as the deployment
if !isWorkloadResourceSet(container.Resources, corev1.ResourceMemory) &&
!isContainerResourceLimitSet(context.Background(), incomingSo.Namespace, corev1.ResourceMemory) {
err := fmt.Errorf("the scaledobject has a memory trigger but the container %s doesn't have the memory request defined", container.Name)
resourceType := corev1.ResourceName(trigger.Type)
if !isWorkloadResourceSet(container.Resources, resourceType) &&
!isContainerResourceLimitSet(context.Background(), incomingSo.Namespace, resourceType) {
err := fmt.Errorf("the scaledobject has a %v trigger but the container %s doesn't have the %v request defined", resourceType, container.Name, resourceType)
scaledobjectlog.Error(err, "validation error")
metricscollector.RecordScaledObjectValidatingErrors(incomingSo.Namespace, action, "missing-requests")
return err
Expand Down

0 comments on commit cd57dd9

Please sign in to comment.