Skip to content

Commit 4adace9

Browse files
authored
Bump golangci-lint (#605)
* lint: Upgrade golangci-lint Upgrade to -1 minor latest version. Run or install golangci-lint using Go is no longer the recommended way [1]. Change installation method to recommended one [2]. Following changes were done to have Makefile install golnagci-lint: - GOLANGCI_LINT_VERSION - var, control version to install. - GOLANGCI_LINT_BIN - var, control path where the binary is stored. Defaults to the project binary dir. The target ensure binary is installed only when not exist. The config will be migrated to V2 and following linter issues will be resolved in follow-up commits. [1] https://golangci-lint.run/docs/welcome/install/local/#:~:text=Warning [2] https://golangci-lint.run/docs/welcome/install/local/#binaries Signed-off-by: Or Mergi <ormergi@redhat.com> * lint: Migrate config to V2 This commit changes is the result for the following command: $ make install-golangci-lint $ ./build/_output/bin/golangci-lint migrate Migration failed with the following errors, requiring manual changes: - jsonschema: "linters-settings.nolintlint" does not validate with "/properties/linters-settings/properties/nolintlint/additionalProperties": additional properties 'allow-leading-space' not allowed Rm linters-settings.nolintlint.[].allow-leading-space setting. - jsonschema: "linters-settings.goimports" does not validate with "/properties/linters-settings/properties/goimports/type": got null, want object Rm empty goimports stanza. - jsonschema: "linters-settings.mnd" does not validate with "/properties/linters-settings/properties/mnd/additionalProperties": additional properties 'settings' not allowed Change mnd spec from 'mnd.settings.mnd.checks' to 'mnd.checks'. Config no longer support timeout. Update lint make targets to specify timeout flag. Signed-off-by: Or Mergi <ormergi@redhat.com> * tests: Fix linter issues Signed-off-by: Or Mergi <ormergi@redhat.com> --------- Signed-off-by: Or Mergi <ormergi@redhat.com>
1 parent 0d865b0 commit 4adace9

6 files changed

Lines changed: 149 additions & 140 deletions

File tree

.golangci.yml

Lines changed: 122 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,124 @@
1-
run:
2-
timeout: 3m
3-
4-
linters-settings:
5-
dupl:
6-
threshold: 150
7-
8-
funlen:
9-
lines: 150
10-
statements: 50
11-
12-
gci:
13-
sections:
14-
- Prefix(github.com/k8snetworkplumbingwg/kubemacpool)
15-
16-
goconst:
17-
min-len: 2
18-
min-occurrences: 2
19-
20-
gocritic:
21-
enabled-tags: [diagnostic, experimental, opinionated, performance, style]
22-
disabled-checks:
23-
- dupImport # https://github.com/go-critic/go-critic/issues/845
24-
- ifElseChain
25-
- octalLiteral
26-
- whyNoLint
27-
- wrapperFunc
28-
settings:
29-
hugeParam:
30-
sizeThreshold: 1024
31-
rangeValCopy:
32-
sizeThreshold: 1024
33-
34-
gocyclo:
35-
min-complexity: 15
36-
37-
goimports:
38-
39-
depguard:
40-
rules:
41-
main:
42-
deny:
43-
- pkg: "github.com/sirupsen/logrus"
44-
desc: "logging is allowed only by logutils.Log"
45-
allow:
46-
- $gostd
47-
- github.com/onsi/ginkgo/v2
48-
- github.com/onsi/gomega
49-
- github.com/k8snetworkplumbingwg/kubemacpool
50-
- k8s.io
51-
- kubevirt.io
52-
- sigs.k8s.io
53-
- github.com/pkg/errors
54-
- github.com/prometheus/client_golang
55-
- github.com/prometheus/common
56-
57-
mnd:
58-
settings:
59-
mnd:
60-
checks:
61-
- argument
62-
- case
63-
- condition
64-
- return
65-
66-
govet:
67-
enable:
68-
- shadow
69-
70-
lll:
71-
line-length: 140
72-
73-
misspell:
74-
locale: US
75-
76-
nolintlint:
77-
allow-leading-space: true
78-
allow-unused: false
79-
require-explanation: false
80-
require-specific: false
81-
82-
stylecheck:
83-
dot-import-whitelist:
84-
- github.com/onsi/ginkgo/v2
85-
- github.com/onsi/gomega
86-
1+
version: "2"
872
linters:
88-
disable-all: true
3+
default: none
894
enable:
90-
- bodyclose
91-
- dogsled
92-
- dupl
93-
- errcheck
94-
- funlen
95-
- gochecknoinits
96-
- goconst
97-
- gofmt
98-
- goheader
99-
- goimports
100-
- goprintffuncname
101-
- govet
102-
- ineffassign
103-
- misspell
104-
- nakedret
105-
- noctx
106-
- nolintlint
107-
- rowserrcheck
108-
- unparam
109-
- unused
110-
- whitespace
111-
- exhaustive
112-
- gocritic
113-
- gocyclo
114-
- gosec
115-
- gosimple
116-
- lll
117-
- mnd
118-
- depguard
119-
- staticcheck
120-
- stylecheck
121-
- unconvert
5+
- bodyclose
6+
- depguard
7+
- dogsled
8+
- dupl
9+
- errcheck
10+
- exhaustive
11+
- funlen
12+
- gochecknoinits
13+
- goconst
14+
- gocritic
15+
- gocyclo
16+
- goheader
17+
- goprintffuncname
18+
- gosec
19+
- govet
20+
- ineffassign
21+
- lll
22+
- misspell
23+
- mnd
24+
- nakedret
25+
- noctx
26+
- nolintlint
27+
- rowserrcheck
28+
- staticcheck
29+
- unconvert
30+
- unparam
31+
- unused
32+
- whitespace
33+
settings:
34+
depguard:
35+
rules:
36+
main:
37+
allow:
38+
- $gostd
39+
- github.com/onsi/ginkgo/v2
40+
- github.com/onsi/gomega
41+
- github.com/k8snetworkplumbingwg/kubemacpool
42+
- k8s.io
43+
- kubevirt.io
44+
- sigs.k8s.io
45+
- github.com/pkg/errors
46+
- github.com/prometheus/client_golang
47+
- github.com/prometheus/common
48+
deny:
49+
- pkg: github.com/sirupsen/logrus
50+
desc: logging is allowed only by logutils.Log
51+
dupl:
52+
threshold: 150
53+
funlen:
54+
lines: 150
55+
statements: 50
56+
goconst:
57+
min-len: 2
58+
min-occurrences: 2
59+
gocritic:
60+
disabled-checks:
61+
- dupImport
62+
- ifElseChain
63+
- octalLiteral
64+
- whyNoLint
65+
- wrapperFunc
66+
enabled-tags:
67+
- diagnostic
68+
- experimental
69+
- opinionated
70+
- performance
71+
- style
72+
settings:
73+
hugeParam:
74+
sizeThreshold: 1024
75+
rangeValCopy:
76+
sizeThreshold: 1024
77+
gocyclo:
78+
min-complexity: 15
79+
govet:
80+
enable:
81+
- shadow
82+
lll:
83+
line-length: 140
84+
misspell:
85+
locale: US
86+
mnd:
87+
checks:
88+
- argument
89+
- case
90+
- condition
91+
- return
92+
nolintlint:
93+
require-explanation: false
94+
require-specific: false
95+
allow-unused: false
96+
staticcheck:
97+
dot-import-whitelist:
98+
- github.com/onsi/ginkgo/v2
99+
- github.com/onsi/gomega
100+
exclusions:
101+
generated: lax
102+
presets:
103+
- comments
104+
- common-false-positives
105+
- legacy
106+
- std-error-handling
107+
paths:
108+
- third_party$
109+
- builtin$
110+
- examples$
111+
formatters:
112+
enable:
113+
- gofmt
114+
- goimports
115+
settings:
116+
gci:
117+
sections:
118+
- Prefix(github.com/k8snetworkplumbingwg/kubemacpool)
119+
exclusions:
120+
generated: lax
121+
paths:
122+
- third_party$
123+
- builtin$
124+
- examples$

Makefile

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ GOFMT := GOFLAGS=-mod=mod $(GO)fmt
3434
VET := GOFLAGS=-mod=mod $(GO) vet
3535
DEEPCOPY_GEN := GOFLAGS=-mod=mod $(GO) install k8s.io/code-generator/cmd/deepcopy-gen@latest
3636
GO_VERSION = $(shell hack/go-version.sh)
37-
GOLANGICI_LINT ?= GOFLAGS=-mod=mod $(GO) run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.64.8
37+
GOLANGCI_LINT_VERSION := v2.10.1
38+
GOLANGCI_LINT_BIN := $(BIN_DIR)/golangci-lint
3839
LINTER_COVERAGE ?= tests/...
3940

4041
E2E_TEST_EXTRA_ARGS ?=
@@ -101,13 +102,16 @@ fmt: $(GO)
101102
vet: $(GO)
102103
$(VET) ./pkg/... ./cmd/... ./tests/...
103104

104-
lint:
105-
GOTOOLCHAIN=$$(grep '^toolchain' go.mod | awk '{print $$2}' | sed 's/go//' | awk -F. '{print $$1"."$$2}' || echo ""); \
106-
$(GOLANGICI_LINT) run --verbose $(LINTER_COVERAGE)
105+
$(GOLANGCI_LINT_BIN):
106+
@echo "Installing golangci-lint $(GOLANGCI_LINT_VERSION)..."
107+
@mkdir -p $(BIN_DIR)
108+
curl -sSfL https://golangci-lint.run/install.sh | sh -s -- -b $(BIN_DIR) $(GOLANGCI_LINT_VERSION)
109+
110+
lint: $(GOLANGCI_LINT_BIN)
111+
$(GOLANGCI_LINT_BIN) run --timeout=10m --verbose $(LINTER_COVERAGE)
107112

108-
lint-fix:
109-
GOTOOLCHAIN=$$(grep '^toolchain' go.mod | awk '{print $$2}' | sed 's/go//' | awk -F. '{print $$1"."$$2}' || echo ""); \
110-
$(GOLANGICI_LINT) run --verbose --fix $(LINTER_COVERAGE)
113+
lint-fix: $(GOLANGCI_LINT_BIN)
114+
$(GOLANGCI_LINT_BIN) run --timeout=10m --verbose --fix $(LINTER_COVERAGE)
111115

112116
lint-metrics: $(GO)
113117
OCI_BIN=$(OCI_BIN) ./hack/prom_metric_linter.sh --operator-name="kmp" --sub-operator-name="kmp"

tests/alert_collision_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ func expectAlertNotFiring(p *PromClient, alertName string) {
192192
func waitForPortForwardReady(port int) {
193193
By("Waiting for Prometheus port-forward to be ready")
194194
Eventually(func() bool {
195-
conn, err := net.DialTimeout("tcp", fmt.Sprintf("127.0.0.1:%d", port), time.Second)
195+
d := net.Dialer{Timeout: timeout}
196+
conn, err := d.DialContext(context.Background(), "tcp", fmt.Sprintf("127.0.0.1:%d", port))
196197
if err != nil {
197198
return false
198199
}

tests/kubectl/kubectl.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package kubectl
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67
"os"
78
"os/exec"
89
)
910

1011
func Kubectl(command ...string) (stdout, stderr string, err error) {
1112
var stdoutBuf, stderrBuf bytes.Buffer
12-
cmd := exec.Command("./cluster/kubectl.sh", command...)
13+
cmd := exec.CommandContext(context.Background(), "./cluster/kubectl.sh", command...)
1314
cmd.Dir = getClusterRootDirectory()
1415
cmd.Stderr = &stderrBuf
1516
cmd.Stdout = &stdoutBuf
@@ -30,7 +31,7 @@ func getClusterRootDirectory() string {
3031
// StartPortForwardCommand starts a port-forward command in the background and returns the process
3132
func StartPortForwardCommand(namespace, podName string, sourcePort, targetPort int) (*exec.Cmd, error) {
3233
// #nosec G204 -- test code with controlled inputs
33-
cmd := exec.Command("./cluster/kubectl.sh", "port-forward", "-n", namespace,
34+
cmd := exec.CommandContext(context.Background(), "./cluster/kubectl.sh", "port-forward", "-n", namespace,
3435
fmt.Sprintf("pod/%s", podName), fmt.Sprintf("%d:%d", sourcePort, targetPort))
3536
cmd.Dir = getClusterRootDirectory()
3637

tests/tests.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
apierrors "k8s.io/apimachinery/pkg/api/errors"
2020
"k8s.io/apimachinery/pkg/api/resource"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22-
"k8s.io/apimachinery/pkg/labels"
22+
k8slabels "k8s.io/apimachinery/pkg/labels"
2323
"k8s.io/apimachinery/pkg/runtime"
2424
"k8s.io/apimachinery/pkg/util/rand"
2525
"k8s.io/client-go/kubernetes"
@@ -164,7 +164,7 @@ func findManagerNamespace() string {
164164
pods, err := getKubemacpoolPods()
165165
ExpectWithOffset(1, err).ToNot(HaveOccurred(), "should be able to retrieve manager pods")
166166
ExpectWithOffset(1, pods.Items).ToNot(BeEmpty(), "should have multiple manager pods")
167-
namespace := pods.Items[0].ObjectMeta.Namespace
167+
namespace := pods.Items[0].Namespace
168168
ExpectWithOffset(1, namespace).ToNot(BeEmpty(), "should be a namespace at manager pods")
169169
return namespace
170170
}
@@ -272,7 +272,7 @@ func enableKubeVirtFeatureGate(featureGate string) error {
272272
}
273273

274274
func restartVirtualMachine(namespace, name string) error {
275-
cmd := exec.Command("virtctl", "restart", name, "-n", namespace)
275+
cmd := exec.CommandContext(context.Background(), "virtctl", "restart", name, "-n", namespace)
276276
output, err := cmd.CombinedOutput()
277277
if err != nil {
278278
return fmt.Errorf("failed to restart VM %s/%s: %v, output: %s", namespace, name, err, string(output))
@@ -353,7 +353,7 @@ func restartPodsFromDeployment(deploymentName string) error {
353353
if err != nil {
354354
return err
355355
}
356-
labelSelector := labels.Set(deployment.Spec.Selector.MatchLabels).String()
356+
labelSelector := k8slabels.Set(deployment.Spec.Selector.MatchLabels).String()
357357

358358
podList, err := testClient.K8sClient.CoreV1().Pods(managerNamespace).List(context.TODO(),
359359
metav1.ListOptions{LabelSelector: labelSelector})
@@ -578,7 +578,7 @@ func addFinalizer(virtualMachine *kubevirtv1.VirtualMachine, finalizerName strin
578578
if helper.ContainsString(finalizersList, finalizerName) {
579579
return nil
580580
}
581-
virtualMachine.ObjectMeta.Finalizers = append(virtualMachine.ObjectMeta.Finalizers, finalizerName)
581+
virtualMachine.Finalizers = append(virtualMachine.Finalizers, finalizerName)
582582
err = testClient.CRClient.Update(context.TODO(), virtualMachine)
583583
return err
584584
})

0 commit comments

Comments
 (0)