chore: replacement of helper functions to ptr packge

This commit is contained in:
PatrickLaabs 2025-07-07 21:28:54 +02:00
parent 101c68a3d9
commit cfd65c5f74
6 changed files with 26 additions and 39 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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