From cfd65c5f74dbb5fa02e7b20c3b1039193567ba53 Mon Sep 17 00:00:00 2001 From: PatrickLaabs Date: Mon, 7 Jul 2025 21:28:54 +0200 Subject: [PATCH] chore: replacement of helper functions to ptr packge --- pkg/kubelet/container/helpers_test.go | 8 +++----- .../pkg/apis/meta/v1/validation/validation_test.go | 12 ++++-------- .../apiserver/pkg/audit/policy/checker_test.go | 9 +++------ .../pkg/genericclioptions/builder_flags.go | 9 +++------ test/integration/replicaset/replicaset_test.go | 13 ++++++------- .../replicationcontroller_test.go | 14 +++++++------- 6 files changed, 26 insertions(+), 39 deletions(-) diff --git a/pkg/kubelet/container/helpers_test.go b/pkg/kubelet/container/helpers_test.go index 38adccc2098..80abe5b9920 100644 --- a/pkg/kubelet/container/helpers_test.go +++ b/pkg/kubelet/container/helpers_test.go @@ -29,6 +29,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" + "k8s.io/utils/ptr" ) func TestEnvVarsToMap(t *testing.T) { @@ -493,9 +494,6 @@ func TestShouldContainerBeRestarted(t *testing.T) { } func TestHasPrivilegedContainer(t *testing.T) { - newBoolPtr := func(b bool) *bool { - return &b - } tests := map[string]struct { securityContext *v1.SecurityContext expected bool @@ -509,11 +507,11 @@ func TestHasPrivilegedContainer(t *testing.T) { expected: false, }, "false privileged": { - securityContext: &v1.SecurityContext{Privileged: newBoolPtr(false)}, + securityContext: &v1.SecurityContext{Privileged: ptr.To(false)}, expected: false, }, "true privileged": { - securityContext: &v1.SecurityContext{Privileged: newBoolPtr(true)}, + securityContext: &v1.SecurityContext{Privileged: ptr.To(true)}, expected: true, }, } diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/validation/validation_test.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/validation/validation_test.go index 6cf7629e828..5bb363d3a68 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/validation/validation_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/validation/validation_test.go @@ -132,10 +132,6 @@ func TestInvalidDryRun(t *testing.T) { } -func boolPtr(b bool) *bool { - return &b -} - func TestValidateDeleteOptionsWithIgnoreStoreReadError(t *testing.T) { fieldPath := field.NewPath("ignoreStoreReadErrorWithClusterBreakingPotential") tests := []struct { @@ -232,7 +228,7 @@ func TestValidPatchOptions(t *testing.T) { patchType types.PatchType }{{ opts: metav1.PatchOptions{ - Force: boolPtr(true), + Force: ptr.To(true), FieldManager: "kubectl", }, patchType: types.ApplyYAMLPatchType, @@ -243,7 +239,7 @@ func TestValidPatchOptions(t *testing.T) { patchType: types.ApplyYAMLPatchType, }, { opts: metav1.PatchOptions{ - Force: boolPtr(true), + Force: ptr.To(true), FieldManager: "kubectl", }, patchType: types.ApplyCBORPatchType, @@ -290,7 +286,7 @@ func TestInvalidPatchOptions(t *testing.T) { // force on non-apply { opts: metav1.PatchOptions{ - Force: boolPtr(true), + Force: ptr.To(true), }, patchType: types.MergePatchType, }, @@ -298,7 +294,7 @@ func TestInvalidPatchOptions(t *testing.T) { { opts: metav1.PatchOptions{ FieldManager: "kubectl", - Force: boolPtr(false), + Force: ptr.To(false), }, patchType: types.MergePatchType, }, diff --git a/staging/src/k8s.io/apiserver/pkg/audit/policy/checker_test.go b/staging/src/k8s.io/apiserver/pkg/audit/policy/checker_test.go index dd109ceaa0a..1e889c3c678 100644 --- a/staging/src/k8s.io/apiserver/pkg/audit/policy/checker_test.go +++ b/staging/src/k8s.io/apiserver/pkg/audit/policy/checker_test.go @@ -26,6 +26,7 @@ import ( "k8s.io/apiserver/pkg/apis/audit" "k8s.io/apiserver/pkg/authentication/user" "k8s.io/apiserver/pkg/authorization/authorizer" + "k8s.io/utils/ptr" ) var ( @@ -359,10 +360,6 @@ func TestOmitManagedFields(t *testing.T) { }, } - boolPtr := func(v bool) *bool { - return &v - } - tests := []struct { name string policy func() audit.Policy @@ -395,7 +392,7 @@ func TestOmitManagedFields(t *testing.T) { name: "global policy default is true, rule overrides to false", policy: func() audit.Policy { rule := matchingPolicyRule.DeepCopy() - rule.OmitManagedFields = boolPtr(false) + rule.OmitManagedFields = ptr.To(false) return audit.Policy{ OmitManagedFields: true, Rules: []audit.PolicyRule{*rule}, @@ -407,7 +404,7 @@ func TestOmitManagedFields(t *testing.T) { name: "global policy default is false, rule overrides to true", policy: func() audit.Policy { rule := matchingPolicyRule.DeepCopy() - rule.OmitManagedFields = boolPtr(true) + rule.OmitManagedFields = ptr.To(true) return audit.Policy{ OmitManagedFields: false, Rules: []audit.PolicyRule{*rule}, diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags.go index acf576a366e..8adb1d97d46 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags.go @@ -21,6 +21,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/cli-runtime/pkg/resource" + "k8s.io/utils/ptr" ) // ResourceBuilderFlags are flags for finding resources @@ -47,7 +48,7 @@ func NewResourceBuilderFlags() *ResourceBuilderFlags { FileNameFlags: &FileNameFlags{ Usage: "identifying the resource.", Filenames: &filenames, - Recursive: boolPtr(true), + Recursive: ptr.To(true), }, } } @@ -59,7 +60,7 @@ func (o *ResourceBuilderFlags) WithFile(recurse bool, files ...string) *Resource o.FileNameFlags = &FileNameFlags{ Usage: "identifying the resource.", Filenames: &files, - Recursive: boolPtr(recurse), + Recursive: ptr.To(recurse), } return o @@ -225,7 +226,3 @@ func ResourceFinderForResult(result resource.Visitor) ResourceFinder { return result }) } - -func boolPtr(val bool) *bool { - return &val -} diff --git a/test/integration/replicaset/replicaset_test.go b/test/integration/replicaset/replicaset_test.go index 48918a28dde..e44793fab95 100644 --- a/test/integration/replicaset/replicaset_test.go +++ b/test/integration/replicaset/replicaset_test.go @@ -394,7 +394,6 @@ func testScalingUsingScaleSubresource(t *testing.T, c clientset.Interface, rs *a } func TestAdoption(t *testing.T) { - boolPtr := func(b bool) *bool { return &b } testCases := []struct { name string existingOwnerReferences func(rs *apps.ReplicaSet) []metav1.OwnerReference @@ -406,7 +405,7 @@ func TestAdoption(t *testing.T) { return []metav1.OwnerReference{{UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet"}} }, func(rs *apps.ReplicaSet) []metav1.OwnerReference { - return []metav1.OwnerReference{{UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: boolPtr(true), BlockOwnerDeletion: boolPtr(true)}} + return []metav1.OwnerReference{{UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: ptr.To(true), BlockOwnerDeletion: ptr.To(true)}} }, }, { @@ -415,29 +414,29 @@ func TestAdoption(t *testing.T) { return []metav1.OwnerReference{} }, func(rs *apps.ReplicaSet) []metav1.OwnerReference { - return []metav1.OwnerReference{{UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: boolPtr(true), BlockOwnerDeletion: boolPtr(true)}} + return []metav1.OwnerReference{{UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: ptr.To(true), BlockOwnerDeletion: ptr.To(true)}} }, }, { "pod refers rs as a controller", func(rs *apps.ReplicaSet) []metav1.OwnerReference { - return []metav1.OwnerReference{{UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: boolPtr(true)}} + return []metav1.OwnerReference{{UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: ptr.To(true)}} }, func(rs *apps.ReplicaSet) []metav1.OwnerReference { - return []metav1.OwnerReference{{UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: boolPtr(true)}} + return []metav1.OwnerReference{{UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: ptr.To(true)}} }, }, { "pod refers other rs as the controller, refers the rs as an owner", func(rs *apps.ReplicaSet) []metav1.OwnerReference { return []metav1.OwnerReference{ - {UID: "1", Name: "anotherRS", APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: boolPtr(true)}, + {UID: "1", Name: "anotherRS", APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: ptr.To(true)}, {UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet"}, } }, func(rs *apps.ReplicaSet) []metav1.OwnerReference { return []metav1.OwnerReference{ - {UID: "1", Name: "anotherRS", APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: boolPtr(true)}, + {UID: "1", Name: "anotherRS", APIVersion: "apps/v1", Kind: "ReplicaSet", Controller: ptr.To(true)}, {UID: rs.UID, Name: rs.Name, APIVersion: "apps/v1", Kind: "ReplicaSet"}, } }, diff --git a/test/integration/replicationcontroller/replicationcontroller_test.go b/test/integration/replicationcontroller/replicationcontroller_test.go index 632fee80902..26bf7f7a9a4 100644 --- a/test/integration/replicationcontroller/replicationcontroller_test.go +++ b/test/integration/replicationcontroller/replicationcontroller_test.go @@ -44,6 +44,7 @@ import ( "k8s.io/kubernetes/pkg/features" "k8s.io/kubernetes/test/integration/framework" "k8s.io/kubernetes/test/utils/ktesting" + "k8s.io/utils/ptr" ) const ( @@ -367,7 +368,6 @@ func testScalingUsingScaleSubresource(t *testing.T, c clientset.Interface, rc *v } func TestAdoption(t *testing.T) { - boolPtr := func(b bool) *bool { return &b } testCases := []struct { name string existingOwnerReferences func(rc *v1.ReplicationController) []metav1.OwnerReference @@ -379,7 +379,7 @@ func TestAdoption(t *testing.T) { return []metav1.OwnerReference{{UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController"}} }, func(rc *v1.ReplicationController) []metav1.OwnerReference { - return []metav1.OwnerReference{{UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController", Controller: boolPtr(true), BlockOwnerDeletion: boolPtr(true)}} + return []metav1.OwnerReference{{UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController", Controller: ptr.To(true), BlockOwnerDeletion: ptr.To(true)}} }, }, { @@ -388,29 +388,29 @@ func TestAdoption(t *testing.T) { return []metav1.OwnerReference{} }, func(rc *v1.ReplicationController) []metav1.OwnerReference { - return []metav1.OwnerReference{{UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController", Controller: boolPtr(true), BlockOwnerDeletion: boolPtr(true)}} + return []metav1.OwnerReference{{UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController", Controller: ptr.To(true), BlockOwnerDeletion: ptr.To(true)}} }, }, { "pod refers rc as a controller", func(rc *v1.ReplicationController) []metav1.OwnerReference { - return []metav1.OwnerReference{{UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController", Controller: boolPtr(true)}} + return []metav1.OwnerReference{{UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController", Controller: ptr.To(true)}} }, func(rc *v1.ReplicationController) []metav1.OwnerReference { - return []metav1.OwnerReference{{UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController", Controller: boolPtr(true)}} + return []metav1.OwnerReference{{UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController", Controller: ptr.To(true)}} }, }, { "pod refers other rc as the controller, refers the rc as an owner", func(rc *v1.ReplicationController) []metav1.OwnerReference { return []metav1.OwnerReference{ - {UID: "1", Name: "anotherRC", APIVersion: "v1", Kind: "ReplicationController", Controller: boolPtr(true)}, + {UID: "1", Name: "anotherRC", APIVersion: "v1", Kind: "ReplicationController", Controller: ptr.To(true)}, {UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController"}, } }, func(rc *v1.ReplicationController) []metav1.OwnerReference { return []metav1.OwnerReference{ - {UID: "1", Name: "anotherRC", APIVersion: "v1", Kind: "ReplicationController", Controller: boolPtr(true)}, + {UID: "1", Name: "anotherRC", APIVersion: "v1", Kind: "ReplicationController", Controller: ptr.To(true)}, {UID: rc.UID, Name: rc.Name, APIVersion: "v1", Kind: "ReplicationController"}, } },