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:
parent
9f5e75a2b5
commit
bd3a6f066b
5 changed files with 41 additions and 61 deletions
|
@ -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-----
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
|
|
|
@ -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,30 +122,23 @@ 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 {
|
||||
if annotations == nil {
|
||||
return nil
|
||||
}
|
||||
switch annotations[kyverno.AnnotationCleanupPropagationPolicy] {
|
||||
case "Foreground":
|
||||
fg := metav1.DeletePropagationForeground
|
||||
policy = &fg
|
||||
return ptr.To(metav1.DeletePropagationForeground)
|
||||
case "Background":
|
||||
bg := metav1.DeletePropagationBackground
|
||||
policy = &bg
|
||||
return ptr.To(metav1.DeletePropagationBackground)
|
||||
case "Orphan":
|
||||
orphan := metav1.DeletePropagationOrphan
|
||||
policy = &orphan
|
||||
return ptr.To(metav1.DeletePropagationOrphan)
|
||||
case "":
|
||||
return nil
|
||||
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 {
|
||||
namespace, name, err := cache.SplitMetaNamespaceKey(itemKey)
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue