Skip to content

Commit

Permalink
initialize pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
moh-osman3 committed Dec 12, 2022
1 parent b2bd95a commit f76931b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
7 changes: 5 additions & 2 deletions apis/v1alpha1/opentelemetrycollector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,13 @@ func (r *OpenTelemetryCollector) validateCRDSpec() error {
}
}

var maxReplicas *int32
maxReplicas := new(int32)
if r.Spec.Autoscaler != nil && r.Spec.Autoscaler.MaxReplicas != nil {
maxReplicas = r.Spec.Autoscaler.MaxReplicas
} else {
}

// check deprecated .Spec.MaxReplicas if maxReplicas is not set
if *maxReplicas == 0 {
maxReplicas = r.Spec.MaxReplicas
}

Expand Down
25 changes: 19 additions & 6 deletions pkg/collector/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,24 @@ func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1al

// check if fields are provided by .Spec.Autoscaler
// otherwise check deprecated fields
var maxReplicas, minReplicas *int32
maxReplicas := new(int32)
minReplicas := new(int32)
if otelcol.Spec.Autoscaler != nil {
if otelcol.Spec.Autoscaler.MaxReplicas != nil {
maxReplicas = otelcol.Spec.Autoscaler.MaxReplicas
}
if otelcol.Spec.Autoscaler.MinReplicas != nil {
minReplicas = otelcol.Spec.Autoscaler.MinReplicas
}
} else {
}
// if maxReplicas not found in .Spec.Autoscaler
if *maxReplicas == 0 {
maxReplicas = otelcol.Spec.MaxReplicas
}

if *minReplicas == 0 {
// this field is optional so if it's not set then use .Spec.Replicas
if otelcol.Spec.MinReplicas != nil {
// this field is optional so if it's not set then use .Spec.Replicas
minReplicas = otelcol.Spec.MinReplicas
} else {
minReplicas = otelcol.Spec.Replicas
Expand Down Expand Up @@ -145,18 +151,25 @@ func HorizontalPodAutoscaler(cfg config.Config, logger logr.Logger, otelcol v1al

// check if fields are provided by .Spec.Autoscaler
// otherwise check deprecated fields
var maxReplicas, minReplicas *int32
maxReplicas := new(int32)
minReplicas := new(int32)
if otelcol.Spec.Autoscaler != nil {
if otelcol.Spec.Autoscaler.MaxReplicas != nil {
maxReplicas = otelcol.Spec.Autoscaler.MaxReplicas
}
if otelcol.Spec.Autoscaler.MinReplicas != nil {
minReplicas = otelcol.Spec.Autoscaler.MinReplicas
}
} else {
}

// did not find the field in autoscaler
if *maxReplicas == 0 {
maxReplicas = otelcol.Spec.MaxReplicas
}

if *minReplicas == 0 {
// this field is optional so if it's not set then use .Spec.Replicas
if otelcol.Spec.MinReplicas != nil {
// this field is optional so if it's not set then use .Spec.Replicas
minReplicas = otelcol.Spec.MinReplicas
} else {
minReplicas = otelcol.Spec.Replicas
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector/reconcile/horizontalpodautoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func expectedHorizontalPodAutoscalers(ctx context.Context, params Params, expect

func setAutoscalerSpec(params Params, autoscalingVersion autodetect.AutoscalingVersion, updated client.Object) {
one := int32(1)
var maxReplicas, minReplicas *int32
maxReplicas := new(int32)
minReplicas := new(int32)
if params.Instance.Spec.Autoscaler != nil {
if params.Instance.Spec.Autoscaler.MaxReplicas != nil {
maxReplicas = params.Instance.Spec.Autoscaler.MaxReplicas
Expand Down

0 comments on commit f76931b

Please sign in to comment.