Skip to content

Commit

Permalink
505: fail with message when webhook not enabled and minReplicas is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
imaffe committed Dec 5, 2022
1 parent 698546e commit 9775ea9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
6 changes: 6 additions & 0 deletions controllers/spec/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1671,3 +1671,9 @@ func getFilenameOfComponentPackage(componentPackage string) string {
}
return componentPackage
}

func panicIfNil(value any, message string) {
if value == nil {
panic(message)
}
}
2 changes: 1 addition & 1 deletion controllers/spec/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
11 changes: 8 additions & 3 deletions controllers/spec/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/spec/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/spec/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 9775ea9

Please sign in to comment.