Skip to content

Commit 6a9c13e

Browse files
committed
wip: add kal linter
Signed-off-by: Richard Case <[email protected]>
1 parent a0522bd commit 6a9c13e

20 files changed

+497
-469
lines changed

.github/workflows/pr-golangci-lint.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
- name: golangci-lint
2929
uses: golangci/golangci-lint-action@2e788936b09dd82dc280e845628a40d2ba6b204c # tag=v6.3.1
3030
with:
31-
version: v1.62.2
31+
version: v2.1.0
3232
args: --out-format=colored-line-number
3333
working-directory: ${{matrix.working-directory}}
34+
- name: Lint API
35+
run: make lint-api

.golangci-kal.yml

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,110 @@
1+
version: "2"
2+
13
run:
24
timeout: 10m
35
go: "1.22"
46
allow-parallel-runners: true
57

68
linters:
7-
disable-all: true
9+
default: none
810
enable:
9-
- kal # linter for Kube API conventions
10-
11-
linters-settings:
12-
custom:
13-
kal:
14-
type: "module"
15-
description: KAL is the 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.
11+
- kubeapilinter # linter for Kube API conventions
12+
settings:
13+
custom:
14+
kubeapilinter:
15+
type: "module"
16+
description: KAL is the Kube-API-Linter and lints Kube like APIs based on API conventions and best practices.
17+
settings:
18+
linters:
19+
enable:
20+
#- "commentstart" # Ensure comments start with the serialized version of the field name.
21+
#- "conditions" # Ensure conditions have the correct json tags and markers.
22+
#- "integers" # Ensure only int32 and int64 are used for integers.
23+
#- "jsontags" # Ensure every field has a json tag.
24+
#- "maxlength" # Ensure all strings and arrays have maximum lengths/maximum items.
25+
#- "nobools" # Bools do not evolve over time, should use enums instead.
26+
#- "nofloats" # Ensure floats are not used.
27+
#- "optionalorrequired" # Every field should be marked as `+optional` or `+required`.
28+
# - "requiredfields" # Required fields should not be pointers, and should not have `omitempty`.
29+
- "statussubresource" # All root objects that have a `status` field should have a status subresource.
2930

30-
# Linters below this line are disabled, pending conversation on how and when to enable them.
31-
disable:
32-
- "*" # We will manually enable new linters after understanding the impact. Disable all by default.
33-
lintersConfig:
34-
conditions:
35-
isFirstField: Warn # Require conditions to be the first field in the status struct.
36-
usePatchStrategy: Forbid # Conditions should not use the patch strategy on CRDs.
37-
useProtobuf: Forbid # We don't use protobuf, so protobuf tags are not required.
38-
# jsonTags:
39-
# jsonTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$" # The default regex is appropriate for our use case.
40-
# optionalOrRequired:
41-
# preferredOptionalMarker: optional | kubebuilder:validation:Optional # The preferred optional marker to use, fixes will suggest to use this marker. Defaults to `optional`.
42-
# preferredRequiredMarker: required | kubebuilder:validation:Required # The preferred required marker to use, fixes will suggest to use this marker. Defaults to `required`.
43-
# requiredFields:
44-
# pointerPolicy: Warn | SuggestFix # Defaults to `SuggestFix`. We want our required fields to not be pointers.
31+
# Linters below this line are disabled, pending conversation on how and when to enable them.
32+
disable:
33+
- "*" # We will manually enable new linters after understanding the impact. Disable all by default.
34+
lintersConfig:
35+
conditions:
36+
isFirstField: Warn # Require conditions to be the first field in the status struct.
37+
usePatchStrategy: Forbid # Conditions should not use the patch strategy on CRDs.
38+
useProtobuf: Forbid # We don't use protobuf, so protobuf tags are not required.
39+
# jsonTags:
40+
# jsonTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$" # The default regex is appropriate for our use case.
41+
# optionalOrRequired:
42+
# preferredOptionalMarker: optional | kubebuilder:validation:Optional # The preferred optional marker to use, fixes will suggest to use this marker. Defaults to `optional`.
43+
# preferredRequiredMarker: required | kubebuilder:validation:Required # The preferred required marker to use, fixes will suggest to use this marker. Defaults to `required`.
44+
# requiredFields:
45+
# pointerPolicy: Warn | SuggestFix # Defaults to `SuggestFix`. We want our required fields to not be pointers.
4546

46-
issues:
47-
exclude-files:
48-
- "zz_generated.*\\.go$"
49-
- "vendored_openapi\\.go$"
50-
# We don't want to invest time to fix new linter findings in old API types.
51-
- "internal/apis/.*"
52-
- ".*_test.go" # Exclude test files.
53-
max-same-issues: 0
54-
max-issues-per-linter: 0
55-
exclude-rules:
47+
exclusions:
48+
generated: strict
49+
paths:
50+
- zz_generated.*\.go$
51+
- ".*_test.go" # Exclude test files.
52+
rules:
5653
# KAL should only run on API folders.
5754
- path-except: "api//*"
5855
linters:
59-
- kal
56+
- kubeapilinter
6057
- path: "api/v1beta1/*|api/v1alpha1/*"
6158
text: "Conditions field must be a slice of metav1.Condition"
6259
linters:
63-
- kal
60+
- kubeapilinter
6461
- path: "api/v1beta1/*"
6562
text: "type ClusterIPFamily should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
6663
linters:
67-
- kal
64+
- kubeapilinter
6865
- path: "exp/ipam/api/v1alpha1/*|exp/ipam/api/v1beta1/*"
6966
text: "field Prefix should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
7067
linters:
71-
- kal
68+
- kubeapilinter
7269
# clusterctl and Runtime Hooks can be fixed once we bump their apiVersion.
7370
- path: "cmd/clusterctl/api/v1alpha3|exp/runtime/hooks/api/v1alpha1"
7471
text: "maxlength"
7572
linters:
76-
- kal
73+
- kubeapilinter
7774
# controller-gen does not allow to add MaxItems to Schemaless fields
7875
- path: "api/v1beta1/*"
7976
text: "maxlength: field (AllOf|OneOf|AnyOf) must have a maximum items, add kubebuilder:validation:MaxItems marker"
8077
linters:
81-
- kal
78+
- kubeapilinter
8279
# It does not make sense to add a maxItems marker on the *List structs as they are not used to generate CRD YAMLs.
8380
# This exclude will be removed once https://github.com/JoelSpeed/kal/issues/38 is resolved.
8481
- path: "api/v1beta1/*|api/v1alpha1/*"
8582
text: "maxlength: field Items must have a maximum items, add kubebuilder:validation:MaxItems marker"
8683
linters:
87-
- kal
84+
- kubeapilinter
8885
- path: "api/v1alpha1/*|api/v1beta1/*"
8986
text: "nobools"
9087
linters:
91-
- kal
88+
- kubeapilinter
9289
# We follow the current CustomResourceDefinition field's json tag pattern.
9390
- path: "api/v1beta1/*"
9491
text: "field (XPreserveUnknownFields|XPreserveUnknownFields|XValidations|XMetadata|XIntOrString) json tag does not match pattern"
9592
linters:
96-
- kal
93+
- kubeapilinter
9794
# The following rules are disabled until we migrate to the new API.
9895
- path: "bootstrap/kubeadm/api/v1beta1/kubeadm_types.go"
9996
text: "field Token is marked as required, should not be a pointer"
10097
linters:
101-
- kal
98+
- kubeapilinter
10299
- path: "api/v1beta1/clusterclass_types.go"
103100
text: "field Ref is marked as required, should not be a pointer"
104101
linters:
105-
- kal
102+
- kubeapilinter
106103
- path: "api/v1alpha1/*|api/v1beta1/*|api/v1alpha3/*"
107104
text: "field Items must be marked as optional or required"
108105
linters:
109-
- kal
106+
- kubeapilinter
107+
108+
issues:
109+
max-same-issues: 0
110+
max-issues-per-linter: 0

0 commit comments

Comments
 (0)