From 3e64800668ebdcb32c81504a95e174f1a5add3a1 Mon Sep 17 00:00:00 2001 From: Sergey Kanzhelev Date: Thu, 11 Sep 2025 18:47:20 +0000 Subject: [PATCH] enabled a few more linter rules --- .golangci.yml | 109 +++++++++++++++++- pkg/healthchecker/health_checker.go | 17 ++- .../logwatchers/filelog/log_watcher_test.go | 2 +- pkg/systemstatsmonitor/net_collector_test.go | 1 + pkg/util/helpers_linux_test.go | 1 + pkg/util/metrics/helpers_test.go | 2 +- pkg/util/metrics/system/cmdline_args.go | 8 +- pkg/util/metrics/system/module_stats.go | 8 +- 8 files changed, 139 insertions(+), 9 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index d373f8c0..26046de9 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -6,11 +6,114 @@ run: linters: default: none enable: - - ineffassign - - unused - - govet + - arangolint + - asasalint + - asciicheck + - bidichk + - bodyclose + - canonicalheader + - containedctx + - contextcheck + # - copyloopvar 1 + # - cyclop 15 + - decorder + # - depguard 265 + - dogsled + # - dupl 2 + - dupword + - durationcheck + # - embeddedstructfieldcheck 5 + # - err113 64 - errcheck + - errchkjson + - errname + # - errorlint 4 + # - exhaustive 2 + # - exhaustruct 260 + - exptostd + - fatcontext + # - forbidigo 6 + # - forcetypeassert: 5 + # - funcorder: 5 + # - funlen: 24 + - ginkgolinter + - gocheckcompilerdirectives + # - gochecknoglobals 31 + # - gochecknoinits 14 + - gochecksumtype + # - gocognit 5 + - goconst + # - gocritic 3 + # - gocyclo 1 + # - godot 24 + # - godox 8 + - goheader + - gomoddirectives + - gomodguard + - goprintffuncname + # - gosec 55 + # - gosmopolitan 5 + - govet + - grouper + - iface + - importas + # - inamedparam 6 + - ineffassign + - interfacebloat + # - intrange 1 + # - ireturn 15 + # - lll 59 + - loggercheck + - maintidx + - makezero + - mirror + - misspell + # - mnd 29 + - musttag + - nakedret + # - nestif 7 + # - nilerr 2 + # - nilnesserr 2 + # - nilnil 3 + # - nlreturn 149 + - noctx + # - noinlineerr 81 + - nolintlint + # - nonamedreturns 2 + - nosprintfhostport + # - paralleltest 54 + # - perfsprint 14 + # - prealloc 5 + # - predeclared 1 + - promlinter + # - protogetter 7 + - reassign + # - recvcheck 3 + # - revive 133 + - rowserrcheck + - sloglint + - spancheck + - sqlclosecheck - staticcheck + - tagalign + # - tagliatelle 6 + - testableexamples + # - testifylint 10 + # - testpackage 34 + # - thelper 4 + - tparallel + - unconvert + # - unparam 1 + - unused + - usestdlibvars + # - usetesting 3 + # - varnamelen 33 + - wastedassign + - whitespace + # - wrapcheck 26 + # - wsl 403 + # - wsl_v5 58 + - zerologlint exclusions: generated: lax paths: diff --git a/pkg/healthchecker/health_checker.go b/pkg/healthchecker/health_checker.go index b81a63ad..7f74ba73 100644 --- a/pkg/healthchecker/health_checker.go +++ b/pkg/healthchecker/health_checker.go @@ -126,9 +126,22 @@ func logPatternHealthCheck(service string, loopBackTime time.Duration, logPatter // healthCheckEndpointOKFunc returns a function to check the status of an http endpoint func healthCheckEndpointOKFunc(endpoint string, timeout time.Duration) func() (bool, error) { return func() (bool, error) { + req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, endpoint, nil) + if err != nil { + return false, err + } httpClient := http.Client{Timeout: timeout} - response, err := httpClient.Get(endpoint) - if err != nil || response.StatusCode != http.StatusOK { + response, err := httpClient.Do(req) + if err != nil { + return false, nil + } + defer func() { + err := response.Body.Close() + if err != nil { + klog.Warningf("failed to close http client: %v", err) + } + }() + if response.StatusCode != http.StatusOK { return false, nil } return true, nil diff --git a/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_test.go b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_test.go index 74978384..28b2fb38 100644 --- a/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_test.go +++ b/pkg/systemlogmonitor/logwatchers/filelog/log_watcher_test.go @@ -148,7 +148,7 @@ Jan 2 03:04:05 kernel: [2.000000] 3 t.Logf("failed to remove temporary file %s: %v", f.Name(), err) } }() - _, err = f.Write([]byte(test.log)) + _, err = f.WriteString(test.log) assert.NoError(t, err) w := NewSyslogWatcherOrDie(types.WatcherConfig{ diff --git a/pkg/systemstatsmonitor/net_collector_test.go b/pkg/systemstatsmonitor/net_collector_test.go index 58a3b450..fab5459b 100644 --- a/pkg/systemstatsmonitor/net_collector_test.go +++ b/pkg/systemstatsmonitor/net_collector_test.go @@ -85,6 +85,7 @@ func testCollectAux(t *testing.T, name string, excludeInterfaceRegexp ssmtypes.N } func TestCollect(t *testing.T) { + t.Parallel() tcs := []struct { Name string ExcludeInterfaceRegexp ssmtypes.NetStatsInterfaceRegexp diff --git a/pkg/util/helpers_linux_test.go b/pkg/util/helpers_linux_test.go index 78bd4660..eb6c99c6 100644 --- a/pkg/util/helpers_linux_test.go +++ b/pkg/util/helpers_linux_test.go @@ -21,6 +21,7 @@ import ( ) func TestGetOSVersionLinux(t *testing.T) { + t.Parallel() testCases := []struct { name string fakeOSReleasePath string diff --git a/pkg/util/metrics/helpers_test.go b/pkg/util/metrics/helpers_test.go index 548921cc..1cb88091 100644 --- a/pkg/util/metrics/helpers_test.go +++ b/pkg/util/metrics/helpers_test.go @@ -56,7 +56,7 @@ func TestPrometheusMetricsParsingAndMatching(t *testing.T) { // Metric with non-existent label. { Name: "host_uptime", - Labels: map[string]string{"non-existant-version": "0.0.1"}, + Labels: map[string]string{"non-existent-version": "0.0.1"}, }, // Metric with incorrect label. { diff --git a/pkg/util/metrics/system/cmdline_args.go b/pkg/util/metrics/system/cmdline_args.go index 46e6d0e6..c1c8c751 100644 --- a/pkg/util/metrics/system/cmdline_args.go +++ b/pkg/util/metrics/system/cmdline_args.go @@ -17,6 +17,8 @@ import ( "encoding/json" "fmt" "strings" + + "k8s.io/klog/v2" ) type CmdlineArg struct { @@ -25,7 +27,11 @@ type CmdlineArg struct { } func (d CmdlineArg) String() string { - s, _ := json.Marshal(d) + s, err := json.Marshal(d) + if err != nil { + klog.Errorf("failed to marshal cmdline arg: %v", err) + return "" + } return string(s) } diff --git a/pkg/util/metrics/system/module_stats.go b/pkg/util/metrics/system/module_stats.go index b9e98eab..b5d540d1 100644 --- a/pkg/util/metrics/system/module_stats.go +++ b/pkg/util/metrics/system/module_stats.go @@ -18,6 +18,8 @@ import ( "fmt" "strconv" "strings" + + "k8s.io/klog/v2" ) type Module struct { @@ -29,7 +31,11 @@ type Module struct { } func (d Module) String() string { - s, _ := json.Marshal(d) + s, err := json.Marshal(d) + if err != nil { + klog.Errorf("failed to marshal module stat: %v", err) + return "" + } return string(s) }