diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml new file mode 100644 index 00000000..6bca864e --- /dev/null +++ b/.github/workflows/golangci-lint.yaml @@ -0,0 +1,57 @@ +name: golangci-lint +on: + push: + branches: + - master + - main + - gh_actions_linter_test + 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.54.2 + + # 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..e7bdfa58 --- /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. 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 + - 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 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) {