From bd3a6f066b5d6deea89adb9ebc6e43137dea2594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Mon, 7 Oct 2024 16:10:20 +0200 Subject: [PATCH] chore: use ptr package (#11346) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: use ptr package Signed-off-by: Charles-Edouard Brétéché * nit Signed-off-by: Charles-Edouard Brétéché --------- Signed-off-by: Charles-Edouard Brétéché --- pkg/autogen/autogen_test.go | 6 ++-- pkg/config/metricsconfig_test.go | 23 +++++++--------- pkg/config/types_test.go | 17 ++++++------ pkg/controllers/ttl/controller.go | 38 +++++++++++--------------- pkg/controllers/ttl/controller_test.go | 18 +++--------- 5 files changed, 41 insertions(+), 61 deletions(-) diff --git a/pkg/autogen/autogen_test.go b/pkg/autogen/autogen_test.go index 4174f1561a..922a9abfeb 100644 --- a/pkg/autogen/autogen_test.go +++ b/pkg/autogen/autogen_test.go @@ -12,6 +12,7 @@ import ( "gotest.tools/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/sets" + "k8s.io/utils/ptr" ) func Test_getAutogenRuleName(t *testing.T) { @@ -392,7 +393,6 @@ func TestUpdateCELFields(t *testing.T) { } func Test_ComputeRules(t *testing.T) { - intPtr := func(i int) *int { return &i } testCases := []struct { name string policy string @@ -455,7 +455,7 @@ spec: VerifyImages: []kyvernov1.ImageVerification{{ ImageReferences: []string{"*"}, Attestors: []kyvernov1.AttestorSet{{ - Count: intPtr(1), + Count: ptr.To(1), Entries: []kyvernov1.Attestor{{ Keyless: &kyvernov1.KeylessAttestor{ Roots: `-----BEGIN CERTIFICATE----- @@ -494,7 +494,7 @@ kA== VerifyImages: []kyvernov1.ImageVerification{{ ImageReferences: []string{"*"}, Attestors: []kyvernov1.AttestorSet{{ - Count: intPtr(1), + Count: ptr.To(1), Entries: []kyvernov1.Attestor{{ Keyless: &kyvernov1.KeylessAttestor{ Roots: `-----BEGIN CERTIFICATE----- diff --git a/pkg/config/metricsconfig_test.go b/pkg/config/metricsconfig_test.go index b2078db79a..517e6677b7 100644 --- a/pkg/config/metricsconfig_test.go +++ b/pkg/config/metricsconfig_test.go @@ -8,12 +8,9 @@ import ( "go.opentelemetry.io/otel/attribute" sdkmetric "go.opentelemetry.io/otel/sdk/metric" corev1 "k8s.io/api/core/v1" + "k8s.io/utils/ptr" ) -func boolPtr(b bool) *bool { - return &b -} - func Test_metricsConfig_load(t *testing.T) { tests := []struct { name string @@ -47,8 +44,8 @@ func Test_metricsConfig_load(t *testing.T) { namespaces: namespacesConfig{IncludeNamespaces: []string{"namespace1"}, ExcludeNamespaces: []string{"namespace2"}}, bucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05}, metricsExposure: map[string]metricExposureConfig{ - "metric1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05}}, - "metric2": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: []float64{0.025, 0.05}}, + "metric1": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05}}, + "metric2": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: []float64{0.025, 0.05}}, }, }, }, @@ -65,8 +62,8 @@ func Test_metricsConfig_load(t *testing.T) { namespaces: namespacesConfig{IncludeNamespaces: []string{"namespace1"}, ExcludeNamespaces: []string{"namespace2"}}, bucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 15, 20, 25, 30}, metricsExposure: map[string]metricExposureConfig{ - "metric1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 15, 20, 25, 30}}, - "metric2": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: []float64{0.025, 0.05}}, + "metric1": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 15, 20, 25, 30}}, + "metric2": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: []float64{0.025, 0.05}}, }, }, }, @@ -107,7 +104,7 @@ func Test_metricsConfig_BuildMeterProviderViews(t *testing.T) { { name: "Case 2: there is no matching entry on the exposure config", metricsExposure: map[string]metricExposureConfig{ - "metric1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 15, 20, 25, 30}}, + "metric1": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 15, 20, 25, 30}}, }, expectedSize: 1, validateFunc: func(views []sdkmetric.View) bool { @@ -120,7 +117,7 @@ func Test_metricsConfig_BuildMeterProviderViews(t *testing.T) { { name: "Case 3: metrics enabled, no transformation configured", metricsExposure: map[string]metricExposureConfig{ - "metric1": {Enabled: boolPtr(true)}, + "metric1": {Enabled: ptr.To(true)}, }, expectedSize: 1, validateFunc: func(views []sdkmetric.View) bool { @@ -133,7 +130,7 @@ func Test_metricsConfig_BuildMeterProviderViews(t *testing.T) { { name: "Case 4: metrics enabled, histogram metric", metricsExposure: map[string]metricExposureConfig{ - "metric1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 15, 20, 25, 30}}, + "metric1": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 15, 20, 25, 30}}, }, expectedSize: 1, validateFunc: func(views []sdkmetric.View) bool { @@ -150,7 +147,7 @@ func Test_metricsConfig_BuildMeterProviderViews(t *testing.T) { { name: "Case 5: metrics enabled, non histogram metric", metricsExposure: map[string]metricExposureConfig{ - "metric1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 15, 20, 25, 30}}, + "metric1": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 15, 20, 25, 30}}, }, expectedSize: 1, validateFunc: func(views []sdkmetric.View) bool { @@ -164,7 +161,7 @@ func Test_metricsConfig_BuildMeterProviderViews(t *testing.T) { { name: "Case 6: metrics disabled", metricsExposure: map[string]metricExposureConfig{ - "metric1": {Enabled: boolPtr(false)}, + "metric1": {Enabled: ptr.To(false)}, }, expectedSize: 1, validateFunc: func(views []sdkmetric.View) bool { diff --git a/pkg/config/types_test.go b/pkg/config/types_test.go index ba9e9c278c..3984d8e107 100644 --- a/pkg/config/types_test.go +++ b/pkg/config/types_test.go @@ -4,6 +4,8 @@ import ( "errors" "reflect" "testing" + + "k8s.io/utils/ptr" ) func Test_parseExclusions(t *testing.T) { @@ -328,9 +330,6 @@ func Test_parseBucketBoundariesConfig(t *testing.T) { } func Test_parseMetricExposureConfig(t *testing.T) { - boolPtr := func(b bool) *bool { - return &b - } defaultBoundaries := []float64{0.005, 0.01} tests := []struct { input string @@ -344,8 +343,8 @@ func Test_parseMetricExposureConfig(t *testing.T) { "key2": {"enabled": false, "disabledLabelDimensions": [], "bucketBoundaries": [1.01, 2.5, 5, 10]} }`, expected: map[string]metricExposureConfig{ - "key1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: []float64{}}, - "key2": {Enabled: boolPtr(false), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{1.01, 2.5, 5, 10}}, + "key1": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: []float64{}}, + "key2": {Enabled: ptr.To(false), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{1.01, 2.5, 5, 10}}, }, expectedError: false, }, @@ -355,7 +354,7 @@ func Test_parseMetricExposureConfig(t *testing.T) { "key1": {"disabledLabelDimensions": ["dim1", "dim2"]} }`, expected: map[string]metricExposureConfig{ - "key1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: defaultBoundaries}, + "key1": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: defaultBoundaries}, }, expectedError: false, }, @@ -365,7 +364,7 @@ func Test_parseMetricExposureConfig(t *testing.T) { "key1": {"enabled": false} }`, expected: map[string]metricExposureConfig{ - "key1": {Enabled: boolPtr(false), DisabledLabelDimensions: []string{}, BucketBoundaries: defaultBoundaries}, + "key1": {Enabled: ptr.To(false), DisabledLabelDimensions: []string{}, BucketBoundaries: defaultBoundaries}, }, expectedError: false, }, @@ -376,8 +375,8 @@ func Test_parseMetricExposureConfig(t *testing.T) { "key2": {"bucketBoundaries": [1.01, 2.5, 5, 10]} }`, expected: map[string]metricExposureConfig{ - "key1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{}}, - "key2": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{1.01, 2.5, 5, 10}}, + "key1": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{}}, + "key2": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{1.01, 2.5, 5, 10}}, }, expectedError: false, }, diff --git a/pkg/controllers/ttl/controller.go b/pkg/controllers/ttl/controller.go index 477febb26e..e0beae73cc 100644 --- a/pkg/controllers/ttl/controller.go +++ b/pkg/controllers/ttl/controller.go @@ -19,6 +19,7 @@ import ( "k8s.io/client-go/metadata" "k8s.io/client-go/tools/cache" "k8s.io/client-go/util/workqueue" + "k8s.io/utils/ptr" ) const ( @@ -121,29 +122,22 @@ func (c *controller) deregisterEventHandlers() { // Function to determine the deletion propagation policy func determinePropagationPolicy(metaObj metav1.Object, logger logr.Logger) *metav1.DeletionPropagation { annotations := metaObj.GetAnnotations() - var policy *metav1.DeletionPropagation - - if annotations != nil { - annotationPolicy := annotations[kyverno.AnnotationCleanupPropagationPolicy] - if annotationPolicy != "" { - switch annotationPolicy { - case "Foreground": - fg := metav1.DeletePropagationForeground - policy = &fg - case "Background": - bg := metav1.DeletePropagationBackground - policy = &bg - case "Orphan": - orphan := metav1.DeletePropagationOrphan - policy = &orphan - case "": - return nil - default: - logger.Info("Unknown propagationPolicy annotation, no global policy found", "policy", annotationPolicy) - } - } + if annotations == nil { + return nil + } + switch annotations[kyverno.AnnotationCleanupPropagationPolicy] { + case "Foreground": + return ptr.To(metav1.DeletePropagationForeground) + case "Background": + return ptr.To(metav1.DeletePropagationBackground) + case "Orphan": + return ptr.To(metav1.DeletePropagationOrphan) + case "": + return nil + default: + logger.Info("Unknown propagationPolicy annotation, no global policy found", "policy", annotations[kyverno.AnnotationCleanupPropagationPolicy]) + return nil } - return policy } func (c *controller) reconcile(ctx context.Context, logger logr.Logger, itemKey string, _, _ string) error { diff --git a/pkg/controllers/ttl/controller_test.go b/pkg/controllers/ttl/controller_test.go index 7078f54107..029a2c03ea 100644 --- a/pkg/controllers/ttl/controller_test.go +++ b/pkg/controllers/ttl/controller_test.go @@ -7,6 +7,7 @@ import ( "github.com/kyverno/kyverno/api/kyverno" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" ) // TestDeterminePropagationPolicy tests the determinePropagationPolicy function @@ -28,30 +29,21 @@ func TestDeterminePropagationPolicy(t *testing.T) { annotations: map[string]string{ kyverno.AnnotationCleanupPropagationPolicy: "Foreground", }, - expectedPolicy: func() *metav1.DeletionPropagation { - fg := metav1.DeletePropagationForeground - return &fg - }(), + expectedPolicy: ptr.To(metav1.DeletePropagationForeground), }, { name: "Background policy", annotations: map[string]string{ kyverno.AnnotationCleanupPropagationPolicy: "Background", }, - expectedPolicy: func() *metav1.DeletionPropagation { - bg := metav1.DeletePropagationBackground - return &bg - }(), + expectedPolicy: ptr.To(metav1.DeletePropagationBackground), }, { name: "Orphan policy", annotations: map[string]string{ kyverno.AnnotationCleanupPropagationPolicy: "Orphan", }, - expectedPolicy: func() *metav1.DeletionPropagation { - orphan := metav1.DeletePropagationOrphan - return &orphan - }(), + expectedPolicy: ptr.To(metav1.DeletePropagationOrphan), }, { name: "Empty annotation", @@ -75,10 +67,8 @@ func TestDeterminePropagationPolicy(t *testing.T) { metaObj := &metav1.ObjectMeta{ Annotations: tc.annotations, } - // Call the function policy := determinePropagationPolicy(metaObj, logger) - // Assert the result assert.Equal(t, tc.expectedPolicy, policy) })