diff --git a/.github/workflows/codequality.yml b/.github/workflows/codequality.yml index 7787e251..948781b1 100644 --- a/.github/workflows/codequality.yml +++ b/.github/workflows/codequality.yml @@ -3,10 +3,17 @@ on: pull_request: branches: - main + - "release-**" paths-ignore: - "docs/**" - "**.md" - "sec-scanners-config.yaml" + - CODEOWNERS + - LICENSE + - LICENSES + - OWNERS + - OWNERS_ALIASES + - PROJECT push: branches: - main @@ -19,11 +26,24 @@ permissions: contents: read # Optional: allow read access to pull request. Use with `only-new-issues` option. pull-requests: read + # Optional: Allow write access to checks to allow the action to annotate code in the PR. + checks: write jobs: code-linter: name: "Run golangci-lint" - uses: kyma-project/eventing-tools/.github/workflows/lint-go-reusable.yml@main - with: - go-version: "1.22" - lint-config-uri: https://raw.githubusercontent.com/kyma-project/eventing-tools/abb93fa301d4b1aa07b46e5b01ffb0463f06e1bb/config/lint/.golangci.yaml + runs-on: ubuntu-latest + + steps: + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + + - name: Checkout code + uses: actions/checkout@v4 + + - name: golangci-lint + uses: golangci/golangci-lint-action@v4.0.0 + with: + version: v1.57 diff --git a/.gitignore b/.gitignore index 5b725d9f..e3ccdd27 100644 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,4 @@ module-config.yaml .env.dev - -.golangci.* -lint-report.json \ No newline at end of file +lint-report.json diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000..9ab95ee5 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,236 @@ +linters: + enable-all: true + disable: + - contextcheck # too many false positives + - deadcode # deprecated (since v1.49.0) and replaced by 'unused' + - depguard # checks if package imports are whitelisted + - exhaustivestruct # deprecated (since v1.46.0) and replaced by 'exhaustruct' + - exhaustruct # TODO enable and fix, use regex to exclude certain cases + - golint # deprecated (since v1.41.0) and replaced by 'revive' + - ifshort # deprecated (since v1.48.0) + - interfacer # deprecated (since v1.38.0) + - lll + - maligned # deprecated (since v1.38.0) + - nlreturn # too strict and mostly code is not more readable + - nosnakecase # deprecated (since v1.48.1) and replaced by 'revive' + - scopelint # deprecated (since v1.39.0) and replaced by 'exportloopref' + - structcheck # deprecated (since v1.49.0) and replaced by 'unused' + - sqlclosecheck # not needed for this project + - varcheck # deprecated (since v1.49.0) and replaced by 'unused' + - wsl # too strict and mostly code is not more readable + - dupl + - forcetypeassert + - gosec + - inamedparam + - ireturn + - maintidx + - nolintlint + - paralleltest + - prealloc + - testpackage + - tparallel + - unconvert + - varnamelen + - wrapcheck + - testifylint + - bodyclose + - containedctx + - tagliatelle + - errchkjson + - noctx + - copyloopvar + +linters-settings: + gomoddirectives: + replace-allow-list: + - github.com/prometheus/client_golang + stylecheck: + dot-import-whitelist: + - github.com/onsi/ginkgo/v2 + - github.com/onsi/gomega + revive: + enable-all-rules: false + severity: error + rules: + - name: comment-spacings + disabled: true + - name: dot-imports + severity: warning + disabled: true + - name: line-length-limit + severity: warning + disabled: true + arguments: [120] + funlen: + lines: 100 + cyclop: + max-complexity: 20 + nestif: + min-complexity: 6 + gci: + sections: + - standard # Standard packages. + - default # Imports that could not be matched to another section type. + - prefix(github.com/kyma-project/eventing-manager) # Imports with the specified prefix. + - blank # Blank imports. + - dot # Dot imports. + custom-order: true + skip-generated: true + importas: + no-unaliased: false + no-extra-aliases: true + ## Version number must be added as suffix to the package name. As an example check `k8s.io/api/core/v1` + ## kubernetes APIs get the prefix `k`. As an example check `kcorev1` + ## Eventing Manager APIs get the prefix `em`. As an example check `emeventingv1alpha1` + ## Eventing Publisher Proxy APIs get the prefix `epp`. As an example check `eppapi` + ## Cloud Events APIs get the prefix `ce`. As an example check `ce` + ## Eventing Auth Manager APIs get the prefix `eam`. + ## Kyma Metrics Collector APIs get the prefix `kmc`. + ## NATS Manager APIs get the prefix `nm`. + + alias: + - pkg: "log" + alias: log + - pkg: k8s.io/api/core/v1 + alias: kcorev1 + - pkg: k8s.io/apimachinery/pkg/apis/meta/v1 + alias: kmetav1 + - pkg: k8s.io/client-go/dynamic/fake + alias: kdynamicfake + - pkg: github.com/nats-io/nats.go + alias: natsgo + - pkg: go.opencensus.io/trace/propagation + alias: ocpropagation + - pkg: github.com/kyma-project/eventing-publisher-proxy/testing + alias: epptestingutils + - pkg: github.com/kyma-project/eventing-publisher-proxy/pkg/legacy/api + alias: eppapi + - pkg: github.com/kyma-project/eventing-publisher-proxy/pkg/nats + alias: eppnats + - pkg: github.com/kyma-project/eventing-manager/pkg/logger + alias: emlogger + - pkg: github.com/kyma-project/eventing-manager/api/eventing/v1alpha1 + alias: emeventingv1alpha1 + - pkg: github.com/kyma-project/eventing-manager/api/eventing/v1alpha2 + alias: emeventingv2alpha1 + - pkg: github.com/cloudevents/sdk-go/v2 + alias: ce + - pkg: github.com/cloudevents/sdk-go/v2/protocol/http + alias: cehttp + - pkg: github.com/cloudevents/sdk-go/v2/event + alias: ceevent + - pkg: github.com/cloudevents/sdk-go/v2/client + alias: ceclient + - pkg: github.com/kyma-project/kyma/components/central-application-gateway/pkg/apis/applicationconnector/v1alpha1 + alias: kymaappconnv1alpha1 + + ###### + ### The following imports are used by github.com/kyma-project/eventing-auth-manager repository. + ###### + - pkg: github.com/kyma-project/eventing-auth-manager/internal/ias + alias: eamias + - pkg: github.com/kyma-project/eventing-auth-manager/controllers + alias: eamcontrollers + - pkg: github.com/kyma-project/eventing-auth-manager/api/v1alpha1 + alias: eamapiv1alpha1 + - pkg: github.com/kyma-project/eventing-auth-manager/internal/ias/internal/oidc/mocks + alias: eamoidcmocks + - pkg: github.com/kyma-project/lifecycle-manager/api/v1beta1 + alias: klmapiv1beta1 + - pkg: github.com/kyma-project/lifecycle-manager/api/v1beta2 + alias: klmapiv1beta2 + - pkg: k8s.io/apimachinery/pkg/api/errors + alias: kapierrors + - pkg: sigs.k8s.io/controller-runtime/pkg/client + alias: kpkgclient + - pkg: k8s.io/apimachinery/pkg/util/runtime + alias: kutilruntime + - pkg: k8s.io/client-go/kubernetes/scheme + alias: kscheme + - pkg: sigs.k8s.io/controller-runtime + alias: kcontrollerruntime + - pkg: github.com/onsi/gomega/types + alias: onsigomegatypes + - pkg: sigs.k8s.io/controller-runtime/pkg/log + alias: kpkglog + + ###### + ### The following imports are used by github.com/kyma-project/nats-manager repository. + ###### + - pkg: k8s.io/api/apps/v1 + alias: kappsv1 + - pkg: k8s.io/apimachinery/pkg/types + alias: ktypes + - pkg: sigs.k8s.io/controller-runtime/pkg/log/zap + alias: klogzap + - pkg: github.com/kyma-project/nats-manager/pkg/k8s/mocks + alias: nmkmocks + - pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1 + alias: kapiextv1 + - pkg: k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset + alias: kapiextclientset + - pkg: github.com/kyma-project/nats-manager/internal/controller/nats + alias: nmctrl + - pkg: github.com/kyma-project/nats-manager/api/v1alpha1 + alias: nmapiv1alpha1 + - pkg: k8s.io/api/policy/v1 + alias: kapipolicyv1 + - pkg: github.com/kyma-project/nats-manager/pkg/k8s/chart/mocks + alias: nmkchartmocks + - pkg: k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake + alias: kapiextclientsetfake + - pkg: k8s.io/apimachinery/pkg/util/yaml + alias: kutilyaml + - pkg: github.com/kyma-project/nats-manager/pkg/manager/mocks + alias: nmmgrmocks + - pkg: github.com/kyma-project/nats-manager/internal/controller/nats/mocks + alias: nmctrlmocks + - pkg: github.com/kyma-project/nats-manager/pkg/manager + alias: nmmgr + - pkg: github.com/kyma-project/nats-manager/internal/controller/nats/url + alias: nmctrlurl + - pkg: github.com/kyma-project/nats-manager/testutils/matchers/nats + alias: nmtsmatchers + - pkg: k8s.io/api/events/v1 + alias: keventsv1 + - pkg: github.com/kyma-project/nats-manager/pkg/nats + alias: nmnats + - pkg: k8s.io/api/rbac/v1 + alias: krbacv1 + - pkg: k8s.io/api/autoscaling/v1 + alias: kautoscalingv1 + - pkg: github.com/kyma-project/nats-manager/internal/controller/cache + alias: nmctrlcache + - pkg: github.com/kyma-project/nats-manager/pkg/labels + alias: nmlabels + + ireturn: + allow: + - anon + - error + - empty + - stdlib + - Client + - client.Object + - (or|er)$ + wrapcheck: + ignorePackageGlobs: +issues: + exclude-rules: + - path: "_test\\.go" + linters: + - wrapcheck + - gochecknoglobals + - funlen # Table driven unit and integration tests exceed function length by design + - maintidx # Table driven unit and integration tests exceed maintainability index by design + - linters: + - importas + text: has alias "" which is not part of config # Ignore false positives that emerged due to https://github.com/julz/importas/issues/15. + max-issues-per-linter: 0 + max-same-issues: 0 +output: + sort-results: true +run: + timeout: 15m + skip-files: + - zz_generated.deepcopy.go diff --git a/Makefile b/Makefile index 0351d788..c3c413c8 100644 --- a/Makefile +++ b/Makefile @@ -107,21 +107,22 @@ generate-and-test: vendor manifests generate fmt imports vet lint test; test: envtest ## Run only tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out + .PHONY: lint -lint: ## Check lint issues using `golangci-lint` - golangci-lint run --timeout 5m --config=./.golangci.yaml +lint: golangci_lint + $(LOCALBIN)/golangci-lint run + +.PHONY: lint-fix +lint-fix: golangci_lint + $(LOCALBIN)/golangci-lint run --fix .PHONY: lint-compact lint-compact: ## Check lint issues using `golangci-lint` in compact result format - golangci-lint run --timeout 5m --config=./.golangci.yaml --print-issued-lines=false - -.PHONY: lint-fix -lint-fix: ## Check and fix lint issues using `golangci-lint` - golangci-lint run --fix --timeout 5m --config=./.golangci.yaml + $(LOCALBIN)/golangci-lint run --print-issued-lines=false .PHONY: lint-report -lint-report: ## Check lint issues using `golangci-lint` then export them to a file, then print the list of linters used - golangci-lint run --timeout 5m --config=./.golangci.yaml --issues-exit-code 0 --out-format json > ./lint-report.json +lint-report: golangci_lint ## Check lint issues using `golangci-lint` then export them to a file, then print the list of linters used + $(LOCALBIN)/golangci-lint run --issues-exit-code 0 --out-format json > ./lint-report.json .PHONY: lint-report-issue-category lint-report-issue-category: ## Get lint issues categories @@ -237,6 +238,7 @@ ENVTEST ?= $(LOCALBIN)/setup-envtest ## Tool Versions KUSTOMIZE_VERSION ?= v5.3.0 CONTROLLER_TOOLS_VERSION ?= v0.14.0 +GOLANG_CI_LINT_VERSION ?= v1.57 KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" .PHONY: kustomize @@ -259,6 +261,10 @@ envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. $(ENVTEST): $(LOCALBIN) test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest +golangci_lint: + test -s $(LOCALBIN)/golangci-lint && $(LOCALBIN)/golangci-lint version | grep -q $(GOLANG_CI_LINT_VERSION) || \ + GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANG_CI_LINT_VERSION) + go-gen: go generate ./... diff --git a/internal/controller/nats/controller.go b/internal/controller/nats/controller.go index 10bd4adb..b7cd2b22 100644 --- a/internal/controller/nats/controller.go +++ b/internal/controller/nats/controller.go @@ -200,7 +200,7 @@ func (r *Reconciler) initNATSInstance(ctx context.Context, nats *nmapiv1alpha1.N log.Infof("Istio enabled on cluster: %t", istioExists) // Check if NATS account secret exists. - accountSecretName := fmt.Sprintf("%s-secret", nats.Name) + accountSecretName := nats.Name + "-secret" accountSecret, err := r.kubeClient.GetSecret(ctx, accountSecretName, nats.Namespace) if err != nil && !kapierrors.IsNotFound(err) { log.Errorf("Failed to fetch secret: %s", accountSecretName) diff --git a/internal/controller/nats/controller_test.go b/internal/controller/nats/controller_test.go index a2bc08f2..98ab38c1 100644 --- a/internal/controller/nats/controller_test.go +++ b/internal/controller/nats/controller_test.go @@ -216,7 +216,7 @@ func Test_handleNATSCRAllowedCheck(t *testing.T) { } require.True(t, nmapiv1alpha1.ConditionsEquals(wantConditions, gotNATS.Status.Conditions)) - wantK8sEventMsg := fmt.Sprintf("Warning Forbidden %s", CreationNotAllowedMsg) + wantK8sEventMsg := "Warning Forbidden " + CreationNotAllowedMsg wantK8sEvent := []string{ fmt.Sprintf(wantK8sEventMsg, givenAllowedNATS.Name, givenAllowedNATS.Namespace), } diff --git a/pkg/manager/nats.go b/pkg/manager/nats.go index 52b94732..2e491291 100644 --- a/pkg/manager/nats.go +++ b/pkg/manager/nats.go @@ -2,7 +2,7 @@ package manager import ( "context" - "fmt" + "errors" nmapiv1alpha1 "github.com/kyma-project/nats-manager/api/v1alpha1" "github.com/kyma-project/nats-manager/pkg/k8s" @@ -10,7 +10,7 @@ import ( "go.uber.org/zap" ) -var ErrNATSStatefulSetNotFound = fmt.Errorf("NATS StatefulSet not found in manifests") +var ErrNATSStatefulSetNotFound = errors.New("NATS StatefulSet not found in manifests") type NatsConfig struct { ClusterSize int diff --git a/pkg/manager/overrides_test.go b/pkg/manager/overrides_test.go index c753ef2d..b41df326 100644 --- a/pkg/manager/overrides_test.go +++ b/pkg/manager/overrides_test.go @@ -1,7 +1,6 @@ package manager import ( - "fmt" "strings" "testing" @@ -158,7 +157,7 @@ func Test_Overrides_Keys(t *testing.T) { // run test cases for key := range keysToTest { key := key - t.Run(fmt.Sprintf("Testing key: %s", key), func(t *testing.T) { + t.Run("Testing key: "+key, func(t *testing.T) { t.Parallel() // when diff --git a/testutils/integration/integration.go b/testutils/integration/integration.go index 432b30ca..3e6adfb9 100644 --- a/testutils/integration/integration.go +++ b/testutils/integration/integration.go @@ -193,7 +193,7 @@ func (env TestEnvironment) TearDown() error { sleepTime := 1 * time.Second var err error const retries = 20 - for i := 0; i < retries; i++ { + for range retries { if err = env.EnvTestInstance.Stop(); err == nil { break } @@ -509,7 +509,7 @@ func (env TestEnvironment) EnsureNATSSpecFileStorageReflected(t *testing.T, nats } // check if file storage size is correctly defined in NATS config. - if !strings.Contains(natsConfigStr, fmt.Sprintf("max_file_store: %s", nats.Spec.FileStorage.Size.String())) { + if !strings.Contains(natsConfigStr, "max_file_store: "+nats.Spec.FileStorage.Size.String()) { return false } @@ -554,7 +554,7 @@ func (env TestEnvironment) EnsureNATSSpecMemStorageReflected(t *testing.T, nats } // check if mem storage size is correctly defined in NATS config. - return strings.Contains(natsConfigStr, fmt.Sprintf("max_memory_store: %s", nats.Spec.MemStorage.Size.String())) + return strings.Contains(natsConfigStr, "max_memory_store: "+nats.Spec.MemStorage.Size.String()) }, SmallTimeOut, SmallPollingInterval, "failed to ensure NATS CR Spec.jetStream.memStorage") } diff --git a/testutils/utils.go b/testutils/utils.go index 9814fc54..5b5ed658 100644 --- a/testutils/utils.go +++ b/testutils/utils.go @@ -47,7 +47,7 @@ func GetRandString(length int) string { // GetRandK8sName returns a valid name for K8s objects. func GetRandK8sName(length int) string { - return fmt.Sprintf("name-%s", GetRandString(length)) + return "name-" + GetRandString(length) } func NewNamespace(name string) *kcorev1.Namespace {