From 8d78c0d356973a7a3acc017f77fed5893fa7d20d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 5 Sep 2023 08:46:58 +0200 Subject: [PATCH 1/8] should configure linter github action job --- .github/workflows/golangci-lint.yaml | 57 +++++++++++++++ .golangci.yaml | 105 +++++++++++++++++++++++++++ 2 files changed, 162 insertions(+) create mode 100644 .github/workflows/golangci-lint.yaml create mode 100644 .golangci.yaml diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml new file mode 100644 index 00000000..e7fd545f --- /dev/null +++ b/.github/workflows/golangci-lint.yaml @@ -0,0 +1,57 @@ +name: golangci-lint +on: + push: + branches: + - master + - main + - fresh + pull_request: + +permissions: + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + # pull-requests: read + +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.20' + cache: false + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + # Require: The version of golangci-lint to use. + # When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. + # When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. + version: v1.53 + + # Optional: working directory, useful for monorepos + # working-directory: somedir + + # Optional: golangci-lint command line arguments. + # + # Note: By default, the `.golangci.yml` file should be at the root of the repository. + # The location of the configuration file can be changed by using `--config=` + # args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0 + args: --timeout=5m + + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true + + # Optional: if set to true, then all caching functionality will be completely disabled, + # takes precedence over all other caching options. + # skip-cache: true + + # Optional: if set to true, then the action won't cache or restore ~/go/pkg. + # skip-pkg-cache: true + + # Optional: if set to true, then the action won't cache or restore ~/.cache/go-build. + # skip-build-cache: true + + # Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'. + # install-mode: "goinstall" diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 00000000..b2d72a9a --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,105 @@ +run: + modules-download-mode: readonly + timeout: 10m + +output: + format: tab + sort-results: true + + +# This file contains only configs which differ from defaults. +# All possible options can be found here https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml +linters-settings: + gocritic: + # Settings passed to gocritic. + # The settings key is the name of a supported gocritic checker. + # The list of supported checkers can be find in https://go-critic.github.io/overview. + settings: + captLocal: + # Whether to restrict checker to params only. + # Default: true + paramsOnly: false + underef: + # Whether to skip (*x).method() calls where x is a pointer receiver. + # Default: true + skipRecvDeref: false + + gomnd: + # List of function patterns to exclude from analysis. + # Values always ignored: `time.Date`, + # `strconv.FormatInt`, `strconv.FormatUint`, `strconv.FormatFloat`, + # `strconv.ParseInt`, `strconv.ParseUint`, `strconv.ParseFloat`. + # Default: [] + ignored-functions: + - os.Chmod + - os.Mkdir + - os.MkdirAll + - os.OpenFile + - os.WriteFile + + govet: + # Enable all analyzers. + # Default: false + enable-all: true + # Disable analyzers by name. + # Run `go tool vet help` to see all analyzers. + # Default: [] + disable: + - fieldalignment # too strict + # Settings per analyzer. + settings: + shadow: + # Whether to be strict about shadowing; can be noisy. + # Default: false + strict: false + + +linters: + disable-all: true + enable: + ## enabled by default + - errcheck # checking for unchecked errors, these unchecked errors can be critical bugs in some cases + - gosimple # specializes in simplifying a code + - govet # reports suspicious constructs, such as Printf calls whose arguments do not align with the format string + - ineffassign # detects when assignments to existing variables are not used + - staticcheck # is a go vet on steroids, applying a ton of static analysis checks + - typecheck # like the front-end of a Go compiler, parses and type-checks Go code + - unused # checks for unused constants, variables, functions and types + ## disabled by default + - asasalint # checks for pass []any as any in variadic func(...any) + - asciicheck # checks that your code does not contain non-ASCII identifiers + - durationcheck # checks for two durations multiplied together + - errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error + - errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13 + - exportloopref # checks for pointers to enclosing loop variables + - gci # controls golang package import order and makes it always deterministic + - gochecknoglobals # checks that no global variables exist + - goconst # finds repeated strings that could be replaced by a constant + - gocritic # provides diagnostics that check for bugs, performance and style issues + - godox # detects TODOs keywords + - goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt + - gomnd # detects magic numbers + - gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod + - nestif # reports deeply nested if statements + - 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 + - revive # fast, configurable, extensible, flexible, and beautiful linter for Go, drop-in replacement of golint + - unconvert # removes unnecessary type conversions + - unparam # reports unused function parameters + - whitespace # detects leading and trailing whitespace + + +issues: + # Maximum count of issues with the same text. + # Set to 0 to disable. + # Default: 3 + max-same-issues: 5 + + exclude-rules: + - source: "(noinspection|TODO)" + linters: [ godot ] + - source: "//noinspection" + linters: [ gocritic ] + - path: "_test\\.go" + linters: + - goconst From d1e8eca738db4d9f44a529aaec3eed1e9d9c37f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Wed, 6 Sep 2023 18:24:10 +0200 Subject: [PATCH 2/8] removes fresh branch from linter job --- .github/workflows/golangci-lint.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml index e7fd545f..5b0ee4e1 100644 --- a/.github/workflows/golangci-lint.yaml +++ b/.github/workflows/golangci-lint.yaml @@ -4,7 +4,6 @@ on: branches: - master - main - - fresh pull_request: permissions: From 2cef27957537927b269def70da1f9c7c97ce3c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 12 Sep 2023 10:33:12 +0200 Subject: [PATCH 3/8] adds a test branch to GHA linter --- .github/workflows/golangci-lint.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml index 5b0ee4e1..1cb213af 100644 --- a/.github/workflows/golangci-lint.yaml +++ b/.github/workflows/golangci-lint.yaml @@ -4,6 +4,7 @@ on: branches: - master - main + - gh_actions_linter_test pull_request: permissions: From c31ad778921f1469890a811fed5993d3e4f486ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 12 Sep 2023 11:28:20 +0200 Subject: [PATCH 4/8] fix linter issues --- api/v1/groupversion_info.go | 6 +++--- cmd/main.go | 19 ++++++++----------- internal/controller/cluster_controller.go | 9 ++++----- internal/controller/suite_test.go | 11 ++++------- pkg/provisioning/dynamic_kubeconfigs_test.go | 3 ++- pkg/reconciler/verify_reconciler_test.go | 3 ++- 6 files changed, 23 insertions(+), 28 deletions(-) diff --git a/api/v1/groupversion_info.go b/api/v1/groupversion_info.go index fc2f3632..03774255 100644 --- a/api/v1/groupversion_info.go +++ b/api/v1/groupversion_info.go @@ -26,11 +26,11 @@ import ( var ( // GroupVersion is group version used to register these objects - GroupVersion = schema.GroupVersion{Group: "clusterinventory.kyma-project.io", Version: "v1"} + GroupVersion = schema.GroupVersion{Group: "clusterinventory.kyma-project.io", Version: "v1"} //nolint:gochecknoglobals // SchemeBuilder is used to add go types to the GroupVersionKind scheme - SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} + SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} //nolint:gochecknoglobals // AddToScheme adds the types in this group-version to the given scheme. - AddToScheme = SchemeBuilder.AddToScheme + AddToScheme = SchemeBuilder.AddToScheme //nolint:gochecknoglobals ) diff --git a/cmd/main.go b/cmd/main.go index 03881545..01e40ab0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -20,25 +20,22 @@ import ( "flag" "os" - // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) - // to ensure that exec-entrypoint and run can make use of them. - _ "k8s.io/client-go/plugin/pkg/client/auth" - + clusterinventoryv1 "github.com/kyma-project/cluster-inventory/api/v1" + "github.com/kyma-project/cluster-inventory/internal/controller" "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" + // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) + // to ensure that exec-entrypoint and run can make use of them. + _ "k8s.io/client-go/plugin/pkg/client/auth" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/healthz" "sigs.k8s.io/controller-runtime/pkg/log/zap" - - clusterinventoryv1 "github.com/kyma-project/cluster-inventory/api/v1" - "github.com/kyma-project/cluster-inventory/internal/controller" - //+kubebuilder:scaffold:imports ) var ( - scheme = runtime.NewScheme() - setupLog = ctrl.Log.WithName("setup") + scheme = runtime.NewScheme() //nolint:gochecknoglobals + setupLog = ctrl.Log.WithName("setup") //nolint:gochecknoglobals ) func init() { @@ -68,7 +65,7 @@ func main() { mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, MetricsBindAddress: metricsAddr, - Port: 9443, + Port: 9443, //nolint:gomnd HealthProbeBindAddress: probeAddr, LeaderElection: enableLeaderElection, LeaderElectionID: "f1c68560.kyma-project.io", diff --git a/internal/controller/cluster_controller.go b/internal/controller/cluster_controller.go index b1704eb8..e4093062 100644 --- a/internal/controller/cluster_controller.go +++ b/internal/controller/cluster_controller.go @@ -19,12 +19,11 @@ package controller import ( "context" + clusterinventoryv1 "github.com/kyma-project/cluster-inventory/api/v1" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" - - clusterinventoryv1 "github.com/kyma-project/cluster-inventory/api/v1" ) // ClusterReconciler reconciles a Cluster object @@ -39,17 +38,17 @@ type ClusterReconciler struct { // Reconcile is part of the main kubernetes reconciliation loop which aims to // move the current state of the cluster closer to the desired state. -// TODO(user): Modify the Reconcile function to compare the state specified by +// nolint:all TODO(user): Modify the Reconcile function to compare the state specified by // the Cluster object against the actual cluster state, and then // perform operations to make the cluster state reflect the state specified by // the user. // // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.15.0/pkg/reconcile -func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { +func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { //nolint:revive _ = log.FromContext(ctx) - // TODO(user): your logic here + // nolint:all TODO(user): your logic here return ctrl.Result{}, nil } diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index b59d7e26..343410cb 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -20,26 +20,23 @@ import ( "path/filepath" "testing" + clusterinventoryv1 "github.com/kyma-project/cluster-inventory/api/v1" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" - - clusterinventoryv1 "github.com/kyma-project/cluster-inventory/api/v1" - //+kubebuilder:scaffold:imports ) // These tests use Ginkgo (BDD-style Go testing framework). Refer to // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. -var cfg *rest.Config -var k8sClient client.Client -var testEnv *envtest.Environment +var cfg *rest.Config //nolint:gochecknoglobals +var k8sClient client.Client //nolint:gochecknoglobals +var testEnv *envtest.Environment //nolint:gochecknoglobals func TestControllers(t *testing.T) { RegisterFailHandler(Fail) diff --git a/pkg/provisioning/dynamic_kubeconfigs_test.go b/pkg/provisioning/dynamic_kubeconfigs_test.go index 9f640e29..95f18b10 100644 --- a/pkg/provisioning/dynamic_kubeconfigs_test.go +++ b/pkg/provisioning/dynamic_kubeconfigs_test.go @@ -1,8 +1,9 @@ package provisioning import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestDynamicKubeconfig(t *testing.T) { diff --git a/pkg/reconciler/verify_reconciler_test.go b/pkg/reconciler/verify_reconciler_test.go index a9f97e10..628f655f 100644 --- a/pkg/reconciler/verify_reconciler_test.go +++ b/pkg/reconciler/verify_reconciler_test.go @@ -1,8 +1,9 @@ package provisioning import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestReconciler(t *testing.T) { From c44f0e6f3c503245f5425bac0868aec017efe3f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 12 Sep 2023 11:33:30 +0200 Subject: [PATCH 5/8] bumps golangci-lint from 1.53 to 1.54.2 --- .github/workflows/golangci-lint.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml index 1cb213af..6bca864e 100644 --- a/.github/workflows/golangci-lint.yaml +++ b/.github/workflows/golangci-lint.yaml @@ -28,7 +28,7 @@ jobs: # Require: The version of golangci-lint to use. # When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. # When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. - version: v1.53 + version: v1.54.2 # Optional: working directory, useful for monorepos # working-directory: somedir From 16d625d0f35ed46ac52753389010e01beafa726d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 12 Sep 2023 11:39:41 +0200 Subject: [PATCH 6/8] fix goimports --- cmd/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cmd/main.go b/cmd/main.go index 01e40ab0..1f3e33c0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -25,6 +25,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" + // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. _ "k8s.io/client-go/plugin/pkg/client/auth" From 0bcd3ac8d083c518b9e6e70c53419ac10632c60f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 12 Sep 2023 11:45:32 +0200 Subject: [PATCH 7/8] dissable goimports linter --- .golangci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yaml b/.golangci.yaml index b2d72a9a..e7bdfa58 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -77,7 +77,7 @@ linters: - goconst # finds repeated strings that could be replaced by a constant - gocritic # provides diagnostics that check for bugs, performance and style issues - godox # detects TODOs keywords - - goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt + # - goimports # in addition to fixing imports, goimports also formats your code in the same style as gofmt. Dissabled as can't work together with `gci` - gomnd # detects magic numbers - gomoddirectives # manages the use of 'replace', 'retract', and 'excludes' directives in go.mod - nestif # reports deeply nested if statements From 84adf306cec31ec324b6657afee20bec9b237a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Drzewiecki?= Date: Tue, 12 Sep 2023 11:57:06 +0200 Subject: [PATCH 8/8] optimizes imports for gci linter --- cmd/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index 1f3e33c0..01e40ab0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -25,7 +25,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" - // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. _ "k8s.io/client-go/plugin/pkg/client/auth"