Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

505: fail with message when webhook not enabled and minReplicas is nil #543

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ func applyVPA(ctx context.Context, r client.Client, logger logr.Logger, conditio
}
return nil
}

func panicIfNil(value interface{}, message string) {
if value == nil {
panic(message)
}
Comment on lines +137 to +140
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should never panic in a controller's reconciliation loop

}
4 changes: 4 additions & 0 deletions controllers/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func (r *FunctionReconciler) ApplyFunctionHPA(ctx context.Context, function *v1a
// HPA not enabled, skip further action
return nil
}

panicIfNil(function.Spec.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(function.Spec.Replicas, "Replicas 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")

condition := function.Status.Conditions[v1alpha1.HPA]
if condition.Status == metav1.ConditionTrue && !newGeneration {
return nil
Expand Down
4 changes: 4 additions & 0 deletions controllers/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func (r *SinkReconciler) ApplySinkHPA(ctx context.Context, sink *v1alpha1.Sink,
// HPA not enabled, skip further action
return nil
}

panicIfNil(sink.Spec.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(sink.Spec.Replicas, "Replicas 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")

condition := sink.Status.Conditions[v1alpha1.HPA]
if condition.Status == metav1.ConditionTrue && !newGeneration {
return nil
Expand Down
4 changes: 4 additions & 0 deletions controllers/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ func (r *SourceReconciler) ApplySourceHPA(ctx context.Context, source *v1alpha1.
// HPA not enabled, skip further action
return nil
}

panicIfNil(source.Spec.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(source.Spec.Replicas, "Replicas 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")

condition := source.Status.Conditions[v1alpha1.HPA]
if condition.Status == metav1.ConditionTrue && !newGeneration {
return nil
Expand Down
8 changes: 5 additions & 3 deletions controllers/spec/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,13 @@ 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 {
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