Skip to content

Commit bce0ec7

Browse files
authored
Merge pull request #3489 from kunalmemane/fix-modernize-lint
🌱 Adopt new(x) over ptr.To(x) and re-enable newexpr lint
2 parents 598e330 + cca31b0 commit bce0ec7

38 files changed

+150
-184
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ linters:
8181
disable:
8282
- omitzero
8383
- fmtappendf
84-
- newexpr
8584
revive:
8685
rules:
8786
# The following rules are recommended https://github.com/mgechev/revive#recommended-configuration

designs/warmreplicas.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Warmup runnables are also stopped in parallel with non-leader runnables during s
4242

4343
Controllers expose the option via:
4444

45-
- `ctrl.Options{Controller: config.Controller{EnableWarmup: ptr.To(true)}}`
45+
- `ctrl.Options{Controller: config.Controller{EnableWarmup: new(true)}}`
4646

4747
If both `EnableWarmup` and `NeedLeaderElection` are true, controller-runtime registers the controller as a warmup runnable. Calling `Warmup` launches the same event sources and cache sync logic as `Start`, but it does **not** start worker goroutines. Once the manager becomes leader, the controller’s normal `Start` simply spins up workers against the already-initialized queue. Enabling warmup on a controller that does not use leader election is a no-op, as the worker threads do not block on leader election being won.
4848

@@ -51,7 +51,7 @@ If both `EnableWarmup` and `NeedLeaderElection` are true, controller-runtime reg
5151
```go
5252
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
5353
Controller: config.Controller{
54-
EnableWarmup: ptr.To(true), // make every controller warm up
54+
EnableWarmup: new(true), // make every controller warm up
5555
},
5656
})
5757
if err != nil {
@@ -61,7 +61,7 @@ if err != nil {
6161
builder.ControllerManagedBy(mgr).
6262
Named("slow-source").
6363
WithOptions(controller.Options{
64-
EnableWarmup: ptr.To(true), // optional per-controller override
64+
EnableWarmup: new(true), // optional per-controller override
6565
}).
6666
For(&examplev1.Example{}).
6767
Complete(reconciler)

pkg/builder/controller_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"k8s.io/apimachinery/pkg/types"
3434
"k8s.io/client-go/rest"
3535
"k8s.io/client-go/util/workqueue"
36-
"k8s.io/utils/ptr"
3736

3837
"sigs.k8s.io/controller-runtime/pkg/cache"
3938
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -711,7 +710,7 @@ func doReconcileTest(ctx context.Context, nameSuffix string, mgr manager.Manager
711710
Name: deployName,
712711
Kind: "Deployment",
713712
APIVersion: "apps/v1",
714-
Controller: ptr.To(true),
713+
Controller: new(true),
715714
UID: dep.UID,
716715
},
717716
},

pkg/cache/cache_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import (
4242
kscheme "k8s.io/client-go/kubernetes/scheme"
4343
"k8s.io/client-go/rest"
4444
kcache "k8s.io/client-go/tools/cache"
45-
"k8s.io/utils/ptr"
4645

4746
"sigs.k8s.io/controller-runtime/pkg/cache"
4847
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -148,10 +147,10 @@ var _ = Describe("Multi-Namespace Informer Cache", func() {
148147

149148
var _ = Describe("Informer Cache without global DeepCopy", func() {
150149
CacheTest(cache.New, cache.Options{
151-
DefaultUnsafeDisableDeepCopy: ptr.To(true),
150+
DefaultUnsafeDisableDeepCopy: new(true),
152151
})
153152
NonBlockingGetTest(cache.New, cache.Options{
154-
DefaultUnsafeDisableDeepCopy: ptr.To(true),
153+
DefaultUnsafeDisableDeepCopy: new(true),
155154
})
156155
})
157156

pkg/cache/defaulting_test.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"k8s.io/apimachinery/pkg/runtime/schema"
3333
"k8s.io/client-go/rest"
3434
"k8s.io/client-go/tools/cache"
35-
"k8s.io/utils/ptr"
3635
"sigs.k8s.io/controller-runtime/pkg/client"
3736
)
3837

@@ -205,71 +204,71 @@ func TestDefaultOpts(t *testing.T) {
205204
name: "ByObject.UnsafeDisableDeepCopy gets defaulted from DefaultUnsafeDisableDeepCopy",
206205
in: Options{
207206
ByObject: map[client.Object]ByObject{pod: {}},
208-
DefaultUnsafeDisableDeepCopy: ptr.To(true),
207+
DefaultUnsafeDisableDeepCopy: new(true),
209208
},
210209

211210
verification: func(o Options) string {
212-
expected := ptr.To(true)
211+
expected := new(true)
213212
return cmp.Diff(expected, o.ByObject[pod].UnsafeDisableDeepCopy)
214213
},
215214
},
216215
{
217216
name: "ByObject.UnsafeDisableDeepCopy doesn't get defaulted when set",
218217
in: Options{
219-
ByObject: map[client.Object]ByObject{pod: {UnsafeDisableDeepCopy: ptr.To(false)}},
220-
DefaultUnsafeDisableDeepCopy: ptr.To(true),
218+
ByObject: map[client.Object]ByObject{pod: {UnsafeDisableDeepCopy: new(false)}},
219+
DefaultUnsafeDisableDeepCopy: new(true),
221220
},
222221

223222
verification: func(o Options) string {
224-
expected := ptr.To(false)
223+
expected := new(false)
225224
return cmp.Diff(expected, o.ByObject[pod].UnsafeDisableDeepCopy)
226225
},
227226
},
228227
{
229228
name: "ByObject.EnableWatchBookmarks gets defaulted from DefaultEnableWatchBookmarks",
230229
in: Options{
231230
ByObject: map[client.Object]ByObject{pod: {}},
232-
DefaultEnableWatchBookmarks: ptr.To(true),
231+
DefaultEnableWatchBookmarks: new(true),
233232
},
234233

235234
verification: func(o Options) string {
236-
expected := ptr.To(true)
235+
expected := new(true)
237236
return cmp.Diff(expected, o.ByObject[pod].EnableWatchBookmarks)
238237
},
239238
},
240239
{
241240
name: "ByObject.EnableWatchBookmarks doesn't get defaulted when set",
242241
in: Options{
243-
ByObject: map[client.Object]ByObject{pod: {EnableWatchBookmarks: ptr.To(false)}},
244-
DefaultEnableWatchBookmarks: ptr.To(true),
242+
ByObject: map[client.Object]ByObject{pod: {EnableWatchBookmarks: new(false)}},
243+
DefaultEnableWatchBookmarks: new(true),
245244
},
246245

247246
verification: func(o Options) string {
248-
expected := ptr.To(false)
247+
expected := new(false)
249248
return cmp.Diff(expected, o.ByObject[pod].EnableWatchBookmarks)
250249
},
251250
},
252251
{
253252
name: "ByObject.SyncPeriod gets defaulted from SyncPeriod",
254253
in: Options{
255254
ByObject: map[client.Object]ByObject{pod: {}},
256-
SyncPeriod: ptr.To(5 * time.Minute),
255+
SyncPeriod: new(5 * time.Minute),
257256
},
258257

259258
verification: func(o Options) string {
260-
expected := ptr.To(5 * time.Minute)
259+
expected := new(5 * time.Minute)
261260
return cmp.Diff(expected, o.ByObject[pod].SyncPeriod)
262261
},
263262
},
264263
{
265264
name: "ByObject.SyncPeriod doesn't get defaulted when set",
266265
in: Options{
267-
ByObject: map[client.Object]ByObject{pod: {SyncPeriod: ptr.To(1 * time.Minute)}},
268-
SyncPeriod: ptr.To(5 * time.Minute),
266+
ByObject: map[client.Object]ByObject{pod: {SyncPeriod: new(1 * time.Minute)}},
267+
SyncPeriod: new(5 * time.Minute),
269268
},
270269

271270
verification: func(o Options) string {
272-
expected := ptr.To(1 * time.Minute)
271+
expected := new(1 * time.Minute)
273272
return cmp.Diff(expected, o.ByObject[pod].SyncPeriod)
274273
},
275274
},

pkg/client/apiutil/restmapper.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"k8s.io/client-go/discovery"
2929
"k8s.io/client-go/rest"
3030
"k8s.io/client-go/restmapper"
31-
"k8s.io/utils/ptr"
3231
)
3332

3433
// NewDynamicRESTMapper returns a dynamic RESTMapper for cfg. The dynamic
@@ -197,7 +196,7 @@ func (m *mapper) addKnownGroupAndReload(groupName string, versions ...string) er
197196
}
198197
}
199198
if len(failedGroups) > 0 {
200-
return ptr.To(ErrResourceDiscoveryFailed(failedGroups))
199+
return new(ErrResourceDiscoveryFailed(failedGroups))
201200
}
202201
return nil
203202
}

pkg/client/client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
516516

517517
Context("with the DryRun option", func() {
518518
It("should not create a new object, global option", func(ctx SpecContext) {
519-
cl, err := client.New(cfg, client.Options{DryRun: ptr.To(true)})
519+
cl, err := client.New(cfg, client.Options{DryRun: new(true)})
520520
Expect(err).NotTo(HaveOccurred())
521521
Expect(cl).NotTo(BeNil())
522522

@@ -1038,7 +1038,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
10381038
Expect(cm.Data).To(BeComparableTo(obj.Data))
10391039

10401040
// Apply with ResourceVersion unset
1041-
obj.ResourceVersion = ptr.To("")
1041+
obj.ResourceVersion = new("")
10421042
data = map[string]string{
10431043
"a-new-key": "a-new-value",
10441044
}
@@ -1149,7 +1149,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
11491149

11501150
By("Creating the eviction")
11511151
eviction := &policyv1.Eviction{
1152-
DeleteOptions: &metav1.DeleteOptions{GracePeriodSeconds: ptr.To(int64(0))},
1152+
DeleteOptions: &metav1.DeleteOptions{GracePeriodSeconds: new(int64(0))},
11531153
}
11541154
err = cl.SubResource("eviction").Create(ctx, pod, eviction, &client.SubResourceCreateOptions{})
11551155
Expect((err)).NotTo(HaveOccurred())
@@ -1665,7 +1665,7 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
16651665
deploymentAC, err := appsv1applyconfigurations.ExtractDeployment(dep, "foo")
16661666
Expect(err).NotTo(HaveOccurred())
16671667
deploymentAC.WithStatus(&appsv1applyconfigurations.DeploymentStatusApplyConfiguration{
1668-
Replicas: ptr.To(int32(1)),
1668+
Replicas: new(int32(1)),
16691669
})
16701670
Expect(cl.Status().Apply(ctx, deploymentAC, client.FieldOwner("foo"))).To(Succeed())
16711671

pkg/client/dryrun_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/types"
3030
appsv1applyconfigurations "k8s.io/client-go/applyconfigurations/apps/v1"
31-
"k8s.io/utils/ptr"
3231

3332
"sigs.k8s.io/controller-runtime/pkg/client"
3433
)
@@ -40,7 +39,7 @@ var _ = Describe("DryRunClient", func() {
4039
var ns = "default"
4140

4241
getClient := func() client.Client {
43-
cl, err := client.New(cfg, client.Options{DryRun: ptr.To(true)})
42+
cl, err := client.New(cfg, client.Options{DryRun: new(true)})
4443
Expect(err).NotTo(HaveOccurred())
4544
Expect(cl).NotTo(BeNil())
4645
return cl
@@ -266,7 +265,7 @@ var _ = Describe("DryRunClient", func() {
266265
deploymentAC, err := appsv1applyconfigurations.ExtractDeployment(dep, "test-owner")
267266
Expect(err).NotTo(HaveOccurred())
268267
deploymentAC.WithStatus(&appsv1applyconfigurations.DeploymentStatusApplyConfiguration{
269-
Replicas: ptr.To(int32(99)),
268+
Replicas: new(int32(99)),
270269
})
271270

272271
Expect(getClient().Status().Apply(ctx, deploymentAC, client.FieldOwner("test-owner"))).NotTo(HaveOccurred())
@@ -281,7 +280,7 @@ var _ = Describe("DryRunClient", func() {
281280
deploymentAC, err := appsv1applyconfigurations.ExtractDeployment(dep, "test-owner")
282281
Expect(err).NotTo(HaveOccurred())
283282
deploymentAC.WithStatus(&appsv1applyconfigurations.DeploymentStatusApplyConfiguration{
284-
Replicas: ptr.To(int32(99)),
283+
Replicas: new(int32(99)),
285284
})
286285

287286
opts := &client.SubResourceApplyOptions{ApplyOptions: client.ApplyOptions{DryRun: []string{"Bye", "Pippa"}}}

pkg/client/fake/client.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ import (
6767
clientgoapplyconfigurations "k8s.io/client-go/applyconfigurations"
6868
"k8s.io/client-go/kubernetes/scheme"
6969
"k8s.io/client-go/testing"
70-
"k8s.io/utils/ptr"
7170

7271
"sigs.k8s.io/controller-runtime/pkg/client"
7372
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
@@ -1621,13 +1620,13 @@ func extractScale(obj client.Object) (*autoscalingv1.Scale, error) {
16211620
func applyScale(obj client.Object, scale *autoscalingv1.Scale) error {
16221621
switch obj := obj.(type) {
16231622
case *appsv1.Deployment:
1624-
obj.Spec.Replicas = ptr.To(scale.Spec.Replicas)
1623+
obj.Spec.Replicas = new(scale.Spec.Replicas)
16251624
case *appsv1.ReplicaSet:
1626-
obj.Spec.Replicas = ptr.To(scale.Spec.Replicas)
1625+
obj.Spec.Replicas = new(scale.Spec.Replicas)
16271626
case *corev1.ReplicationController:
1628-
obj.Spec.Replicas = ptr.To(scale.Spec.Replicas)
1627+
obj.Spec.Replicas = new(scale.Spec.Replicas)
16291628
case *appsv1.StatefulSet:
1630-
obj.Spec.Replicas = ptr.To(scale.Spec.Replicas)
1629+
obj.Spec.Replicas = new(scale.Spec.Replicas)
16311630
default:
16321631
// TODO: CRDs https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#scale-subresource
16331632
return fmt.Errorf("unimplemented scale subresource for resource %T", obj)

pkg/client/fake/client_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ import (
5050
"k8s.io/client-go/kubernetes/fake"
5151
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
5252
"k8s.io/client-go/testing"
53-
"k8s.io/utils/ptr"
5453

5554
"sigs.k8s.io/controller-runtime/pkg/client"
5655
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
@@ -1727,7 +1726,7 @@ var _ = Describe("Fake client", func() {
17271726
objOriginal.APIVersion = actual.APIVersion
17281727
objOriginal.Kind = actual.Kind
17291728
objOriginal.ResourceVersion = actual.ResourceVersion
1730-
objOriginal.Spec.Replicas = ptr.To(int32(2))
1729+
objOriginal.Spec.Replicas = new(int32(2))
17311730
Expect(cmp.Diff(objOriginal, actual)).To(BeEmpty())
17321731
})
17331732

@@ -2590,7 +2589,7 @@ var _ = Describe("Fake client", func() {
25902589
Name: "foo",
25912590
},
25922591
Spec: appsv1.DeploymentSpec{
2593-
Replicas: ptr.To[int32](2),
2592+
Replicas: new(int32(2)),
25942593
},
25952594
}
25962595
cl := NewClientBuilder().Build()
@@ -3011,7 +3010,7 @@ var _ = Describe("Fake client", func() {
30113010
Expect(cm.Data).To(BeComparableTo(map[string]string{"other": "data"}))
30123011

30133012
// Apply with ResourceVersion unset
3014-
obj.ResourceVersion = ptr.To("")
3013+
obj.ResourceVersion = new("")
30153014
obj.Data = map[string]string{"another": "data"}
30163015
Expect(cl.Apply(ctx, obj, &client.ApplyOptions{FieldManager: "test-manager"})).To(Succeed())
30173016

@@ -3193,7 +3192,7 @@ var _ = Describe("Fake client", func() {
31933192
Expect(cl.Apply(ctx, obj, client.FieldOwner("foo"))).NotTo(HaveOccurred())
31943193
// Ideally we should only test for it to not be empty, realistically we will
31953194
// break ppl if we ever start setting a different value.
3196-
Expect(obj.ResourceVersion).To(BeEquivalentTo(ptr.To("1")))
3195+
Expect(obj.ResourceVersion).To(BeEquivalentTo(new("1")))
31973196
})
31983197

31993198
It("ignores a passed resourceVersion on SSA create", func(ctx SpecContext) {
@@ -3204,7 +3203,7 @@ var _ = Describe("Fake client", func() {
32043203

32053204
cl := NewClientBuilder().Build()
32063205
Expect(cl.Apply(ctx, obj, client.FieldOwner("foo"))).NotTo(HaveOccurred())
3207-
Expect(obj.ResourceVersion).To(BeEquivalentTo(ptr.To("1")))
3206+
Expect(obj.ResourceVersion).To(BeEquivalentTo(new("1")))
32083207
})
32093208

32103209
It("allows to set deletionTimestamp on an object during SSA create", func(ctx SpecContext) {
@@ -3331,31 +3330,31 @@ var _ = Describe("Fake client", func() {
33313330
Name: "foo",
33323331
},
33333332
Spec: appsv1.DeploymentSpec{
3334-
Replicas: ptr.To[int32](2),
3333+
Replicas: new(int32(2)),
33353334
},
33363335
},
33373336
&appsv1.ReplicaSet{
33383337
ObjectMeta: metav1.ObjectMeta{
33393338
Name: "foo",
33403339
},
33413340
Spec: appsv1.ReplicaSetSpec{
3342-
Replicas: ptr.To[int32](2),
3341+
Replicas: new(int32(2)),
33433342
},
33443343
},
33453344
&corev1.ReplicationController{
33463345
ObjectMeta: metav1.ObjectMeta{
33473346
Name: "foo",
33483347
},
33493348
Spec: corev1.ReplicationControllerSpec{
3350-
Replicas: ptr.To[int32](2),
3349+
Replicas: new(int32(2)),
33513350
},
33523351
},
33533352
&appsv1.StatefulSet{
33543353
ObjectMeta: metav1.ObjectMeta{
33553354
Name: "foo",
33563355
},
33573356
Spec: appsv1.StatefulSetSpec{
3358-
Replicas: ptr.To[int32](2),
3357+
Replicas: new(int32(2)),
33593358
},
33603359
},
33613360
}
@@ -3392,16 +3391,16 @@ var _ = Describe("Fake client", func() {
33923391
switch expected := objExpected.(type) {
33933392
case *appsv1.Deployment:
33943393
expected.ResourceVersion = objActual.GetResourceVersion()
3395-
expected.Spec.Replicas = ptr.To(int32(3))
3394+
expected.Spec.Replicas = new(int32(3))
33963395
case *appsv1.ReplicaSet:
33973396
expected.ResourceVersion = objActual.GetResourceVersion()
3398-
expected.Spec.Replicas = ptr.To(int32(3))
3397+
expected.Spec.Replicas = new(int32(3))
33993398
case *corev1.ReplicationController:
34003399
expected.ResourceVersion = objActual.GetResourceVersion()
3401-
expected.Spec.Replicas = ptr.To(int32(3))
3400+
expected.Spec.Replicas = new(int32(3))
34023401
case *appsv1.StatefulSet:
34033402
expected.ResourceVersion = objActual.GetResourceVersion()
3404-
expected.Spec.Replicas = ptr.To(int32(3))
3403+
expected.Spec.Replicas = new(int32(3))
34053404
}
34063405
Expect(cmp.Diff(objExpected, objActual)).To(BeEmpty())
34073406

0 commit comments

Comments
 (0)