Skip to content

Commit

Permalink
505: detect nil pointer for replicas
Browse files Browse the repository at this point in the history
  • Loading branch information
imaffe committed Dec 16, 2022
1 parent 698546e commit bd8b2c0
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
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)
}
}
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

0 comments on commit bd8b2c0

Please sign in to comment.