From b00e520f660ed90e2cf35bd9daf8bf9e5b23609f Mon Sep 17 00:00:00 2001 From: Nuutti Hakala Date: Thu, 27 Nov 2025 14:36:01 +0200 Subject: [PATCH] Bump golangci-lint to v2 and fix issues Signed-off-by: Nuutti Hakala Signed-off-by: Kashif Khan --- .github/workflows/golangci-lint.yml | 4 +- .golangci.yaml | 169 ++++++++++++++++------------ Makefile | 4 +- pkg/ironic/containers_test.go | 2 +- pkg/ironic/ironic.go | 8 +- pkg/ironic/secrets_test.go | 26 ++--- pkg/ironic/upgrades.go | 8 +- pkg/ironic/utils.go | 4 +- 8 files changed, 122 insertions(+), 103 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index ba7ef55a..80f1a64c 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -25,8 +25,8 @@ jobs: with: go-version: ${{ steps.vars.outputs.go_version }} - name: golangci-lint-${{matrix.working-directory}} - uses: golangci/golangci-lint-action@55c2c1448f86e01eaae002a5a3a9624417608d84 # v6.5.2 + uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0 with: - version: v1.64.7 + version: v2.6.2 working-directory: ${{matrix.working-directory}} args: --timeout=10m diff --git a/.golangci.yaml b/.golangci.yaml index d25b2289..21563146 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -1,7 +1,8 @@ +version: "2" run: go: "1.24" linters: - disable-all: true + default: none enable: - asasalint - asciicheck @@ -30,11 +31,8 @@ linters: - goconst - gocritic - godot - - gofmt - - goimports - goprintffuncname - gosec - - gosimple - gosmopolitan - govet - iface @@ -60,88 +58,109 @@ linters: #- revive - rowserrcheck - staticcheck - - stylecheck - tagliatelle - testifylint - thelper - tparallel - - typecheck - unconvert - unparam - unused - usestdlibvars - usetesting - whitespace - # Run with --fast=false for more extensive checks - fast: true -linters-settings: - gosec: - severity: medium - confidence: medium - concurrency: 8 - # (NOTE)elfosardo: we should try removing this exclude once we bump golangci-lint to 1.61 - excludes: + settings: + gocritic: + enabled-tags: + - experimental + gosec: + # (NOTE)elfosardo: we should try removing this exclude once we bump golangci-lint to 1.61 + excludes: - G115 - importas: - no-unaliased: true - alias: - # Kubernetes - - pkg: k8s.io/api/core/v1 - alias: corev1 - - pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1 - alias: apiextensionsv1 - - pkg: k8s.io/apimachinery/pkg/apis/meta/v1 - alias: metav1 - - pkg: k8s.io/apimachinery/pkg/api/errors - alias: k8serrors - - pkg: k8s.io/apimachinery/pkg/util/errors - alias: kerrors - - pkg: k8s.io/component-base/logs/api/v1 - alias: logsv1 - # Controller Runtime - - pkg: sigs.k8s.io/controller-runtime - alias: ctrl - # IrSO - - pkg: github.com/metal3-io/ironic-standalone-operator/api/v1alpha1 - alias: metal3api - nolintlint: - allow-unused: false - require-specific: true - gocritic: - enabled-tags: - - experimental - tagliatelle: - case: - rules: - json: goCamel - govet: - enable: - - shadow + severity: medium + confidence: medium + concurrency: 8 + govet: + enable: + - shadow + importas: + alias: + # Kubernetes + - pkg: k8s.io/api/core/v1 + alias: corev1 + - pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1 + alias: apiextensionsv1 + - pkg: k8s.io/apimachinery/pkg/apis/meta/v1 + alias: metav1 + - pkg: k8s.io/apimachinery/pkg/api/errors + alias: k8serrors + - pkg: k8s.io/apimachinery/pkg/util/errors + alias: kerrors + - pkg: k8s.io/component-base/logs/api/v1 + alias: logsv1 + # Controller Runtime + - pkg: sigs.k8s.io/controller-runtime + alias: ctrl + # IrSO + - pkg: github.com/metal3-io/ironic-standalone-operator/api/v1alpha1 + alias: metal3api + no-unaliased: true + nolintlint: + require-specific: true + allow-unused: false + tagliatelle: + case: + rules: + json: goCamel + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + rules: + - path: test/.*.go + text: should not use dot imports + - linters: + - importas + path: cmd/main.go + text: metal3iov1alpha1 + - linters: + - gosec + path: suite_test.go + text: 'G404: Use of weak random number generator .*' + - linters: + - gosec + path: test/.*.go + text: 'G703:' + - linters: + - gosec + path: test/.*.go + text: 'G704:' + - linters: + - tagliatelle + text: CIDR + - linters: + - tagliatelle + text: CA + paths: + - zz_generated.*\.go$ + - .*conversion.*\.go$ + - third_party$ + - builtin$ + - examples$ issues: - exclude-files: - - "zz_generated.*\\.go$" - - ".*conversion.*\\.go$" - include: - - EXC0002 # include "missing comments" issues from golangci-lint max-issues-per-linter: 0 max-same-issues: 0 - exclude-rules: - # Dot imports for gomega or ginkgo are allowed - # within test files. - - path: test/.*.go - text: should not use dot imports - # NOTE(dtantsur): Kubebuilder insists on a specific way to call imports - - linters: - - importas - path: cmd/main.go - text: metal3iov1alpha1 - - linters: - - gosec - path: suite_test.go - text: "G404: Use of weak random number generator .*" - - linters: - - tagliatelle - text: "CIDR" - - linters: - - tagliatelle - text: "CA" +formatters: + enable: + - gofmt + - goimports + exclusions: + generated: lax + paths: + - zz_generated.*\.go$ + - .*conversion.*\.go$ + - third_party$ + - builtin$ + - examples$ diff --git a/Makefile b/Makefile index 7cf6b82a..2d740783 100644 --- a/Makefile +++ b/Makefile @@ -217,7 +217,7 @@ GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint ## Tool Versions KUSTOMIZE_VERSION ?= v5.4.2 CONTROLLER_TOOLS_VERSION ?= v0.17.0 -GOLANGCI_LINT_VERSION ?= v1.64.7 +GOLANGCI_LINT_VERSION ?= v2.6.2 KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" .PHONY: kustomize @@ -237,7 +237,7 @@ $(CONTROLLER_GEN): $(LOCALBIN) $(GOLANGCI_LINT): $(LOCALBIN) ## Download golangci-lint locally if necessary. If wrong version is installed, it will be overwritten. test -s $(GOLANGCI_LINT) && $(GOLANGCI_LINT) --version | grep -q $(GOLANGCI_LINT_VERSION) || \ - GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION) + GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION) .PHONY: lint lint: $(GOLANGCI_LINT) diff --git a/pkg/ironic/containers_test.go b/pkg/ironic/containers_test.go index c1364a57..4dc51ba0 100644 --- a/pkg/ironic/containers_test.go +++ b/pkg/ironic/containers_test.go @@ -255,7 +255,7 @@ func TestAnnotationLabelOverrides(t *testing.T) { require.NoError(t, err) assert.Equal(t, "my-annotation", podTemplate.Annotations["annotation.example.com"]) - assert.NotEqual(t, "", podTemplate.Annotations["ironic.metal3.io/api-secret-version"]) + assert.NotEmpty(t, podTemplate.Annotations["ironic.metal3.io/api-secret-version"]) assert.Equal(t, "my-label", podTemplate.Labels["label.example.com"]) assert.Equal(t, "test", podTemplate.Labels[metal3api.IronicServiceLabel]) } diff --git a/pkg/ironic/ironic.go b/pkg/ironic/ironic.go index 1e5fa68a..0dc799c5 100644 --- a/pkg/ironic/ironic.go +++ b/pkg/ironic/ironic.go @@ -36,7 +36,7 @@ func ensureIronicDaemonSet(cctx ControllerContext, resources Resources) (Status, }, } result, err := controllerutil.CreateOrUpdate(cctx.Context, cctx.Client, deploy, func() error { - if deploy.ObjectMeta.CreationTimestamp.IsZero() { + if deploy.CreationTimestamp.IsZero() { cctx.Logger.Info("creating a new ironic daemon set") } if deploy.Labels == nil { @@ -74,7 +74,7 @@ func ensureIronicDeployment(cctx ControllerContext, resources Resources) (Status }, } result, err := controllerutil.CreateOrUpdate(cctx.Context, cctx.Client, deploy, func() error { - if deploy.ObjectMeta.CreationTimestamp.IsZero() { + if deploy.CreationTimestamp.IsZero() { cctx.Logger.Info("creating a new ironic deployment") } if deploy.Labels == nil { @@ -113,9 +113,9 @@ func ensureIronicService(cctx ControllerContext, ironic *metal3api.Ironic) (Stat exposedPort = httpsExposedPort } result, err := controllerutil.CreateOrUpdate(cctx.Context, cctx.Client, service, func() error { - if service.ObjectMeta.Labels == nil { + if service.Labels == nil { cctx.Logger.Info("creating a new ironic service") - service.ObjectMeta.Labels = make(map[string]string, 2) + service.Labels = make(map[string]string, 2) } service.Labels[metal3api.IronicServiceLabel] = ironic.Name service.Labels[metal3api.IronicVersionLabel] = cctx.VersionInfo.InstalledVersion.String() diff --git a/pkg/ironic/secrets_test.go b/pkg/ironic/secrets_test.go index 09bafa3d..23505a81 100644 --- a/pkg/ironic/secrets_test.go +++ b/pkg/ironic/secrets_test.go @@ -57,26 +57,26 @@ func TestGenerateHtpasswd(t *testing.T) { Scenario string User string - Password string + password string }{ { User: "admin", - Password: "pa$$w0rd", + password: "pa$$w0rd", }, { User: "admin\n", - Password: "pa$$w0rd\n", + password: "pa$$w0rd\n", }, } for _, tc := range testCases { t.Run(tc.Scenario, func(t *testing.T) { - result, err := generateHtpasswd([]byte(tc.User), []byte(tc.Password)) + result, err := generateHtpasswd([]byte(tc.User), []byte(tc.password)) require.NoError(t, err) user, password, ok := strings.Cut(result, ":") assert.Truef(t, ok, "%s is not separated with a colon", result) assert.Equal(t, strings.Trim(tc.User, "\n"), user) - assert.NoError(t, bcrypt.CompareHashAndPassword([]byte(password), []byte(strings.Trim(tc.Password, "\n")))) + assert.NoError(t, bcrypt.CompareHashAndPassword([]byte(password), []byte(strings.Trim(tc.password, "\n")))) }) } } @@ -86,7 +86,7 @@ func TestSecretNeedsUpdating(t *testing.T) { Scenario string User string - Password string + password string CurrentHtpasswd string ExpectedChanged bool @@ -95,28 +95,28 @@ func TestSecretNeedsUpdating(t *testing.T) { Scenario: "nothing-changed", User: "admin", - Password: "password", + password: "password", CurrentHtpasswd: "admin:$2y$05$CJozjmp4SHJjNWcJn1vVsOx4OEBQTDTVTdNFc0I.CVt5xpEZMK4pW", }, { Scenario: "newlines", User: "admin\n", - Password: "password\n", + password: "password\n", CurrentHtpasswd: "admin:$2y$05$CJozjmp4SHJjNWcJn1vVsOx4OEBQTDTVTdNFc0I.CVt5xpEZMK4pW", }, { Scenario: "new-value", User: "admin", - Password: "password", + password: "password", ExpectedChanged: true, }, { Scenario: "user-changed", User: "admin2", - Password: "password", + password: "password", CurrentHtpasswd: "admin:$2y$05$CJozjmp4SHJjNWcJn1vVsOx4OEBQTDTVTdNFc0I.CVt5xpEZMK4pW", ExpectedChanged: true, }, @@ -124,14 +124,14 @@ func TestSecretNeedsUpdating(t *testing.T) { Scenario: "password-changed", User: "admin", - Password: "password2", + password: "password2", CurrentHtpasswd: "admin:$2y$05$CJozjmp4SHJjNWcJn1vVsOx4OEBQTDTVTdNFc0I.CVt5xpEZMK4pW", ExpectedChanged: true, }, { Scenario: "missing-user", - Password: "password", + password: "password", CurrentHtpasswd: "admin:$2y$05$CJozjmp4SHJjNWcJn1vVsOx4OEBQTDTVTdNFc0I.CVt5xpEZMK4pW", ExpectedChanged: true, }, @@ -149,7 +149,7 @@ func TestSecretNeedsUpdating(t *testing.T) { secret := &corev1.Secret{ Data: map[string][]byte{ "username": []byte(tc.User), - "password": []byte(tc.Password), + "password": []byte(tc.password), }, } if tc.CurrentHtpasswd != "" { diff --git a/pkg/ironic/upgrades.go b/pkg/ironic/upgrades.go index 06b9d514..4a18a711 100644 --- a/pkg/ironic/upgrades.go +++ b/pkg/ironic/upgrades.go @@ -125,12 +125,12 @@ func ensureIronicUpgradeJob(cctx ControllerContext, resources Resources, phase u template := newMigrationTemplate(cctx, resources.Ironic, phase) result, err := controllerutil.CreateOrUpdate(cctx.Context, cctx.Client, job, func() error { - if job.ObjectMeta.Labels == nil { + if job.Labels == nil { cctx.Logger.Info("creating a new upgrade job", "Phase", phase, "From", fromVersion, "To", toVersion.String()) - job.ObjectMeta.Labels = make(map[string]string, 2) + job.Labels = make(map[string]string, 2) } - job.ObjectMeta.Labels[metal3api.IronicServiceLabel] = resources.Ironic.Name - job.ObjectMeta.Labels[metal3api.IronicVersionLabel] = cctx.VersionInfo.InstalledVersion.String() + job.Labels[metal3api.IronicServiceLabel] = resources.Ironic.Name + job.Labels[metal3api.IronicVersionLabel] = cctx.VersionInfo.InstalledVersion.String() job.Spec.TTLSecondsAfterFinished = ptr.To(jobTTLSeconds) mergePodTemplates(&job.Spec.Template, template) diff --git a/pkg/ironic/utils.go b/pkg/ironic/utils.go index 254426c2..8c572818 100644 --- a/pkg/ironic/utils.go +++ b/pkg/ironic/utils.go @@ -86,8 +86,8 @@ func mergeContainers(target, source []corev1.Container) []corev1.Container { // mergePodTemplates updates an existing pod template, taking care to avoid // overriding defaulted values. func mergePodTemplates(target *corev1.PodTemplateSpec, source corev1.PodTemplateSpec) { - if target.ObjectMeta.Labels == nil { - target.ObjectMeta.Labels = make(map[string]string, len(source.ObjectMeta.Labels)) + if target.Labels == nil { + target.Labels = make(map[string]string, len(source.Labels)) } maps.Copy(target.Labels, source.Labels) if source.Annotations != nil {