Skip to content

Commit

Permalink
Update golangci-lint, adjust workflows, fix uncaught errors (#76)
Browse files Browse the repository at this point in the history
* Update golangci-lint, adjust workflows, fix uncaught errors

* Add go version to lint config
  • Loading branch information
berkayoz authored Oct 25, 2024
1 parent 05b0352 commit 1e5230f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 41 deletions.
15 changes: 6 additions & 9 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
uses: step-security/harden-runner@v2
with:
egress-policy: audit

- name: Check out code
uses: actions/checkout@v4

Expand All @@ -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
19 changes: 7 additions & 12 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
19 changes: 10 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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://github.com/raw/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)
Expand Down
14 changes: 7 additions & 7 deletions bootstrap/controllers/ck8sconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions controlplane/controllers/ck8scontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion controlplane/controllers/remediation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/ck8s/workload_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 1e5230f

Please sign in to comment.