Skip to content

Commit 562715a

Browse files
✨ (go/v4): Upgrade golangci-lint to v2.7.2 and add modernize check (#5258)
add golangci-lint and support modernize lint Signed-off-by: dongjiang1989 <dongjiang1989@126.com>
1 parent e2bc0ac commit 562715a

File tree

64 files changed

+248
-263
lines changed

Some content is hidden

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

64 files changed

+248
-263
lines changed

.github/workflows/lint-sample.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
- name: Run linter
4444
uses: golangci/golangci-lint-action@v9
4545
with:
46-
version: v2.6.0
46+
version: v2.7.2
4747
working-directory: ${{ matrix.folder }}
4848
- name: Run linter via makefile target
4949
working-directory: ${{ matrix.folder }}

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
- name: Run linter
2727
uses: golangci/golangci-lint-action@v9
2828
with:
29-
version: v2.6.0
29+
version: v2.7.2
3030

3131
yamllint:
3232
runs-on: ubuntu-latest

.golangci.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ linters:
1616
- importas
1717
- ineffassign
1818
- lll
19+
- modernize
1920
- misspell
2021
- nakedret
2122
- nolintlint
@@ -52,6 +53,12 @@ linters:
5253
alias: golangv4
5354
- pkg: sigs.k8s.io/kubebuilder/v4/pkg/plugins/common/kustomize/v2
5455
alias: kustomizecommonv2
56+
modernize:
57+
disable:
58+
# Suggest replacing omitempty with omitzero for struct fields.
59+
# Disable this check for now since it introduces too many changes in our existing codebase.
60+
# See https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize#hdr-Analyzer_omitzero for more details.
61+
- omitzero
5562
nolintlint:
5663
allow-unused: false
5764
revive:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
233233

234234
## Tool Versions
235235
GO_APIDIFF_VERSION ?= v0.8.3
236-
GOLANGCI_LINT_VERSION ?= v2.6.0
236+
GOLANGCI_LINT_VERSION ?= v2.7.2
237237

238238
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
239239
# $1 - target path with name of binary

docs/book/src/cronjob-tutorial/testdata/project/.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jobs:
2020
- name: Run linter
2121
uses: golangci/golangci-lint-action@v8
2222
with:
23-
version: v2.6.0
23+
version: v2.7.2

docs/book/src/cronjob-tutorial/testdata/project/.golangci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ linters:
1313
- govet
1414
- ineffassign
1515
- lll
16+
- modernize
1617
- misspell
1718
- nakedret
1819
- prealloc
@@ -26,6 +27,9 @@ linters:
2627
rules:
2728
- name: comment-spacings
2829
- name: import-shadowing
30+
modernize:
31+
disable:
32+
- omitzero
2933
exclusions:
3034
generated: lax
3135
rules:

docs/book/src/cronjob-tutorial/testdata/project/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ ENVTEST_K8S_VERSION ?= $(shell v='$(call gomodver,k8s.io/api)'; \
204204
[ -n "$$v" ] || { echo "Set ENVTEST_K8S_VERSION manually (k8s.io/api replace has no tag)" >&2; exit 1; }; \
205205
printf '%s\n' "$$v" | sed -E 's/^v?[0-9]+\.([0-9]+).*/1.\1/')
206206

207-
GOLANGCI_LINT_VERSION ?= v2.6.0
207+
GOLANGCI_LINT_VERSION ?= v2.7.2
208208
.PHONY: kustomize
209209
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
210210
$(KUSTOMIZE): $(LOCALBIN)

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package controller
2525
import (
2626
"context"
2727
"fmt"
28+
"maps"
2829
"sort"
2930
"time"
3031

@@ -602,13 +603,9 @@ func (r *CronJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
602603
},
603604
Spec: *cronJob.Spec.JobTemplate.Spec.DeepCopy(),
604605
}
605-
for k, v := range cronJob.Spec.JobTemplate.Annotations {
606-
job.Annotations[k] = v
607-
}
606+
maps.Copy(job.Annotations, cronJob.Spec.JobTemplate.Annotations)
608607
job.Annotations[scheduledTimeAnnotation] = scheduledTime.Format(time.RFC3339)
609-
for k, v := range cronJob.Spec.JobTemplate.Labels {
610-
job.Labels[k] = v
611-
}
608+
maps.Copy(job.Labels, cronJob.Spec.JobTemplate.Labels)
612609
if err := ctrl.SetControllerReference(cronJob, job, r.Scheme); err != nil {
613610
return nil, err
614611
}

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ var _ = Describe("CronJob controller", func() {
172172
}
173173

174174
// Note that your CronJob’s GroupVersionKind is required to set up this owner reference.
175-
kind := reflect.TypeOf(cronjobv1.CronJob{}).Name()
175+
kind := reflect.TypeFor[cronjobv1.CronJob]().Name()
176176
gvk := cronjobv1.GroupVersion.WithKind(kind)
177177

178178
controllerRef := metav1.NewControllerRef(createdCronjob, gvk)

docs/book/src/cronjob-tutorial/testdata/project/test/utils/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ func LoadImageToKindClusterWithName(name string) error {
201201
// according to line breakers, and ignores the empty elements in it.
202202
func GetNonEmptyLines(output string) []string {
203203
var res []string
204-
elements := strings.Split(output, "\n")
205-
for _, element := range elements {
204+
elements := strings.SplitSeq(output, "\n")
205+
for element := range elements {
206206
if element != "" {
207207
res = append(res, element)
208208
}

0 commit comments

Comments
 (0)