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"
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-----

View file

@ -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 {

View file

@ -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,
},

View file

@ -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 {

View file

@ -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)
})