diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 000000000..b8ec0c66b --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,15 @@ +name: golangci-lint +on: + pull_request: +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: 1.24 + - name: golangci-lint + run: make lint diff --git a/.gitignore b/.gitignore index e5309d6b0..fd790a322 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /bin/ +.bin /test/bin/ /*.tar.gz* ci.env diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 000000000..d2453ff4d --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,27 @@ +version: "2" +run: + tests: true +# build-tags: +# - journald +linters: + default: none + enable: + - ineffassign + exclusions: + generated: lax + paths: + - vendor + - third_party + - third_party$ + - builtin$ + - examples$ +issues: + max-issues-per-linter: 0 + max-same-issues: 0 +formatters: + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e8e938733..f5210acbc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,7 +4,7 @@ repos: hooks: - id: gitleaks - repo: https://github.com/golangci/golangci-lint - rev: v1.52.2 + rev: v2.3.1 hooks: - id: golangci-lint - repo: https://github.com/jumanjihouse/pre-commit-hooks diff --git a/Makefile b/Makefile index bcee7ff56..087d4191c 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ # Build the node-problem-detector image. .PHONY: all \ - vet fmt version test e2e-test \ + lint vet fmt version test e2e-test \ build-binaries build-container build-tar build \ docker-builder build-in-docker \ push-container push-tar push release clean depup \ @@ -113,6 +113,18 @@ else LOGCOUNTER=*dont-include-log-counter endif +GOLANGCI_LINT_VERSION := v2.2.0 +GOLANGCI_LINT := ./.bin/golangci-lint + +lint: $(GOLANGCI_LINT) + $(GOLANGCI_LINT) run --config .golangci.yml ./... + +$(GOLANGCI_LINT): + @echo "golangci-lint not found, downloading..." + @mkdir -p ./.bin + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./.bin $(GOLANGCI_LINT_VERSION) + + vet: go list -tags "$(HOST_PLATFORM_BUILD_TAGS)" ./... | \ grep -v "./vendor/*" | \ diff --git a/pkg/logcounter/types/types.go b/pkg/logcounter/types/types.go index 455f5cae9..0a3b933af 100644 --- a/pkg/logcounter/types/types.go +++ b/pkg/logcounter/types/types.go @@ -14,8 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. */ +// Package types contains the types for log counter. package types +// LogCounter is the interface for a log counter. type LogCounter interface { Count() (int, error) } diff --git a/pkg/systemstatsmonitor/cpu_collector_unix.go b/pkg/systemstatsmonitor/cpu_collector_unix.go index 3181fe0d4..193311bd2 100644 --- a/pkg/systemstatsmonitor/cpu_collector_unix.go +++ b/pkg/systemstatsmonitor/cpu_collector_unix.go @@ -62,6 +62,10 @@ func (cc *cpuCollector) recordSystemStats() { } fs, err := procfs.NewFS(cc.procPath) + if err != nil { + klog.Errorf("Failed to open procfs: %v", err) + return + } stats, err := fs.Stat() if err != nil { klog.Errorf("Failed to retrieve cpu/process stats: %v", err) diff --git a/pkg/systemstatsmonitor/net_collector.go b/pkg/systemstatsmonitor/net_collector.go index fb0fdac6b..542865115 100644 --- a/pkg/systemstatsmonitor/net_collector.go +++ b/pkg/systemstatsmonitor/net_collector.go @@ -219,6 +219,10 @@ func (nc *netCollector) mustRegisterMetric(metricID metrics.MetricID, descriptio func (nc *netCollector) recordNetDev() { fs, err := procfs.NewFS(nc.procPath) + if err != nil { + klog.Errorf("Failed to open procfs: %v", err) + return + } stats, err := fs.NetDev() if err != nil { klog.Errorf("Failed to retrieve net dev stat: %v", err)