Skip to content

Commit 8025ba2

Browse files
committed
migrate golangci-lint-kal for v2
Signed-off-by: sivchari <[email protected]>
1 parent 1835bfa commit 8025ba2

File tree

4 files changed

+169
-52
lines changed

4 files changed

+169
-52
lines changed

.golangci-kal.yml

Lines changed: 163 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,117 @@
1+
version: "2"
12
run:
2-
timeout: 10m
33
go: "1.23"
44
allow-parallel-runners: true
5-
65
linters:
7-
disable-all: true
6+
default: none
87
enable:
98
- kubeapilinter # linter for Kube API conventions
10-
11-
linters-settings:
12-
custom:
13-
kubeapilinter:
14-
type: "module"
15-
description: kube-api-linter and lints Kube like APIs based on API conventions and best practices.
16-
settings:
17-
linters:
18-
enable:
19-
- "commentstart" # Ensure comments start with the serialized version of the field name.
20-
- "conditions" # Ensure conditions have the correct json tags and markers.
21-
- "integers" # Ensure only int32 and int64 are used for integers.
22-
- "jsontags" # Ensure every field has a json tag.
23-
- "maxlength" # Ensure all strings and arrays have maximum lengths/maximum items.
24-
- "nobools" # Bools do not evolve over time, should use enums instead.
25-
- "nofloats" # Ensure floats are not used.
26-
- "optionalorrequired" # Every field should be marked as `+optional` or `+required`.
27-
- "requiredfields" # Required fields should not be pointers, and should not have `omitempty`.
28-
- "statussubresource" # All root objects that have a `status` field should have a status subresource.
9+
settings:
10+
custom:
11+
kal:
12+
type: module
13+
description: kube-api-linter and lints Kube like APIs based on API conventions and best practices.
14+
settings:
15+
linters:
16+
enable:
17+
- "commentstart" # Ensure comments start with the serialized version of the field name.
18+
- "conditions" # Ensure conditions have the correct json tags and markers.
19+
- "integers" # Ensure only int32 and int64 are used for integers.
20+
- "jsontags" # Ensure every field has a json tag.
21+
- "maxlength" # Ensure all strings and arrays have maximum lengths/maximum items.
22+
- "nobools" # Bools do not evolve over time, should use enums instead.
23+
- "nofloats" # Ensure floats are not used.
24+
- "optionalorrequired" # Every field should be marked as `+optional` or `+required`.
25+
- "requiredfields" # Required fields should not be pointers, and should not have `omitempty`.
26+
- "statussubresource" # All root objects that have a `status` field should have a status subresource.
27+
28+
# Per discussion in July 2024, we are keeping phase fields for now.
29+
# See https://github.com/kubernetes-sigs/cluster-api/pull/10897#discussion_r1685929508
30+
# and https://github.com/kubernetes-sigs/cluster-api/pull/10897#discussion_r1685919394.
31+
# - "nophase" # Phase fields are discouraged by the Kube API conventions, use conditions instead.
2932

30-
# Per discussion in July 2024, we are keeping phase fields for now.
31-
# See https://github.com/kubernetes-sigs/cluster-api/pull/10897#discussion_r1685929508
32-
# and https://github.com/kubernetes-sigs/cluster-api/pull/10897#discussion_r1685919394.
33-
# - "nophase" # Phase fields are discouraged by the Kube API conventions, use conditions instead.
34-
35-
# Linters below this line are disabled, pending conversation on how and when to enable them.
36-
disable:
37-
- "*" # We will manually enable new linters after understanding the impact. Disable all by default.
38-
lintersConfig:
39-
conditions:
40-
isFirstField: Warn # Require conditions to be the first field in the status struct.
41-
usePatchStrategy: Forbid # Conditions should not use the patch strategy on CRDs.
42-
useProtobuf: Forbid # We don't use protobuf, so protobuf tags are not required.
43-
# jsonTags:
44-
# jsonTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$" # The default regex is appropriate for our use case.
45-
# optionalOrRequired:
46-
# preferredOptionalMarker: optional | kubebuilder:validation:Optional # The preferred optional marker to use, fixes will suggest to use this marker. Defaults to `optional`.
47-
# preferredRequiredMarker: required | kubebuilder:validation:Required # The preferred required marker to use, fixes will suggest to use this marker. Defaults to `required`.
48-
# requiredFields:
49-
# pointerPolicy: Warn | SuggestFix # Defaults to `SuggestFix`. We want our required fields to not be pointers.
33+
# Linters below this line are disabled, pending conversation on how and when to enable them.
34+
disable:
35+
- "*" # We will manually enable new linters after understanding the impact. Disable all by default.
36+
lintersConfig:
37+
conditions:
38+
isFirstField: Warn
39+
usePatchStrategy: Forbid
40+
useProtobuf: Forbid
41+
# jsonTags:
42+
# jsonTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$" # The default regex is appropriate for our use case.
43+
# optionalOrRequired:
44+
# preferredOptionalMarker: optional | kubebuilder:validation:Optional # The preferred optional marker to use, fixes will suggest to use this marker. Defaults to `optional`.
45+
# preferredRequiredMarker: required | kubebuilder:validation:Required # The preferred required marker to use, fixes will suggest to use this marker. Defaults to `required`.
46+
# requiredFields:
47+
# pointerPolicy: Warn | SuggestFix # Defaults to `SuggestFix`. We want our required fields to not be pointers.
5048

49+
exclusions:
50+
generated: lax
51+
rules:
52+
# KAL should only run on API folders.
53+
- path-except: "api//*"
54+
linters:
55+
- kal
56+
- path: "api/v1beta2/*|api/addons/v1beta2/*|api/v1beta1/*|api/v1alpha1/*|api/addons/v1beta1/*"
57+
text: "Conditions field must be a slice of metav1.Condition"
58+
linters:
59+
- kal
60+
- path: "api/v1beta2/*|api/v1beta1/*"
61+
text: "type ClusterIPFamily should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
62+
linters:
63+
- kal
64+
- path: "exp/ipam/api/v1beta2/*|exp/ipam/api/v1alpha1/*|exp/ipam/api/v1beta1/*"
65+
text: "field Prefix should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
66+
linters:
67+
- kal
68+
# clusterctl and Runtime Hooks can be fixed once we bump their apiVersion.
69+
- path: "cmd/clusterctl/api/v1alpha3|exp/runtime/hooks/api/v1alpha1"
70+
text: "maxlength"
71+
linters:
72+
- kal
73+
# controller-gen does not allow to add MaxItems to Schemaless fields
74+
- path: "api/v1beta2/*|api/v1beta1/*"
75+
text: "maxlength: field (AllOf|OneOf|AnyOf) must have a maximum items, add kubebuilder:validation:MaxItems marker"
76+
linters:
77+
- kal
78+
# It does not make sense to add a maxItems marker on the *List structs as they are not used to generate CRD YAMLs.
79+
# This exclude will be removed once https://github.com/JoelSpeed/kal/issues/38 is resolved.
80+
- path: "api/v1beta2/*|api/addons/v1beta2/*|api/v1beta1/*|api/v1alpha1/*|api/addons/v1beta1/*"
81+
text: "maxlength: field Items must have a maximum items, add kubebuilder:validation:MaxItems marker"
82+
linters:
83+
- kal
84+
- path: "api/v1beta2/*|api/addons/v1beta2/*|api/v1alpha1/*|api/v1beta1/*|api/addons/v1beta1/*"
85+
text: "nobools"
86+
linters:
87+
- kal
88+
# We follow the current CustomResourceDefinition field's json tag pattern.
89+
- path: "api/v1beta2/*|api/v1beta1/*"
90+
text: "field (XPreserveUnknownFields|XPreserveUnknownFields|XValidations|XMetadata|XIntOrString) json tag does not match pattern"
91+
linters:
92+
- kal
93+
# The following rules are disabled until we migrate to the new API.
94+
- path: "bootstrap/kubeadm/api/v1beta2/kubeadm_types.go|bootstrap/kubeadm/api/v1beta1/kubeadm_types.go"
95+
text: "field Token is marked as required, should not be a pointer"
96+
linters:
97+
- kal
98+
- path: "api/v1beta2/clusterclass_types.go|api/v1beta1/clusterclass_types.go"
99+
text: "field Ref is marked as required, should not be a pointer"
100+
linters:
101+
- kal
102+
- path: "api/v1beta2/*|api/addons/v1beta2/*|api/v1alpha1/*|api/v1beta1/*|api/v1alpha3/*|api/addons/v1beta1/*"
103+
text: "field Items must be marked as optional or required"
104+
linters:
105+
- kal
106+
paths:
107+
- zz_generated.*\.go$
108+
- vendored_openapi\.go$
109+
# We don't want to invest time to fix new linter findings in old API types.
110+
- internal/apis/.*
111+
- ".*_test.go" # Exclude test files.
51112
issues:
52-
exclude-files:
53-
- "zz_generated.*\\.go$"
54-
- "vendored_openapi\\.go$"
55-
# We don't want to invest time to fix new linter findings in old API types.
56-
- "internal/apis/.*"
57-
- ".*_test.go" # Exclude test files.
58-
max-same-issues: 0
59113
max-issues-per-linter: 0
114+
<<<<<<< HEAD
60115
exclude-rules:
61116
# KAL should only run on API folders.
62117
- path-except: "api//*"
@@ -112,3 +167,62 @@ issues:
112167
text: "field Items must be marked as optional or required"
113168
linters:
114169
- kubeapilinter
170+
||||||| parent of d183f0737 (migrate golangci-lint-kal for v2)
171+
exclude-rules:
172+
# KAL should only run on API folders.
173+
- path-except: "api//*"
174+
linters:
175+
- kal
176+
- path: "api/v1beta2/*|api/addons/v1beta2/*|api/v1beta1/*|api/v1alpha1/*|api/addons/v1beta1/*"
177+
text: "Conditions field must be a slice of metav1.Condition"
178+
linters:
179+
- kal
180+
- path: "api/v1beta2/*|api/v1beta1/*"
181+
text: "type ClusterIPFamily should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
182+
linters:
183+
- kal
184+
- path: "exp/ipam/api/v1beta2/*|exp/ipam/api/v1alpha1/*|exp/ipam/api/v1beta1/*"
185+
text: "field Prefix should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
186+
linters:
187+
- kal
188+
# clusterctl and Runtime Hooks can be fixed once we bump their apiVersion.
189+
- path: "cmd/clusterctl/api/v1alpha3|exp/runtime/hooks/api/v1alpha1"
190+
text: "maxlength"
191+
linters:
192+
- kal
193+
# controller-gen does not allow to add MaxItems to Schemaless fields
194+
- path: "api/v1beta2/*|api/v1beta1/*"
195+
text: "maxlength: field (AllOf|OneOf|AnyOf) must have a maximum items, add kubebuilder:validation:MaxItems marker"
196+
linters:
197+
- kal
198+
# It does not make sense to add a maxItems marker on the *List structs as they are not used to generate CRD YAMLs.
199+
# This exclude will be removed once https://github.com/JoelSpeed/kal/issues/38 is resolved.
200+
- path: "api/v1beta2/*|api/addons/v1beta2/*|api/v1beta1/*|api/v1alpha1/*|api/addons/v1beta1/*"
201+
text: "maxlength: field Items must have a maximum items, add kubebuilder:validation:MaxItems marker"
202+
linters:
203+
- kal
204+
- path: "api/v1beta2/*|api/addons/v1beta2/*|api/v1alpha1/*|api/v1beta1/*|api/addons/v1beta1/*"
205+
text: "nobools"
206+
linters:
207+
- kal
208+
# We follow the current CustomResourceDefinition field's json tag pattern.
209+
- path: "api/v1beta2/*|api/v1beta1/*"
210+
text: "field (XPreserveUnknownFields|XPreserveUnknownFields|XValidations|XMetadata|XIntOrString) json tag does not match pattern"
211+
linters:
212+
- kal
213+
# The following rules are disabled until we migrate to the new API.
214+
- path: "bootstrap/kubeadm/api/v1beta2/kubeadm_types.go|bootstrap/kubeadm/api/v1beta1/kubeadm_types.go"
215+
text: "field Token is marked as required, should not be a pointer"
216+
linters:
217+
- kal
218+
- path: "api/v1beta2/clusterclass_types.go|api/v1beta1/clusterclass_types.go"
219+
text: "field Ref is marked as required, should not be a pointer"
220+
linters:
221+
- kal
222+
- path: "api/v1beta2/*|api/addons/v1beta2/*|api/v1alpha1/*|api/v1beta1/*|api/v1alpha3/*|api/addons/v1beta1/*"
223+
text: "field Items must be marked as optional or required"
224+
linters:
225+
- kal
226+
=======
227+
max-same-issues: 0
228+
>>>>>>> d183f0737 (migrate golangci-lint-kal for v2)

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ linters:
344344
- zz_generated.*\.go$
345345
- vendored_openapi\.go$
346346
- internal/apis/.*
347+
- third_party$
348+
- builtin$
349+
- examples$
347350
issues:
348351
max-issues-per-linter: 0
349352
max-same-issues: 0

hack/tools/.custom-gcl.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
version: v1.63.4
2-
name: golangci-lint-kube-api-linter
1+
version: v2.0.2
2+
name: golangci-lint-kube-api-linter-v2.0.2
33
destination: ./bin
44
plugins:
55
- module: 'sigs.k8s.io/kube-api-linter'

test/infrastructure/kind/mapper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ func GetMapping(k8sVersion semver.Version, customImage string) Mapping {
537537
}
538538
for _, m := range preBuiltMappings {
539539
// If the mapping isn't for the right Major/Minor, ignore it.
540-
if k8sVersion.Major != m.KubernetesVersion.Major && k8sVersion.Minor != m.KubernetesVersion.Minor {
540+
if k8sVersion.Major != m.KubernetesVersion.Major || k8sVersion.Minor != m.KubernetesVersion.Minor {
541541
continue
542542
}
543543

0 commit comments

Comments
 (0)