1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2024-12-14 11:57:48 +00:00

chore: use ptr package (#11346)

* chore: use ptr package

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

* nit

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>

---------

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2024-10-07 16:10:20 +02:00 committed by GitHub
parent 9f5e75a2b5
commit bd3a6f066b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 41 additions and 61 deletions

View file

@ -12,6 +12,7 @@ import (
"gotest.tools/assert" "gotest.tools/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/ptr"
) )
func Test_getAutogenRuleName(t *testing.T) { func Test_getAutogenRuleName(t *testing.T) {
@ -392,7 +393,6 @@ func TestUpdateCELFields(t *testing.T) {
} }
func Test_ComputeRules(t *testing.T) { func Test_ComputeRules(t *testing.T) {
intPtr := func(i int) *int { return &i }
testCases := []struct { testCases := []struct {
name string name string
policy string policy string
@ -455,7 +455,7 @@ spec:
VerifyImages: []kyvernov1.ImageVerification{{ VerifyImages: []kyvernov1.ImageVerification{{
ImageReferences: []string{"*"}, ImageReferences: []string{"*"},
Attestors: []kyvernov1.AttestorSet{{ Attestors: []kyvernov1.AttestorSet{{
Count: intPtr(1), Count: ptr.To(1),
Entries: []kyvernov1.Attestor{{ Entries: []kyvernov1.Attestor{{
Keyless: &kyvernov1.KeylessAttestor{ Keyless: &kyvernov1.KeylessAttestor{
Roots: `-----BEGIN CERTIFICATE----- Roots: `-----BEGIN CERTIFICATE-----
@ -494,7 +494,7 @@ kA==
VerifyImages: []kyvernov1.ImageVerification{{ VerifyImages: []kyvernov1.ImageVerification{{
ImageReferences: []string{"*"}, ImageReferences: []string{"*"},
Attestors: []kyvernov1.AttestorSet{{ Attestors: []kyvernov1.AttestorSet{{
Count: intPtr(1), Count: ptr.To(1),
Entries: []kyvernov1.Attestor{{ Entries: []kyvernov1.Attestor{{
Keyless: &kyvernov1.KeylessAttestor{ Keyless: &kyvernov1.KeylessAttestor{
Roots: `-----BEGIN CERTIFICATE----- Roots: `-----BEGIN CERTIFICATE-----

View file

@ -8,12 +8,9 @@ import (
"go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/attribute"
sdkmetric "go.opentelemetry.io/otel/sdk/metric" sdkmetric "go.opentelemetry.io/otel/sdk/metric"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"
) )
func boolPtr(b bool) *bool {
return &b
}
func Test_metricsConfig_load(t *testing.T) { func Test_metricsConfig_load(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
@ -47,8 +44,8 @@ func Test_metricsConfig_load(t *testing.T) {
namespaces: namespacesConfig{IncludeNamespaces: []string{"namespace1"}, ExcludeNamespaces: []string{"namespace2"}}, namespaces: namespacesConfig{IncludeNamespaces: []string{"namespace1"}, ExcludeNamespaces: []string{"namespace2"}},
bucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05}, bucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05},
metricsExposure: map[string]metricExposureConfig{ metricsExposure: map[string]metricExposureConfig{
"metric1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1"}, BucketBoundaries: []float64{0.005, 0.01, 0.025, 0.05}}, "metric1": {Enabled: ptr.To(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}}, "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"}}, 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}, 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{ 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}},
"metric2": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: []float64{0.025, 0.05}}, "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", name: "Case 2: there is no matching entry on the exposure config",
metricsExposure: map[string]metricExposureConfig{ 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, expectedSize: 1,
validateFunc: func(views []sdkmetric.View) bool { 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", name: "Case 3: metrics enabled, no transformation configured",
metricsExposure: map[string]metricExposureConfig{ metricsExposure: map[string]metricExposureConfig{
"metric1": {Enabled: boolPtr(true)}, "metric1": {Enabled: ptr.To(true)},
}, },
expectedSize: 1, expectedSize: 1,
validateFunc: func(views []sdkmetric.View) bool { validateFunc: func(views []sdkmetric.View) bool {
@ -133,7 +130,7 @@ func Test_metricsConfig_BuildMeterProviderViews(t *testing.T) {
{ {
name: "Case 4: metrics enabled, histogram metric", name: "Case 4: metrics enabled, histogram metric",
metricsExposure: map[string]metricExposureConfig{ 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, expectedSize: 1,
validateFunc: func(views []sdkmetric.View) bool { 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", name: "Case 5: metrics enabled, non histogram metric",
metricsExposure: map[string]metricExposureConfig{ 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, expectedSize: 1,
validateFunc: func(views []sdkmetric.View) bool { validateFunc: func(views []sdkmetric.View) bool {
@ -164,7 +161,7 @@ func Test_metricsConfig_BuildMeterProviderViews(t *testing.T) {
{ {
name: "Case 6: metrics disabled", name: "Case 6: metrics disabled",
metricsExposure: map[string]metricExposureConfig{ metricsExposure: map[string]metricExposureConfig{
"metric1": {Enabled: boolPtr(false)}, "metric1": {Enabled: ptr.To(false)},
}, },
expectedSize: 1, expectedSize: 1,
validateFunc: func(views []sdkmetric.View) bool { validateFunc: func(views []sdkmetric.View) bool {

View file

@ -4,6 +4,8 @@ import (
"errors" "errors"
"reflect" "reflect"
"testing" "testing"
"k8s.io/utils/ptr"
) )
func Test_parseExclusions(t *testing.T) { func Test_parseExclusions(t *testing.T) {
@ -328,9 +330,6 @@ func Test_parseBucketBoundariesConfig(t *testing.T) {
} }
func Test_parseMetricExposureConfig(t *testing.T) { func Test_parseMetricExposureConfig(t *testing.T) {
boolPtr := func(b bool) *bool {
return &b
}
defaultBoundaries := []float64{0.005, 0.01} defaultBoundaries := []float64{0.005, 0.01}
tests := []struct { tests := []struct {
input string input string
@ -344,8 +343,8 @@ func Test_parseMetricExposureConfig(t *testing.T) {
"key2": {"enabled": false, "disabledLabelDimensions": [], "bucketBoundaries": [1.01, 2.5, 5, 10]} "key2": {"enabled": false, "disabledLabelDimensions": [], "bucketBoundaries": [1.01, 2.5, 5, 10]}
}`, }`,
expected: map[string]metricExposureConfig{ expected: map[string]metricExposureConfig{
"key1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: []float64{}}, "key1": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{"dim1", "dim2"}, BucketBoundaries: []float64{}},
"key2": {Enabled: boolPtr(false), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{1.01, 2.5, 5, 10}}, "key2": {Enabled: ptr.To(false), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{1.01, 2.5, 5, 10}},
}, },
expectedError: false, expectedError: false,
}, },
@ -355,7 +354,7 @@ func Test_parseMetricExposureConfig(t *testing.T) {
"key1": {"disabledLabelDimensions": ["dim1", "dim2"]} "key1": {"disabledLabelDimensions": ["dim1", "dim2"]}
}`, }`,
expected: map[string]metricExposureConfig{ 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, expectedError: false,
}, },
@ -365,7 +364,7 @@ func Test_parseMetricExposureConfig(t *testing.T) {
"key1": {"enabled": false} "key1": {"enabled": false}
}`, }`,
expected: map[string]metricExposureConfig{ expected: map[string]metricExposureConfig{
"key1": {Enabled: boolPtr(false), DisabledLabelDimensions: []string{}, BucketBoundaries: defaultBoundaries}, "key1": {Enabled: ptr.To(false), DisabledLabelDimensions: []string{}, BucketBoundaries: defaultBoundaries},
}, },
expectedError: false, expectedError: false,
}, },
@ -376,8 +375,8 @@ func Test_parseMetricExposureConfig(t *testing.T) {
"key2": {"bucketBoundaries": [1.01, 2.5, 5, 10]} "key2": {"bucketBoundaries": [1.01, 2.5, 5, 10]}
}`, }`,
expected: map[string]metricExposureConfig{ expected: map[string]metricExposureConfig{
"key1": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{}}, "key1": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{}},
"key2": {Enabled: boolPtr(true), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{1.01, 2.5, 5, 10}}, "key2": {Enabled: ptr.To(true), DisabledLabelDimensions: []string{}, BucketBoundaries: []float64{1.01, 2.5, 5, 10}},
}, },
expectedError: false, expectedError: false,
}, },

View file

@ -19,6 +19,7 @@ import (
"k8s.io/client-go/metadata" "k8s.io/client-go/metadata"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue" "k8s.io/client-go/util/workqueue"
"k8s.io/utils/ptr"
) )
const ( const (
@ -121,29 +122,22 @@ func (c *controller) deregisterEventHandlers() {
// Function to determine the deletion propagation policy // Function to determine the deletion propagation policy
func determinePropagationPolicy(metaObj metav1.Object, logger logr.Logger) *metav1.DeletionPropagation { func determinePropagationPolicy(metaObj metav1.Object, logger logr.Logger) *metav1.DeletionPropagation {
annotations := metaObj.GetAnnotations() annotations := metaObj.GetAnnotations()
var policy *metav1.DeletionPropagation if annotations == nil {
return nil
if annotations != nil { }
annotationPolicy := annotations[kyverno.AnnotationCleanupPropagationPolicy] switch annotations[kyverno.AnnotationCleanupPropagationPolicy] {
if annotationPolicy != "" {
switch annotationPolicy {
case "Foreground": case "Foreground":
fg := metav1.DeletePropagationForeground return ptr.To(metav1.DeletePropagationForeground)
policy = &fg
case "Background": case "Background":
bg := metav1.DeletePropagationBackground return ptr.To(metav1.DeletePropagationBackground)
policy = &bg
case "Orphan": case "Orphan":
orphan := metav1.DeletePropagationOrphan return ptr.To(metav1.DeletePropagationOrphan)
policy = &orphan
case "": case "":
return nil return nil
default: default:
logger.Info("Unknown propagationPolicy annotation, no global policy found", "policy", annotationPolicy) 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 { func (c *controller) reconcile(ctx context.Context, logger logr.Logger, itemKey string, _, _ string) error {

View file

@ -7,6 +7,7 @@ import (
"github.com/kyverno/kyverno/api/kyverno" "github.com/kyverno/kyverno/api/kyverno"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
) )
// TestDeterminePropagationPolicy tests the determinePropagationPolicy function // TestDeterminePropagationPolicy tests the determinePropagationPolicy function
@ -28,30 +29,21 @@ func TestDeterminePropagationPolicy(t *testing.T) {
annotations: map[string]string{ annotations: map[string]string{
kyverno.AnnotationCleanupPropagationPolicy: "Foreground", kyverno.AnnotationCleanupPropagationPolicy: "Foreground",
}, },
expectedPolicy: func() *metav1.DeletionPropagation { expectedPolicy: ptr.To(metav1.DeletePropagationForeground),
fg := metav1.DeletePropagationForeground
return &fg
}(),
}, },
{ {
name: "Background policy", name: "Background policy",
annotations: map[string]string{ annotations: map[string]string{
kyverno.AnnotationCleanupPropagationPolicy: "Background", kyverno.AnnotationCleanupPropagationPolicy: "Background",
}, },
expectedPolicy: func() *metav1.DeletionPropagation { expectedPolicy: ptr.To(metav1.DeletePropagationBackground),
bg := metav1.DeletePropagationBackground
return &bg
}(),
}, },
{ {
name: "Orphan policy", name: "Orphan policy",
annotations: map[string]string{ annotations: map[string]string{
kyverno.AnnotationCleanupPropagationPolicy: "Orphan", kyverno.AnnotationCleanupPropagationPolicy: "Orphan",
}, },
expectedPolicy: func() *metav1.DeletionPropagation { expectedPolicy: ptr.To(metav1.DeletePropagationOrphan),
orphan := metav1.DeletePropagationOrphan
return &orphan
}(),
}, },
{ {
name: "Empty annotation", name: "Empty annotation",
@ -75,10 +67,8 @@ func TestDeterminePropagationPolicy(t *testing.T) {
metaObj := &metav1.ObjectMeta{ metaObj := &metav1.ObjectMeta{
Annotations: tc.annotations, Annotations: tc.annotations,
} }
// Call the function // Call the function
policy := determinePropagationPolicy(metaObj, logger) policy := determinePropagationPolicy(metaObj, logger)
// Assert the result // Assert the result
assert.Equal(t, tc.expectedPolicy, policy) assert.Equal(t, tc.expectedPolicy, policy)
}) })