diff --git a/apis/keda/v1alpha1/scaledobject_webhook.go b/apis/keda/v1alpha1/scaledobject_webhook.go index cc4267ff502..63e81752f75 100644 --- a/apis/keda/v1alpha1/scaledobject_webhook.go +++ b/apis/keda/v1alpha1/scaledobject_webhook.go @@ -257,16 +257,14 @@ func verifyCPUMemoryScalers(incomingSo *ScaledObject, action string) error { } if trigger.Type == cpuString { - if (container.Resources.Requests == nil || container.Resources.Requests.Cpu() == nil || container.Resources.Requests.Cpu().AsApproximateFloat64() == 0) && - (container.Resources.Limits == nil || container.Resources.Limits.Cpu() == nil || container.Resources.Limits.Cpu().AsApproximateFloat64() == 0) { + if !isWorkloadResourceSet(container.Resources, 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") prommetrics.RecordScaledObjectValidatingErrors(incomingSo.Namespace, action, "missing-requests") return err } } else if trigger.Type == memoryString { - if (container.Resources.Requests == nil || container.Resources.Requests.Memory() == nil || container.Resources.Requests.Memory().AsApproximateFloat64() == 0) && - (container.Resources.Limits == nil || container.Resources.Limits.Memory() == nil || container.Resources.Limits.Memory().AsApproximateFloat64() == 0) { + if !isWorkloadResourceSet(container.Resources, corev1.ResourceMemory) { err := fmt.Errorf("the scaledobject has a memory trigger but the container %s doesn't have the memory request defined", container.Name) scaledobjectlog.Error(err, "validation error") prommetrics.RecordScaledObjectValidatingErrors(incomingSo.Namespace, action, "missing-requests") @@ -296,3 +294,9 @@ func verifyCPUMemoryScalers(incomingSo *ScaledObject, action string) error { } return nil } + +func isWorkloadResourceSet(rr corev1.ResourceRequirements, name corev1.ResourceName) bool { + requests, requestsSet := rr.Requests[name] + limits, limitsSet := rr.Limits[name] + return (requestsSet || limitsSet) && (requests.AsApproximateFloat64() > 0 || limits.AsApproximateFloat64() > 0) +}