From 3ba3b61dacdbfd0bb7ec3d343f69e624bee454ea Mon Sep 17 00:00:00 2001 From: Joris Baum Date: Thu, 18 Sep 2025 15:41:27 +0200 Subject: [PATCH] Upgrade to golangci-lint v2 for improved linting Noteworthy changes in golangci-lint v2: * New config format (migrated with `migrate` command) * Default timeout is now 0 instead of 1 minute. Also fixes issues reported by golangci-lint v2: * Do not use dot imports for sources under `cmd`. * Have nicer logs * Remove redundant type `int` * Use `fmt.Fprintf(x, ...)` instead of `x.Write(fmt.Sprintf(...))` --- scripts/subtests/lint | 2 +- src/.golangci.yml | 25 +++++++++++++++-- src/cmd/cf-auth-proxy/main.go | 20 +++++++------- src/cmd/gateway/main.go | 20 +++++++------- src/cmd/log-cache/main.go | 32 +++++++++++----------- src/cmd/syslog-server/main.go | 12 ++++---- src/internal/gateway/gateway.go | 2 +- src/internal/promql/promql.go | 2 +- src/internal/routing/local_store_reader.go | 6 ++-- src/internal/routing/routing_table.go | 2 +- src/internal/testing/acceptance.go | 2 +- src/internal/testing/auth.go | 2 +- src/internal/testing/time.go | 2 +- 13 files changed, 75 insertions(+), 54 deletions(-) diff --git a/scripts/subtests/lint b/scripts/subtests/lint index bb8f5c95b..bc83445e5 100755 --- a/scripts/subtests/lint +++ b/scripts/subtests/lint @@ -9,7 +9,7 @@ set +e golangci_lint_executable=$(which golangci-lint) set -e if [ -z "${golangci_lint_executable}" ] || [ ! -x "${golangci_lint_executable}" ]; then - go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest fi pushd "${SCRIPT_DIR}/../../src" > /dev/null diff --git a/src/.golangci.yml b/src/.golangci.yml index 155df3786..0f67598be 100644 --- a/src/.golangci.yml +++ b/src/.golangci.yml @@ -1,6 +1,8 @@ +version: "2" + run: - # Timeout for analysis. - # Default: 1m. + # Timeout full work, e.g. 30s, 5m. + # Default: none timeout: 5m linters: @@ -13,9 +15,28 @@ linters: - gosec # Enforces standards of using ginkgo and gomega. - ginkgolinter + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + paths: + - third_party$ + - builtin$ + - examples$ issues: # Disable max issues per linter. max-issues-per-linter: 0 # Disable max same issues. max-same-issues: 0 + +formatters: + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/src/cmd/cf-auth-proxy/main.go b/src/cmd/cf-auth-proxy/main.go index 20683a43e..ccafa1fab 100644 --- a/src/cmd/cf-auth-proxy/main.go +++ b/src/cmd/cf-auth-proxy/main.go @@ -22,7 +22,7 @@ import ( "code.cloudfoundry.org/go-envstruct" client "code.cloudfoundry.org/go-log-cache/v3" "code.cloudfoundry.org/log-cache/internal/auth" - . "code.cloudfoundry.org/log-cache/internal/cfauthproxy" + "code.cloudfoundry.org/log-cache/internal/cfauthproxy" "code.cloudfoundry.org/log-cache/internal/plumbing" "code.cloudfoundry.org/log-cache/internal/promql" ) @@ -109,7 +109,7 @@ func main() { if cfg.ProxyCAPath != "" { proxyCACertPool := loadCA(cfg.ProxyCAPath, loggr) - metaHTTPClient.Transport = NewTransportWithRootCA(proxyCACertPool) + metaHTTPClient.Transport = cfauthproxy.NewTransportWithRootCA(proxyCACertPool) } metaFetcher := client.NewClient( @@ -125,26 +125,26 @@ func main() { capiClient, ) - proxyOptions := []CFAuthProxyOption{ - WithAuthMiddleware(middlewareProvider.Middleware), + proxyOptions := []cfauthproxy.CFAuthProxyOption{ + cfauthproxy.WithAuthMiddleware(middlewareProvider.Middleware), } if cfg.ProxyCAPath != "" { proxyCACertPool := loadCA(cfg.ProxyCAPath, loggr) - proxyOptions = append(proxyOptions, WithCFAuthProxyCACertPool(proxyCACertPool)) + proxyOptions = append(proxyOptions, cfauthproxy.WithCFAuthProxyCACertPool(proxyCACertPool)) } if cfg.PromQLUnimplemented { - proxyOptions = append(proxyOptions, WithPromMiddleware(promql.UnimplementedMiddleware)) + proxyOptions = append(proxyOptions, cfauthproxy.WithPromMiddleware(promql.UnimplementedMiddleware)) } if cfg.CertPath == "" && cfg.KeyPath == "" { - proxyOptions = append(proxyOptions, WithCFAuthProxyTLSDisabled()) + proxyOptions = append(proxyOptions, cfauthproxy.WithCFAuthProxyTLSDisabled()) } else { - proxyOptions = append(proxyOptions, WithCFAuthProxyTLSServer(cfg.CertPath, cfg.KeyPath)) + proxyOptions = append(proxyOptions, cfauthproxy.WithCFAuthProxyTLSServer(cfg.CertPath, cfg.KeyPath)) } - proxy := NewCFAuthProxy( + proxy := cfauthproxy.NewCFAuthProxy( gatewayURL.String(), cfg.Addr, proxyOptions..., @@ -169,7 +169,7 @@ func main() { accessLogger := auth.NewAccessLogger(accessLog) accessMiddleware := auth.NewAccessMiddleware(accessLogger, cfg.InternalIP, localPort) - WithAccessMiddleware(accessMiddleware)(proxy) + cfauthproxy.WithAccessMiddleware(accessMiddleware)(proxy) } proxy.Start(uaaClient.RefreshTokenKeys) diff --git a/src/cmd/gateway/main.go b/src/cmd/gateway/main.go index 8854530da..247bf2754 100644 --- a/src/cmd/gateway/main.go +++ b/src/cmd/gateway/main.go @@ -14,7 +14,7 @@ import ( //nolint: gosec _ "net/http/pprof" - . "code.cloudfoundry.org/log-cache/internal/gateway" + "code.cloudfoundry.org/log-cache/internal/gateway" "code.cloudfoundry.org/log-cache/internal/plumbing" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -67,14 +67,14 @@ func main() { go func() { log.Println("PPROF SERVER STOPPED " + pprofServer.ListenAndServe().Error()) }() } - gatewayOptions := []GatewayOption{ - WithGatewayLogger(gatewayLoggr), - WithGatewayVersion(cfg.Version), - WithGatewayBlock(), + gatewayOptions := []gateway.GatewayOption{ + gateway.WithGatewayLogger(gatewayLoggr), + gateway.WithGatewayVersion(cfg.Version), + gateway.WithGatewayBlock(), } if cfg.ProxyCertPath != "" || cfg.ProxyKeyPath != "" { - gatewayOptions = append(gatewayOptions, WithGatewayTLSServer(cfg.ProxyCertPath, cfg.ProxyKeyPath)) + gatewayOptions = append(gatewayOptions, gateway.WithGatewayTLSServer(cfg.ProxyCertPath, cfg.ProxyKeyPath)) } if cfg.TLS.HasAnyCredential() { tlsConfig, err := tlsconfig.Build( @@ -88,13 +88,13 @@ func main() { panic(err) } - gatewayOptions = append(gatewayOptions, WithGatewayLogCacheDialOpts( + gatewayOptions = append(gatewayOptions, gateway.WithGatewayLogCacheDialOpts( grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(50*1024*1024)), ), ) } else { - gatewayOptions = append(gatewayOptions, WithGatewayLogCacheDialOpts( + gatewayOptions = append(gatewayOptions, gateway.WithGatewayLogCacheDialOpts( grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(50*1024*1024)), ), @@ -102,7 +102,7 @@ func main() { } - gateway := NewGateway(cfg.LogCacheAddr, cfg.Addr, gatewayOptions...) + gw := gateway.NewGateway(cfg.LogCacheAddr, cfg.Addr, gatewayOptions...) - gateway.Start() + gw.Start() } diff --git a/src/cmd/log-cache/main.go b/src/cmd/log-cache/main.go index bdcf51687..f8d197064 100644 --- a/src/cmd/log-cache/main.go +++ b/src/cmd/log-cache/main.go @@ -17,7 +17,7 @@ import ( "code.cloudfoundry.org/tlsconfig" "code.cloudfoundry.org/go-envstruct" - . "code.cloudfoundry.org/log-cache/internal/cache" + "code.cloudfoundry.org/log-cache/internal/cache" "code.cloudfoundry.org/log-cache/internal/plumbing" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -89,17 +89,17 @@ func main() { } }(time.Now()) - logCacheOptions := []LogCacheOption{ - WithAddr(cfg.Addr), - WithMemoryLimitPercent(float64(cfg.MemoryLimitPercent)), - WithMemoryLimit(cfg.MemoryLimit), - WithMaxPerSource(cfg.MaxPerSource), - WithQueryTimeout(cfg.QueryTimeout), - WithTruncationInterval(cfg.TruncationInterval), - WithPrunesPerGC(cfg.PrunesPerGC), - WithIngressBufferSize(cfg.IngressBufferSize), - WithIngressBufferReadBatchSize(cfg.IngressBufferReadBatchSize), - WithIngressBufferReadBatchInterval(cfg.IngressBufferReadBatchInterval), + logCacheOptions := []cache.LogCacheOption{ + cache.WithAddr(cfg.Addr), + cache.WithMemoryLimitPercent(float64(cfg.MemoryLimitPercent)), + cache.WithMemoryLimit(cfg.MemoryLimit), + cache.WithMaxPerSource(cfg.MaxPerSource), + cache.WithQueryTimeout(cfg.QueryTimeout), + cache.WithTruncationInterval(cfg.TruncationInterval), + cache.WithPrunesPerGC(cfg.PrunesPerGC), + cache.WithIngressBufferSize(cfg.IngressBufferSize), + cache.WithIngressBufferReadBatchSize(cfg.IngressBufferReadBatchSize), + cache.WithIngressBufferReadBatchInterval(cfg.IngressBufferReadBatchInterval), } var transport grpc.DialOption if cfg.TLS.HasAnyCredential() { @@ -125,24 +125,24 @@ func main() { if err != nil { panic(err) } - logCacheOptions = append(logCacheOptions, WithServerOpts(grpc.Creds(credentials.NewTLS(tlsConfigServer)), grpc.MaxRecvMsgSize(50*1024*1024))) + logCacheOptions = append(logCacheOptions, cache.WithServerOpts(grpc.Creds(credentials.NewTLS(tlsConfigServer)), grpc.MaxRecvMsgSize(50*1024*1024))) } else { transport = grpc.WithTransportCredentials(insecure.NewCredentials()) } - logCacheOptions = append(logCacheOptions, WithClustered( + logCacheOptions = append(logCacheOptions, cache.WithClustered( cfg.NodeIndex, cfg.NodeAddrs, transport, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(50*1024*1024)), )) - cache := New( + lc := cache.New( m, logger, logCacheOptions..., ) - cache.Start() + lc.Start() waitForTermination() } diff --git a/src/cmd/syslog-server/main.go b/src/cmd/syslog-server/main.go index efe127976..a8d1b0800 100644 --- a/src/cmd/syslog-server/main.go +++ b/src/cmd/syslog-server/main.go @@ -13,7 +13,7 @@ import ( "code.cloudfoundry.org/go-envstruct" metrics "code.cloudfoundry.org/go-metric-registry" - . "code.cloudfoundry.org/log-cache/internal/nozzle" + "code.cloudfoundry.org/log-cache/internal/nozzle" "code.cloudfoundry.org/log-cache/internal/plumbing" "code.cloudfoundry.org/log-cache/internal/syslog" "code.cloudfoundry.org/tlsconfig" @@ -96,7 +96,7 @@ func main() { go server.Start() - nozzleOptions := []NozzleOption{} + nozzleOptions := []nozzle.NozzleOption{} if cfg.LogCacheTLS.HasAnyCredential() { tlsConfig, err := tlsconfig.Build( tlsconfig.WithInternalServiceDefaults(), @@ -108,12 +108,12 @@ func main() { if err != nil { panic(err) } - nozzleOptions = append(nozzleOptions, WithDialOpts(grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))) + nozzleOptions = append(nozzleOptions, nozzle.WithDialOpts(grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))) } else { - nozzleOptions = append(nozzleOptions, WithDialOpts(grpc.WithTransportCredentials(insecure.NewCredentials()))) + nozzleOptions = append(nozzleOptions, nozzle.WithDialOpts(grpc.WithTransportCredentials(insecure.NewCredentials()))) } - nozzle := NewNozzle( + noz := nozzle.NewNozzle( server, cfg.LogCacheAddr, m, @@ -121,5 +121,5 @@ func main() { nozzleOptions..., ) - nozzle.Start() + noz.Start() } diff --git a/src/internal/gateway/gateway.go b/src/internal/gateway/gateway.go index 6be3282c2..eb04f2374 100644 --- a/src/internal/gateway/gateway.go +++ b/src/internal/gateway/gateway.go @@ -179,7 +179,7 @@ func (g *Gateway) listenAndServe() { } func (g *Gateway) handleInfoEndpoint(w http.ResponseWriter, r *http.Request) { - _, err := w.Write([]byte(fmt.Sprintf(`{"version":"%s","vm_uptime":"%d"}`+"\n", g.logCacheVersion, g.uptimeFn()))) + _, err := fmt.Fprintf(w, `{"version":"%s","vm_uptime":"%d"}`+"\n", g.logCacheVersion, g.uptimeFn()) if err != nil { g.log.Println("Cannot send result for the info endpoint") } diff --git a/src/internal/promql/promql.go b/src/internal/promql/promql.go index 7211baa9a..033d00b52 100644 --- a/src/internal/promql/promql.go +++ b/src/internal/promql/promql.go @@ -340,7 +340,7 @@ func (l *LogCacheQuerier) Select(params *storage.SelectParams, ll ...*labels.Mat } if len(sourceIDs) == 0 { - err := fmt.Errorf("Metric '%s' does not have a 'source_id' label.", metric) + err := fmt.Errorf("metric '%s' does not have a 'source_id' label", metric) l.errf(err) return nil, nil, err } diff --git a/src/internal/routing/local_store_reader.go b/src/internal/routing/local_store_reader.go index 3245b6279..2a00b6acf 100644 --- a/src/internal/routing/local_store_reader.go +++ b/src/internal/routing/local_store_reader.go @@ -49,11 +49,11 @@ func (r *LocalStoreReader) Read(ctx context.Context, req *logcache_v1.ReadReques } if req.Limit > 1000 { - return nil, fmt.Errorf("Limit (%d) must be 1000 or less", req.Limit) + return nil, fmt.Errorf("limit (%d) must be 1000 or less", req.Limit) } if req.Limit < 0 { - return nil, fmt.Errorf("Limit (%d) must be greater than zero", req.Limit) + return nil, fmt.Errorf("limit (%d) must be greater than zero", req.Limit) } if req.EndTime == 0 { @@ -69,7 +69,7 @@ func (r *LocalStoreReader) Read(ctx context.Context, req *logcache_v1.ReadReques if req.NameFilter != "" { nameFilter, err = regexp.Compile(req.NameFilter) if err != nil { - return nil, fmt.Errorf("Name filter must be a valid regular expression: %s", err) + return nil, fmt.Errorf("name filter must be a valid regular expression: %s", err) } } diff --git a/src/internal/routing/routing_table.go b/src/internal/routing/routing_table.go index cdc729021..51b4bb667 100644 --- a/src/internal/routing/routing_table.go +++ b/src/internal/routing/routing_table.go @@ -47,7 +47,7 @@ func (t *RoutingTable) Lookup(item string) []int { node := t.hasher.Hash(hashValue) var result []int - var replicationFactor int = int(t.replicationFactor) //#nosec G115 + var replicationFactor = int(t.replicationFactor) //#nosec G115 for n := 0; n < replicationFactor; n++ { result = append(result, (node+n*replicationFactor)%len(t.addresses)) } diff --git a/src/internal/testing/acceptance.go b/src/internal/testing/acceptance.go index 587b3f083..3a5ddc08a 100644 --- a/src/internal/testing/acceptance.go +++ b/src/internal/testing/acceptance.go @@ -3,7 +3,7 @@ package testing import ( "net" - . "github.com/onsi/gomega" + . "github.com/onsi/gomega" //nolint:staticcheck ) func GetFreePort() int { diff --git a/src/internal/testing/auth.go b/src/internal/testing/auth.go index b513b5614..0518b875b 100644 --- a/src/internal/testing/auth.go +++ b/src/internal/testing/auth.go @@ -9,7 +9,7 @@ import ( "strings" "time" - . "github.com/onsi/gomega" + . "github.com/onsi/gomega" //nolint:staticcheck ) func NewServerRequest(method, uri string, body io.Reader) (*http.Request, error) { diff --git a/src/internal/testing/time.go b/src/internal/testing/time.go index c7f05df16..e164a8d34 100644 --- a/src/internal/testing/time.go +++ b/src/internal/testing/time.go @@ -5,7 +5,7 @@ import ( "strconv" "time" - . "github.com/onsi/gomega" + . "github.com/onsi/gomega" //nolint:staticcheck ) func FormatTimeWithDecimalMillis(t time.Time) string {