diff --git a/.golangci.yml b/.golangci.yml index c1973beb5f..5897114d86 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,79 +1,107 @@ +version: "2" run: - timeout: 10m + modules-download-mode: readonly issues-exit-code: 1 tests: true - modules-download-mode: readonly allow-parallel-runners: false - linters: - fast: false enable: - - errcheck - - copyloopvar - - gocritic - - gofumpt - - goimports - - gomodguard - - gosec - - govet - - misspell - - revive - - unconvert - - unparam - - unused - - whitespace - disable-all: false - presets: - - bugs - - unused - -# all available settings of specific linters -linters-settings: - gofmt: - # simplify code: gofmt with `-s` option, true by default - simplify: true - goimports: - local-prefixes: sigs.k8s.io/gateway-api - govet: - enable: - - shadow - settings: - shadow: - # Whether to be strict about shadowing; can be noisy. - strict: false - misspell: - locale: US - ignore-words: [] - gomodguard: - blocked: - # List of blocked modules. - modules: - - io/ioutil: - recommendations: - - io - - os - reason: "Deprecation of package ioutil in Go 1.16." - + - asasalint + - asciicheck + - bidichk + - bodyclose + - contextcheck + - copyloopvar + - durationcheck + - errchkjson + - errorlint + - exhaustive + - gocheckcompilerdirectives + - gochecksumtype + - gocritic + - gomodguard + - gosec + - gosmopolitan + - loggercheck + - makezero + - misspell + - musttag + - nilerr + - nilnesserr + - noctx + - protogetter + - reassign + - recvcheck + - revive + - rowserrcheck + - spancheck + - sqlclosecheck + - testifylint + - unconvert + - unparam + - whitespace + - zerologlint + settings: + gomodguard: + blocked: + modules: + - io/ioutil: + recommendations: + - io + - os + reason: Deprecation of package ioutil in Go 1.16. + govet: + enable: + - shadow + settings: + shadow: + strict: false + misspell: + locale: US + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + rules: + - linters: + - dupl + - errcheck + - gocyclo + path: _test\.go + - linters: + - testifylint + text: require must only be used in the goroutine running the test function + - linters: + - testifylint + text: contains assertions that must only be used in the goroutine running the test function + - linters: + - errorlint + text: non-wrapping format verb for fmt.Errorf. Use `%w` to format errors + - path: (.+)\.go$ + text: Using the variable on range scope `tc` in function literal + paths: + - third_party$ + - builtin$ + - examples$ issues: max-issues-per-linter: 0 max-same-issues: 0 - exclude-rules: - # Exclude some linters from running on tests files. - - path: _test\.go - linters: - - gocyclo - - errcheck - - dupl - # Too many false positives - for examples see: https://github.com/Antonboom/testifylint/issues/67 - - linters: - - testifylint - text: "require must only be used in the goroutine running the test function" - - linters: - - testifylint - text: "contains assertions that must only be used in the goroutine running the test function" - # It is valid usage to wrap errors without using %w to not make them part of - # the API contract. - - linters: ["errorlint"] - text: "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors" - exclude: - - Using the variable on range scope `tc` in function literal +formatters: + enable: + - gofumpt + - goimports + settings: + gofmt: + simplify: true + goimports: + local-prefixes: + - sigs.k8s.io/gateway-api + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/conformance/echo-basic/echo-basic_test.go b/conformance/echo-basic/echo-basic_test.go index aec399b1b3..b7318d9a1e 100644 --- a/conformance/echo-basic/echo-basic_test.go +++ b/conformance/echo-basic/echo-basic_test.go @@ -171,8 +171,8 @@ func TestEchoHandler(t *testing.T) { // Test RequestAssertions struct contains expected context namespace expectedNamespace := context.Namespace - if responseAssertions.Context.Namespace != expectedNamespace { - t.Errorf("Expected X-Content-Type-Options header %s, but got %s", expectedNamespace, responseAssertions.Context.Namespace) + if responseAssertions.Namespace != expectedNamespace { + t.Errorf("Expected X-Content-Type-Options header %s, but got %s", expectedNamespace, responseAssertions.Namespace) } } diff --git a/conformance/echo-basic/grpc/grpc.go b/conformance/echo-basic/grpc/grpc.go index e2f1b88ab8..4f33f05a27 100644 --- a/conformance/echo-basic/grpc/grpc.go +++ b/conformance/echo-basic/grpc/grpc.go @@ -173,7 +173,7 @@ func runServer(config serverConfig) (int, int) { //nolint:unparam fullService := string(svcd.FullName()) // Set up plaintext server. - lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", config.HTTPPort)) + lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", config.HTTPPort)) //nolint:noctx if err != nil { fmt.Printf("failed to listen: %v\n", err) os.Exit(1) @@ -200,7 +200,7 @@ func runServer(config serverConfig) (int, int) { //nolint:unparam fmt.Printf("failed to create credentials: %v\n", err) os.Exit(1) } - secureListener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", config.HTTPSPort)) + secureListener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", config.HTTPSPort)) //nolint:noctx if err != nil { fmt.Printf("failed to listen: %v\n", err) os.Exit(1) diff --git a/conformance/tests/gateway-static-addresses.go b/conformance/tests/gateway-static-addresses.go index 89654c9ed8..9d1bc75790 100644 --- a/conformance/tests/gateway-static-addresses.go +++ b/conformance/tests/gateway-static-addresses.go @@ -166,7 +166,7 @@ func filterAddr(addrs []v1.GatewaySpecAddress, filter v1.GatewaySpecAddress) (ne newAddrs = append(newAddrs, addr) } } - return + return //nolint:gofumpt } var finalExpectedListenerState = []v1.ListenerStatus{ diff --git a/conformance/tests/httproute-request-percentage-mirror.go b/conformance/tests/httproute-request-percentage-mirror.go index 843fac8043..21419f9545 100644 --- a/conformance/tests/httproute-request-percentage-mirror.go +++ b/conformance/tests/httproute-request-percentage-mirror.go @@ -145,8 +145,7 @@ var HTTPRouteRequestPercentageMirror = suite.ConformanceTest{ http.MakeRequestAndExpectEventuallyConsistentResponse(t, suite.RoundTripper, suite.TimeoutConfig, gwAddr, expected) // Override to not have more requests than expected - var dedicatedTimeoutConfig config.TimeoutConfig - dedicatedTimeoutConfig = suite.TimeoutConfig + dedicatedTimeoutConfig := suite.TimeoutConfig dedicatedTimeoutConfig.RequiredConsecutiveSuccesses = 1 // used to limit number of parallel go routines semaphore := make(chan struct{}, concurrentRequests) @@ -225,7 +224,7 @@ func testMirroredRequestsDistribution(t *testing.T, suite *suite.ConformanceTest tlog.Logf(t, "Pod: %s, Expected: %f (min: %f, max: %f), Actual: %f", mirrorPod.Name, expected, minExpected, maxExpected, actual) if actual < minExpected || actual > maxExpected { - errs = append(errs, fmt.Errorf("Pod %s did not meet the mirroring percentage within tolerance. Expected between %f and %f, but got %f", mirrorPod.Name, minExpected, maxExpected, actual)) + errs = append(errs, fmt.Errorf("pod %s did not meet the mirroring percentage within tolerance. Expected between %f and %f, but got %f", mirrorPod.Name, minExpected, maxExpected, actual)) } } if len(errs) > 0 { diff --git a/conformance/utils/http/http.go b/conformance/utils/http/http.go index 4f50eb566a..f38523c93d 100644 --- a/conformance/utils/http/http.go +++ b/conformance/utils/http/http.go @@ -241,7 +241,7 @@ func AwaitConvergence(t *testing.T, threshold int, maxTimeToConsistency time.Dur default: } - completed := fn(time.Now().Sub(start)) + completed := fn(time.Since(start)) attempts++ if completed { successes++ diff --git a/conformance/utils/kubernetes/apply.go b/conformance/utils/kubernetes/apply.go index 3f31b308d7..7e2a3d5f65 100644 --- a/conformance/utils/kubernetes/apply.go +++ b/conformance/utils/kubernetes/apply.go @@ -371,5 +371,5 @@ func convertGatewayAddrsToPrimitives(gwaddrs []v1beta1.GatewaySpecAddress) (raw "value": addr.Value, }) } - return + return //nolint:gofumpt } diff --git a/conformance/utils/kubernetes/apply_test.go b/conformance/utils/kubernetes/apply_test.go index 0c75753de3..e8af3a67fb 100644 --- a/conformance/utils/kubernetes/apply_test.go +++ b/conformance/utils/kubernetes/apply_test.go @@ -179,7 +179,7 @@ spec: resources, err := tc.applier.prepareResources(t, decoder) require.NoError(t, err, "unexpected error preparing resources") - require.EqualValues(t, tc.expected, resources) + require.Equal(t, tc.expected, resources) }) } } diff --git a/conformance/utils/kubernetes/helpers_test.go b/conformance/utils/kubernetes/helpers_test.go index 9c2ed0eb6d..8d90991541 100644 --- a/conformance/utils/kubernetes/helpers_test.go +++ b/conformance/utils/kubernetes/helpers_test.go @@ -68,7 +68,7 @@ func TestNewGatewayRef(t *testing.T) { require.IsType(t, GatewayRef{}, ref) if test.listenerNames == nil { require.Len(t, ref.listenerNames, 1) - assert.Equal(t, "", string(*ref.listenerNames[0])) + assert.Empty(t, string(*ref.listenerNames[0])) } else { require.Len(t, ref.listenerNames, len(test.listenerNames)) for i := 0; i < len(ref.listenerNames); i++ { diff --git a/conformance/utils/suite/reports.go b/conformance/utils/suite/reports.go index 4efcc4e77f..70f2223460 100644 --- a/conformance/utils/suite/reports.go +++ b/conformance/utils/suite/reports.go @@ -45,6 +45,13 @@ var ( type profileReportsMap map[ConformanceProfileName]confv1.ProfileReport +func (p profileReportsMap) list() (profileReports []confv1.ProfileReport) { + for _, profileReport := range p { + profileReports = append(profileReports, profileReport) + } + return //nolint:gofumpt +} + func newReports() profileReportsMap { return make(profileReportsMap) } @@ -66,9 +73,9 @@ func (p profileReportsMap) addTestResults(conformanceProfile ConformanceProfile, if report.Extended == nil { report.Extended = &confv1.ExtendedStatus{} } - report.Extended.Statistics.Passed++ + report.Extended.Passed++ } else { - report.Core.Statistics.Passed++ + report.Core.Passed++ } case testFailed: if testIsExtended { @@ -76,9 +83,9 @@ func (p profileReportsMap) addTestResults(conformanceProfile ConformanceProfile, report.Extended = &confv1.ExtendedStatus{} } report.Extended.FailedTests = append(report.Extended.FailedTests, result.test.ShortName) - report.Extended.Statistics.Failed++ + report.Extended.Failed++ } else { - report.Core.Statistics.Failed++ + report.Core.Failed++ if report.Core.FailedTests == nil { report.Core.FailedTests = []string{} } @@ -89,23 +96,16 @@ func (p profileReportsMap) addTestResults(conformanceProfile ConformanceProfile, if report.Extended == nil { report.Extended = &confv1.ExtendedStatus{} } - report.Extended.Statistics.Skipped++ + report.Extended.Skipped++ report.Extended.SkippedTests = append(report.Extended.SkippedTests, result.test.ShortName) } else { - report.Core.Statistics.Skipped++ + report.Core.Skipped++ report.Core.SkippedTests = append(report.Core.SkippedTests, result.test.ShortName) } } p[conformanceProfile.Name] = report } -func (p profileReportsMap) list() (profileReports []confv1.ProfileReport) { - for _, profileReport := range p { - profileReports = append(profileReports, profileReport) - } - return -} - func (p profileReportsMap) compileResults(supportedFeaturesMap map[ConformanceProfileName]FeaturesSet, unsupportedFeaturesMap map[ConformanceProfileName]FeaturesSet) { for key, report := range p { // report the overall result for core features @@ -196,9 +196,9 @@ func buildReportSummary(status confv1.Status) string { case confv1.Success: message = "succeeded" case confv1.Partial: - message = fmt.Sprintf("partially succeeded with %d test skips", status.Statistics.Skipped) + message = fmt.Sprintf("partially succeeded with %d test skips", status.Skipped) case confv1.Failure: - message = fmt.Sprintf("failed with %d test failures", status.Statistics.Failed) + message = fmt.Sprintf("failed with %d test failures", status.Failed) } return message } diff --git a/hack/verify-golint.sh b/hack/verify-golint.sh index 66a5279c20..316858fc90 100755 --- a/hack/verify-golint.sh +++ b/hack/verify-golint.sh @@ -18,7 +18,7 @@ set -o errexit set -o nounset set -o pipefail -readonly VERSION="v1.64.8" +readonly VERSION="v2.6.2" readonly KUBE_ROOT=$(dirname "${BASH_SOURCE}")/.. cd "${KUBE_ROOT}" diff --git a/pkg/test/crd/crd_test.go b/pkg/test/crd/crd_test.go index 9becc1aaf4..fa23323d05 100644 --- a/pkg/test/crd/crd_test.go +++ b/pkg/test/crd/crd_test.go @@ -146,7 +146,7 @@ func executeKubectlCommand(t *testing.T, kubectl, kubeconfig string, args []stri cacheDir := filepath.Dir(kubeconfig) args = append([]string{"--cache-dir", cacheDir}, args...) - cmd := exec.Command(kubectl, args...) + cmd := exec.CommandContext(t.Context(), kubectl, args...) cmd.Env = []string{ fmt.Sprintf("KUBECONFIG=%s", kubeconfig), } diff --git a/pkg/utils/duration.go b/pkg/utils/duration.go index f7e54ab655..1024e21893 100644 --- a/pkg/utils/duration.go +++ b/pkg/utils/duration.go @@ -38,7 +38,7 @@ func ParseDuration(s string) (*time.Duration, error) { See https://gateway-api.sigs.k8s.io/geps/gep-2257/ for more details. */ if !re.MatchString(s) { - return nil, errors.New("Invalid duration format") + return nil, errors.New("invalid duration format") } parsedTime, err := time.ParseDuration(s) if err != nil { @@ -72,17 +72,17 @@ func FormatDuration(duration time.Duration) (string, error) { // check if a negative value if duration < 0 { - return "", errors.New("Invalid duration format. Cannot have negative durations") + return "", errors.New("invalid duration format. Cannot have negative durations") } // check for the maximum value allowed to be expressed if duration > maxDuration { - return "", errors.New("Invalid duration format. Duration larger than maximum expression allowed in GEP-2257") + return "", errors.New("invalid duration format. Duration larger than maximum expression allowed in GEP-2257") } // time.Duration allows for floating point ms, which is not allowed in GEP-2257 durationMicroseconds := duration.Microseconds() if durationMicroseconds%1000 != 0 { - return "", errors.New("Cannot express sub-milliseconds precision in GEP-2257") + return "", errors.New("cannot express sub-milliseconds precision in GEP-2257") } output := ""