Skip to content

Commit

Permalink
chore: minor refactor to reduce copy/paste code in ScaledObject webhook
Browse files Browse the repository at this point in the history
closes: kedacore#5397

Signed-off-by: Jan Wozniak <[email protected]>
  • Loading branch information
wozniakjan committed Jan 18, 2024
1 parent 05bbf8d commit 004f5d7
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 @@ -63,6 +63,7 @@ Here is an overview of all new **experimental** features:
### Improvements

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

### Fixes

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 004f5d7

Please sign in to comment.