Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Replace k8s.io/utils/pointer with k8s.io/utils/ptr #2488

Merged
merged 1 commit into from Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/scratch-env/go.sum
Expand Up @@ -2018,8 +2018,9 @@ k8s.io/kms v0.28.3/go.mod h1:kSMjU2tg7vjqqoWVVCcmPmNZ/CofPsoTbSxAipCvZuE=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk=
modernc.org/cc/v3 v3.36.0/go.mod h1:NFUHyPn4ekoC/JHeZFfZurN6ixxawE1BnVonP/oahEI=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -26,7 +26,7 @@ require (
k8s.io/client-go v0.28.3
k8s.io/component-base v0.28.3
k8s.io/klog/v2 v2.110.1
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
sigs.k8s.io/yaml v1.4.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -644,8 +644,8 @@ k8s.io/kms v0.28.3 h1:jYwwAe96XELNjYWv1G4kNzizcFoZ50OOElvPansbw70=
k8s.io/kms v0.28.3/go.mod h1:kSMjU2tg7vjqqoWVVCcmPmNZ/CofPsoTbSxAipCvZuE=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 h1:LyMgNKD2P8Wn1iAwQU5OhxCKlKJy0sHc+PcDwFB24dQ=
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9/go.mod h1:wZK2AVp1uHCp4VamDVgBP2COHZjqD1T68Rf0CM3YjSM=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
4 changes: 2 additions & 2 deletions pkg/builder/controller_test.go
Expand Up @@ -33,7 +33,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -668,7 +668,7 @@ func doReconcileTest(ctx context.Context, nameSuffix string, mgr manager.Manager
Name: deployName,
Kind: "Deployment",
APIVersion: "apps/v1",
Controller: pointer.Bool(true),
Controller: ptr.To(true),
UID: dep.UID,
},
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/cache/cache.go
Expand Up @@ -33,7 +33,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
toolscache "k8s.io/client-go/tools/cache"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/cache/internal"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -381,7 +381,7 @@ func newCache(restConfig *rest.Config, opts Options) newCacheFunc {
},
Transform: config.Transform,
WatchErrorHandler: opts.DefaultWatchErrorHandler,
UnsafeDisableDeepCopy: pointer.BoolDeref(config.UnsafeDisableDeepCopy, false),
UnsafeDisableDeepCopy: ptr.Deref(config.UnsafeDisableDeepCopy, false),
NewInformer: opts.newInformer,
}),
readerFailOnMissingInformer: opts.ReaderFailOnMissingInformer,
Expand Down
6 changes: 3 additions & 3 deletions pkg/cache/cache_test.go
Expand Up @@ -40,7 +40,7 @@ import (
kscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
kcache "k8s.io/client-go/tools/cache"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -146,10 +146,10 @@ var _ = Describe("Multi-Namespace Informer Cache", func() {

var _ = Describe("Informer Cache without global DeepCopy", func() {
CacheTest(cache.New, cache.Options{
DefaultUnsafeDisableDeepCopy: pointer.Bool(true),
DefaultUnsafeDisableDeepCopy: ptr.To(true),
})
NonBlockingGetTest(cache.New, cache.Options{
DefaultUnsafeDisableDeepCopy: pointer.Bool(true),
DefaultUnsafeDisableDeepCopy: ptr.To(true),
})
})

Expand Down
12 changes: 6 additions & 6 deletions pkg/cache/defaulting_test.go
Expand Up @@ -30,7 +30,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -187,23 +187,23 @@ func TestDefaultOpts(t *testing.T) {
name: "ByObject.UnsafeDisableDeepCopy gets defaulted from DefaultUnsafeDisableDeepCopy",
in: Options{
ByObject: map[client.Object]ByObject{pod: {}},
DefaultUnsafeDisableDeepCopy: pointer.Bool(true),
DefaultUnsafeDisableDeepCopy: ptr.To(true),
},

verification: func(o Options) string {
expected := pointer.Bool(true)
expected := ptr.To(true)
return cmp.Diff(expected, o.ByObject[pod].UnsafeDisableDeepCopy)
},
},
{
name: "ByObject.UnsafeDisableDeepCopy doesn't get defaulted when set",
in: Options{
ByObject: map[client.Object]ByObject{pod: {UnsafeDisableDeepCopy: pointer.Bool(false)}},
DefaultUnsafeDisableDeepCopy: pointer.Bool(true),
ByObject: map[client.Object]ByObject{pod: {UnsafeDisableDeepCopy: ptr.To(false)}},
DefaultUnsafeDisableDeepCopy: ptr.To(true),
},

verification: func(o Options) string {
expected := pointer.Bool(false)
expected := ptr.To(false)
return cmp.Diff(expected, o.ByObject[pod].UnsafeDisableDeepCopy)
},
},
Expand Down
10 changes: 3 additions & 7 deletions pkg/client/client_test.go
Expand Up @@ -41,7 +41,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
kscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/examples/crd/pkg"
"sigs.k8s.io/controller-runtime/pkg/cache"
Expand Down Expand Up @@ -391,7 +391,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC

Context("with the DryRun option", func() {
It("should not create a new object, global option", func() {
cl, err := client.New(cfg, client.Options{DryRun: pointer.Bool(true)})
cl, err := client.New(cfg, client.Options{DryRun: ptr.To(true)})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())

Expand Down Expand Up @@ -843,7 +843,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC

By("Creating the eviction")
eviction := &policyv1.Eviction{
DeleteOptions: &metav1.DeleteOptions{GracePeriodSeconds: ptr(int64(0))},
DeleteOptions: &metav1.DeleteOptions{GracePeriodSeconds: ptr.To(int64(0))},
}
err = cl.SubResource("eviction").Create(ctx, pod, eviction)
Expect((err)).NotTo(HaveOccurred())
Expand Down Expand Up @@ -3965,10 +3965,6 @@ func (f *fakeUncachedReader) List(_ context.Context, _ client.ObjectList, _ ...c
return &cache.ErrResourceNotCached{}
}

func ptr[T any](to T) *T {
return &to
}

func toUnstructured(o client.Object) (*unstructured.Unstructured, error) {
serialized, err := json.Marshal(o)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/dryrun_test.go
Expand Up @@ -28,7 +28,7 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand All @@ -41,7 +41,7 @@ var _ = Describe("DryRunClient", func() {
ctx := context.Background()

getClient := func() client.Client {
cl, err := client.New(cfg, client.Options{DryRun: pointer.Bool(true)})
cl, err := client.New(cfg, client.Options{DryRun: ptr.To(true)})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())
return cl
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/fake/client_test.go
Expand Up @@ -40,7 +40,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
Expand Down Expand Up @@ -1610,7 +1610,7 @@ var _ = Describe("Fake client", func() {
objOriginal.APIVersion = actual.APIVersion
objOriginal.Kind = actual.Kind
objOriginal.ResourceVersion = actual.ResourceVersion
objOriginal.Spec.Replicas = pointer.Int32(2)
objOriginal.Spec.Replicas = ptr.To(int32(2))
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
})

Expand Down
8 changes: 4 additions & 4 deletions pkg/client/options_test.go
Expand Up @@ -23,7 +23,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
utilpointer "k8s.io/utils/pointer"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -112,7 +112,7 @@ var _ = Describe("CreateOptions", func() {

var _ = Describe("DeleteOptions", func() {
It("Should set GracePeriodSeconds", func() {
o := &client.DeleteOptions{GracePeriodSeconds: utilpointer.Int64(42)}
o := &client.DeleteOptions{GracePeriodSeconds: ptr.To(int64(42))}
newDeleteOpts := &client.DeleteOptions{}
o.ApplyToDelete(newDeleteOpts)
Expect(newDeleteOpts).To(Equal(o))
Expand Down Expand Up @@ -185,7 +185,7 @@ var _ = Describe("PatchOptions", func() {
Expect(newPatchOpts).To(Equal(o))
})
It("Should set Force", func() {
o := &client.PatchOptions{Force: utilpointer.Bool(true)}
o := &client.PatchOptions{Force: ptr.To(true)}
newPatchOpts := &client.PatchOptions{}
o.ApplyToPatch(newPatchOpts)
Expect(newPatchOpts).To(Equal(o))
Expand Down Expand Up @@ -218,7 +218,7 @@ var _ = Describe("DeleteAllOfOptions", func() {
Expect(newDeleteAllOfOpts).To(Equal(o))
})
It("Should set DeleleteOptions", func() {
o := &client.DeleteAllOfOptions{DeleteOptions: client.DeleteOptions{GracePeriodSeconds: utilpointer.Int64(44)}}
o := &client.DeleteAllOfOptions{DeleteOptions: client.DeleteOptions{GracePeriodSeconds: ptr.To(int64(44))}}
newDeleteAllOfOpts := &client.DeleteAllOfOptions{}
o.ApplyToDeleteAllOf(newDeleteAllOfOpts)
Expect(newDeleteAllOfOpts).To(Equal(o))
Expand Down
16 changes: 8 additions & 8 deletions pkg/controller/controller_test.go
Expand Up @@ -24,7 +24,7 @@ import (
. "github.com/onsi/gomega"
"go.uber.org/goleak"
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -134,7 +134,7 @@ var _ = Describe("controller.Controller", func() {
})

It("should default RecoverPanic from the manager", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{RecoverPanic: pointer.Bool(true)}})
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{RecoverPanic: ptr.To(true)}})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Expand All @@ -150,11 +150,11 @@ var _ = Describe("controller.Controller", func() {
})

It("should not override RecoverPanic on the controller", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{RecoverPanic: pointer.Bool(true)}})
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{RecoverPanic: ptr.To(true)}})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
RecoverPanic: pointer.Bool(false),
RecoverPanic: ptr.To(false),
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
Expand All @@ -167,7 +167,7 @@ var _ = Describe("controller.Controller", func() {
})

It("should default NeedLeaderElection from the manager", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{NeedLeaderElection: pointer.Bool(true)}})
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{NeedLeaderElection: ptr.To(true)}})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
Expand All @@ -182,11 +182,11 @@ var _ = Describe("controller.Controller", func() {
})

It("should not override NeedLeaderElection on the controller", func() {
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{NeedLeaderElection: pointer.Bool(true)}})
m, err := manager.New(cfg, manager.Options{Controller: config.Controller{NeedLeaderElection: ptr.To(true)}})
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
NeedLeaderElection: pointer.Bool(false),
NeedLeaderElection: ptr.To(false),
Reconciler: reconcile.Func(nil),
})
Expect(err).NotTo(HaveOccurred())
Expand Down Expand Up @@ -309,7 +309,7 @@ var _ = Describe("controller.Controller", func() {
Expect(err).NotTo(HaveOccurred())

c, err := controller.New("new-controller", m, controller.Options{
NeedLeaderElection: pointer.Bool(false),
NeedLeaderElection: ptr.To(false),
Reconciler: rec,
})
Expect(err).NotTo(HaveOccurred())
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/controllerutil/controllerutil.go
Expand Up @@ -27,7 +27,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
Expand Down Expand Up @@ -77,8 +77,8 @@ func SetControllerReference(owner, controlled metav1.Object, scheme *runtime.Sch
Kind: gvk.Kind,
Name: owner.GetName(),
UID: owner.GetUID(),
BlockOwnerDeletion: pointer.Bool(true),
Controller: pointer.Bool(true),
BlockOwnerDeletion: ptr.To(true),
Controller: ptr.To(true),
}

// Return early with an error if the object is already controlled.
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/controllerutil/controllerutil_test.go
Expand Up @@ -30,7 +30,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -733,7 +733,7 @@ var _ = Describe("Controllerutil", func() {
assertLocalDeployWasUpdated(nil)

op, err = controllerutil.CreateOrPatch(context.TODO(), c, deploy, func() error {
deploy.Spec.Replicas = pointer.Int32(5)
deploy.Spec.Replicas = ptr.To(int32(5))
deploy.Status.Conditions = []appsv1.DeploymentCondition{{
Type: appsv1.DeploymentProgressing,
Status: corev1.ConditionTrue,
Expand Down
4 changes: 2 additions & 2 deletions pkg/envtest/crd.go
Expand Up @@ -38,7 +38,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/util/retry"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
"sigs.k8s.io/yaml"

"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -364,7 +364,7 @@ func modifyConversionWebhooks(crds []*apiextensionsv1.CustomResourceDefinition,
if err != nil {
return err
}
url := pointer.String(fmt.Sprintf("https://%s/convert", hostPort))
url := ptr.To(fmt.Sprintf("https://%s/convert", hostPort))

for i := range crds {
// Continue if we're preserving unknown fields.
Expand Down
2 changes: 1 addition & 1 deletion pkg/envtest/komega/default.go
Expand Up @@ -83,7 +83,7 @@ func UpdateStatus(obj client.Object, f func(), opts ...client.SubResourceUpdateO
// It can be used with gomega.Eventually() like this:
//
// deployment := appsv1.Deployment{ ... }
// gomega.Eventually(k.Object(&deployment)).To(HaveField("Spec.Replicas", gomega.Equal(pointer.Int32(3))))
// gomega.Eventually(k.Object(&deployment)).To(HaveField("Spec.Replicas", gomega.Equal(ptr.To(3))))
//
// By calling the returned function directly it can also be used as gomega.Expect(k.Object(...)()).To(...)
func Object(obj client.Object) func() (client.Object, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/envtest/komega/default_test.go
Expand Up @@ -6,7 +6,7 @@ import (
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
"k8s.io/utils/ptr"
)

func TestDefaultGet(t *testing.T) {
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestDefaultObject(t *testing.T) {
}
g.Eventually(Object(&fetched)).Should(And(
Not(BeNil()),
HaveField("Spec.Replicas", Equal(pointer.Int32(5))),
HaveField("Spec.Replicas", Equal(ptr.To(int32(5)))),
))
}

Expand All @@ -110,7 +110,7 @@ func TestDefaultObjectList(t *testing.T) {
Not(BeNil()),
HaveField("Items", And(
HaveLen(1),
ContainElement(HaveField("Spec.Replicas", Equal(pointer.Int32(5)))),
ContainElement(HaveField("Spec.Replicas", Equal(ptr.To(int32(5))))),
)),
))
}