Skip to content

Commit de247ec

Browse files
authored
Merge pull request #3600 from camilamacedo86/add-lint
✨ add golangci integration to the default scaffold
2 parents db55a8a + 46ea676 commit de247ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+538
-58
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
run:
2+
deadline: 5m
3+
allow-parallel-runners: true
4+
5+
issues:
6+
# don't skip warning about doc comments
7+
# don't exclude the default set of lint
8+
exclude-use-default: false
9+
# restore some of the defaults
10+
# (fill in the rest as needed)
11+
exclude-rules:
12+
- path: "api/*"
13+
linters:
14+
- lll
15+
- path: "internal/*"
16+
linters:
17+
- dupl
18+
- lll
19+
linters:
20+
disable-all: true
21+
enable:
22+
- dupl
23+
- errcheck
24+
- exportloopref
25+
- goconst
26+
- gocyclo
27+
- gofmt
28+
- goimports
29+
- gosimple
30+
- govet
31+
- ineffassign
32+
- lll
33+
- misspell
34+
- nakedret
35+
- prealloc
36+
- staticcheck
37+
- typecheck
38+
- unconvert
39+
- unparam
40+
- unused

docs/book/src/component-config-tutorial/testdata/project/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ vet: ## Run go vet against code.
6464
test: manifests generate fmt vet envtest ## Run tests.
6565
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
6666

67+
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
68+
golangci-lint:
69+
@[ -f $(GOLANGCI_LINT) ] || { \
70+
set -e ;\
71+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.54.2 ;\
72+
}
73+
74+
.PHONY: lint
75+
lint: golangci-lint ## Run golangci-lint linter & yamllint
76+
$(GOLANGCI_LINT) run
77+
78+
.PHONY: lint-fix
79+
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
80+
$(GOLANGCI_LINT) run --fix
81+
6782
##@ Build
6883

6984
.PHONY: build
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
run:
2+
deadline: 5m
3+
allow-parallel-runners: true
4+
5+
issues:
6+
# don't skip warning about doc comments
7+
# don't exclude the default set of lint
8+
exclude-use-default: false
9+
# restore some of the defaults
10+
# (fill in the rest as needed)
11+
exclude-rules:
12+
- path: "api/*"
13+
linters:
14+
- lll
15+
- path: "internal/*"
16+
linters:
17+
- dupl
18+
- lll
19+
linters:
20+
disable-all: true
21+
enable:
22+
- dupl
23+
- errcheck
24+
- exportloopref
25+
- goconst
26+
- gocyclo
27+
- gofmt
28+
- goimports
29+
- gosimple
30+
- govet
31+
- ineffassign
32+
- lll
33+
- misspell
34+
- nakedret
35+
- prealloc
36+
- staticcheck
37+
- typecheck
38+
- unconvert
39+
- unparam
40+
- unused

docs/book/src/cronjob-tutorial/testdata/project/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ vet: ## Run go vet against code.
6464
test: manifests generate fmt vet envtest ## Run tests.
6565
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
6666

67+
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
68+
golangci-lint:
69+
@[ -f $(GOLANGCI_LINT) ] || { \
70+
set -e ;\
71+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.54.2 ;\
72+
}
73+
74+
.PHONY: lint
75+
lint: golangci-lint ## Run golangci-lint linter & yamllint
76+
$(GOLANGCI_LINT) run
77+
78+
.PHONY: lint-fix
79+
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
80+
$(GOLANGCI_LINT) run --fix
81+
6782
##@ Build
6883

6984
.PHONY: build

docs/book/src/cronjob-tutorial/testdata/project/api/v1/cronjob_webhook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var cronjoblog = logf.Log.WithName("cronjob-resource")
4242
Then, we set up the webhook with the manager.
4343
*/
4444

45+
// SetupWebhookWithManager will setup the manager to manage the webhooks
4546
func (r *CronJob) SetupWebhookWithManager(mgr ctrl.Manager) error {
4647
return ctrl.NewWebhookManagedBy(mgr).
4748
For(r).

docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@ var _ = BeforeSuite(func() {
132132
if err != nil {
133133
return err
134134
}
135-
conn.Close()
136-
return nil
135+
return conn.Close()
137136
}).Should(Succeed())
138137

139138
})

pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller-test.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import (
8181
"time"
8282
"fmt"
8383
84+
//nolint:golint
8485
. "github.com/onsi/ginkgo/v2"
8586
. "github.com/onsi/gomega"
8687
appsv1 "k8s.io/api/apps/v1"
@@ -109,7 +110,10 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
109110
},
110111
}
111112
112-
typeNamespaceName := types.NamespacedName{Name: {{ .Resource.Kind }}Name, Namespace: {{ .Resource.Kind }}Name}
113+
typeNamespaceName := types.NamespacedName{
114+
Name: {{ .Resource.Kind }}Name,
115+
Namespace: {{ .Resource.Kind }}Name,
116+
}
113117
{{ lower .Resource.Kind }} := &{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}
114118
115119
BeforeEach(func() {
@@ -190,13 +194,20 @@ var _ = Describe("{{ .Resource.Kind }} controller", func() {
190194
191195
By("Checking the latest Status Condition added to the {{ .Resource.Kind }} instance")
192196
Eventually(func() error {
193-
if {{ lower .Resource.Kind }}.Status.Conditions != nil && len({{ lower .Resource.Kind }}.Status.Conditions) != 0 {
197+
if {{ lower .Resource.Kind }}.Status.Conditions != nil &&
198+
len({{ lower .Resource.Kind }}.Status.Conditions) != 0 {
194199
latestStatusCondition := {{ lower .Resource.Kind }}.Status.Conditions[len({{ lower .Resource.Kind }}.Status.Conditions)-1]
195-
expectedLatestStatusCondition := metav1.Condition{Type: typeAvailable{{ .Resource.Kind }},
196-
Status: metav1.ConditionTrue, Reason: "Reconciling",
197-
Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", {{ lower .Resource.Kind }}.Name, {{ lower .Resource.Kind }}.Spec.Size)}
200+
expectedLatestStatusCondition := metav1.Condition{
201+
Type: typeAvailable{{ .Resource.Kind }},
202+
Status: metav1.ConditionTrue,
203+
Reason: "Reconciling",
204+
Message: fmt.Sprintf(
205+
"Deployment for custom resource (%s) with %d replicas created successfully",
206+
{{ lower .Resource.Kind }}.Name,
207+
{{ lower .Resource.Kind }}.Spec.Size),
208+
}
198209
if latestStatusCondition != expectedLatestStatusCondition {
199-
return fmt.Errorf("The latest status condition added to the {{ lower .Resource.Kind }} instance is not as expected")
210+
return fmt.Errorf("The latest status condition added to the {{ .Resource.Kind }} instance is not as expected")
200211
}
201212
}
202213
return nil

pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ type {{ .Resource.Kind }}Reconciler struct {
137137
138138
// Reconcile is part of the main kubernetes reconciliation loop which aims to
139139
// move the current state of the cluster closer to the desired state.
140-
141140
// It is essential for the controller's reconciliation loop to be idempotent. By following the Operator
142141
// pattern you will create Controllers which provide a reconcile function
143142
// responsible for synchronizing resources until the desired state is reached on the cluster.

pkg/plugins/golang/v4/scaffolds/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package scaffolds
1919
import (
2020
log "github.com/sirupsen/logrus"
2121
"github.com/spf13/afero"
22-
2322
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2423
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2524
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
@@ -139,5 +138,6 @@ func (s *initScaffolder) Scaffold() error {
139138
&templates.Dockerfile{},
140139
&templates.DockerIgnore{},
141140
&templates.Readme{},
141+
&templates.Golangci{},
142142
)
143143
}

pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import (
9898
// log is for logging in this package.
9999
var {{ lower .Resource.Kind }}log = logf.Log.WithName("{{ lower .Resource.Kind }}-resource")
100100
101+
// SetupWebhookWithManager will setup the manager to manage the webhooks
101102
func (r *{{ .Resource.Kind }}) SetupWebhookWithManager(mgr ctrl.Manager) error {
102103
return ctrl.NewWebhookManagedBy(mgr).
103104
For(r).

0 commit comments

Comments
 (0)