Skip to content

Commit d2a68a1

Browse files
authored
Merge pull request #5392 from richardcase/add_kal_linter
🌱 feat: add kal linter
2 parents 647a83e + 36c7812 commit d2a68a1

File tree

20 files changed

+565
-408
lines changed

20 files changed

+565
-408
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ jobs:
1717
working-directory:
1818
- ""
1919
steps:
20-
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # tag=v4.1.1
20+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # tag=v4.2.2
2121
- name: Calculate go version
2222
id: vars
2323
run: echo "go_version=$(make go-version)" >> $GITHUB_OUTPUT
2424
- name: Set up Go
25-
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # tag=v5.0.0
25+
uses: actions/setup-go@d35c59abb061a4a6fb18e82ac0862c26744d6ab5 # tag=v5.5.0
2626
with:
2727
go-version: ${{ steps.vars.outputs.go_version }}
2828
- name: golangci-lint
29-
uses: golangci/golangci-lint-action@2e788936b09dd82dc280e845628a40d2ba6b204c # tag=v6.3.1
29+
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # tag=v8.0.0
3030
with:
31-
version: v1.62.2
32-
args: --out-format=colored-line-number
31+
version: v2.1.0
3332
working-directory: ${{matrix.working-directory}}
33+
- name: Lint API
34+
run: make lint-api

.golangci-kal.yml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
version: "2"
2+
3+
run:
4+
timeout: 10m
5+
go: "1.22"
6+
allow-parallel-runners: true
7+
8+
linters:
9+
default: none
10+
enable:
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.
30+
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.
46+
47+
exclusions:
48+
generated: strict
49+
paths:
50+
- zz_generated.*\.go$
51+
- ".*_test.go" # Exclude test files.
52+
rules:
53+
# KAL should only run on API folders.
54+
- path-except: "api//*"
55+
linters:
56+
- kubeapilinter
57+
- path: "api/v1beta1/*|api/v1alpha1/*"
58+
text: "Conditions field must be a slice of metav1.Condition"
59+
linters:
60+
- kubeapilinter
61+
- path: "api/v1beta1/*"
62+
text: "type ClusterIPFamily should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
63+
linters:
64+
- kubeapilinter
65+
- path: "exp/ipam/api/v1alpha1/*|exp/ipam/api/v1beta1/*"
66+
text: "field Prefix should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
67+
linters:
68+
- kubeapilinter
69+
# clusterctl and Runtime Hooks can be fixed once we bump their apiVersion.
70+
- path: "cmd/clusterctl/api/v1alpha3|exp/runtime/hooks/api/v1alpha1"
71+
text: "maxlength"
72+
linters:
73+
- kubeapilinter
74+
# controller-gen does not allow to add MaxItems to Schemaless fields
75+
- path: "api/v1beta1/*"
76+
text: "maxlength: field (AllOf|OneOf|AnyOf) must have a maximum items, add kubebuilder:validation:MaxItems marker"
77+
linters:
78+
- kubeapilinter
79+
# It does not make sense to add a maxItems marker on the *List structs as they are not used to generate CRD YAMLs.
80+
# This exclude will be removed once https://github.com/JoelSpeed/kal/issues/38 is resolved.
81+
- path: "api/v1beta1/*|api/v1alpha1/*"
82+
text: "maxlength: field Items must have a maximum items, add kubebuilder:validation:MaxItems marker"
83+
linters:
84+
- kubeapilinter
85+
- path: "api/v1alpha1/*|api/v1beta1/*"
86+
text: "nobools"
87+
linters:
88+
- kubeapilinter
89+
# We follow the current CustomResourceDefinition field's json tag pattern.
90+
- path: "api/v1beta1/*"
91+
text: "field (XPreserveUnknownFields|XPreserveUnknownFields|XValidations|XMetadata|XIntOrString) json tag does not match pattern"
92+
linters:
93+
- kubeapilinter
94+
# The following rules are disabled until we migrate to the new API.
95+
- path: "bootstrap/kubeadm/api/v1beta1/kubeadm_types.go"
96+
text: "field Token is marked as required, should not be a pointer"
97+
linters:
98+
- kubeapilinter
99+
- path: "api/v1beta1/clusterclass_types.go"
100+
text: "field Ref is marked as required, should not be a pointer"
101+
linters:
102+
- kubeapilinter
103+
- path: "api/v1alpha1/*|api/v1beta1/*|api/v1alpha3/*"
104+
text: "field Items must be marked as optional or required"
105+
linters:
106+
- kubeapilinter
107+
108+
issues:
109+
max-same-issues: 0
110+
max-issues-per-linter: 0

0 commit comments

Comments
 (0)