From 9775ea966871a2b590d4db19b9cd10ff19009c65 Mon Sep 17 00:00:00 2001 From: Yufei Zhang Date: Mon, 5 Dec 2022 17:07:36 +0800 Subject: [PATCH] 505: fail with message when webhook not enabled and minReplicas is nil --- controllers/spec/common.go | 6 ++++++ controllers/spec/function.go | 2 +- controllers/spec/hpa.go | 11 ++++++++--- controllers/spec/sink.go | 2 +- controllers/spec/source.go | 2 +- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/controllers/spec/common.go b/controllers/spec/common.go index 7c30b49cf..da483a073 100644 --- a/controllers/spec/common.go +++ b/controllers/spec/common.go @@ -1671,3 +1671,9 @@ func getFilenameOfComponentPackage(componentPackage string) string { } return componentPackage } + +func panicIfNil(value any, message string) { + if value == nil { + panic(message) + } +} diff --git a/controllers/spec/function.go b/controllers/spec/function.go index 564f17d46..0be151903 100644 --- a/controllers/spec/function.go +++ b/controllers/spec/function.go @@ -41,7 +41,7 @@ func MakeFunctionHPA(function *v1alpha1.Function) *autov2beta2.HorizontalPodAuto return makeBuiltinHPA(objectMeta, *function.Spec.MinReplicas, *function.Spec.MaxReplicas, targetRef, function.Spec.Pod.BuiltinAutoscaler) } else if !isDefaultHPAEnabled(function.Spec.MinReplicas, function.Spec.MaxReplicas, function.Spec.Pod) { - return makeHPA(objectMeta, *function.Spec.MinReplicas, *function.Spec.MaxReplicas, function.Spec.Pod, targetRef) + return makeHPA(objectMeta, function.Spec.MinReplicas, function.Spec.MaxReplicas, function.Spec.Pod, targetRef) } return makeDefaultHPA(objectMeta, *function.Spec.MinReplicas, *function.Spec.MaxReplicas, targetRef) } diff --git a/controllers/spec/hpa.go b/controllers/spec/hpa.go index 6191145cd..c5c3fdf6f 100644 --- a/controllers/spec/hpa.go +++ b/controllers/spec/hpa.go @@ -154,11 +154,16 @@ func makeBuiltinHPA(objectMeta *metav1.ObjectMeta, minReplicas, maxReplicas int3 } } -func makeHPA(objectMeta *metav1.ObjectMeta, minReplicas, maxReplicas int32, podPolicy v1alpha1.PodPolicy, targetRef autov2beta2.CrossVersionObjectReference) *autov2beta2.HorizontalPodAutoscaler { +func makeHPA(objectMeta *metav1.ObjectMeta, minReplicas, maxReplicas *int32, podPolicy v1alpha1.PodPolicy, targetRef autov2beta2.CrossVersionObjectReference) *autov2beta2.HorizontalPodAutoscaler { + + panicIfNil(minReplicas, "MinReplicas should not be nil but is nil; This is likely because the webhook is not installed properly so it does not have a default value") + panicIfNil(maxReplicas, "MaxReplicas should not be nil but is nil; This is likely because the webhook is not installed properly so it does not have a default value") + min := *minReplicas + max := *maxReplicas spec := autov2beta2.HorizontalPodAutoscalerSpec{ ScaleTargetRef: targetRef, - MinReplicas: &minReplicas, - MaxReplicas: maxReplicas, + MinReplicas: &min, + MaxReplicas: max, Metrics: podPolicy.AutoScalingMetrics, Behavior: podPolicy.AutoScalingBehavior, } diff --git a/controllers/spec/sink.go b/controllers/spec/sink.go index d6ff56204..89e6f4e25 100644 --- a/controllers/spec/sink.go +++ b/controllers/spec/sink.go @@ -37,7 +37,7 @@ func MakeSinkHPA(sink *v1alpha1.Sink) *autov2beta2.HorizontalPodAutoscaler { return makeBuiltinHPA(objectMeta, *sink.Spec.MinReplicas, *sink.Spec.MaxReplicas, targetRef, sink.Spec.Pod.BuiltinAutoscaler) } else if !isDefaultHPAEnabled(sink.Spec.MinReplicas, sink.Spec.MaxReplicas, sink.Spec.Pod) { - return makeHPA(objectMeta, *sink.Spec.MinReplicas, *sink.Spec.MaxReplicas, sink.Spec.Pod, targetRef) + return makeHPA(objectMeta, sink.Spec.MinReplicas, sink.Spec.MaxReplicas, sink.Spec.Pod, targetRef) } return makeDefaultHPA(objectMeta, *sink.Spec.MinReplicas, *sink.Spec.MaxReplicas, targetRef) } diff --git a/controllers/spec/source.go b/controllers/spec/source.go index 8a9019cea..3d3024931 100644 --- a/controllers/spec/source.go +++ b/controllers/spec/source.go @@ -37,7 +37,7 @@ func MakeSourceHPA(source *v1alpha1.Source) *autov2beta2.HorizontalPodAutoscaler return makeBuiltinHPA(objectMeta, *source.Spec.MinReplicas, *source.Spec.MaxReplicas, targetRef, source.Spec.Pod.BuiltinAutoscaler) } else if !isDefaultHPAEnabled(source.Spec.MinReplicas, source.Spec.MaxReplicas, source.Spec.Pod) { - return makeHPA(objectMeta, *source.Spec.MinReplicas, *source.Spec.MaxReplicas, source.Spec.Pod, targetRef) + return makeHPA(objectMeta, source.Spec.MinReplicas, source.Spec.MaxReplicas, source.Spec.Pod, targetRef) } return makeDefaultHPA(objectMeta, *source.Spec.MinReplicas, *source.Spec.MaxReplicas, targetRef) }