Skip to content

Commit 27bedd7

Browse files
🌱chore: Add golangci-lint linters (#1134)
* add golangci linters Signed-off-by: dongjiang <[email protected]> * sync golangci linter from controller-runtime Signed-off-by: dongjiang <[email protected]> * Update cmd/controller-gen/main.go Co-authored-by: Stefan Büringer <[email protected]> * fix asasalint linter Signed-off-by: dongjiang <[email protected]> --------- Signed-off-by: dongjiang <[email protected]> Co-authored-by: Stefan Büringer <[email protected]>
1 parent bbc5992 commit 27bedd7

39 files changed

+180
-107
lines changed

Diff for: .golangci.yml

+98-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,113 @@
11
run:
2+
allow-parallel-runners: true
23
modules-download-mode: readonly
34
# Increase the default deadline from 1m as some module operations can take a
45
# while if uncached!
56
timeout: 10m
7+
go: "1.23"
68

79
linters:
10+
# sync from https://github.com/kubernetes-sigs/controller-runtime/blob/main/.golangci.yml
11+
disable-all: true
812
enable:
9-
- errcheck
10-
- gosimple
11-
- govet
12-
- ineffassign
13-
- staticcheck
14-
- unused
15-
- testifylint
16-
- iface
17-
- exptostd
18-
- nilnesserr
13+
- asasalint
14+
- asciicheck
15+
- bidichk
16+
- bodyclose
17+
- copyloopvar
18+
- dogsled
19+
- dupl
20+
- errcheck
21+
- errchkjson
22+
- errorlint
23+
- exhaustive
24+
- ginkgolinter
25+
- goconst
26+
- gocritic
27+
- gocyclo
28+
- gofmt
29+
- goimports
30+
- goprintffuncname
31+
- gosimple
32+
- govet
33+
- importas
34+
- ineffassign
35+
- makezero
36+
- misspell
37+
- nakedret
38+
- nilerr
39+
- nolintlint
40+
- prealloc
41+
- revive
42+
- staticcheck
43+
- stylecheck
44+
- tagliatelle
45+
- typecheck
46+
- unconvert
47+
- unparam
48+
- unused
49+
- whitespace
1950

2051
issues:
2152
exclude-rules:
2253
# Dot imports for gomega and ginkgo are allowed
2354
# within test files.
2455
- path: _test\.go
2556
text: should not use dot imports
57+
# Ignore error type switch case
58+
- path: "pkg/loader/loader.go"
59+
linters:
60+
- errorlint
61+
# Ignore test files
62+
- linters:
63+
- dupl
64+
- ginkgolinter
65+
path: _test\.go
66+
- linters:
67+
- gocritic
68+
path: "pkg/markers/help.go"
69+
- linters:
70+
- exhaustive
71+
path: "pkg/markers/parse.go|pkg/deepcopy/traverse.go|pkg/genall/help/types.go|pkg/crd/schema.go|pkg/crd/flatten.go"
72+
# Ignore consider pre-allocating variables
73+
- linters:
74+
- prealloc
75+
text: Consider pre-allocating
76+
linters-settings:
77+
govet:
78+
enable-all: true
79+
disable:
80+
- fieldalignment
81+
- shadow
82+
importas:
83+
no-unaliased: true
84+
revive:
85+
# By default, revive will enable only the linting rules that are named in the configuration file.
86+
# So, it's needed to explicitly enable all required rules here.
87+
rules:
88+
# The following rules are recommended https://github.com/mgechev/revive#recommended-configuration
89+
- name: blank-imports
90+
- name: context-as-argument
91+
- name: context-keys-type
92+
- name: dot-imports
93+
- name: error-return
94+
- name: error-strings
95+
- name: error-naming
96+
- name: if-return
97+
- name: increment-decrement
98+
- name: var-naming
99+
- name: var-declaration
100+
- name: range
101+
- name: receiver-naming
102+
- name: time-naming
103+
- name: unexported-return
104+
- name: indent-error-flow
105+
- name: errorf
106+
- name: superfluous-else
107+
- name: unreachable-code
108+
- name: redefines-builtin-id
109+
#
110+
# Rules in addition to the recommended configuration above.
111+
#
112+
- name: bool-literal-in-expr
113+
- name: constant-logical-expr

Diff for: cmd/controller-gen/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ package main
1717

1818
import (
1919
"encoding/json"
20+
"errors"
2021
"fmt"
2122
"io"
2223
"os"
2324
"strings"
2425

2526
"github.com/spf13/cobra"
26-
2727
"sigs.k8s.io/controller-tools/pkg/crd"
2828
"sigs.k8s.io/controller-tools/pkg/deepcopy"
2929
"sigs.k8s.io/controller-tools/pkg/genall"
@@ -198,7 +198,8 @@ func main() {
198198
})
199199

200200
if err := cmd.Execute(); err != nil {
201-
if _, noUsage := err.(noUsageError); !noUsage {
201+
var errNoUsage noUsageError
202+
if !errors.As(err, &errNoUsage) {
202203
// print the usage unless we suppressed it
203204
if err := cmd.Usage(); err != nil {
204205
panic(err)

Diff for: pkg/crd/desc_visitor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func truncateString(desc string, maxLen int) string {
7373

7474
// Trying to chop off at closest word boundary (i.e. whitespace).
7575
if n := strings.LastIndexFunc(desc, isWhiteSpace); n > 0 {
76-
return desc[0 : n] + "..."
76+
return desc[0:n] + "..."
7777
}
7878

7979
return desc[0:maxLen] + "..."

Diff for: pkg/crd/desc_visitor_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ package crd_test
1919
import (
2020
. "github.com/onsi/ginkgo"
2121
. "github.com/onsi/gomega"
22-
2322
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
24-
2523
"sigs.k8s.io/controller-tools/pkg/crd"
2624
)
2725

Diff for: pkg/crd/flatten.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"sync"
2525

2626
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
27-
2827
"sigs.k8s.io/controller-tools/pkg/loader"
2928
)
3029

@@ -295,8 +294,8 @@ func RefParts(ref string) (typ string, pkgName string, err error) {
295294
}
296295
ref = ref[len(defPrefix):]
297296
// decode the json pointer encodings
298-
ref = strings.Replace(ref, "~1", "/", -1)
299-
ref = strings.Replace(ref, "~0", "~", -1)
297+
ref = strings.ReplaceAll(ref, "~1", "/")
298+
ref = strings.ReplaceAll(ref, "~0", "~")
300299
nameParts := strings.SplitN(ref, "~", 2)
301300

302301
if len(nameParts) == 1 {

Diff for: pkg/crd/flatten_all_of_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
. "github.com/onsi/gomega"
2222
. "github.com/onsi/gomega/gstruct"
2323
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
24-
2524
"sigs.k8s.io/controller-tools/pkg/crd"
2625
)
2726

Diff for: pkg/crd/flatten_type_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import (
2121

2222
. "github.com/onsi/ginkgo"
2323
. "github.com/onsi/gomega"
24-
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
25-
2624
"golang.org/x/tools/go/packages"
25+
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2726
"sigs.k8s.io/controller-tools/pkg/crd"
2827
"sigs.k8s.io/controller-tools/pkg/loader"
2928
)

Diff for: pkg/crd/gen.go

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525

2626
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2727
"k8s.io/apimachinery/pkg/runtime/schema"
28-
2928
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
3029
"sigs.k8s.io/controller-tools/pkg/genall"
3130
"sigs.k8s.io/controller-tools/pkg/loader"

Diff for: pkg/crd/gen_integration_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/google/go-cmp/cmp"
2626
. "github.com/onsi/ginkgo"
2727
. "github.com/onsi/gomega"
28-
2928
"sigs.k8s.io/controller-tools/pkg/crd"
3029
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
3130
"sigs.k8s.io/controller-tools/pkg/genall"
@@ -159,10 +158,10 @@ var _ = Describe("CRD Generation proper defaulting", func() {
159158

160159
It("should truncate CRD descriptions", func() {
161160
By("calling Generate")
162-
var fifty int = 50
161+
fifty := 50
163162
gen := &crd.Generator{
164163
CRDVersions: []string{"v1"},
165-
MaxDescLen: &fifty,
164+
MaxDescLen: &fifty,
166165
}
167166
Expect(gen.Generate(ctx)).NotTo(HaveOccurred())
168167

Diff for: pkg/crd/known_types.go

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package crd
1818
import (
1919
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2020
"k8s.io/utils/ptr"
21-
2221
"sigs.k8s.io/controller-tools/pkg/loader"
2322
)
2423

Diff for: pkg/crd/markers/crd.go

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"strings"
2222

2323
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
24-
2524
"sigs.k8s.io/controller-tools/pkg/markers"
2625
)
2726

Diff for: pkg/crd/markers/topology.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ func init() {
6666
// are typically manipulated together by the same actor.
6767
type ListType string
6868

69+
const (
70+
Map ListType = "map"
71+
Set ListType = "set"
72+
Atomic ListType = "atomic"
73+
Array ListType = "array"
74+
Object ListType = "object"
75+
Integer ListType = "integer"
76+
Number ListType = "number"
77+
)
78+
6979
// +controllertools:marker:generateHelp:category="CRD processing"
7080

7181
// ListMapKey specifies the keys to map listTypes.
@@ -108,10 +118,10 @@ type MapType string
108118
type StructType string
109119

110120
func (l ListType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
111-
if schema.Type != "array" {
121+
if schema.Type != string(Array) {
112122
return fmt.Errorf("must apply listType to an array, found %s", schema.Type)
113123
}
114-
if l != "map" && l != "atomic" && l != "set" {
124+
if l != Map && l != Atomic && l != Set {
115125
return fmt.Errorf(`ListType must be either "map", "set" or "atomic"`)
116126
}
117127
p := string(l)
@@ -124,18 +134,18 @@ func (l ListType) ApplyPriority() ApplyPriority {
124134
}
125135

126136
func (l ListMapKey) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
127-
if schema.Type != "array" {
137+
if schema.Type != string(Array) {
128138
return fmt.Errorf("must apply listMapKey to an array, found %s", schema.Type)
129139
}
130-
if schema.XListType == nil || *schema.XListType != "map" {
140+
if schema.XListType == nil || *schema.XListType != string(Map) {
131141
return fmt.Errorf("must apply listMapKey to an associative-list")
132142
}
133143
schema.XListMapKeys = append(schema.XListMapKeys, string(l))
134144
return nil
135145
}
136146

137147
func (m MapType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
138-
if schema.Type != "object" {
148+
if schema.Type != string(Object) {
139149
return fmt.Errorf("must apply mapType to an object")
140150
}
141151

@@ -150,7 +160,7 @@ func (m MapType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
150160
}
151161

152162
func (s StructType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
153-
if schema.Type != "object" && schema.Type != "" {
163+
if schema.Type != string(Object) && schema.Type != "" {
154164
return fmt.Errorf("must apply structType to an object; either explicitly set or defaulted through an empty schema type")
155165
}
156166

Diff for: pkg/crd/markers/validation.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"strings"
2424

2525
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
26-
2726
"sigs.k8s.io/controller-tools/pkg/markers"
2827
)
2928

@@ -313,7 +312,7 @@ type XIntOrString struct{}
313312
type Schemaless struct{}
314313

315314
func hasNumericType(schema *apiext.JSONSchemaProps) bool {
316-
return schema.Type == "integer" || schema.Type == "number"
315+
return schema.Type == string(Integer) || schema.Type == string(Number)
317316
}
318317

319318
func hasTextualType(schema *apiext.JSONSchemaProps) bool {
@@ -343,7 +342,7 @@ func (m Maximum) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
343342
return fmt.Errorf("must apply maximum to a numeric value, found %s", schema.Type)
344343
}
345344

346-
if schema.Type == "integer" && !isIntegral(m.Value()) {
345+
if schema.Type == string(Integer) && !isIntegral(m.Value()) {
347346
return fmt.Errorf("cannot apply non-integral maximum validation (%v) to integer value", m.Value())
348347
}
349348

@@ -424,7 +423,7 @@ func (m Pattern) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
424423
}
425424

426425
func (m MaxItems) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
427-
if schema.Type != "array" {
426+
if schema.Type != string(Array) {
428427
return fmt.Errorf("must apply maxitem to an array")
429428
}
430429
val := int64(m)
@@ -433,7 +432,7 @@ func (m MaxItems) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
433432
}
434433

435434
func (m MinItems) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
436-
if schema.Type != "array" {
435+
if schema.Type != string(Array) {
437436
return fmt.Errorf("must apply minitems to an array")
438437
}
439438
val := int64(m)

Diff for: pkg/crd/parser.go

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2323
"k8s.io/apimachinery/pkg/runtime/schema"
24-
2524
"sigs.k8s.io/controller-tools/pkg/loader"
2625
"sigs.k8s.io/controller-tools/pkg/markers"
2726
)

Diff for: pkg/crd/parser_integration_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ import (
2626
"golang.org/x/tools/go/packages"
2727
apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
2828
"k8s.io/apimachinery/pkg/runtime/schema"
29-
"sigs.k8s.io/yaml"
30-
3129
"sigs.k8s.io/controller-tools/pkg/crd"
3230
crdmarkers "sigs.k8s.io/controller-tools/pkg/crd/markers"
3331
"sigs.k8s.io/controller-tools/pkg/loader"
3432
"sigs.k8s.io/controller-tools/pkg/markers"
33+
"sigs.k8s.io/yaml"
3534
)
3635

3736
func packageErrors(pkg *loader.Package, filterKinds ...packages.ErrorKind) error {

0 commit comments

Comments
 (0)