Skip to content

Commit c6f2967

Browse files
dongjiang1989jiangdong
authored andcommitted
update golangci-lint version and add modernize lint
Signed-off-by: jiangdong <jiangdong@iflytek.com>
1 parent dd11b6f commit c6f2967

Some content is hidden

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

52 files changed

+343
-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: 7 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,9 @@ linters:
7576
- pkg: sigs.k8s.io/controller-runtime
7677
alias: ctrl
7778
no-unaliased: true
79+
modernize:
80+
disable:
81+
- omitzero
7882
revive:
7983
rules:
8084
# The following rules are recommended https://github.com/mgechev/revive#recommended-configuration
@@ -125,6 +129,9 @@ linters:
125129
- linters:
126130
- staticcheck
127131
text: 'SA1019: .*The component config package has been deprecated and will be removed in a future release.'
132+
- linters:
133+
- staticcheck
134+
text: 'SA1019: .* is deprecated: .* is deprecated, use .* instead '
128135
# With Go 1.16, the new embed directive can be used with an un-named import,
129136
# revive (previously, golint) only allows these to be imported in a main.go, which wouldn't work for us.
130137
# This directive allows the embed package to be imported with an underscore everywhere.

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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

pkg/cache/delegating_by_gvk_cache.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,11 @@ func (dbt *delegatingByGVKCache) Start(ctx context.Context) error {
8181
errs := make(chan error)
8282
for idx := range allCaches {
8383
cache := allCaches[idx]
84-
wg.Add(1)
85-
go func() {
86-
defer wg.Done()
84+
wg.Go(func() {
8785
if err := cache.Start(ctx); err != nil {
8886
errs <- err
8987
}
90-
}()
88+
})
9189
}
9290

9391
select {

pkg/cache/informer_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func (ic *informerCache) IndexField(ctx context.Context, obj client.Object, fiel
221221
}
222222

223223
func indexByField(informer Informer, field string, extractValue client.IndexerFunc) error {
224-
indexFunc := func(objRaw interface{}) ([]string, error) {
224+
indexFunc := func(objRaw any) ([]string, error) {
225225
// TODO(directxman12): check if this is the correct type?
226226
obj, isObj := objRaw.(client.Object)
227227
if !isObj {

0 commit comments

Comments
 (0)