Skip to content

Commit a0b87dc

Browse files
committed
update golangci-lint version and add modernize lint
Signed-off-by: dongjiang1989 <[email protected]> by codereview Signed-off-by: dongjiang1989 <[email protected]>
1 parent dd11b6f commit a0b87dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+347
-370
lines changed

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ jobs:
3434
- name: golangci-lint
3535
uses: golangci/golangci-lint-action@0a35821d5c230e903fcfe077583637dea1b27b47 # tag=v9.0.0
3636
with:
37-
version: v2.5.0
37+
version: v2.6.1
3838
args: --output.text.print-linter-name=true --output.text.colors=true --timeout 10m
3939
working-directory: ${{matrix.working-directory}}

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ linters:
3131
- iotamixing
3232
- makezero
3333
- misspell
34+
- modernize
3435
- nakedret
3536
- nilerr
3637
- nolintlint
@@ -75,6 +76,10 @@ linters:
7576
- pkg: sigs.k8s.io/controller-runtime
7677
alias: ctrl
7778
no-unaliased: true
79+
modernize:
80+
disable:
81+
- omitzero
82+
- fmtappendf
7883
revive:
7984
rules:
8085
# The following rules are recommended https://github.com/mgechev/revive#recommended-configuration

examples/crd/pkg/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hack/tools/cmd/gomodcheck/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func modulesFromUpstreamModGraph(upstreamRefList []string) (map[string]map[strin
162162
}
163163

164164
modToVersionToUpstreamRef := make(map[string]map[string]string)
165-
for _, line := range strings.Split(graph, "\n") {
165+
for line := range strings.SplitSeq(graph, "\n") {
166166
ref := strings.SplitN(line, "@", 2)[0]
167167

168168
if _, ok := upstreamRefs[ref]; !ok {

pkg/builder/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func (blder *TypedBuilder[request]) doWatch() error {
312312
return err
313313
}
314314

315-
if reflect.TypeFor[request]() != reflect.TypeOf(reconcile.Request{}) {
315+
if reflect.TypeFor[request]() != reflect.TypeFor[reconcile.Request]() {
316316
return fmt.Errorf("For() can only be used with reconcile.Request, got %T", *new(request))
317317
}
318318

pkg/builder/controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ func (l *testLogger) Enabled(int) bool {
6161
return true
6262
}
6363

64-
func (l *testLogger) Info(level int, msg string, keysAndValues ...interface{}) {
64+
func (l *testLogger) Info(level int, msg string, keysAndValues ...any) {
6565
}
6666

67-
func (l *testLogger) WithValues(keysAndValues ...interface{}) logr.LogSink {
67+
func (l *testLogger) WithValues(keysAndValues ...any) logr.LogSink {
6868
return l
6969
}
7070

pkg/builder/webhook.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ import (
3939
// WebhookBuilder builds a Webhook.
4040
type WebhookBuilder[T runtime.Object] struct {
4141
apiType runtime.Object
42-
customDefaulter admission.CustomDefaulter
42+
customDefaulter admission.CustomDefaulter //nolint:staticcheck
4343
defaulter admission.Defaulter[T]
4444
customDefaulterOpts []admission.DefaulterOption
45-
customValidator admission.CustomValidator
45+
customValidator admission.CustomValidator //nolint:staticcheck
4646
validator admission.Validator[T]
4747
customPath string
4848
customValidatorCustomPath string
@@ -64,6 +64,7 @@ func WebhookManagedBy[T runtime.Object](m manager.Manager, object T) *WebhookBui
6464

6565
// WithCustomDefaulter takes an admission.CustomDefaulter interface, a MutatingWebhook with the provided opts (admission.DefaulterOption)
6666
// will be wired for this type.
67+
//
6768
// Deprecated: Use WithDefaulter instead.
6869
func (blder *WebhookBuilder[T]) WithCustomDefaulter(defaulter admission.CustomDefaulter, opts ...admission.DefaulterOption) *WebhookBuilder[T] {
6970
blder.customDefaulter = defaulter
@@ -79,6 +80,7 @@ func (blder *WebhookBuilder[T]) WithDefaulter(defaulter admission.Defaulter[T],
7980
}
8081

8182
// WithCustomValidator takes a admission.CustomValidator interface, a ValidatingWebhook will be wired for this type.
83+
//
8284
// Deprecated: Use WithValidator instead.
8385
func (blder *WebhookBuilder[T]) WithCustomValidator(validator admission.CustomValidator) *WebhookBuilder[T] {
8486
blder.customValidator = validator
@@ -306,6 +308,7 @@ func (blder *WebhookBuilder[T]) getValidatingWebhook() (*admission.Webhook, erro
306308
}
307309
w = admission.WithValidator(blder.mgr.GetScheme(), blder.validator)
308310
} else if blder.customValidator != nil {
311+
//nolint:staticcheck
309312
w = admission.WithCustomValidator(blder.mgr.GetScheme(), blder.apiType, blder.customValidator)
310313
}
311314
if w != nil && blder.recoverPanic != nil {

pkg/builder/webhook_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,7 @@ func (*testDefaulter) Default(ctx context.Context, obj *TestDefaulterObject) err
11801180
return nil
11811181
}
11821182

1183+
//nolint:staticcheck
11831184
var _ admission.CustomDefaulter = &TestCustomDefaulter{}
11841185

11851186
type TestCustomValidator struct{}
@@ -1263,6 +1264,7 @@ func (*testValidator) ValidateDelete(ctx context.Context, obj *TestValidatorObje
12631264
return nil, nil
12641265
}
12651266

1267+
//nolint:staticcheck
12661268
var _ admission.CustomValidator = &TestCustomValidator{}
12671269

12681270
// TestCustomDefaultValidator for default
@@ -1286,6 +1288,7 @@ func (*TestCustomDefaultValidator) Default(ctx context.Context, obj runtime.Obje
12861288
return nil
12871289
}
12881290

1291+
//nolint:staticcheck
12891292
var _ admission.CustomDefaulter = &TestCustomDefaulter{}
12901293

12911294
// TestCustomDefaultValidator for validation
@@ -1345,6 +1348,7 @@ func (*TestCustomDefaultValidator) ValidateDelete(ctx context.Context, obj runti
13451348
return nil, nil
13461349
}
13471350

1351+
//nolint:staticcheck
13481352
var _ admission.CustomValidator = &TestCustomValidator{}
13491353

13501354
type testValidatorDefaulter struct{}

pkg/cache/cache_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ var _ = Describe("Cache with transformers", func() {
216216

217217
By("creating the informer cache")
218218
informerCache, err = cache.New(cfg, cache.Options{
219-
DefaultTransform: func(i interface{}) (interface{}, error) {
219+
DefaultTransform: func(i any) (any, error) {
220220
obj := i.(runtime.Object)
221221
Expect(obj).NotTo(BeNil())
222222

@@ -238,7 +238,7 @@ var _ = Describe("Cache with transformers", func() {
238238
},
239239
ByObject: map[client.Object]cache.ByObject{
240240
&corev1.Pod{}: {
241-
Transform: func(i interface{}) (interface{}, error) {
241+
Transform: func(i any) (any, error) {
242242
obj := i.(runtime.Object)
243243
Expect(obj).NotTo(BeNil())
244244
accessor, err := meta.Accessor(obj)
@@ -1103,7 +1103,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
11031103
Expect(out).To(Equal(uKnownPod2))
11041104

11051105
By("altering a field in the retrieved pod")
1106-
m, _ := out.Object["spec"].(map[string]interface{})
1106+
m, _ := out.Object["spec"].(map[string]any)
11071107
m["activeDeadlineSeconds"] = 4
11081108

11091109
By("verifying the pods are no longer equal")
@@ -1954,8 +1954,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
19541954
Expect(sii.HasSynced()).To(BeTrue())
19551955

19561956
By("adding an event handler listening for object creation which sends the object to a channel")
1957-
out := make(chan interface{})
1958-
addFunc := func(obj interface{}) {
1957+
out := make(chan any)
1958+
addFunc := func(obj any) {
19591959
out <- obj
19601960
}
19611961
_, _ = sii.AddEventHandler(kcache.ResourceEventHandlerFuncs{AddFunc: addFunc})
@@ -2014,8 +2014,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
20142014
Expect(sii.HasSynced()).To(BeTrue())
20152015

20162016
By("adding an event handler listening for object creation which sends the object to a channel")
2017-
out := make(chan interface{})
2018-
addFunc := func(obj interface{}) {
2017+
out := make(chan any)
2018+
addFunc := func(obj any) {
20192019
out <- obj
20202020
}
20212021
_, _ = sii.AddEventHandler(kcache.ResourceEventHandlerFuncs{AddFunc: addFunc})
@@ -2196,9 +2196,9 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
21962196
By("getting a shared index informer for a pod")
21972197

21982198
pod := &unstructured.Unstructured{
2199-
Object: map[string]interface{}{
2200-
"spec": map[string]interface{}{
2201-
"containers": []map[string]interface{}{
2199+
Object: map[string]any{
2200+
"spec": map[string]any{
2201+
"containers": []map[string]any{
22022202
{
22032203
"name": "nginx",
22042204
"image": "nginx",
@@ -2220,8 +2220,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
22202220
Expect(sii.HasSynced()).To(BeTrue())
22212221

22222222
By("adding an event handler listening for object creation which sends the object to a channel")
2223-
out := make(chan interface{})
2224-
addFunc := func(obj interface{}) {
2223+
out := make(chan any)
2224+
addFunc := func(obj any) {
22252225
out <- obj
22262226
}
22272227
_, _ = sii.AddEventHandler(kcache.ResourceEventHandlerFuncs{AddFunc: addFunc})
@@ -2239,9 +2239,9 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
22392239
It("should be able to stop and restart informers", func(ctx SpecContext) {
22402240
By("getting a shared index informer for a pod")
22412241
pod := &unstructured.Unstructured{
2242-
Object: map[string]interface{}{
2243-
"spec": map[string]interface{}{
2244-
"containers": []map[string]interface{}{
2242+
Object: map[string]any{
2243+
"spec": map[string]any{
2244+
"containers": []map[string]any{
22452245
{
22462246
"name": "nginx",
22472247
"image": "nginx",
@@ -2294,7 +2294,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
22942294
if !ok {
22952295
return []string{}
22962296
}
2297-
m, ok := s.(map[string]interface{})
2297+
m, ok := s.(map[string]any)
22982298
if !ok {
22992299
return []string{}
23002300
}
@@ -2379,8 +2379,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
23792379
Expect(sii.HasSynced()).To(BeTrue())
23802380

23812381
By("adding an event handler listening for object creation which sends the object to a channel")
2382-
out := make(chan interface{})
2383-
addFunc := func(obj interface{}) {
2382+
out := make(chan any)
2383+
addFunc := func(obj any) {
23842384
out <- obj
23852385
}
23862386
_, _ = sii.AddEventHandler(kcache.ResourceEventHandlerFuncs{AddFunc: addFunc})

pkg/cache/defaulting_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,9 @@ func TestDefaultOptsRace(t *testing.T) {
474474
// Start go routines which re-use the above options struct.
475475
wg := sync.WaitGroup{}
476476
for range 2 {
477-
wg.Add(1)
478-
go func() {
477+
wg.Go(func() {
479478
_, _ = defaultOpts(&rest.Config{}, opts)
480-
wg.Done()
481-
}()
479+
})
482480
}
483481

484482
// Wait for the go routines to finish.
@@ -509,7 +507,7 @@ func TestDefaultConfigConsidersAllFields(t *testing.T) {
509507
},
510508
)
511509

512-
for i := 0; i < 100; i++ {
510+
for range 100 {
513511
fuzzed := Config{}
514512
f.Fuzz(&fuzzed)
515513

0 commit comments

Comments
 (0)