From 3c85a52abb8b6660bf00b3b13f74af30551bfb11 Mon Sep 17 00:00:00 2001 From: Pavol Loffay Date: Wed, 6 Mar 2024 18:17:27 +0100 Subject: [PATCH] Handle deprecated replicas in conversion (#2723) Signed-off-by: Pavol Loffay --- apis/v1alpha1/convert.go | 6 +++++- apis/v1alpha1/convert_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/apis/v1alpha1/convert.go b/apis/v1alpha1/convert.go index 9f7e01fb78..a831659666 100644 --- a/apis/v1alpha1/convert.go +++ b/apis/v1alpha1/convert.go @@ -181,10 +181,14 @@ func tov1beta1TA(in OpenTelemetryTargetAllocator) v1beta1.TargetAllocatorEmbedde } } +// The conversion takes into account deprecated v1alpha1 spec.minReplicas and spec.maxReplicas. func tov1beta1Autoscaler(in *AutoscalerSpec, minReplicas, maxReplicas *int32) *v1beta1.AutoscalerSpec { - if in == nil { + if in == nil && minReplicas == nil && maxReplicas == nil { return nil } + if in == nil { + in = &AutoscalerSpec{} + } var metrics []v1beta1.MetricSpec for _, m := range in.Metrics { diff --git a/apis/v1alpha1/convert_test.go b/apis/v1alpha1/convert_test.go index 4f0af2602c..61a3df1aad 100644 --- a/apis/v1alpha1/convert_test.go +++ b/apis/v1alpha1/convert_test.go @@ -382,6 +382,31 @@ func Test_tov1beta1AndBack_prometheus_selectors(t *testing.T) { }) } +func Test_tov1beta1AndBack_deprecated_replicas(t *testing.T) { + one := int32(1) + two := int32(2) + colalpha1 := OpenTelemetryCollector{ + Spec: OpenTelemetryCollectorSpec{ + MinReplicas: &one, + MaxReplicas: &two, + }, + } + + colbeta1 := v1beta1.OpenTelemetryCollector{} + err := colalpha1.ConvertTo(&colbeta1) + require.NoError(t, err) + + assert.Equal(t, one, *colbeta1.Spec.Autoscaler.MinReplicas) + assert.Equal(t, two, *colbeta1.Spec.Autoscaler.MaxReplicas) + + err = colalpha1.ConvertFrom(&colbeta1) + require.NoError(t, err) + assert.Nil(t, colalpha1.Spec.MinReplicas) + assert.Nil(t, colalpha1.Spec.MaxReplicas) + assert.Equal(t, one, *colalpha1.Spec.Autoscaler.MinReplicas) + assert.Equal(t, two, *colalpha1.Spec.Autoscaler.MaxReplicas) +} + func createTA() OpenTelemetryTargetAllocator { replicas := int32(2) runAsNonRoot := true