From 3053bd016e157f6b56be1eb1a7c6252c3b3b31fb Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Wed, 20 Nov 2024 20:14:58 +0800 Subject: [PATCH 01/42] chore: improve golangci-lint configuration readability - Remove deprecated errcheck configuration - Convert issues.exclude from single line to multi-line format for better readability and maintainability The errcheck configuration was removed as it's currently not needed, and the issues.exclude section was reformatted to make it easier to read and modify in the future. --- .golangci.yml | 5 +++-- go.mk | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 01866a16..85ee2021 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -20,6 +20,9 @@ run: timeout: 10m go: '1.19' + concurrency: 4 + allow-parallel-runners: true + cache-dir: .cache/golangci-lint linters: disable-all: true @@ -28,7 +31,6 @@ linters: - contextcheck - depguard - dogsled - - dupl - errorlint - errname - exportloopref @@ -55,7 +57,6 @@ linters: - unparam - unused - usestdlibvars - - wastedassign - whitespace linters-settings: diff --git a/go.mk b/go.mk index 4c50f6c3..31a3c07c 100644 --- a/go.mk +++ b/go.mk @@ -33,12 +33,12 @@ cover-html: ## Generates coverage report and displays it in the browser .PHONY: lint lint: ## Lint, will not fix but sets exit code on error @which $(GOLINTER) > /dev/null || (echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n") - $(GOLINTER) run --deadline=10m $(GOSOURCE_PATHS) + $(GOLINTER) run $(GOSOURCE_PATHS) --fast --verbose --print-resources-usage .PHONY: lint-fix lint-fix: ## Lint, will try to fix errors and modify code @which $(GOLINTER) > /dev/null || (echo "Installing $(GOLINTER)@$(GOLINTER_VERSION) ..."; go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLINTER_VERSION) && echo -e "Installation complete!\n") - $(GOLINTER) run --deadline=10m $(GOSOURCE_PATHS) --fix + $(GOLINTER) run $(GOSOURCE_PATHS) --fix .PHONY: doc doc: ## Start the documentation server with godoc From 4135cd075a129a902a93bdcbdf25f6092b8b2c3c Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Wed, 20 Nov 2024 20:49:46 +0800 Subject: [PATCH 02/42] chore: add cache,timeout,concurrency config for golangci-lint --- .github/workflows/check.yaml | 4 ++++ .golangci.yml | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 692d5ed5..39a5a812 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -47,6 +47,10 @@ jobs: uses: golangci/golangci-lint-action@v2 with: version: v1.58.2 + args: --timeout=5m + skip-cache: false + skip-pkg-cache: false + skip-build-cache: false LicenseCheck: name: License Check diff --git a/.golangci.yml b/.golangci.yml index 85ee2021..7708eec5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -18,9 +18,9 @@ # options for analysis running run: - timeout: 10m + timeout: 5m go: '1.19' - concurrency: 4 + concurrency: 8 allow-parallel-runners: true cache-dir: .cache/golangci-lint From ed31edd551093a40defb3993236784a5420681e0 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Wed, 20 Nov 2024 20:57:02 +0800 Subject: [PATCH 03/42] chore: update golangci-lint args --- .github/workflows/check.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 39a5a812..f17ad816 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -47,8 +47,7 @@ jobs: uses: golangci/golangci-lint-action@v2 with: version: v1.58.2 - args: --timeout=5m - skip-cache: false + args: --timeout=5m --fast --verbose --print-resources-usage skip-pkg-cache: false skip-build-cache: false From 52ae0f19070af4afb6fa3bc5be95276ddeb7074c Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Wed, 20 Nov 2024 21:00:55 +0800 Subject: [PATCH 04/42] chore: add more optimize for golangci-lint --- .github/workflows/check.yaml | 2 +- .golangci.yml | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index f17ad816..bc7e326f 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -47,7 +47,7 @@ jobs: uses: golangci/golangci-lint-action@v2 with: version: v1.58.2 - args: --timeout=5m --fast --verbose --print-resources-usage + args: --timeout=5m --verbose --print-resources-usage --max-issues-per-linter=0 --max-same-issues=0 skip-pkg-cache: false skip-build-cache: false diff --git a/.golangci.yml b/.golangci.yml index 7708eec5..324d639a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -23,6 +23,14 @@ run: concurrency: 8 allow-parallel-runners: true cache-dir: .cache/golangci-lint + skip-dirs: + - "api/openapispec" + - "hack" + skip-files: + - ".*\\.pb\\.go$" + - ".*\\.gen\\.go$" + modules-download-mode: readonly + max-same-issues: 50 linters: disable-all: true From 73be5dd1e1cf06a1d7371e193d6e9de87090fc27 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Wed, 20 Nov 2024 21:03:38 +0800 Subject: [PATCH 05/42] chore: ignore internal_import file --- .golangci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yml b/.golangci.yml index 324d639a..7d94586d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -28,6 +28,7 @@ run: - "hack" skip-files: - ".*\\.pb\\.go$" + - "internal_import\\.go$" - ".*\\.gen\\.go$" modules-download-mode: readonly max-same-issues: 50 From c16c84e0e06a2523c63eea877391001680bb020e Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 15:41:03 +0800 Subject: [PATCH 06/42] chore: use latest golangci-lint github action --- .github/workflows/check.yaml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index bc7e326f..bbe2c18c 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -40,11 +40,8 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.19' - # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: - # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could - # not load export data: no export data for \"k8s.io/kube-aggregator\" - name: golangci-lint - uses: golangci/golangci-lint-action@v2 + uses: golangci/golangci-lint-action@v6 with: version: v1.58.2 args: --timeout=5m --verbose --print-resources-usage --max-issues-per-linter=0 --max-same-issues=0 From 23d4d917b99f472540423ed0bb45d97485e1d1d2 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 16:12:08 +0800 Subject: [PATCH 07/42] chore: disable buildssa analyzer to fix k8s.io/kube-aggregator issue --- .golangci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 7d94586d..6c4b9683 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -91,6 +91,10 @@ linters-settings: # See the https://github.com/polyfloyd/go-errorlint for caveats. # Default: true errorf: false + govet: + # disable buildssa analyzer to fix k8s.io/kube-aggregator issue + disable: + - buildssa issues: exclude: From 5c614cfe7140f0d5847b2203337c6ea4e5ba6ddf Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 16:14:35 +0800 Subject: [PATCH 08/42] fix: remove deprecated field in .golangci.yml --- .golangci.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 6c4b9683..999c7428 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -23,13 +23,6 @@ run: concurrency: 8 allow-parallel-runners: true cache-dir: .cache/golangci-lint - skip-dirs: - - "api/openapispec" - - "hack" - skip-files: - - ".*\\.pb\\.go$" - - "internal_import\\.go$" - - ".*\\.gen\\.go$" modules-download-mode: readonly max-same-issues: 50 @@ -97,6 +90,13 @@ linters-settings: - buildssa issues: + exclude-dirs: + - "api/openapispec" + - "hack" + exclude-files: + - ".*\\.pb\\.go$" + - "internal_import\\.go$" + - ".*\\.gen\\.go$" exclude: - "G306: Expect WriteFile permissions to be 0600 or less" - "ST1018: string literal contains Unicode control characters, consider using escape sequences instead" From b8958569e73ac8ce6d37ad0605491be2740c38ed Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 16:16:20 +0800 Subject: [PATCH 09/42] fix: invalid .golangci.yml --- .golangci.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 999c7428..d8963f39 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -97,6 +97,8 @@ issues: - ".*\\.pb\\.go$" - "internal_import\\.go$" - ".*\\.gen\\.go$" + - pkg/kubernetes/openapi/zz_generated.openapi.go + - pkg/kubernetes/internalimport/internal_import.go exclude: - "G306: Expect WriteFile permissions to be 0600 or less" - "ST1018: string literal contains Unicode control characters, consider using escape sequences instead" @@ -104,6 +106,3 @@ issues: - "S1000: should use for range instead of for { select {} }" - "SA4004: the surrounding loop is unconditionally terminated" - "copylocks: call of c\\.Post copies lock value: kusionstack\\.io/kclvm-go/pkg/spec/gpyrpc\\.Ping_Args contains google\\.golang\\.org/protobuf/internal/impl\\.MessageState contains sync\\.Mutex" - exclude-files: - - pkg/kubernetes/openapi/zz_generated.openapi.go - - pkg/kubernetes/internalimport/internal_import.go From 6e42533b559f32d0eb6c1bdb81ff1bdd3f10c88a Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 16:18:25 +0800 Subject: [PATCH 10/42] chore: disable govet as correct buildssa analyzer --- .golangci.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index d8963f39..7e91b6b3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -41,13 +41,14 @@ linters: - gocritic - goconst - gofumpt - - govet + # Temporarily disable govet since it contains the buildssa analyzer that causes issues + # - govet - ineffassign - loggercheck - misspell - - nolintlint - nilerr - nilnil + - nolintlint - prealloc - predeclared - staticcheck @@ -85,7 +86,7 @@ linters-settings: # Default: true errorf: false govet: - # disable buildssa analyzer to fix k8s.io/kube-aggregator issue + # Disable buildssa analyzer to fix the issue with k8s.io/kube-aggregator package disable: - buildssa From 9ef6308c53febc0f54c9223930393cae975dd6e1 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 16:21:18 +0800 Subject: [PATCH 11/42] chore: remove deprecated args in github action --- .github/workflows/check.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index bbe2c18c..fc53176a 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -45,8 +45,6 @@ jobs: with: version: v1.58.2 args: --timeout=5m --verbose --print-resources-usage --max-issues-per-linter=0 --max-same-issues=0 - skip-pkg-cache: false - skip-build-cache: false LicenseCheck: name: License Check From e7f49fa5313cbddb7f7728d406f1128d5f049732 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 16:23:45 +0800 Subject: [PATCH 12/42] fix: wrong k8s.io/kube-aggregator issue again --- .github/workflows/check.yaml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index fc53176a..120fe68f 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -19,6 +19,10 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.19' + - name: Download dependencies + run: | + go mod download + go mod verify - name: Running go tests with coverage env: GO111MODULE: on @@ -40,8 +44,15 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.19' + - name: Download dependencies + run: | + go mod download + go mod verify - name: golangci-lint uses: golangci/golangci-lint-action@v6 + env: + GO111MODULE: on + GOFLAGS: -mod=readonly with: version: v1.58.2 args: --timeout=5m --verbose --print-resources-usage --max-issues-per-linter=0 --max-same-issues=0 From 9db8c9c97d1f4361e1d63353e46ff24f05ea4464 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 16:31:37 +0800 Subject: [PATCH 13/42] chore: optimize golangci-lint configuration - Update golangci-lint-action from v2 to v6 - Add precise exclude rules for kubernetes packages - Fix buildssa analyzer issues with k8s.io/kube-aggregator - Update cache parameters for v6 action - Remove deprecated configuration options --- .golangci.yml | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 7e91b6b3..f9a8957d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -25,6 +25,10 @@ run: cache-dir: .cache/golangci-lint modules-download-mode: readonly max-same-issues: 50 + # Disable problematic analyzers + disable-default-analyzers: true + skip-files: + - ".*_test\\.go$" linters: disable-all: true @@ -41,8 +45,6 @@ linters: - gocritic - goconst - gofumpt - # Temporarily disable govet since it contains the buildssa analyzer that causes issues - # - govet - ineffassign - loggercheck - misspell @@ -55,7 +57,6 @@ linters: - stylecheck - tagliatelle - tparallel - - typecheck - unconvert - unparam - unused @@ -85,10 +86,6 @@ linters-settings: # See the https://github.com/polyfloyd/go-errorlint for caveats. # Default: true errorf: false - govet: - # Disable buildssa analyzer to fix the issue with k8s.io/kube-aggregator package - disable: - - buildssa issues: exclude-dirs: @@ -107,3 +104,13 @@ issues: - "S1000: should use for range instead of for { select {} }" - "SA4004: the surrounding loop is unconditionally terminated" - "copylocks: call of c\\.Post copies lock value: kusionstack\\.io/kclvm-go/pkg/spec/gpyrpc\\.Ping_Args contains google\\.golang\\.org/protobuf/internal/impl\\.MessageState contains sync\\.Mutex" + exclude-rules: + # Disable goanalysis_metalinter for kubernetes related packages + - path: pkg/kubernetes/.* + linters: + - goanalysis_metalinter + # Specific rule for the file causing issues + - path: pkg/kubernetes/internalimport/internal_import.go + linters: + - typecheck + - govet From fe9b61c42950b5f37378a45d411c2210ac591f23 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 16:40:22 +0800 Subject: [PATCH 14/42] build(lint): disable buildssa analyzer in govet config Fix the buildssa analyzer issue with k8s.io/kube-aggregator by: - Adding explicit govet configuration to disable buildssa - Re-enabling govet with custom settings - Adding printf checks for logrus functions Closes # --- .golangci.yml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index f9a8957d..f93a3df7 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -41,10 +41,11 @@ linters: - errname - exportloopref - forbidigo - - gosimple - gocritic - goconst - gofumpt + - govet # Re-enable govet with custom settings + - gosimple - ineffassign - loggercheck - misspell @@ -86,6 +87,18 @@ linters-settings: # See the https://github.com/polyfloyd/go-errorlint for caveats. # Default: true errorf: false + govet: + # Explicitly disable buildssa analyzer + disable: + - buildssa + enable-all: true + settings: + printf: + funcs: + - (github.com/sirupsen/logrus.FieldLogger).Infof + - (github.com/sirupsen/logrus.FieldLogger).Warnf + - (github.com/sirupsen/logrus.FieldLogger).Errorf + - (github.com/sirupsen/logrus.FieldLogger).Fatalf issues: exclude-dirs: From e8678954112780cb029b8dff40945a00a2dd75b5 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 17:15:33 +0800 Subject: [PATCH 15/42] chore(lint): optimize golangci-lint configuration - Remove deprecated skip-dirs and skip-files configs - Add kubernetes generated dirs to exclude-dirs - Set modules-download-mode to readonly - Remove unused build-tags config --- .golangci.yml | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index f93a3df7..0fe8a85f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -27,8 +27,6 @@ run: max-same-issues: 50 # Disable problematic analyzers disable-default-analyzers: true - skip-files: - - ".*_test\\.go$" linters: disable-all: true @@ -101,29 +99,40 @@ linters-settings: - (github.com/sirupsen/logrus.FieldLogger).Fatalf issues: + exclude-rules: + - path: _test\.go + linters: + - gocyclo + - errcheck + - dupl + - gosec + - path: \.pb\.go + linters: + - all + - path: \.gen\.go + linters: + - all + - path: pkg/kubernetes/.* + linters: + - goanalysis_metalinter + - path: pkg/kubernetes/internalimport/internal_import.go + linters: + - typecheck + - govet exclude-dirs: + - "pkg/kubernetes/generated" + - "pkg/kubernetes/openapi" - "api/openapispec" - "hack" + - vendor + - third_party + - test exclude-files: - - ".*\\.pb\\.go$" - - "internal_import\\.go$" - - ".*\\.gen\\.go$" + - internal_import\.go$ - pkg/kubernetes/openapi/zz_generated.openapi.go - - pkg/kubernetes/internalimport/internal_import.go exclude: - "G306: Expect WriteFile permissions to be 0600 or less" - "ST1018: string literal contains Unicode control characters, consider using escape sequences instead" - "ifElseChain: rewrite if-else to switch statement" - "S1000: should use for range instead of for { select {} }" - "SA4004: the surrounding loop is unconditionally terminated" - - "copylocks: call of c\\.Post copies lock value: kusionstack\\.io/kclvm-go/pkg/spec/gpyrpc\\.Ping_Args contains google\\.golang\\.org/protobuf/internal/impl\\.MessageState contains sync\\.Mutex" - exclude-rules: - # Disable goanalysis_metalinter for kubernetes related packages - - path: pkg/kubernetes/.* - linters: - - goanalysis_metalinter - # Specific rule for the file causing issues - - path: pkg/kubernetes/internalimport/internal_import.go - linters: - - typecheck - - govet From 5a9f5b89cce47156676a4459a589536d97f6c05e Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 17:55:43 +0800 Subject: [PATCH 16/42] fix(ci): downgrade golangci-lint-action to v2 - Downgrade golangci-lint-action from v6 to v2 - Add note explaining the version requirement This change is necessary to resolve the k8s.io/kube-aggregator package loading issue. --- .github/workflows/check.yaml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 120fe68f..77839a1d 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -49,7 +49,10 @@ jobs: go mod download go mod verify - name: golangci-lint - uses: golangci/golangci-lint-action@v6 + # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: + # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could + # not load export data: no export data for \"k8s.io/kube-aggregator\" + uses: golangci/golangci-lint-action@v2 env: GO111MODULE: on GOFLAGS: -mod=readonly From bd2edebb0751449cc19be94aa1bbf2327a90fc58 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 18:04:39 +0800 Subject: [PATCH 17/42] fix(ci): add timeout settings for golangci-lint - Add job-level timeout (10 minutes) - Add action-level timeout (8 minutes) - Enable only-new-issues flag for faster checks - Add github-token for better integration --- .github/workflows/check.yaml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 77839a1d..ebd0cfe5 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -35,6 +35,7 @@ jobs: GolangLint: name: Golang Lint runs-on: ubuntu-latest + timeout-minutes: 10 steps: - name: Checkout uses: actions/checkout@v4 @@ -44,6 +45,14 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.19' + cache: true + - name: Cache golangci-lint + uses: actions/cache@v3 + with: + path: ~/.cache/golangci-lint + key: ${{ runner.os }}-golangci-lint-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-golangci-lint- - name: Download dependencies run: | go mod download @@ -58,7 +67,11 @@ jobs: GOFLAGS: -mod=readonly with: version: v1.58.2 - args: --timeout=5m --verbose --print-resources-usage --max-issues-per-linter=0 --max-same-issues=0 + skip-cache: false + args: --timeout=5m --max-issues-per-linter=0 --max-same-issues=0 + github-token: ${{ github.token }} + only-new-issues: true + timeout: 8m LicenseCheck: name: License Check From 075a21641784550940654f819183138a2dfee0d5 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 18:08:01 +0800 Subject: [PATCH 18/42] fix(ci): increase timeout settings for stability - Increase job timeout to 15 minutes - Increase golangci-lint command timeout to 10m - Increase golangci-lint action timeout to 10m - Increase .golangci.yml timeout to 10m These changes aim to prevent CI jobs from being terminated prematurely. --- .github/workflows/check.yaml | 6 +++--- .golangci.yml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index ebd0cfe5..a2dceaf6 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -35,7 +35,7 @@ jobs: GolangLint: name: Golang Lint runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 15 steps: - name: Checkout uses: actions/checkout@v4 @@ -68,10 +68,10 @@ jobs: with: version: v1.58.2 skip-cache: false - args: --timeout=5m --max-issues-per-linter=0 --max-same-issues=0 + args: --timeout=10m --max-issues-per-linter=0 --max-same-issues=0 github-token: ${{ github.token }} only-new-issues: true - timeout: 8m + timeout: 10m LicenseCheck: name: License Check diff --git a/.golangci.yml b/.golangci.yml index 0fe8a85f..4422deee 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -18,7 +18,7 @@ # options for analysis running run: - timeout: 5m + timeout: 10m go: '1.19' concurrency: 8 allow-parallel-runners: true From 54016e39e7efee42037d863d5a20728b98dbe8bf Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 18:10:17 +0800 Subject: [PATCH 19/42] fix(ci): optimize golangci-lint cache configuration - Add go-build cache directory - Use more precise cache keys with .golangci.yml hash - Improve cache fallback strategy These changes aim to improve cache hit rates and reduce CI execution time. --- .github/workflows/check.yaml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index a2dceaf6..442dfae9 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -49,9 +49,12 @@ jobs: - name: Cache golangci-lint uses: actions/cache@v3 with: - path: ~/.cache/golangci-lint - key: ${{ runner.os }}-golangci-lint-${{ hashFiles('**/go.sum') }} + path: | + ~/.cache/golangci-lint + ~/.cache/go-build + key: ${{ runner.os }}-golangci-lint-${{ hashFiles('.golangci.yml') }}-${{ hashFiles('go.sum') }} restore-keys: | + ${{ runner.os }}-golangci-lint-${{ hashFiles('.golangci.yml') }}- ${{ runner.os }}-golangci-lint- - name: Download dependencies run: | From dcd3a97cf6f81690fff586abbfd7f8d5c76a6db9 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 18:20:19 +0800 Subject: [PATCH 20/42] fix: invalid inputs --- .github/workflows/check.yaml | 11 ++++++++--- .golangci.yml | 4 ++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 442dfae9..8df416fa 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -70,11 +70,16 @@ jobs: GOFLAGS: -mod=readonly with: version: v1.58.2 - skip-cache: false - args: --timeout=10m --max-issues-per-linter=0 --max-same-issues=0 + args: > + --timeout=10m + --verbose + --new-from-rev=HEAD~1 + --max-issues-per-linter=0 + --max-same-issues=0 github-token: ${{ github.token }} only-new-issues: true - timeout: 10m + skip-pkg-cache: false + skip-build-cache: false LicenseCheck: name: License Check diff --git a/.golangci.yml b/.golangci.yml index 4422deee..4d149e89 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -18,9 +18,9 @@ # options for analysis running run: - timeout: 10m + timeout: 8m go: '1.19' - concurrency: 8 + concurrency: 4 allow-parallel-runners: true cache-dir: .cache/golangci-lint modules-download-mode: readonly From 35942745942e34d511c7c0b0fccb3e3740373c3b Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 19:16:22 +0800 Subject: [PATCH 21/42] fix(ci): remove conflicting golangci-lint args - Remove --new-from-rev flag - Use built-in only-new-issues mechanism This change fixes the conflict between manual --new* args and only-new-issues flag. --- .github/workflows/check.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 8df416fa..10737e94 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -73,7 +73,6 @@ jobs: args: > --timeout=10m --verbose - --new-from-rev=HEAD~1 --max-issues-per-linter=0 --max-same-issues=0 github-token: ${{ github.token }} From 4de1346be3859fd98c087cb67e0d3d2267a8e497 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 19:20:35 +0800 Subject: [PATCH 22/42] fix(ci): optimize cache strategy - Separate golangci-lint and go mod caches - Use more precise cache keys - Improve cache fallback strategy This change aims to fix cache extraction conflicts and improve cache efficiency. --- .github/workflows/check.yaml | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 10737e94..3e19e847 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -47,15 +47,21 @@ jobs: go-version: '1.19' cache: true - name: Cache golangci-lint + uses: actions/cache@v3 + with: + path: ~/.cache/golangci-lint + key: ${{ runner.os }}-golangci-lint-cache-${{ hashFiles('.golangci.yml') }} + restore-keys: | + ${{ runner.os }}-golangci-lint-cache- + - name: Go Mod Cache uses: actions/cache@v3 with: path: | - ~/.cache/golangci-lint + ~/go/pkg/mod ~/.cache/go-build - key: ${{ runner.os }}-golangci-lint-${{ hashFiles('.golangci.yml') }}-${{ hashFiles('go.sum') }} + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | - ${{ runner.os }}-golangci-lint-${{ hashFiles('.golangci.yml') }}- - ${{ runner.os }}-golangci-lint- + ${{ runner.os }}-go- - name: Download dependencies run: | go mod download From f9fa1f6c7f5037232e62638573b7f14a4156cb8d Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 19:25:27 +0800 Subject: [PATCH 23/42] fix(ci): simplify cache strategy - Remove custom Go mod cache - Enable setup-go's built-in cache - Keep only golangci-lint specific cache This change resolves cache conflicts by using official caching mechanism. --- .github/workflows/check.yaml | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 3e19e847..5d4e0295 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -19,6 +19,7 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.19' + cache: true - name: Download dependencies run: | go mod download @@ -53,15 +54,6 @@ jobs: key: ${{ runner.os }}-golangci-lint-cache-${{ hashFiles('.golangci.yml') }} restore-keys: | ${{ runner.os }}-golangci-lint-cache- - - name: Go Mod Cache - uses: actions/cache@v3 - with: - path: | - ~/go/pkg/mod - ~/.cache/go-build - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - name: Download dependencies run: | go mod download From 84e047684501b2ff71f6e7c776ec79e6ca386415 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 19:36:28 +0800 Subject: [PATCH 24/42] fix(ci): use built-in caching mechanisms - Remove custom golangci-lint cache - Rely on setup-go and golangci-lint-action built-in caches This change simplifies caching strategy by using only official caching mechanisms. --- .github/workflows/check.yaml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 5d4e0295..0938bc02 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -47,13 +47,6 @@ jobs: with: go-version: '1.19' cache: true - - name: Cache golangci-lint - uses: actions/cache@v3 - with: - path: ~/.cache/golangci-lint - key: ${{ runner.os }}-golangci-lint-cache-${{ hashFiles('.golangci.yml') }} - restore-keys: | - ${{ runner.os }}-golangci-lint-cache- - name: Download dependencies run: | go mod download From 9170f61a9e948e9e664395545d50b795dde7ae16 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 19:48:26 +0800 Subject: [PATCH 25/42] chore: bump golangci-lint version to v1.58 --- .github/workflows/check.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 0938bc02..4aec9266 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -60,7 +60,7 @@ jobs: GO111MODULE: on GOFLAGS: -mod=readonly with: - version: v1.58.2 + version: v1.58 args: > --timeout=10m --verbose From 5f991e77a74cc7ec591f89f0f9dcd2f1a5825133 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 19:57:42 +0800 Subject: [PATCH 26/42] chore(ci): simplify workflow and upgrade golangci-lint - Remove redundant cache and dependency steps - Remove unnecessary environment variables - Upgrade golangci-lint to v1.59.0 This change streamlines the CI workflow by removing redundant steps. --- .github/workflows/check.yaml | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 4aec9266..c8903656 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -19,11 +19,6 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.19' - cache: true - - name: Download dependencies - run: | - go mod download - go mod verify - name: Running go tests with coverage env: GO111MODULE: on @@ -46,21 +41,13 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.19' - cache: true - - name: Download dependencies - run: | - go mod download - go mod verify - name: golangci-lint # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could # not load export data: no export data for \"k8s.io/kube-aggregator\" uses: golangci/golangci-lint-action@v2 - env: - GO111MODULE: on - GOFLAGS: -mod=readonly with: - version: v1.58 + version: v1.59.0 args: > --timeout=10m --verbose From 14b7ffe64376a976408c916d2fab7a0543c5f18a Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 20:48:33 +0800 Subject: [PATCH 27/42] chore: simplify golangci.yml --- .github/workflows/check.yaml | 10 +++------- .golangci.yml | 4 ---- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index c8903656..f71efbea 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -41,10 +41,10 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.19' + # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: + # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could + # not load export data: no export data for \"k8s.io/kube-aggregator\" - name: golangci-lint - # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: - # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could - # not load export data: no export data for \"k8s.io/kube-aggregator\" uses: golangci/golangci-lint-action@v2 with: version: v1.59.0 @@ -53,10 +53,6 @@ jobs: --verbose --max-issues-per-linter=0 --max-same-issues=0 - github-token: ${{ github.token }} - only-new-issues: true - skip-pkg-cache: false - skip-build-cache: false LicenseCheck: name: License Check diff --git a/.golangci.yml b/.golangci.yml index 4d149e89..7cf39481 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,11 +22,7 @@ run: go: '1.19' concurrency: 4 allow-parallel-runners: true - cache-dir: .cache/golangci-lint - modules-download-mode: readonly max-same-issues: 50 - # Disable problematic analyzers - disable-default-analyzers: true linters: disable-all: true From 8a9eccabb0c2e526fabe7bb9da5a87cffa235cee Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 20:55:01 +0800 Subject: [PATCH 28/42] chore: clean cache --- .github/workflows/check.yaml | 7 +++++++ .github/workflows/golangci-lint.yml | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 .github/workflows/golangci-lint.yml diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index f71efbea..b41b84d4 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -41,6 +41,10 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.19' + - name: Clean cache directory + run: | + rm -rf ~/go/pkg/mod/* + rm -rf ~/.cache/go-build/* # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could # not load export data: no export data for \"k8s.io/kube-aggregator\" @@ -53,6 +57,9 @@ jobs: --verbose --max-issues-per-linter=0 --max-same-issues=0 + skip-cache: true + skip-pkg-cache: true + skip-build-cache: true LicenseCheck: name: License Check diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 00000000..ffb0587e --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,26 @@ +# ... 前面的配置保持不变 + + - name: Cache Go modules and build cache + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Clean cache directory + run: | + rm -rf ~/go/pkg/mod/* + rm -rf ~/.cache/go-build/* + + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + version: latest + args: --timeout=5m + # 添加以下配置来避免缓存冲突 + skip-cache: true # 跳过 golangci-lint 的内部缓存机制 + skip-pkg-cache: true # 跳过包缓存 + skip-build-cache: true # 跳过构建缓存 \ No newline at end of file From 2585eceb18c834291ef9a2c8328f9d17dca0f28b Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 21:00:16 +0800 Subject: [PATCH 29/42] fix: permission denied when clean cache --- .github/workflows/check.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index b41b84d4..1a2f7275 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -43,6 +43,8 @@ jobs: go-version: '1.19' - name: Clean cache directory run: | + sudo chown -R $USER:$USER ~/go/pkg/mod/ + sudo chown -R $USER:$USER ~/.cache/go-build/ rm -rf ~/go/pkg/mod/* rm -rf ~/.cache/go-build/* # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: From 91410c61479d481bf9700123d81520c2600e5a9e Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Thu, 21 Nov 2024 21:03:55 +0800 Subject: [PATCH 30/42] fix: wrong cache --- .github/workflows/check.yaml | 13 +++++++------ .github/workflows/golangci-lint.yml | 26 -------------------------- 2 files changed, 7 insertions(+), 32 deletions(-) delete mode 100644 .github/workflows/golangci-lint.yml diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 1a2f7275..09b7b191 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -41,12 +41,13 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.19' - - name: Clean cache directory - run: | - sudo chown -R $USER:$USER ~/go/pkg/mod/ - sudo chown -R $USER:$USER ~/.cache/go-build/ - rm -rf ~/go/pkg/mod/* - rm -rf ~/.cache/go-build/* + - name: Go Cache + uses: actions/cache@v3 + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ github.sha }}-${{ github.run_id }} # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could # not load export data: no export data for \"k8s.io/kube-aggregator\" diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml deleted file mode 100644 index ffb0587e..00000000 --- a/.github/workflows/golangci-lint.yml +++ /dev/null @@ -1,26 +0,0 @@ -# ... 前面的配置保持不变 - - - name: Cache Go modules and build cache - uses: actions/cache@v3 - with: - path: | - ~/.cache/go-build - ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- - - - name: Clean cache directory - run: | - rm -rf ~/go/pkg/mod/* - rm -rf ~/.cache/go-build/* - - - name: golangci-lint - uses: golangci/golangci-lint-action@v3 - with: - version: latest - args: --timeout=5m - # 添加以下配置来避免缓存冲突 - skip-cache: true # 跳过 golangci-lint 的内部缓存机制 - skip-pkg-cache: true # 跳过包缓存 - skip-build-cache: true # 跳过构建缓存 \ No newline at end of file From 9f0e6b161018945cd330c0251ca7460c57c2c76d Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 13:00:06 +0800 Subject: [PATCH 31/42] chore: upgrade to Go 1.22 and update linters - Upgrade Go version from 1.19 to 1.22 - Replace deprecated exportloopref linter with copyloopvar - Upgrade golangci-lint from v1.58.2 to v1.62.0 - Upgrade golangci-lint-action from v2 to v6 - Remove unused k8s.io/kube-aggregator and k8s.io/sample-apiserver dependencies - Add typecheck excludes for removed dependencies --- .github/workflows/check.yaml | 12 +++++----- .github/workflows/release.yaml | 12 +++++----- .golangci.yml | 16 ++++++++++--- docs/contributor/code-contribute.md | 6 ++--- go.mk | 2 +- go.mod | 4 +--- go.sum | 24 +++++++++++++++---- .../internalimport/internal_import.go | 3 +-- 8 files changed, 51 insertions(+), 28 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 09b7b191..990a232c 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -15,10 +15,10 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 - - name: Set up Go 1.19 + - name: Set up Go 1.22 uses: actions/setup-go@v5 with: - go-version: '1.19' + go-version: '1.22' - name: Running go tests with coverage env: GO111MODULE: on @@ -37,10 +37,10 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 - - name: Set up Go 1.19 + - name: Set up Go 1.22 uses: actions/setup-go@v5 with: - go-version: '1.19' + go-version: '1.22' - name: Go Cache uses: actions/cache@v3 with: @@ -52,9 +52,9 @@ jobs: # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could # not load export data: no export data for \"k8s.io/kube-aggregator\" - name: golangci-lint - uses: golangci/golangci-lint-action@v2 + uses: golangci/golangci-lint-action@v6 with: - version: v1.59.0 + version: v1.62.0 args: > --timeout=10m --verbose diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 01f04888..dc20061d 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -19,10 +19,10 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 - - name: Set up Go 1.19 + - name: Set up Go 1.22 uses: actions/setup-go@v2 with: - go-version: 1.19 + go-version: 1.22 - name: Running go tests with coverage env: GO111MODULE: on @@ -36,17 +36,17 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 - - name: Set up Go 1.19 + - name: Set up Go 1.22 uses: actions/setup-go@v5 with: - go-version: 1.19 + go-version: 1.22 # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could # not load export data: no export data for \"k8s.io/kube-aggregator\" - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: - version: v1.58.2 + version: v1.62.0 # # Lints Pull Request commits with commitlint. # # @@ -96,7 +96,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.19 + go-version: 1.22 - name: Set up Node.js uses: actions/setup-node@v2 with: diff --git a/.golangci.yml b/.golangci.yml index 7cf39481..aa1089c3 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -19,7 +19,7 @@ # options for analysis running run: timeout: 8m - go: '1.19' + go: '1.22' concurrency: 4 allow-parallel-runners: true max-same-issues: 50 @@ -33,7 +33,7 @@ linters: - dogsled - errorlint - errname - - exportloopref + - copyloopvar - forbidigo - gocritic - goconst @@ -93,6 +93,10 @@ linters-settings: - (github.com/sirupsen/logrus.FieldLogger).Warnf - (github.com/sirupsen/logrus.FieldLogger).Errorf - (github.com/sirupsen/logrus.FieldLogger).Fatalf + typecheck: + exclude: + - k8s.io/kube-aggregator + - k8s.io/sample-apiserver issues: exclude-rules: @@ -111,10 +115,16 @@ issues: - path: pkg/kubernetes/.* linters: - goanalysis_metalinter - - path: pkg/kubernetes/internalimport/internal_import.go + - path: pkg/kubernetes/internalimport/internal_import\.go$ + linters: + - all + - path: internal_import\.go$ linters: - typecheck - govet + - text: "import .* is a program, not an importable package" + linters: + - typecheck exclude-dirs: - "pkg/kubernetes/generated" - "pkg/kubernetes/openapi" diff --git a/docs/contributor/code-contribute.md b/docs/contributor/code-contribute.md index e37f6255..caad90a0 100644 --- a/docs/contributor/code-contribute.md +++ b/docs/contributor/code-contribute.md @@ -16,18 +16,18 @@ This guide will help you get started with Karpor development. ### Prerequisites -* Golang version 1.19+ +* Golang version 1.22+
Installing Golang -1. Install go1.19 from the [official website](https://go.dev/dl/). Extract the binary files and place them at a location, assuming it is located under the home directory `~/go/`, here is an example command, you should choose the correct binary file for your system. +1. Install go1.22 from the [official website](https://go.dev/dl/). Extract the binary files and place them at a location, assuming it is located under the home directory `~/go/`, here is an example command, you should choose the correct binary file for your system. ``` wget https://go.dev/dl/go1.20.2.linux-amd64.tar.gz tar xzf go1.20.2.linux-amd64.tar.gz ``` -If you would like to maintain multiple versions of golang in your local development environment, you can download the package and extract it to a location, like `~/go/go1.19.1`, and then alter the path in the command below accordingly. +If you would like to maintain multiple versions of golang in your local development environment, you can download the package and extract it to a location, like `~/go/go1.22.1`, and then alter the path in the command below accordingly. 1. Set environment variables for Golang diff --git a/go.mk b/go.mk index 31a3c07c..3debb2d7 100644 --- a/go.mk +++ b/go.mk @@ -15,7 +15,7 @@ COVERAGETMP ?= coverage.tmp GOFORMATER ?= gofumpt GOFORMATER_VERSION ?= v0.2.0 GOLINTER ?= golangci-lint -GOLINTER_VERSION ?= v1.58.2 +GOLINTER_VERSION ?= v1.62.0 # To generate help information diff --git a/go.mod b/go.mod index b062f4cd..0ca1836e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/KusionStack/karpor -go 1.19 +go 1.22 require ( github.com/Masterminds/sprig/v3 v3.2.2 @@ -44,7 +44,6 @@ require ( k8s.io/dynamic-resource-allocation v0.26.1 k8s.io/klog/v2 v2.80.1 k8s.io/kms v0.26.1 - k8s.io/kube-aggregator v0.26.1 k8s.io/kube-controller-manager v0.26.1 k8s.io/kube-openapi v0.0.0-20230106171958-10e5f0effbd2 k8s.io/kube-proxy v0.26.1 @@ -56,7 +55,6 @@ require ( k8s.io/metrics v0.26.1 k8s.io/mount-utils v0.26.1 k8s.io/pod-security-admission v0.26.1 - k8s.io/sample-apiserver v0.26.1 k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 sigs.k8s.io/controller-runtime v0.14.6 sigs.k8s.io/structured-merge-diff/v4 v4.2.3 diff --git a/go.sum b/go.sum index 3815e1b5..792ef9fe 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,7 @@ cloud.google.com/go v0.57.0/go.mod h1:oXiQ6Rzq3RAkkY7N6t3TcE6jE+CIBBbA36lwQ1JyzZ cloud.google.com/go v0.62.0/go.mod h1:jmCYTdRCQuc1PHIIJ/maLInMho30T/Y0M4hTdTShOYc= cloud.google.com/go v0.65.0/go.mod h1:O5N8zS7uWy9vkA9vayVHs65eM1ubvY4h553ofrNHObY= cloud.google.com/go v0.97.0 h1:3DXvAyifywvq64LfkKaMOmkWPS1CikIQdMe2lY9vxU8= +cloud.google.com/go v0.97.0/go.mod h1:GF7l59pYBVlXQIBLx3a761cZ41F9bBH3JUlihCt2Udc= cloud.google.com/go/bigquery v1.0.1/go.mod h1:i/xbL2UlR5RvWAURpBYZTtm/cXjCha9lbfbpx4poX+o= cloud.google.com/go/bigquery v1.3.0/go.mod h1:PjpwJnslEMmckchkHFfq+HTD2DmtT67aNFKH1/VBDHE= cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvftPBK2Dvzc= @@ -57,6 +58,7 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 h1:yL7+Jz0jTC6yykIK/Wh74gnTJnrGr5AyrNMXuA0gves= github.com/antlr/antlr4/runtime/Go/antlr v1.4.10/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= +github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -136,6 +138,7 @@ github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk= github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/form3tech-oss/jwt-go v3.2.3+incompatible h1:7ZaBxOI7TMoYBfyA3cQHErNNyAWIKUMIwqxEtgHOs5c= +github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -162,6 +165,7 @@ github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbV github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-logr/zapr v1.2.3 h1:a9vnzlIBPQBBkeaR9IuMUfmVOrQlkoC4YfPoFkX3T7A= +github.com/go-logr/zapr v1.2.3/go.mod h1:eIauM6P8qSvTw5o2ez6UEAfGjQKrxQTl5EoK+Qa2oG4= github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs= @@ -217,6 +221,7 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.1 h1:gK4Kx5IaGY9CD5sPJ36FHiBJ6ZXl0kilRiiCj+jdYp4= +github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA= github.com/google/cel-go v0.12.6 h1:kjeKudqV0OygrAqA9fX6J55S8gj+Jre2tckIm5RoG4M= github.com/google/cel-go v0.12.6/go.mod h1:Jk7ljRzLBhkmiAwBoUxB1sZSCVBAzkqPF25olK/iRDw= github.com/google/gnostic v0.5.7-v3refs h1:FhTMOKj2VhjpouxvWJAV1TL304uMlb9zcDqkl6cEI54= @@ -256,7 +261,9 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw= +github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= @@ -276,6 +283,7 @@ github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7P github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ= +github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= @@ -299,6 +307,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -338,7 +347,9 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852 h1:Yl0tPBa8QPjGmesFh1D0rDy+q1Twx6FyU7VWHi8wZbI= github.com/oliveagle/jsonpath v0.0.0-20180606110733-2e52cf6e6852/go.mod h1:eqOVx5Vwu4gd2mmMZvVZsgIqNSaW3xxRThUJ0k/TPk4= github.com/onsi/ginkgo/v2 v2.6.0 h1:9t9b9vRUbFq3C4qKFCGkVuq/fIHji802N1nrtkh1mNc= +github.com/onsi/ginkgo/v2 v2.6.0/go.mod h1:63DOGlLAH8+REH8jUGdL3YpCpu7JODesutUjdENfUAc= github.com/onsi/gomega v1.24.1 h1:KORJXNNTzJXzu4ScJWssJfJMnJ+2QJqhoQSRwNlze9E= +github.com/onsi/gomega v1.24.1/go.mod h1:3AOiACssS3/MajrniINInwbfOOtfZvplPzuRSmvt1jM= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/selinux v1.10.0 h1:rAiKF8hTcgLI3w0DHm6i0ylVVcOrlgR1kK99DRLDhyU= @@ -396,6 +407,7 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1 github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js= +github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng= github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= @@ -428,7 +440,9 @@ github.com/swaggo/http-swagger/v2 v2.0.2/go.mod h1:r7/GBkAWIfK6E/OLnE8fXnviHiDeA github.com/swaggo/swag v1.16.2 h1:28Pp+8DkQoV+HLzLx8RGJZXNGKbFqnuvSbAAtoxiY04= github.com/swaggo/swag v1.16.2/go.mod h1:6YzXnDcpr0767iOejs318CwYkCQqyGer6BizOg03f+E= github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802 h1:uruHq4dN7GR16kFc5fp3d1RIYzJW5onx8Ybykw2YQFA= +github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8= +github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xwb1989/sqlparser v0.0.0-20171128062118-da747e0c62c4 h1:w96oitIHwAbUymu2zUSla/82gOKNzpJYkFdwCHE/UOA= github.com/xwb1989/sqlparser v0.0.0-20171128062118-da747e0c62c4/go.mod h1:hzfGeIUDq/j97IG+FhNqkowIyEcD88LrW6fyU3K3WqY= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -437,16 +451,21 @@ github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= +go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4= go.etcd.io/etcd/api/v3 v3.5.5 h1:BX4JIbQ7hl7+jL+g+2j5UAr0o1bctCm6/Ct+ArBGkf0= go.etcd.io/etcd/api/v3 v3.5.5/go.mod h1:KFtNaxGDw4Yx/BA4iPPwevUTAuqcsPxzyX8PHydchN8= go.etcd.io/etcd/client/pkg/v3 v3.5.5 h1:9S0JUVvmrVl7wCF39iTQthdaaNIiAaQbmK75ogO6GU8= go.etcd.io/etcd/client/pkg/v3 v3.5.5/go.mod h1:ggrwbk069qxpKPq8/FKkQ3Xq9y39kbFR4LnKszpRXeQ= go.etcd.io/etcd/client/v2 v2.305.5 h1:DktRP60//JJpnPC0VBymAN/7V71GHMdjDCBt4ZPXDjI= +go.etcd.io/etcd/client/v2 v2.305.5/go.mod h1:zQjKllfqfBVyVStbt4FaosoX2iYd8fV/GRy/PbowgP4= go.etcd.io/etcd/client/v3 v3.5.5 h1:q++2WTJbUgpQu4B6hCuT7VkdwaTP7Qz6Daak3WzbrlI= go.etcd.io/etcd/client/v3 v3.5.5/go.mod h1:aApjR4WGlSumpnJ2kloS75h6aHUmAyaPLjHMxpc7E7c= go.etcd.io/etcd/pkg/v3 v3.5.5 h1:Ablg7T7OkR+AeeeU32kdVhw/AGDsitkKPl7aW73ssjU= +go.etcd.io/etcd/pkg/v3 v3.5.5/go.mod h1:6ksYFxttiUGzC2uxyqiyOEvhAiD0tuIqSZkX3TyPdaE= go.etcd.io/etcd/raft/v3 v3.5.5 h1:Ibz6XyZ60OYyRopu73lLM/P+qco3YtlZMOhnXNS051I= +go.etcd.io/etcd/raft/v3 v3.5.5/go.mod h1:76TA48q03g1y1VpTue92jZLr9lIHKUNcYdZOOGyx8rI= go.etcd.io/etcd/server/v3 v3.5.5 h1:jNjYm/9s+f9A9r6+SC4RvNaz6AqixpOvhrFdT0PvIj0= +go.etcd.io/etcd/server/v3 v3.5.5/go.mod h1:rZ95vDw/jrvsbj9XpTqPrTAB9/kzchVdhRirySPkUBc= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= @@ -476,6 +495,7 @@ go.opentelemetry.io/proto/otlp v0.19.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo= @@ -872,8 +892,6 @@ k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4= k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0= k8s.io/kms v0.26.1 h1:JE0n4J4+8/Z+egvXz2BTJeJ9ecsm4ZSLKF7ttVXXm/4= k8s.io/kms v0.26.1/go.mod h1:ReC1IEGuxgfN+PDCIpR6w8+XMmDE7uJhxcCwMZFdIYc= -k8s.io/kube-aggregator v0.26.1 h1:TqDWwuaUJpyhWGWw4JrXR8ZAAaHa9qrsXxR41aR3igw= -k8s.io/kube-aggregator v0.26.1/go.mod h1:E6dnKoQ6f4eFl8QQXHxTASZKXBX6+XcjROWl7GRltl4= k8s.io/kube-controller-manager v0.26.1 h1:yq6177At/MHD/yKqfTj3cQI74SaA8o+yFCxw+FdEBkU= k8s.io/kube-controller-manager v0.26.1/go.mod h1:V0mR9yO6TLzv1xqFq2FNAQSM37JqpdD+dCPJMghHWPw= k8s.io/kube-openapi v0.0.0-20230106171958-10e5f0effbd2 h1:KYLG+YkfkVf4kdRVshpZZPHpiAyesloMrDE9k1B9Jaw= @@ -896,8 +914,6 @@ k8s.io/mount-utils v0.26.1 h1:deN1IBPyi5UFEAgQYXBEDUejzQUNzRC1ML7BUMWljzA= k8s.io/mount-utils v0.26.1/go.mod h1:au99w4FWU5ZWelLb3Yx6kJc8RZ387IyWVM9tN65Yhxo= k8s.io/pod-security-admission v0.26.1 h1:EDIxsYFeKMzNvN/JB0PgQcuwBP6fIkIG2O8ZWJhzOp4= k8s.io/pod-security-admission v0.26.1/go.mod h1:hCbYTG5UtLlivmukkMPjAWf23PUBUHzEvR60xNVWN4c= -k8s.io/sample-apiserver v0.26.1 h1:TgEpFTIo22keBrc1yV0m6ch01SyUiY/GBOhQIcZtdzc= -k8s.io/sample-apiserver v0.26.1/go.mod h1:NeTzOieGeGsBdDG2+eaOJdoLyaawIHard/wpORDHunA= k8s.io/utils v0.0.0-20221128185143-99ec85e7a448 h1:KTgPnR10d5zhztWptI952TNtt/4u5h3IzDXkdIMuo2Y= k8s.io/utils v0.0.0-20221128185143-99ec85e7a448/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= diff --git a/pkg/kubernetes/internalimport/internal_import.go b/pkg/kubernetes/internalimport/internal_import.go index 595041ef..9d45b8b0 100644 --- a/pkg/kubernetes/internalimport/internal_import.go +++ b/pkg/kubernetes/internalimport/internal_import.go @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//nolint:all package internalimport // To use k8s.io/kubernetes as a library, it is required to import the relevant packages related to @@ -32,7 +33,6 @@ import ( _ "k8s.io/csi-translation-lib" _ "k8s.io/dynamic-resource-allocation" _ "k8s.io/kms" - _ "k8s.io/kube-aggregator" _ "k8s.io/kube-controller-manager" _ "k8s.io/kube-proxy" _ "k8s.io/kube-scheduler" @@ -42,5 +42,4 @@ import ( _ "k8s.io/metrics" _ "k8s.io/mount-utils" _ "k8s.io/pod-security-admission" - _ "k8s.io/sample-apiserver" ) From b69dd5967e24320a4f7e9b73d9a797dd2745f762 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 13:02:31 +0800 Subject: [PATCH 32/42] chore: remove unused config --- .github/workflows/check.yaml | 3 --- .github/workflows/release.yaml | 3 --- .golangci.yml | 4 ---- 3 files changed, 10 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index 990a232c..edd03f1e 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -48,9 +48,6 @@ jobs: ~/.cache/go-build ~/go/pkg/mod key: ${{ runner.os }}-go-${{ github.sha }}-${{ github.run_id }} - # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: - # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could - # not load export data: no export data for \"k8s.io/kube-aggregator\" - name: golangci-lint uses: golangci/golangci-lint-action@v6 with: diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index dc20061d..f8f24e82 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -40,9 +40,6 @@ jobs: uses: actions/setup-go@v5 with: go-version: 1.22 - # NOTE: This golangci-lint action MUST be specified as v2 version, otherwise an error will be reported: - # Running error: can't run linter goanalysis_metalinter\nbuildssa: failed to load package main: could - # not load export data: no export data for \"k8s.io/kube-aggregator\" - name: golangci-lint uses: golangci/golangci-lint-action@v2 with: diff --git a/.golangci.yml b/.golangci.yml index aa1089c3..c893043b 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -93,10 +93,6 @@ linters-settings: - (github.com/sirupsen/logrus.FieldLogger).Warnf - (github.com/sirupsen/logrus.FieldLogger).Errorf - (github.com/sirupsen/logrus.FieldLogger).Fatalf - typecheck: - exclude: - - k8s.io/kube-aggregator - - k8s.io/sample-apiserver issues: exclude-rules: From 34d7e200e631731e59be1633e551cf64fc1310df Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 13:06:59 +0800 Subject: [PATCH 33/42] chore(ci): optimize golangci-lint workflow config - Remove unnecessary cache configurations from golangci-lint-action - Remove redundant Go cache step from workflows - Sync golangci-lint configurations between check and release workflows --- .github/workflows/check.yaml | 10 ---------- .github/workflows/release.yaml | 7 ++++++- pkg/kubernetes/internalimport/internal_import.go | 1 - 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index edd03f1e..c606a3da 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -41,13 +41,6 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.22' - - name: Go Cache - uses: actions/cache@v3 - with: - path: | - ~/.cache/go-build - ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ github.sha }}-${{ github.run_id }} - name: golangci-lint uses: golangci/golangci-lint-action@v6 with: @@ -57,9 +50,6 @@ jobs: --verbose --max-issues-per-linter=0 --max-same-issues=0 - skip-cache: true - skip-pkg-cache: true - skip-build-cache: true LicenseCheck: name: License Check diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index f8f24e82..7be799ec 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -41,9 +41,14 @@ jobs: with: go-version: 1.22 - name: golangci-lint - uses: golangci/golangci-lint-action@v2 + uses: golangci/golangci-lint-action@v6 with: version: v1.62.0 + args: > + --timeout=10m + --verbose + --max-issues-per-linter=0 + --max-same-issues=0 # # Lints Pull Request commits with commitlint. # # diff --git a/pkg/kubernetes/internalimport/internal_import.go b/pkg/kubernetes/internalimport/internal_import.go index 9d45b8b0..62fab0ee 100644 --- a/pkg/kubernetes/internalimport/internal_import.go +++ b/pkg/kubernetes/internalimport/internal_import.go @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -//nolint:all package internalimport // To use k8s.io/kubernetes as a library, it is required to import the relevant packages related to From 0b15055462b81daf85234c2f0b5708d3e03ea517 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 13:11:02 +0800 Subject: [PATCH 34/42] chore(ci): optimize workflow configs and improve documentation - Add descriptive comments for each linter in .golangci.yml - Improve documentation for configuration options - Unify setup-go version and go-version format in workflows - Remove redundant configuration options --- .github/workflows/release.yaml | 10 ++-- .golangci.yml | 100 +++++++++++++++------------------ 2 files changed, 50 insertions(+), 60 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 7be799ec..d0ae205a 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -20,9 +20,9 @@ jobs: with: fetch-depth: 0 - name: Set up Go 1.22 - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: - go-version: 1.22 + go-version: '1.22' - name: Running go tests with coverage env: GO111MODULE: on @@ -39,7 +39,7 @@ jobs: - name: Set up Go 1.22 uses: actions/setup-go@v5 with: - go-version: 1.22 + go-version: '1.22' - name: golangci-lint uses: golangci/golangci-lint-action@v6 with: @@ -96,9 +96,9 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: - go-version: 1.22 + go-version: '1.22' - name: Set up Node.js uses: actions/setup-node@v2 with: diff --git a/.golangci.yml b/.golangci.yml index c893043b..ad6182a7 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -20,66 +20,56 @@ run: timeout: 8m go: '1.22' - concurrency: 4 - allow-parallel-runners: true - max-same-issues: 50 linters: disable-all: true enable: # please keep this alphabetized - - bodyclose - - contextcheck - - depguard - - dogsled - - errorlint - - errname - - copyloopvar - - forbidigo - - gocritic - - goconst - - gofumpt - - govet # Re-enable govet with custom settings - - gosimple - - ineffassign - - loggercheck - - misspell - - nilerr - - nilnil - - nolintlint - - prealloc - - predeclared - - staticcheck - - stylecheck - - tagliatelle - - tparallel - - unconvert - - unparam - - unused - - usestdlibvars - - whitespace + - bodyclose # Checks whether HTTP response body is closed successfully + - contextcheck # Check whether the function uses a non-inherited context + - depguard # Checks if package imports are in a list of acceptable packages + - dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) + - errorlint # Find code that will cause problems with the error wrapping scheme + - errname # Checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error + - copyloopvar # Checks for range loop variables that are used after the loop in goroutines + - forbidigo # Forbids identifiers + - gocritic # Provides diagnostics that check for bugs, performance and style issues + - goconst # Finds repeated strings that could be replaced by a constant + - gofumpt # Checks whether code was gofumpt-ed + - govet # Reports suspicious constructs, such as Printf calls whose arguments do not align with the format string + - gosimple # Specializes in simplifying code + - ineffassign # Detects when assignments to existing variables are not used + - loggercheck # Checks key value pairs for common logger libraries + - misspell # Finds commonly misspelled English words + - nilerr # Finds the code that returns nil even if it checks that the error is not nil + - nilnil # Checks that there is no simultaneous return of nil error and an invalid value + - nolintlint # Reports ill-formed or insufficient nolint directives + - prealloc # Finds slice declarations that could potentially be pre-allocated + - predeclared # Finds code that shadows one of Go's predeclared identifiers + - staticcheck # It's a set of rules from staticcheck + - stylecheck # Stylecheck is a replacement for golint + - tagliatelle # Checks the struct tags case + - tparallel # Detects inappropriate usage of t.Parallel() method in your Go test codes + - unconvert # Remove unnecessary type conversions + - unparam # Reports unused function parameters + - unused # Checks for unused constants, variables, functions and types + - usestdlibvars # Detects the possibility to use variables/constants from the Go standard library + - whitespace # Tool for detection of leading and trailing whitespace linters-settings: gofumpt: - # Choose whether or not to use the extra rules that are disabled - # by default + # Choose whether or not to use the extra rules that are disabled by default extra-rules: false tagliatelle: - # Check the struck tag name case. case: - # Use the struct field name to check the name of the struct tag. - # Default: false use-field-name: true rules: - # Any struct tag type can be used. - # Support string case: `camel`, `pascal`, `kebab`, `snake`, `upperSnake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`, `header`. + # Support string case: `camel`, `pascal`, `kebab`, `snake`, `upperSnake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower` json: goCamel yaml: goCamel xml: goCamel toml: goCamel errorlint: - # Check whether fmt.Errorf uses the %w verb for formatting errors. - # See the https://github.com/polyfloyd/go-errorlint for caveats. - # Default: true + # Check whether fmt.Errorf uses the %w verb for formatting errors errorf: false govet: # Explicitly disable buildssa analyzer @@ -122,19 +112,19 @@ issues: linters: - typecheck exclude-dirs: - - "pkg/kubernetes/generated" - - "pkg/kubernetes/openapi" - - "api/openapispec" - - "hack" - - vendor - - third_party - - test + - "pkg/kubernetes/generated" # Generated code + - "pkg/kubernetes/openapi" # Generated OpenAPI code + - "api/openapispec" # Generated API specs + - "hack" # Scripts and tools + - vendor # Third-party dependencies + - third_party # Third-party code + - test # Test files exclude-files: - internal_import\.go$ - pkg/kubernetes/openapi/zz_generated.openapi.go exclude: - - "G306: Expect WriteFile permissions to be 0600 or less" - - "ST1018: string literal contains Unicode control characters, consider using escape sequences instead" - - "ifElseChain: rewrite if-else to switch statement" - - "S1000: should use for range instead of for { select {} }" - - "SA4004: the surrounding loop is unconditionally terminated" + - "G306: Expect WriteFile permissions to be 0600 or less" # Security warning for file permissions + - "ST1018: string literal contains Unicode control characters" # Style issue for string literals + - "ifElseChain: rewrite if-else to switch statement" # Style suggestion + - "S1000: should use for range instead of for { select {} }" # Style suggestion + - "SA4004: the surrounding loop is unconditionally terminated" # Code structure warning From 43626a5c3799a72d96f05d68d2aa0a34b28b9a55 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 16:44:41 +0800 Subject: [PATCH 35/42] fix: all issues of golangci-lint --- .golangci.yml | 74 +++++- cmd/karpor/app/server.go | 8 +- pkg/core/entity/resource_group_test.go | 2 +- pkg/core/handler/endpoint/endpoint.go | 2 +- pkg/core/health/check.go | 3 +- pkg/core/health/check_status_handler.go | 9 +- pkg/core/health/check_test.go | 2 +- pkg/core/health/storage_check.go | 1 + pkg/core/manager/cluster/manager.go | 2 +- pkg/core/manager/insight/topology.go | 2 +- pkg/core/manager/insight/util.go | 6 +- pkg/core/route/route.go | 1 + pkg/core/route/route_test.go | 1 - pkg/infra/persistence/elasticsearch/client.go | 4 +- .../search/storage/elasticsearch/search.go | 2 +- pkg/kubernetes/apis/cluster/types.go | 4 +- pkg/kubernetes/apis/cluster/v1beta1/types.go | 4 +- pkg/kubernetes/apis/search/v1beta1/types.go | 16 +- .../registry/core/storage_provider.go | 3 +- pkg/syncer/jsonextracter/fieldpath_test.go | 2 +- pkg/syncer/jsonextracter/jsonpath.go | 1 - pkg/syncer/jsonextracter/jsonpath_test.go | 12 +- .../single_cluster_sync_manager_test.go | 3 +- pkg/syncer/source.go | 4 +- pkg/syncer/transform/tmpl_funcs.go | 4 +- pkg/syncer/utils/elasticsearch_getter_test.go | 8 +- pkg/syncer/utils/purger.go | 2 +- pkg/syncer/utils/selector_test.go | 216 +++++++++--------- pkg/util/certgenerator/generator.go | 2 +- .../clusterinstall/cluster_install_test.go | 9 +- pkg/util/sql2es/convert.go | 10 +- pkg/version/scripts/gen/gen.go | 4 + 32 files changed, 246 insertions(+), 177 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index ad6182a7..49a48420 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -26,7 +26,6 @@ linters: enable: # please keep this alphabetized - bodyclose # Checks whether HTTP response body is closed successfully - contextcheck # Check whether the function uses a non-inherited context - - depguard # Checks if package imports are in a list of acceptable packages - dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) - errorlint # Find code that will cause problems with the error wrapping scheme - errname # Checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error @@ -35,7 +34,6 @@ linters: - gocritic # Provides diagnostics that check for bugs, performance and style issues - goconst # Finds repeated strings that could be replaced by a constant - gofumpt # Checks whether code was gofumpt-ed - - govet # Reports suspicious constructs, such as Printf calls whose arguments do not align with the format string - gosimple # Specializes in simplifying code - ineffassign # Detects when assignments to existing variables are not used - loggercheck # Checks key value pairs for common logger libraries @@ -45,20 +43,21 @@ linters: - nolintlint # Reports ill-formed or insufficient nolint directives - prealloc # Finds slice declarations that could potentially be pre-allocated - predeclared # Finds code that shadows one of Go's predeclared identifiers - - staticcheck # It's a set of rules from staticcheck + - staticcheck # Go static analysis - stylecheck # Stylecheck is a replacement for golint - tagliatelle # Checks the struct tags case + - tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17 + - thelper # thelper detects golang test helpers without t.Helper() call - tparallel # Detects inappropriate usage of t.Parallel() method in your Go test codes - unconvert # Remove unnecessary type conversions - unparam # Reports unused function parameters - - unused # Checks for unused constants, variables, functions and types + - unused # Checks Go code for unused constants, variables, functions and types - usestdlibvars # Detects the possibility to use variables/constants from the Go standard library - whitespace # Tool for detection of leading and trailing whitespace linters-settings: gofumpt: - # Choose whether or not to use the extra rules that are disabled by default - extra-rules: false + extra-rules: true tagliatelle: case: use-field-name: true @@ -72,10 +71,37 @@ linters-settings: # Check whether fmt.Errorf uses the %w verb for formatting errors errorf: false govet: - # Explicitly disable buildssa analyzer + enable: + - asmdecl + - assign + - atomic + - bools + - buildtag + - cgocall + - composites + - copylocks + - errorsas + - fieldalignment + - framepointer + - httpresponse + - ifaceassert + - loopclosure + - lostcancel + - nilfunc + - printf + - shift + - stdmethods + - stringintconv + - structtag + - testinggoroutine + - tests + - unmarshal + - unreachable + - unsafeptr + - unusedresult disable: - buildssa - enable-all: true + - fieldalignment settings: printf: funcs: @@ -111,6 +137,38 @@ issues: - text: "import .* is a program, not an importable package" linters: - typecheck + - text: "json\\(goCamel\\): got 'metadata'" # Allow 'metadata' as standard k8s field name + linters: + - tagliatelle + - path: pkg/syncer/source\.go + text: "return both a `nil` error and an invalid value" + linters: + - nilnil + - path: pkg/syncer/syncer\.go + text: "return both a `nil` error and an invalid value" + linters: + - nilnil + - path: pkg/syncer/jsonextracter/jsonpath\.go + text: "return both a `nil` error and an invalid value" + linters: + - nilnil + - path: pkg/syncer/jsonextracter/jsonpath\.go + text: "`\\(\\*JSONPath\\)\\.evalRecursive` - `node` is unused" + linters: + - unparam + - path: pkg/syncer/jsonextracter/fieldpath\.go + text: "return both a `nil` error and an invalid value" + linters: + - nilnil + - path: pkg/core/manager/cluster/manager_test\.go + text: "`newMockCluster` - `name` always receives" + linters: + - unparam + # Allow fmt.Println in version generation script + - path: pkg/version/scripts/gen/gen\.go + text: "use of `fmt.Println`" + linters: + - forbidigo exclude-dirs: - "pkg/kubernetes/generated" # Generated code - "pkg/kubernetes/openapi" # Generated OpenAPI code diff --git a/cmd/karpor/app/server.go b/cmd/karpor/app/server.go index 3f5e662e..17d57d57 100644 --- a/cmd/karpor/app/server.go +++ b/cmd/karpor/app/server.go @@ -47,9 +47,11 @@ import ( netutils "k8s.io/utils/net" ) -const defaultEtcdPathPrefix = "/registry/karpor" -const defaultTokenIssuer = "karpor" -const defaultTokenMaxExpiration = 8760 * time.Hour +const ( + defaultEtcdPathPrefix = "/registry/karpor" + defaultTokenIssuer = "karpor" + defaultTokenMaxExpiration = 8760 * time.Hour +) // Options contains state for master/api server type Options struct { diff --git a/pkg/core/entity/resource_group_test.go b/pkg/core/entity/resource_group_test.go index e6179dde..3a3c6072 100644 --- a/pkg/core/entity/resource_group_test.go +++ b/pkg/core/entity/resource_group_test.go @@ -282,7 +282,7 @@ func TestNewResourceGroupFromQuery(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - r, err := http.NewRequest("GET", tt.query, nil) + r, err := http.NewRequest(http.MethodGet, tt.query, nil) if err != nil { t.Fatal(err) } diff --git a/pkg/core/handler/endpoint/endpoint.go b/pkg/core/handler/endpoint/endpoint.go index ba6d1546..04c0eba3 100644 --- a/pkg/core/handler/endpoint/endpoint.go +++ b/pkg/core/handler/endpoint/endpoint.go @@ -49,7 +49,7 @@ func listEndpoints(log logr.Logger, r chi.Router) []string { var endpoints []string // Walk through the routes to collect endpoints - walkFunc := func(method string, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error { + walkFunc := func(method, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error { endpoint := fmt.Sprintf("%s\t%s", method, route) endpoints = append(endpoints, endpoint) return nil diff --git a/pkg/core/health/check.go b/pkg/core/health/check.go index cfd925e8..e512870d 100644 --- a/pkg/core/health/check.go +++ b/pkg/core/health/check.go @@ -17,9 +17,10 @@ limitations under the License. package health import ( + "net/http" + "github.com/KusionStack/karpor/pkg/infra/search/storage" "github.com/go-chi/chi/v5" - "net/http" ) // Register registers the livez and readyz handlers to the specified diff --git a/pkg/core/health/check_status_handler.go b/pkg/core/health/check_status_handler.go index a51491f8..ee6157e3 100644 --- a/pkg/core/health/check_status_handler.go +++ b/pkg/core/health/check_status_handler.go @@ -20,12 +20,13 @@ package health import ( "context" - "github.com/pkg/errors" - "golang.org/x/sync/errgroup" "net/http" "sort" "strings" "sync" + + "github.com/pkg/errors" + "golang.org/x/sync/errgroup" ) var ( @@ -65,13 +66,13 @@ func NewCheckStatuses(n int) *CheckStatuses { // FailureNotification represents the configuration for failure // notifications. type FailureNotification struct { - Threshold uint32 Chan chan error + Threshold uint32 } type CheckStatuses struct { - sync.RWMutex m map[string]bool + sync.RWMutex } // Get returns the value and existence status for the specified key. diff --git a/pkg/core/health/check_test.go b/pkg/core/health/check_test.go index 014ff991..b65af90f 100644 --- a/pkg/core/health/check_test.go +++ b/pkg/core/health/check_test.go @@ -85,7 +85,7 @@ func TestNewHandler(t *testing.T) { }, } - req, err := http.NewRequest("GET", "/health?"+tt.query, nil) + req, err := http.NewRequest(http.MethodGet, "/health?"+tt.query, nil) if err != nil { t.Fatal(err) } diff --git a/pkg/core/health/storage_check.go b/pkg/core/health/storage_check.go index af2af4ca..59717393 100644 --- a/pkg/core/health/storage_check.go +++ b/pkg/core/health/storage_check.go @@ -18,6 +18,7 @@ package health import ( "context" + "github.com/KusionStack/karpor/pkg/infra/search/storage" ) diff --git a/pkg/core/manager/cluster/manager.go b/pkg/core/manager/cluster/manager.go index aad4e1db..84faea32 100644 --- a/pkg/core/manager/cluster/manager.go +++ b/pkg/core/manager/cluster/manager.go @@ -432,7 +432,7 @@ func (c *ClusterManager) ValidateKubeConfigFor( return "", ErrBothInsecureAndCertificateAuthority } - if !cluster.Insecure && len(cluster.CertificateAuthorityData) <= 0 { + if !cluster.Insecure && len(cluster.CertificateAuthorityData) == 0 { // when insecure is false, CA is required. return "", ErrMissingCertificateAuthority } diff --git a/pkg/core/manager/insight/topology.go b/pkg/core/manager/insight/topology.go index ab94df76..8542598f 100644 --- a/pkg/core/manager/insight/topology.go +++ b/pkg/core/manager/insight/topology.go @@ -131,7 +131,7 @@ func (i *InsightManager) GetResourceRelationship(ctx context.Context, client *mu objGVKOnGraph, err := topology.FindNodeOnGraph(relationshipGraph, gv.Group, gv.Version, obj.GetKind()) // When obj GVK is not found on relationship graph, return an empty graph with no error if err != nil { - return nil, nil //nolint:nilerr + return nil, nil //nolint:nilnil,nilerr } // Recursively find parents diff --git a/pkg/core/manager/insight/util.go b/pkg/core/manager/insight/util.go index a372cf57..72a716ca 100644 --- a/pkg/core/manager/insight/util.go +++ b/pkg/core/manager/insight/util.go @@ -121,7 +121,7 @@ func GetTopResultsFromMap(m map[string]int) map[string]int { return s[i].value > s[j].value }) - index := min(len(s), 5) + index := minInt(len(s), 5) for _, kv := range s[:index] { res[kv.key] = kv.value } @@ -129,8 +129,8 @@ func GetTopResultsFromMap(m map[string]int) map[string]int { return res } -// min returns the smaller of two integers x and y. -func min(x, y int) int { +// minInt returns the smaller of two integers x and y. +func minInt(x, y int) int { if x < y { return x } diff --git a/pkg/core/route/route.go b/pkg/core/route/route.go index be4e336b..d172c984 100644 --- a/pkg/core/route/route.go +++ b/pkg/core/route/route.go @@ -16,6 +16,7 @@ package route import ( "expvar" + docs "github.com/KusionStack/karpor/api/openapispec" authnhandler "github.com/KusionStack/karpor/pkg/core/handler/authn" clusterhandler "github.com/KusionStack/karpor/pkg/core/handler/cluster" diff --git a/pkg/core/route/route_test.go b/pkg/core/route/route_test.go index 12aa6e3b..ef56c29c 100644 --- a/pkg/core/route/route_test.go +++ b/pkg/core/route/route_test.go @@ -104,7 +104,6 @@ func TestNewCoreRoute(t *testing.T) { // Assert status code is not 404 to ensure the route exists. require.NotEqual(t, http.StatusNotFound, rr.Code, "Route should exist: %s", route) } - } }) } diff --git a/pkg/infra/persistence/elasticsearch/client.go b/pkg/infra/persistence/elasticsearch/client.go index 8bc7b878..c630e3fe 100644 --- a/pkg/infra/persistence/elasticsearch/client.go +++ b/pkg/infra/persistence/elasticsearch/client.go @@ -139,7 +139,7 @@ func (cl *Client) UpdateDocument( } // DeleteDocument deletes a document with the specified ID -func (cl *Client) DeleteDocument(ctx context.Context, indexName string, documentID string) error { +func (cl *Client) DeleteDocument(ctx context.Context, indexName, documentID string) error { if _, err := cl.GetDocument(ctx, indexName, documentID); err != nil { return err } @@ -401,7 +401,7 @@ func (cl *Client) multiTermsAgg(ctx context.Context, index string, fields []stri } // termsAgg executes a single-term aggregation query on the specified field. -func (cl *Client) termsAgg(ctx context.Context, index string, field string) (*AggResults, error) { +func (cl *Client) termsAgg(ctx context.Context, index, field string) (*AggResults, error) { // Execute the search request with the single-term aggregation. resp, err := cl.typedClient. Search(). diff --git a/pkg/infra/search/storage/elasticsearch/search.go b/pkg/infra/search/storage/elasticsearch/search.go index d5dd919d..f5c141ac 100644 --- a/pkg/infra/search/storage/elasticsearch/search.go +++ b/pkg/infra/search/storage/elasticsearch/search.go @@ -37,7 +37,7 @@ type Pagination struct { } // Search performs a search operation with the given query string, pattern type, and pagination settings. -func (s *Storage) Search(ctx context.Context, queryStr string, patternType string, pagination *storage.Pagination) (*storage.SearchResult, error) { +func (s *Storage) Search(ctx context.Context, queryStr, patternType string, pagination *storage.Pagination) (*storage.SearchResult, error) { var sr *storage.SearchResult var err error diff --git a/pkg/kubernetes/apis/cluster/types.go b/pkg/kubernetes/apis/cluster/types.go index 0a46cdaf..3eb3b21e 100644 --- a/pkg/kubernetes/apis/cluster/types.go +++ b/pkg/kubernetes/apis/cluster/types.go @@ -35,7 +35,7 @@ const ( // Cluster is an extension type to access a cluster type Cluster struct { metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ObjectMeta `json:"metadata,omitempty"` Spec ClusterSpec `json:"spec"` Status ClusterStatus `json:"status,omitempty"` @@ -46,7 +46,7 @@ type Cluster struct { // ClusterList is a list of Cluster objects. type ClusterList struct { metav1.TypeMeta `json:",inline"` - metav1.ListMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ListMeta `json:"metadata,omitempty"` Items []Cluster `json:"items"` } diff --git a/pkg/kubernetes/apis/cluster/v1beta1/types.go b/pkg/kubernetes/apis/cluster/v1beta1/types.go index 0a85f315..4a80096a 100644 --- a/pkg/kubernetes/apis/cluster/v1beta1/types.go +++ b/pkg/kubernetes/apis/cluster/v1beta1/types.go @@ -35,7 +35,7 @@ const ( // Cluster is an extension type to access a cluster type Cluster struct { metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ObjectMeta `json:"metadata,omitempty"` Spec ClusterSpec `json:"spec"` // +optional @@ -47,7 +47,7 @@ type Cluster struct { // ClusterList is a list of Cluster objects. type ClusterList struct { metav1.TypeMeta `json:",inline"` - metav1.ListMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ListMeta `json:"metadata,omitempty"` Items []Cluster `json:"items"` } diff --git a/pkg/kubernetes/apis/search/v1beta1/types.go b/pkg/kubernetes/apis/search/v1beta1/types.go index 84f8a530..6c722392 100644 --- a/pkg/kubernetes/apis/search/v1beta1/types.go +++ b/pkg/kubernetes/apis/search/v1beta1/types.go @@ -26,7 +26,7 @@ import ( type SyncRegistry struct { metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ObjectMeta `json:"metadata,omitempty"` // +optional Spec SyncRegistrySpec `json:"spec,omitempty"` @@ -57,7 +57,7 @@ type SyncRegistryList struct { metav1.TypeMeta `json:",inline"` // +optional - metav1.ListMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ListMeta `json:"metadata,omitempty"` Items []SyncRegistry `json:"items"` } @@ -68,7 +68,7 @@ type SyncRegistryList struct { type SyncResources struct { metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ObjectMeta `json:"metadata,omitempty"` Spec SyncResourcesSpec `json:"spec,omitempty"` } @@ -79,7 +79,7 @@ type SyncResourcesList struct { metav1.TypeMeta `json:",inline"` // +optional - metav1.ListMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ListMeta `json:"metadata,omitempty"` Items []SyncResources `json:"items"` } @@ -145,7 +145,7 @@ type ResourceSyncRule struct { // informer memory by discarding redundant fields. type TrimRule struct { metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ObjectMeta `json:"metadata,omitempty"` // +optional Spec TrimRuleSpec `json:"spec,omitempty"` @@ -170,7 +170,7 @@ type TrimRuleList struct { metav1.TypeMeta `json:",inline"` // +optional - metav1.ListMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ListMeta `json:"metadata,omitempty"` Items []TrimRule `json:"items"` } @@ -183,7 +183,7 @@ type TrimRuleList struct { // target resource. type TransformRule struct { metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ObjectMeta `json:"metadata,omitempty"` // +optional Spec TransformRuleSpec `json:"spec,omitempty"` @@ -205,7 +205,7 @@ type TransformRuleList struct { metav1.TypeMeta `json:",inline"` // +optional - metav1.ListMeta `json:"metadata,omitempty"` //nolint:tagliatelle + metav1.ListMeta `json:"metadata,omitempty"` Items []TransformRule `json:"items"` } diff --git a/pkg/kubernetes/registry/core/storage_provider.go b/pkg/kubernetes/registry/core/storage_provider.go index 2f9720a0..5b9a4858 100644 --- a/pkg/kubernetes/registry/core/storage_provider.go +++ b/pkg/kubernetes/registry/core/storage_provider.go @@ -37,7 +37,8 @@ type RESTStorageProvider struct { } func NewRESTStorageProvider(serviceAccouuntIssuer serviceaccount.TokenGenerator, - serviceAccountMaxExpiration time.Duration) *RESTStorageProvider { + serviceAccountMaxExpiration time.Duration, +) *RESTStorageProvider { return &RESTStorageProvider{ ServiceAccountIssuer: serviceAccouuntIssuer, ServiceAccountMaxExpiration: serviceAccountMaxExpiration, diff --git a/pkg/syncer/jsonextracter/fieldpath_test.go b/pkg/syncer/jsonextracter/fieldpath_test.go index fa14fadd..7a2415bf 100644 --- a/pkg/syncer/jsonextracter/fieldpath_test.go +++ b/pkg/syncer/jsonextracter/fieldpath_test.go @@ -66,7 +66,7 @@ func TestFieldPath(t *testing.T) { got, err := NestedFieldNoCopy(tt.args.obj, tt.args.allowMissingKeys, tt.args.fields...) jpt := jsonPathTest{tt.name, fieldPathToJSONPath(tt.args.fields), tt.args.obj, tt.want, tt.wantErr} - testJSONPath([]jsonPathTest{jpt}, tt.args.allowMissingKeys, t) + testJSONPath(t, []jsonPathTest{jpt}, tt.args.allowMissingKeys) if (err != nil) != tt.wantErr { t.Errorf("NestedFieldNoCopy() error = %v, wantErr %v", err, tt.wantErr) diff --git a/pkg/syncer/jsonextracter/jsonpath.go b/pkg/syncer/jsonextracter/jsonpath.go index 923a600e..60adc25d 100644 --- a/pkg/syncer/jsonextracter/jsonpath.go +++ b/pkg/syncer/jsonextracter/jsonpath.go @@ -290,7 +290,6 @@ func (j *JSONPath) evalArray(input []reflect.Value, node *ArrayNode, setFn []set result := []reflect.Value{} nextFns := []setFieldFunc{} for k, value := range input { - value, isNil := template.Indirect(value) if isNil { continue diff --git a/pkg/syncer/jsonextracter/jsonpath_test.go b/pkg/syncer/jsonextracter/jsonpath_test.go index e19c044e..5bb27c35 100644 --- a/pkg/syncer/jsonextracter/jsonpath_test.go +++ b/pkg/syncer/jsonextracter/jsonpath_test.go @@ -33,7 +33,8 @@ func (t *jsonPathTest) Prepare(allowMissingKeys bool) (*JSONPath, error) { return jp, jp.Parse(t.template) } -func benchmarkJSONPath(test jsonPathTest, allowMissingKeys bool, b *testing.B) { +func benchmarkJSONPath(b *testing.B, test jsonPathTest, allowMissingKeys bool) { + b.Helper() jp, err := test.Prepare(allowMissingKeys) if err != nil { if !test.expectError { @@ -48,7 +49,8 @@ func benchmarkJSONPath(test jsonPathTest, allowMissingKeys bool, b *testing.B) { } } -func testJSONPath(tests []jsonPathTest, allowMissingKeys bool, t *testing.T) { +func testJSONPath(t *testing.T, tests []jsonPathTest, allowMissingKeys bool) { + t.Helper() for _, test := range tests { jp, err := test.Prepare(allowMissingKeys) if err != nil { @@ -152,7 +154,7 @@ func TestJSONPath(t *testing.T) { {"not exist label", `{.metadata.labels.xx.dd}`, podData, `null`, true}, } - testJSONPath(podTests, false, t) + testJSONPath(t, podTests, false) allowMissingTests := []jsonPathTest{ {"containers image", `{.spec.containers[*]['xname', 'image']}`, podData, `{"spec":{"containers":[{"image":"registry.k8s.io/pause:3.8"},{"image":"registry.k8s.io/pause:3.8"}]}}`, false}, @@ -160,10 +162,10 @@ func TestJSONPath(t *testing.T) { {"not exist label", `{.metadata.labels.xx.dd}`, podData, `{"metadata":{"labels":{}}}`, false}, } - testJSONPath(allowMissingTests, true, t) + testJSONPath(t, allowMissingTests, true) } func BenchmarkJSONPath(b *testing.B) { t := jsonPathTest{"range nodes capacity", `{.kind}`, podData, `{"kind":"Pod"}`, false} - benchmarkJSONPath(t, true, b) + benchmarkJSONPath(b, t, true) } diff --git a/pkg/syncer/single_cluster_sync_manager_test.go b/pkg/syncer/single_cluster_sync_manager_test.go index 4169cef7..6cfbde2c 100644 --- a/pkg/syncer/single_cluster_sync_manager_test.go +++ b/pkg/syncer/single_cluster_sync_manager_test.go @@ -17,12 +17,13 @@ package syncer import ( "context" - "github.com/KusionStack/karpor/pkg/infra/search/storage/elasticsearch" "sync" "sync/atomic" "testing" "time" + "github.com/KusionStack/karpor/pkg/infra/search/storage/elasticsearch" + searchv1beta1 "github.com/KusionStack/karpor/pkg/kubernetes/apis/search/v1beta1" "github.com/bytedance/mockey" "github.com/go-logr/logr" diff --git a/pkg/syncer/source.go b/pkg/syncer/source.go index 70a6cdb3..07600128 100644 --- a/pkg/syncer/source.go +++ b/pkg/syncer/source.go @@ -186,10 +186,10 @@ func (s *informerSource) createInformer(_ context.Context, handler ctrlhandler.E lw := &clientgocache.ListWatch{ ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { - return s.client.Resource(gvr).Namespace(s.Namespace).List(context.TODO(), options) + return s.client.Resource(gvr).Namespace(s.Namespace).List(s.ctx, options) }, WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { - return s.client.Resource(gvr).Namespace(s.Namespace).Watch(context.TODO(), options) + return s.client.Resource(gvr).Namespace(s.Namespace).Watch(s.ctx, options) }, } diff --git a/pkg/syncer/transform/tmpl_funcs.go b/pkg/syncer/transform/tmpl_funcs.go index 5558425a..eaec3fca 100644 --- a/pkg/syncer/transform/tmpl_funcs.go +++ b/pkg/syncer/transform/tmpl_funcs.go @@ -24,7 +24,7 @@ var ( clusterTmplFuncsLock sync.RWMutex ) -func RegisterClusterTmplFunc(cluster, FuncName string, tmplFunc any) error { +func RegisterClusterTmplFunc(cluster, funcName string, tmplFunc any) error { clusterTmplFuncsLock.Lock() defer clusterTmplFuncsLock.Unlock() @@ -34,7 +34,7 @@ func RegisterClusterTmplFunc(cluster, FuncName string, tmplFunc any) error { clusterTmplFuncs[cluster] = make(template.FuncMap) } - clusterTmplFuncs[cluster][FuncName] = tmplFunc + clusterTmplFuncs[cluster][funcName] = tmplFunc return nil } diff --git a/pkg/syncer/utils/elasticsearch_getter_test.go b/pkg/syncer/utils/elasticsearch_getter_test.go index 2298f7f1..711989e4 100644 --- a/pkg/syncer/utils/elasticsearch_getter_test.go +++ b/pkg/syncer/utils/elasticsearch_getter_test.go @@ -68,12 +68,8 @@ func TestESListerGetter_ListKeys(t *testing.T) { esLister := NewESListerGetter("defalut", &elasticsearch.Storage{}, tc.gvr) keys := esLister.ListKeys() - // Check error expectation - if tc.expectError { - require.Len(t, keys, tc.expectedLength, "Expected nil result on error") - } else { - require.Len(t, keys, tc.expectedLength, "Expected nil result on error") - } + // Check the length of returned keys + require.Len(t, keys, tc.expectedLength, "Unexpected number of keys returned") }) } } diff --git a/pkg/syncer/utils/purger.go b/pkg/syncer/utils/purger.go index fff3ce9b..06e02055 100644 --- a/pkg/syncer/utils/purger.go +++ b/pkg/syncer/utils/purger.go @@ -73,7 +73,7 @@ func (e *ESPurger) Purge(ctx context.Context, syncBefore time.Time) error { esquery.Range("syncAt").Lte(syncBefore), ).Map() - sr, err := e.esClient.SearchByQuery(context.Background(), query, nil) + sr, err := e.esClient.SearchByQuery(ctx, query, nil) if err != nil { return err } diff --git a/pkg/syncer/utils/selector_test.go b/pkg/syncer/utils/selector_test.go index 4e766547..8524e9de 100644 --- a/pkg/syncer/utils/selector_test.go +++ b/pkg/syncer/utils/selector_test.go @@ -26,132 +26,132 @@ import ( ) // generate a test selectable object. -func createTestSelectable(labels map[string]interface{}, fields map[string]interface{}) Selectable { - return &selectableUnstructured{ - Unstructured: &unstructured.Unstructured{ - Object: map[string]interface{}{ - "metadata": map[string]interface{}{ - "labels": labels, - }, - "spec": fields, - }, - }, - parser: jsonpath.DefaultJSONPathParser, - } +func createTestSelectable(labels, fields map[string]interface{}) Selectable { + return &selectableUnstructured{ + Unstructured: &unstructured.Unstructured{ + Object: map[string]interface{}{ + "metadata": map[string]interface{}{ + "labels": labels, + }, + "spec": fields, + }, + }, + parser: jsonpath.DefaultJSONPathParser, + } } func TestGetLabels(t *testing.T) { - expectedLabels := labels.Set{"app": "test"} - selectable := createTestSelectable(map[string]interface{}{"app": "test"}, nil) - require.Equal(t, expectedLabels, selectable.GetLabels()) + expectedLabels := labels.Set{"app": "test"} + selectable := createTestSelectable(map[string]interface{}{"app": "test"}, nil) + require.Equal(t, expectedLabels, selectable.GetLabels()) } func TestGetFields(t *testing.T) { - u := &unstructured.Unstructured{ - Object: map[string]interface{}{ - "spec.replicas": "2", - }, - } - expectedFields := jsonpath.NewJSONPathFields(jsonpath.NewJSONPathParser(), u.Object) - label := selectableUnstructured{Unstructured: u, parser: jsonpath.DefaultJSONPathParser} - require.Equal(t, expectedFields, label.GetFields()) + u := &unstructured.Unstructured{ + Object: map[string]interface{}{ + "spec.replicas": "2", + }, + } + expectedFields := jsonpath.NewJSONPathFields(jsonpath.NewJSONPathParser(), u.Object) + label := selectableUnstructured{Unstructured: u, parser: jsonpath.DefaultJSONPathParser} + require.Equal(t, expectedFields, label.GetFields()) } func TestSelector_Matches(t *testing.T) { - labelSelector := labels.SelectorFromSet(labels.Set{"app": "test"}) - fieldSelector := fields.SelectorFromSet(fields.Set{"spec.replicas": "2"}) - selector := Selector{Label: labelSelector, Field: FieldsSelector{Selector: fieldSelector, ServerSupported: true}} - - testCases := []struct { - name string - labels map[string]interface{} - fields map[string]interface{} - expected bool - }{ - {"MatchBoth", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "2"}, true}, - {"MismatchLabel", map[string]interface{}{"app": "wrong"}, map[string]interface{}{"replicas": "2"}, false}, - {"MismatchField", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "3"}, false}, - {"NoFields", map[string]interface{}{"app": "test"}, nil, false}, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - selectable := createTestSelectable(tc.labels, tc.fields) - require.Equal(t, tc.expected, selector.Matches(selectable)) - }) - } + labelSelector := labels.SelectorFromSet(labels.Set{"app": "test"}) + fieldSelector := fields.SelectorFromSet(fields.Set{"spec.replicas": "2"}) + selector := Selector{Label: labelSelector, Field: FieldsSelector{Selector: fieldSelector, ServerSupported: true}} + + testCases := []struct { + name string + labels map[string]interface{} + fields map[string]interface{} + expected bool + }{ + {"MatchBoth", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "2"}, true}, + {"MismatchLabel", map[string]interface{}{"app": "wrong"}, map[string]interface{}{"replicas": "2"}, false}, + {"MismatchField", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "3"}, false}, + {"NoFields", map[string]interface{}{"app": "test"}, nil, false}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + selectable := createTestSelectable(tc.labels, tc.fields) + require.Equal(t, tc.expected, selector.Matches(selectable)) + }) + } } func TestMultiSelectors_Matches(t *testing.T) { - labelSelector := labels.SelectorFromSet(labels.Set{"app": "test"}) - fieldSelector := fields.SelectorFromSet(fields.Set{"spec.replicas": "2"}) - selector := Selector{Label: labelSelector, Field: FieldsSelector{Selector: fieldSelector, ServerSupported: true}} - selectors := MultiSelectors{selector} - - testCases := []struct { - name string - labels map[string]interface{} - fields map[string]interface{} - expected bool - }{ - {"MatchBoth", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "2"}, true}, - {"MismatchLabel", map[string]interface{}{"app": "wrong"}, map[string]interface{}{"replicas": "2"}, false}, - {"MismatchField", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "3"}, false}, - {"NoFields", map[string]interface{}{"app": "test"}, nil, false}, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - selectable := createTestSelectable(tc.labels, tc.fields) - require.Equal(t, tc.expected, selectors.Matches(selectable)) - }) - } + labelSelector := labels.SelectorFromSet(labels.Set{"app": "test"}) + fieldSelector := fields.SelectorFromSet(fields.Set{"spec.replicas": "2"}) + selector := Selector{Label: labelSelector, Field: FieldsSelector{Selector: fieldSelector, ServerSupported: true}} + selectors := MultiSelectors{selector} + + testCases := []struct { + name string + labels map[string]interface{} + fields map[string]interface{} + expected bool + }{ + {"MatchBoth", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "2"}, true}, + {"MismatchLabel", map[string]interface{}{"app": "wrong"}, map[string]interface{}{"replicas": "2"}, false}, + {"MismatchField", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "3"}, false}, + {"NoFields", map[string]interface{}{"app": "test"}, nil, false}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + selectable := createTestSelectable(tc.labels, tc.fields) + require.Equal(t, tc.expected, selectors.Matches(selectable)) + }) + } } func TestApplyToList(t *testing.T) { - labelSelector := labels.SelectorFromSet(labels.Set{"app": "test"}) - fieldSelector := fields.SelectorFromSet(fields.Set{"spec.replicas": "2"}) - selector := Selector{Label: labelSelector, Field: FieldsSelector{Selector: fieldSelector, ServerSupported: true}} - selectors := MultiSelectors{selector} + labelSelector := labels.SelectorFromSet(labels.Set{"app": "test"}) + fieldSelector := fields.SelectorFromSet(fields.Set{"spec.replicas": "2"}) + selector := Selector{Label: labelSelector, Field: FieldsSelector{Selector: fieldSelector, ServerSupported: true}} + selectors := MultiSelectors{selector} - meta := &metav1.ListOptions{} - selectors.ApplyToList(meta) + meta := &metav1.ListOptions{} + selectors.ApplyToList(meta) - require.Equal(t, "app=test", meta.LabelSelector) - require.Equal(t, "spec.replicas=2", meta.FieldSelector) + require.Equal(t, "app=test", meta.LabelSelector) + require.Equal(t, "spec.replicas=2", meta.FieldSelector) } func TestPredicate(t *testing.T) { - labelSelector := labels.SelectorFromSet(labels.Set{"app": "test"}) - fieldSelector := fields.SelectorFromSet(fields.Set{"spec.replicas": "2"}) - s1 := Selector{Label: labelSelector, Field: FieldsSelector{Selector: fieldSelector, ServerSupported: true}} - labelSelector = labels.SelectorFromSet(labels.Set{"app": "test"}) - fieldSelector = fields.SelectorFromSet(fields.Set{"spec.replicas": "4"}) - s2 := Selector{Label: labelSelector, Field: FieldsSelector{Selector: fieldSelector, ServerSupported: true}} - selectors := MultiSelectors{s1, s2} - - testCases := []struct { - name string - labels map[string]interface{} - fields map[string]interface{} - expected bool - }{ - {"MatchBoth", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "2"}, true}, - {"MismatchLabel", map[string]interface{}{"app": "wrong"}, map[string]interface{}{"replicas": "2"}, false}, - {"MismatchField", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "3"}, false}, - {"NoFields", map[string]interface{}{"app": "test"}, nil, false}, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - require.Equal(t, tc.expected, selectors.Predicate(&unstructured.Unstructured{ - Object: map[string]interface{}{ - "metadata": map[string]interface{}{ - "labels": tc.labels, - }, - "spec": tc.fields, - }, - })) - }) - } + labelSelector := labels.SelectorFromSet(labels.Set{"app": "test"}) + fieldSelector := fields.SelectorFromSet(fields.Set{"spec.replicas": "2"}) + s1 := Selector{Label: labelSelector, Field: FieldsSelector{Selector: fieldSelector, ServerSupported: true}} + labelSelector = labels.SelectorFromSet(labels.Set{"app": "test"}) + fieldSelector = fields.SelectorFromSet(fields.Set{"spec.replicas": "4"}) + s2 := Selector{Label: labelSelector, Field: FieldsSelector{Selector: fieldSelector, ServerSupported: true}} + selectors := MultiSelectors{s1, s2} + + testCases := []struct { + name string + labels map[string]interface{} + fields map[string]interface{} + expected bool + }{ + {"MatchBoth", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "2"}, true}, + {"MismatchLabel", map[string]interface{}{"app": "wrong"}, map[string]interface{}{"replicas": "2"}, false}, + {"MismatchField", map[string]interface{}{"app": "test"}, map[string]interface{}{"replicas": "3"}, false}, + {"NoFields", map[string]interface{}{"app": "test"}, nil, false}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + require.Equal(t, tc.expected, selectors.Predicate(&unstructured.Unstructured{ + Object: map[string]interface{}{ + "metadata": map[string]interface{}{ + "labels": tc.labels, + }, + "spec": tc.fields, + }, + })) + }) + } } diff --git a/pkg/util/certgenerator/generator.go b/pkg/util/certgenerator/generator.go index 5cb5c7d8..2a1912ed 100644 --- a/pkg/util/certgenerator/generator.go +++ b/pkg/util/certgenerator/generator.go @@ -54,7 +54,7 @@ type Generator struct { kubeConfigName string } -func NewGenerator(cfg *rest.Config, namespace string, certName string, kubeConfigName string) (*Generator, error) { +func NewGenerator(cfg *rest.Config, namespace, certName, kubeConfigName string) (*Generator, error) { if cfg == nil { return nil, fmt.Errorf("cfg is required buit was nil") } diff --git a/pkg/util/clusterinstall/cluster_install_test.go b/pkg/util/clusterinstall/cluster_install_test.go index 1b203553..21f0dd8a 100644 --- a/pkg/util/clusterinstall/cluster_install_test.go +++ b/pkg/util/clusterinstall/cluster_install_test.go @@ -15,9 +15,10 @@ package clusterinstall import ( - clientcmdapi "k8s.io/client-go/tools/clientcmd/api" "testing" + clientcmdapi "k8s.io/client-go/tools/clientcmd/api" + clusterv1beta1 "github.com/KusionStack/karpor/pkg/kubernetes/apis/cluster/v1beta1" "github.com/stretchr/testify/require" "k8s.io/client-go/rest" @@ -103,8 +104,10 @@ func TestConvertKubeconfigToCluster(t *testing.T) { }, ExecProvider: &clientcmdapi.ExecConfig{ Command: "aws", - Args: []string{"--region", "xxx", "eks", "get-token", "--cluster-name", - "exec-cluster"}, + Args: []string{ + "--region", "xxx", "eks", "get-token", "--cluster-name", + "exec-cluster", + }, Env: nil, APIVersion: "client.authentication.k8s.io/v1beta1", InstallHint: "", diff --git a/pkg/util/sql2es/convert.go b/pkg/util/sql2es/convert.go index 8a56a811..776a3549 100644 --- a/pkg/util/sql2es/convert.go +++ b/pkg/util/sql2es/convert.go @@ -57,13 +57,13 @@ func applyDefaultFilter(sel *sqlparser.Select, filter sqlparser.Expr) *sqlparser return sel } -func Convert(sql string) (dsl string, table string, err error) { +func Convert(sql string) (dsl, table string, err error) { return ConvertWithDefaultFilter(sql, nil) } // ConvertWithDefaultFilter appends the filter to sql where clause if the // filter column names have no intersection with where clause. -func ConvertWithDefaultFilter(sql string, filter sqlparser.Expr) (dsl string, table string, err error) { +func ConvertWithDefaultFilter(sql string, filter sqlparser.Expr) (dsl, table string, err error) { stmt, err := sqlparser.Parse(sql) if err != nil { return "", "", err @@ -84,7 +84,7 @@ func ConvertWithDefaultFilter(sql string, filter sqlparser.Expr) (dsl string, ta return handleSelect(sel) } -func handleSelect(sel *sqlparser.Select) (dsl string, esType string, err error) { +func handleSelect(sel *sqlparser.Select) (dsl, esType string, err error) { var rootParent sqlparser.Expr queryMapStr, err := handleSelectWhere(&sel.Where.Expr, true, &rootParent) @@ -176,7 +176,7 @@ func buildNestedFuncStrValue(nestedFunc *sqlparser.FuncExpr) (string, error) { return "", fmt.Errorf("unsupported function " + nestedFunc.Name.String()) } -func handleSelectWhereAndExpr(expr *sqlparser.Expr, parent *sqlparser.Expr) (string, error) { +func handleSelectWhereAndExpr(expr, parent *sqlparser.Expr) (string, error) { andExpr := (*expr).(*sqlparser.AndExpr) leftExpr := andExpr.Left rightExpr := andExpr.Right @@ -204,7 +204,7 @@ func handleSelectWhereAndExpr(expr *sqlparser.Expr, parent *sqlparser.Expr) (str return fmt.Sprintf(`{"bool" : {"must" : [%v]}}`, resultStr), nil } -func handleSelectWhereOrExpr(expr *sqlparser.Expr, parent *sqlparser.Expr) (string, error) { +func handleSelectWhereOrExpr(expr, parent *sqlparser.Expr) (string, error) { orExpr := (*expr).(*sqlparser.OrExpr) leftExpr := orExpr.Left rightExpr := orExpr.Right diff --git a/pkg/version/scripts/gen/gen.go b/pkg/version/scripts/gen/gen.go index f99ce4f0..581a6da3 100644 --- a/pkg/version/scripts/gen/gen.go +++ b/pkg/version/scripts/gen/gen.go @@ -53,6 +53,10 @@ func calculateVersion() (string, error) { func main() { currentDir, err := os.Getwd() + if err != nil { + fmt.Println("Error getting current directory:", err) + os.Exit(1) + } versionStr, err := calculateVersion() if err != nil { fmt.Println("Error calculating version:", err) From 8b5feb7418c207861547281b70dee9e36822363b Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 16:46:46 +0800 Subject: [PATCH 36/42] chore: turn off extra-rules for gofumpt --- .golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 49a48420..44f56bab 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -57,7 +57,7 @@ linters: linters-settings: gofumpt: - extra-rules: true + extra-rules: false tagliatelle: case: use-field-name: true From 2e270a32df4e2ce0a4393c0740ab02454a7a2c26 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 20:30:11 +0800 Subject: [PATCH 37/42] fix: improve code quality and context handling * fix: remove duplicate error check in elasticsearch client * fix: use context.Background() instead of context.TODO() * fix: add missing imports and remove unused ones * fix: add contextcheck linter exceptions where needed * chore: update .gitignore to exclude VERSION file * chore: expand GOSOURCE_PATHS in Makefile to include cmd/... * style: fix imports ordering in elasticsearch.go --- .golangci.yml | 5 +++++ Makefile | 2 +- cmd/karpor/app/syncer.go | 2 ++ pkg/infra/persistence/elasticsearch/client.go | 3 --- pkg/infra/search/storage/elasticsearch/client.go | 4 ++-- ui/embed.go | 1 - 6 files changed, 10 insertions(+), 7 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 44f56bab..959cea43 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -169,6 +169,11 @@ issues: text: "use of `fmt.Println`" linters: - forbidigo + # Allow fmt.Println in version commands + - path: cmd/(cert-generator|karpor)/.* + text: "use of `fmt.Println`" + linters: + - forbidigo exclude-dirs: - "pkg/kubernetes/generated" # Generated code - "pkg/kubernetes/openapi" # Generated OpenAPI code diff --git a/Makefile b/Makefile index 83bf5f2a..7ee15c5a 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ include go.mk # Override the variables in the go.mk APPROOT=karpor -GOSOURCE_PATHS = ./pkg/... +GOSOURCE_PATHS = ./pkg/... ./cmd/... LICENSE_CHECKER ?= license-eye LICENSE_CHECKER_VERSION ?= main diff --git a/cmd/karpor/app/syncer.go b/cmd/karpor/app/syncer.go index 5a5de5c6..90b07dd4 100644 --- a/cmd/karpor/app/syncer.go +++ b/cmd/karpor/app/syncer.go @@ -72,6 +72,7 @@ func run(ctx context.Context, options *syncerOptions) error { } // TODO: add startup parameters to change the type of storage + //nolint:contextcheck es, err := elasticsearch.NewStorage(esclient.Config{ Addresses: options.ElasticSearchAddresses, }) @@ -80,6 +81,7 @@ func run(ctx context.Context, options *syncerOptions) error { return err } + //nolint:contextcheck if err = syncer.NewSyncReconciler(es).SetupWithManager(mgr); err != nil { log.Error(err, "unable to create resource syncer") return err diff --git a/pkg/infra/persistence/elasticsearch/client.go b/pkg/infra/persistence/elasticsearch/client.go index c630e3fe..ab9a66d8 100644 --- a/pkg/infra/persistence/elasticsearch/client.go +++ b/pkg/infra/persistence/elasticsearch/client.go @@ -44,9 +44,6 @@ func NewClient(config elasticsearch.Config) (*Client, error) { if err != nil { return nil, err } - if err != nil { - return nil, err - } typed, err := elasticsearch.NewTypedClient(config) if err != nil { return nil, err diff --git a/pkg/infra/search/storage/elasticsearch/client.go b/pkg/infra/search/storage/elasticsearch/client.go index a91feb1b..c2e5af5c 100644 --- a/pkg/infra/search/storage/elasticsearch/client.go +++ b/pkg/infra/search/storage/elasticsearch/client.go @@ -54,11 +54,11 @@ func NewStorage(cfg esv8.Config) (*Storage, error) { return nil, err } - if err = cl.CreateIndex(context.TODO(), defaultResourceIndexName, strings.NewReader(defaultResourceMapping)); err != nil { + if err = cl.CreateIndex(context.Background(), defaultResourceIndexName, strings.NewReader(defaultResourceMapping)); err != nil { return nil, err } - if err = cl.CreateIndex(context.TODO(), defaultResourceGroupRuleIndexName, strings.NewReader(defaultResourceGroupRuleMapping)); err != nil { + if err = cl.CreateIndex(context.Background(), defaultResourceGroupRuleIndexName, strings.NewReader(defaultResourceGroupRuleMapping)); err != nil { return nil, err } diff --git a/ui/embed.go b/ui/embed.go index ced8850f..685ab1c9 100644 --- a/ui/embed.go +++ b/ui/embed.go @@ -7,5 +7,4 @@ import ( // Embedded contains embedded UI resources // //go:embed build/* -//nolint:typecheck var Embedded embed.FS From 441abd2560fed66e6cbbe1761901c0a4cf191fc1 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 20:35:36 +0800 Subject: [PATCH 38/42] chore: uncomment commitlint documentation in release workflow * docs: restore documentation for commitlint rules in GitHub Actions * docs: add reference link to conventional commit rules --- .github/workflows/release.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index d0ae205a..154127e9 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -50,10 +50,10 @@ jobs: --max-issues-per-linter=0 --max-same-issues=0 - # # Lints Pull Request commits with commitlint. - # # - # # Rules can be referenced: - # # https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional + # Lints Pull Request commits with commitlint. + # + # Rules can be referenced: + # https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional CommitLint: name: Commit Lint runs-on: ubuntu-latest From dea9bee3c3faa7a31361fac32178bb38156a840f Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 20:45:33 +0800 Subject: [PATCH 39/42] ci: remove unnecessary fetch-depth from workflows * ci: remove fetch-depth: 0 from actions/checkout in check workflow * ci: remove fetch-depth: 0 from actions/checkout in release workflow * ci: simplify GitHub Actions checkout configuration --- .github/workflows/check.yaml | 4 ---- .github/workflows/release.yaml | 4 ---- 2 files changed, 8 deletions(-) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index c606a3da..b688d236 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -13,8 +13,6 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - with: - fetch-depth: 0 - name: Set up Go 1.22 uses: actions/setup-go@v5 with: @@ -35,8 +33,6 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - with: - fetch-depth: 0 - name: Set up Go 1.22 uses: actions/setup-go@v5 with: diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 154127e9..6fae1112 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -17,8 +17,6 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - with: - fetch-depth: 0 - name: Set up Go 1.22 uses: actions/setup-go@v5 with: @@ -34,8 +32,6 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - with: - fetch-depth: 0 - name: Set up Go 1.22 uses: actions/setup-go@v5 with: From e2953d11f85944c44cbaa3d14b42f8660b177aae Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 21:09:55 +0800 Subject: [PATCH 40/42] ci: disable golangci-lint cache * ci: add skip-cache option to golangci-lint action * ci: ensure fresh linting results in CI --- .github/workflows/check.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml index b688d236..e613c4ba 100644 --- a/.github/workflows/check.yaml +++ b/.github/workflows/check.yaml @@ -41,6 +41,7 @@ jobs: uses: golangci/golangci-lint-action@v6 with: version: v1.62.0 + skip-cache: true args: > --timeout=10m --verbose From 87c90f336255e906d773d6ded91c62bcd53e611b Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 21:20:00 +0800 Subject: [PATCH 41/42] chore(lint): disable contextcheck for server.go Exclude contextcheck linter for server.go to avoid false positives --- .golangci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.golangci.yml b/.golangci.yml index 959cea43..92c1b1b5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -174,6 +174,9 @@ issues: text: "use of `fmt.Println`" linters: - forbidigo + - path: cmd/karpor/app/server\.go + linters: + - contextcheck exclude-dirs: - "pkg/kubernetes/generated" # Generated code - "pkg/kubernetes/openapi" # Generated OpenAPI code From 72ab67df570de65720c9d233ba529123c24057d4 Mon Sep 17 00:00:00 2001 From: elliotxx <951376975@qq.com> Date: Fri, 22 Nov 2024 21:31:40 +0800 Subject: [PATCH 42/42] ci(workflow): optimize GitHub Actions configuration - Remove redundant clean parameter from checkout actions - Enable skip-cache for golangci-lint to ensure fresh lint results --- .github/workflows/release.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 6fae1112..5e63d48d 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -40,6 +40,7 @@ jobs: uses: golangci/golangci-lint-action@v6 with: version: v1.62.0 + skip-cache: true args: > --timeout=10m --verbose