From 1e5230ffa988aa42248efdf5618fdab894677f42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20Tekin=20=C3=96z?= Date: Fri, 25 Oct 2024 09:07:10 +0300 Subject: [PATCH] Update golangci-lint, adjust workflows, fix uncaught errors (#76) * Update golangci-lint, adjust workflows, fix uncaught errors * Add go version to lint config --- .github/workflows/go.yaml | 15 ++++++--------- .golangci.yml | 19 +++++++------------ Makefile | 19 ++++++++++--------- .../controllers/ck8sconfig_controller.go | 14 +++++++------- .../ck8scontrolplane_controller.go | 4 ++-- controlplane/controllers/remediation.go | 2 +- pkg/ck8s/workload_cluster.go | 2 +- 7 files changed, 34 insertions(+), 41 deletions(-) diff --git a/.github/workflows/go.yaml b/.github/workflows/go.yaml index 2b621e29..a4a84cd9 100644 --- a/.github/workflows/go.yaml +++ b/.github/workflows/go.yaml @@ -19,6 +19,7 @@ jobs: uses: step-security/harden-runner@v2 with: egress-policy: audit + - name: Check out code uses: actions/checkout@v4 @@ -27,14 +28,10 @@ jobs: with: go-version: "1.22" - - name: go fmt - run: make go-fmt - - - name: go vet - run: make go-vet - - - name: go lint - run: make lint + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.61 - - name: unit tests + - name: Unit Tests run: make test-unit diff --git a/.golangci.yml b/.golangci.yml index 73e8372f..2212455e 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,12 +1,12 @@ run: - skip-files: - - ".*zz_generated.*\\.go" - - "contrib/.*" timeout: 5m -issue: - max-same-issues: 0 - max-per-linter: 0 + go: '1.22' issues: + max-same-issues: 0 + max-issues-per-linter: 0 + exclude-files: + - ".*zz_generated.*\\.go" + - "contrib/.*" exclude-rules: - path: "test/e2e/*" linters: @@ -118,6 +118,7 @@ linters-settings: excludes: - G307 # Deferring unsafe method "Close" on type "\*os.File" - G108 # Profiling endpoint is automatically exposed on /debug/pprof + - G115 # Integer overflow conversion int -> int32, Kubernetes replicas field is type int32 importas: # Do not allow unaliased imports of aliased packages. # Default: false @@ -148,15 +149,9 @@ linters-settings: - disableStutteringCheck - name: unused-parameter disabled: true - staticcheck: - go: "1.22" - stylecheck: - go: "1.22" tagliatelle: case: rules: # Any struct tag type can be used. # Support string case: `camel`, `pascal`, `kebab`, `snake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`, `header` json: goCamel - unused: - go: "1.22" diff --git a/Makefile b/Makefile index b1de8246..ebd64d88 100644 --- a/Makefile +++ b/Makefile @@ -79,7 +79,7 @@ ENVSUBST_BIN := envsubst ENVSUBST := $(TOOLS_BIN_DIR)/$(ENVSUBST_BIN) # Bump as necessary/desired to latest that supports our version of go at https://github.com/golangci/golangci-lint/releases -GOLANGCI_LINT_VER := v1.55.2 +GOLANGCI_LINT_VER := v1.61.0 GOLANGCI_LINT_BIN := golangci-lint GOLANGCI_LINT := $(TOOLS_BIN_DIR)/$(GOLANGCI_LINT_BIN)-$(GOLANGCI_LINT_VER) @@ -193,15 +193,15 @@ test-common: all-bootstrap: manager-bootstrap # Run tests -test-bootstrap: envtest generate-bootstrap generate-bootstrap-conversions lint manifests-bootstrap +test-bootstrap: envtest generate-bootstrap generate-bootstrap-conversions manifests-bootstrap KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(TOOLS_BIN_DIR) -p path)" go test $(shell pwd)/bootstrap/... -coverprofile cover.out # Build manager binary -manager-bootstrap: generate-bootstrap lint +manager-bootstrap: generate-bootstrap CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o bin/manager bootstrap/main.go # Run against the configured Kubernetes cluster in ~/.kube/config -run-bootstrap: generate-bootstrap lint manifests-bootstrap +run-bootstrap: generate-bootstrap manifests-bootstrap go run ./bootstrap/main.go # Install CRDs into a cluster @@ -263,7 +263,7 @@ docker-manifest-bootstrap: docker-push-bootstrap all-controlplane: manager-controlplane # Run tests -test-controlplane: envtest generate-controlplane generate-controlplane-conversions lint manifests-controlplane +test-controlplane: envtest generate-controlplane generate-controlplane-conversions manifests-controlplane KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(TOOLS_BIN_DIR) -p path)" go test $(shell pwd)/controlplane/... -coverprofile cover.out .PHONY: docker-build-e2e @@ -284,11 +284,11 @@ test-e2e: $(GINKGO) $(KUSTOMIZE) ## Run the end-to-end tests -e2e.skip-resource-cleanup=$(SKIP_RESOURCE_CLEANUP) -e2e.use-existing-cluster=$(USE_EXISTING_CLUSTER) # Build manager binary -manager-controlplane: generate-controlplane lint +manager-controlplane: generate-controlplane CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o bin/manager controlplane/main.go # Run against the configured Kubernetes cluster in ~/.kube/config -run-controlplane: generate-controlplane lint manifests-controlplane +run-controlplane: generate-controlplane manifests-controlplane go run ./controlplane/main.go # Install CRDs into a cluster @@ -389,8 +389,9 @@ $(ENVTEST): $(TOOLS_BIN_DIR) $(ENVSUBST): ## Build envsubst from tools folder. GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) github.com/drone/envsubst/v2/cmd/envsubst $(ENVSUBST_BIN) $(ENVSUBST_VER) -$(GOLANGCI_LINT): ## Build golangci-lint from tools folder. - GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) github.com/golangci/golangci-lint/cmd/golangci-lint $(GOLANGCI_LINT_BIN) $(GOLANGCI_LINT_VER) +$(GOLANGCI_LINT): ## Download the golangci-lint binary, `go get` or `go install` is not supported https://golangci-lint.run/welcome/install/#install-from-sources + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(TOOLS_BIN_DIR) $(GOLANGCI_LINT_VER) + mv "$(TOOLS_BIN_DIR)/golangci-lint" $(GOLANGCI_LINT) $(GINKGO): # Build ginkgo from tools folder. GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(GINKGO_PKG) $(GINKGO_BIN) $(GINKGO_VER) diff --git a/bootstrap/controllers/ck8sconfig_controller.go b/bootstrap/controllers/ck8sconfig_controller.go index 60489ef8..2e8c6d6a 100644 --- a/bootstrap/controllers/ck8sconfig_controller.go +++ b/bootstrap/controllers/ck8sconfig_controller.go @@ -254,7 +254,7 @@ func (r *CK8sConfigReconciler) joinControlplane(ctx context.Context, scope *Scop files, err := r.resolveFiles(ctx, scope.Config) if err != nil { - conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) + conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, "%s", err.Error()) return err } @@ -320,7 +320,7 @@ func (r *CK8sConfigReconciler) joinWorker(ctx context.Context, scope *Scope) err authToken, err := token.Lookup(ctx, r.Client, client.ObjectKeyFromObject(scope.Cluster)) if err != nil { - conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) + conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, "%s", err.Error()) return err } @@ -352,7 +352,7 @@ func (r *CK8sConfigReconciler) joinWorker(ctx context.Context, scope *Scope) err files, err := r.resolveFiles(ctx, scope.Config) if err != nil { - conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) + conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, "%s", err.Error()) return err } @@ -615,7 +615,7 @@ func (r *CK8sConfigReconciler) handleClusterNotInitialized(ctx context.Context, *metav1.NewControllerRef(scope.Config, bootstrapv1.GroupVersion.WithKind("CK8sConfig")), ) if err != nil { - conditions.MarkFalse(scope.Config, bootstrapv1.CertificatesAvailableCondition, bootstrapv1.CertificatesGenerationFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) + conditions.MarkFalse(scope.Config, bootstrapv1.CertificatesAvailableCondition, bootstrapv1.CertificatesGenerationFailedReason, clusterv1.ConditionSeverityWarning, "%s", err.Error()) return ctrl.Result{}, err } conditions.MarkTrue(scope.Config, bootstrapv1.CertificatesAvailableCondition) @@ -661,13 +661,13 @@ func (r *CK8sConfigReconciler) handleClusterNotInitialized(ctx context.Context, files, err := r.resolveFiles(ctx, scope.Config) if err != nil { - conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) + conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, "%s", err.Error()) return ctrl.Result{}, err } userSuppliedBootstrapConfig, err := r.resolveUserBootstrapConfig(ctx, scope.Config) if err != nil { - conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) + conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, "%s", err.Error()) return ctrl.Result{}, err } @@ -679,7 +679,7 @@ func (r *CK8sConfigReconciler) handleClusterNotInitialized(ctx context.Context, snapInstallData, err := r.getSnapInstallDataFromSpec(scope.Config.Spec) if err != nil { - conditions.MarkFalse(scope.Config, bootstrapv1.SnapInstallDataValidatedCondition, bootstrapv1.SnapInstallValidationFailedReason, clusterv1.ConditionSeverityError, err.Error()) + conditions.MarkFalse(scope.Config, bootstrapv1.SnapInstallDataValidatedCondition, bootstrapv1.SnapInstallValidationFailedReason, clusterv1.ConditionSeverityError, "%s", err.Error()) return ctrl.Result{Requeue: true}, fmt.Errorf("failed to get snap install data from spec: %w", err) } diff --git a/controlplane/controllers/ck8scontrolplane_controller.go b/controlplane/controllers/ck8scontrolplane_controller.go index 3da984b8..87b009c9 100644 --- a/controlplane/controllers/ck8scontrolplane_controller.go +++ b/controlplane/controllers/ck8scontrolplane_controller.go @@ -449,13 +449,13 @@ func (r *CK8sControlPlaneReconciler) reconcile(ctx context.Context, cluster *clu controllerRef := metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("CK8sControlPlane")) if err := certificates.LookupOrGenerate(ctx, r.Client, util.ObjectKey(cluster), *controllerRef); err != nil { logger.Error(err, "unable to lookup or create cluster certificates") - conditions.MarkFalse(kcp, controlplanev1.CertificatesAvailableCondition, controlplanev1.CertificatesGenerationFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) + conditions.MarkFalse(kcp, controlplanev1.CertificatesAvailableCondition, controlplanev1.CertificatesGenerationFailedReason, clusterv1.ConditionSeverityWarning, "%s", err.Error()) return reconcile.Result{}, err } conditions.MarkTrue(kcp, controlplanev1.CertificatesAvailableCondition) if err := token.Reconcile(ctx, r.Client, client.ObjectKeyFromObject(cluster), kcp); err != nil { - conditions.MarkFalse(kcp, controlplanev1.TokenAvailableCondition, controlplanev1.TokenGenerationFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) + conditions.MarkFalse(kcp, controlplanev1.TokenAvailableCondition, controlplanev1.TokenGenerationFailedReason, clusterv1.ConditionSeverityWarning, "%s", err.Error()) return reconcile.Result{}, err } conditions.MarkTrue(kcp, controlplanev1.TokenAvailableCondition) diff --git a/controlplane/controllers/remediation.go b/controlplane/controllers/remediation.go index 8139a16d..ef14cd39 100644 --- a/controlplane/controllers/remediation.go +++ b/controlplane/controllers/remediation.go @@ -183,7 +183,7 @@ func (r *CK8sControlPlaneReconciler) reconcileUnhealthyMachines(ctx context.Cont // Delete the machine if err := r.Client.Delete(ctx, machineToBeRemediated); err != nil { - conditions.MarkFalse(machineToBeRemediated, clusterv1.MachineOwnerRemediatedCondition, clusterv1.RemediationFailedReason, clusterv1.ConditionSeverityError, err.Error()) + conditions.MarkFalse(machineToBeRemediated, clusterv1.MachineOwnerRemediatedCondition, clusterv1.RemediationFailedReason, clusterv1.ConditionSeverityError, "%s", err.Error()) return ctrl.Result{}, fmt.Errorf("failed to delete unhealthy machine %s: %w", machineToBeRemediated.Name, err) } diff --git a/pkg/ck8s/workload_cluster.go b/pkg/ck8s/workload_cluster.go index a52ae00f..3d0d2345 100644 --- a/pkg/ck8s/workload_cluster.go +++ b/pkg/ck8s/workload_cluster.go @@ -576,7 +576,7 @@ func aggregateFromMachinesToKCP(input aggregateFromMachinesToKCPInput) { input.kcpErrors = append(input.kcpErrors, fmt.Sprintf("Following machines are reporting %s errors: %s", input.note, strings.Join(kcpMachinesWithErrors.List(), ", "))) } if len(input.kcpErrors) > 0 { - conditions.MarkFalse(input.controlPlane.KCP, input.condition, input.unhealthyReason, clusterv1.ConditionSeverityError, strings.Join(input.kcpErrors, "; ")) + conditions.MarkFalse(input.controlPlane.KCP, input.condition, input.unhealthyReason, clusterv1.ConditionSeverityError, "%s", strings.Join(input.kcpErrors, "; ")) return }