Skip to content

Commit ea1a774

Browse files
authored
Merge pull request #12299 from sbueringer/pr-optionalfields-linter
🌱 Enable optionalfields linter and fix remaining findings
2 parents d50f243 + 6fc41b1 commit ea1a774

File tree

111 files changed

+1890
-1089
lines changed

Some content is hidden

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

111 files changed

+1890
-1089
lines changed

.golangci-kal.yml

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ linters:
2323
- "nobools" # Bools do not evolve over time, should use enums instead.
2424
- "nofloats" # Ensure floats are not used.
2525
- "nomaps" # Ensure maps are not used.
26+
- "optionalfields" # Ensure that all fields marked as optional adhere to being pointers and
27+
# having the `omitempty` value in their `json` tag where appropriate.
2628
- "optionalorrequired" # Every field should be marked as `+optional` or `+required`.
2729
- "requiredfields" # Required fields should not be pointers, and should not have `omitempty`.
2830
- "statusoptional" # Ensure all first children within status should be optional.
@@ -42,6 +44,12 @@ linters:
4244
isFirstField: Warn # Require conditions to be the first field in the status struct.
4345
usePatchStrategy: Forbid # Require conditions to be the first field in the status struct.
4446
useProtobuf: Forbid # We don't use protobuf, so protobuf tags are not required.
47+
optionalFields:
48+
pointers:
49+
preference: WhenRequired # Always | WhenRequired # Whether to always require pointers, or only when required. Defaults to `Always`.
50+
policy: SuggestFix # SuggestFix | Warn # The policy for pointers in optional fields. Defaults to `SuggestFix`.
51+
omitempty:
52+
policy: SuggestFix # SuggestFix | Warn | Ignore # The policy for omitempty in optional fields. Defaults to `SuggestFix`.
4553
# jsonTags:
4654
# jsonTagRegex: "^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*$" # The default regex is appropriate for our use case.
4755
# optionalOrRequired:
@@ -96,6 +104,14 @@ linters:
96104
text: "nomaps: FailureDomains should not use a map type, use a list type with a unique name/identifier instead"
97105
linters:
98106
- kubeapilinter
107+
- path: "api/addons/v1beta1/*|api/bootstrap/kubeadm/v1beta1/*|api/controlplane/kubeadm/v1beta1/*|api/core/v1beta1/*|api/ipam/v1beta1/*|api/ipam/v1alpha1/*|api/runtime/v1alpha1/*|cmd/clusterctl/api/v1alpha3/*"
108+
text: "optionalfields"
109+
linters:
110+
- kubeapilinter
111+
- path: "api/core/v1beta1/clusterclass_types.go"
112+
text: "field Ref is marked as required, should not be a pointer"
113+
linters:
114+
- kubeapilinter
99115

100116
## Excludes for clusterctl and Runtime Hooks (can be fixed once we bump their apiVersion)
101117
- path: "cmd/clusterctl/api/v1alpha3|api/runtime/hooks/v1alpha1"
@@ -134,14 +150,60 @@ linters:
134150
linters:
135151
- kubeapilinter
136152

153+
## Excludes for optionalfields
154+
# Empty Bootstrap object is blocked via validating webhooks. This cannot be detected by KAL (same if we move the validation to CEL).
155+
- path: "api/core/v1beta2/machine_types.go"
156+
text: "optionalfields: field (Bootstrap) is optional and (should be a pointer|should have the omitempty tag|has a valid zero value)"
157+
linters:
158+
- kubeapilinter
159+
# KAL incorrectly reports that the Taints field doesn't have to be a pointer (it has to be to preserve []).
160+
- path: "api/bootstrap/kubeadm/v1beta2/kubeadm_types.go"
161+
text: "optionalfields: field Taints is optional but the underlying type does not need to be a pointer. The pointer should be removed."
162+
linters:
163+
- kubeapilinter
164+
137165
## TODO: The following rules are disabled until we migrate to the new API.
138166
# Note: Maybe this has to stay a pointer for marshalling reasons.
139167
- path: "api/bootstrap/kubeadm/v1beta2/kubeadm_types.go|api/bootstrap/kubeadm/v1beta1/kubeadm_types.go"
140168
text: "field Token is marked as required, should not be a pointer"
141169
linters:
142170
- kubeapilinter
143-
- path: "api/core/v1beta2/clusterclass_types.go|api/core/v1beta1/clusterclass_types.go"
144-
text: "field Ref is marked as required, should not be a pointer"
171+
172+
# Audit the entire hook types + builtins from a serialization point of view when bumping the API (this is not a CRD)
173+
- path: "api/runtime/hooks/v1alpha1/*"
174+
text: "optionalfields"
175+
linters:
176+
- kubeapilinter
177+
178+
# KAL does not handle omitzero correctly yet: https://github.com/kubernetes-sigs/kube-api-linter/pull/115
179+
- path: "api/.*"
180+
text: "optionalfields: field Status is optional and should (be a pointer|have the omitempty tag)"
181+
linters:
182+
- kubeapilinter
183+
- path: "api/bootstrap/kubeadm/v1beta2/*"
184+
text: "optionalfields: field (Spec|NodeRegistration|LocalAPIEndpoint|Etcd|APIServer|ControllerManager|Scheduler|DNS|Discovery|ObjectMeta) is optional and should (be a pointer|have the omitempty tag)"
185+
linters:
186+
- kubeapilinter
187+
- path: "api/controlplane/kubeadm/v1beta2/*"
188+
text: "optionalfields: field (Spec|ObjectMeta|KubeadmConfigSpec) is optional and should (be a pointer|have the omitempty tag)"
189+
linters:
190+
- kubeapilinter
191+
- path: "api/core/v1beta2/cluster_types.go"
192+
text: "optionalfields: field (ControlPlaneEndpoint|ControlPlane|Metadata) is optional and should (be a pointer|have the omitempty tag)"
193+
linters:
194+
- kubeapilinter
195+
- path: "api/core/v1beta2/clusterclass_types.go"
196+
text: "optionalfields: field (Workers|Metadata|ControlPlane|Infrastructure|DeprecatedV1Beta1Metadata) is optional and should (be a pointer|have the omitempty tag)"
197+
linters:
198+
- kubeapilinter
199+
- path: "api/ipam/v1beta2/ipaddressclaim_types.go"
200+
text: "optionalfields: field AddressRef is optional and should (be a pointer|have the omitempty tag)"
201+
linters:
202+
- kubeapilinter
203+
204+
# KAL does not handle enum markers on enum types yet: https://github.com/kubernetes-sigs/kube-api-linter/issues/113
205+
- path: ".*"
206+
text: "optionalfields: field (Format|Encoding|Type|DeletePolicy) is optional and (should be a pointer|has a valid zero value)"
145207
linters:
146208
- kubeapilinter
147209
issues:

api/bootstrap/kubeadm/v1beta1/conversion.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,14 @@ func Convert_v1beta2_KubeadmConfigStatus_To_v1beta1_KubeadmConfigStatus(in *boot
375375
return nil
376376
}
377377

378+
func Convert_v1beta2_ControllerManager_To_v1beta1_ControlPlaneComponent(in *bootstrapv1.ControllerManager, out *ControlPlaneComponent, s apimachineryconversion.Scope) error {
379+
return Convert_v1beta2_ControlPlaneComponent_To_v1beta1_ControlPlaneComponent(&in.ControlPlaneComponent, out, s)
380+
}
381+
382+
func Convert_v1beta2_Scheduler_To_v1beta1_ControlPlaneComponent(in *bootstrapv1.Scheduler, out *ControlPlaneComponent, s apimachineryconversion.Scope) error {
383+
return Convert_v1beta2_ControlPlaneComponent_To_v1beta1_ControlPlaneComponent(&in.ControlPlaneComponent, out, s)
384+
}
385+
378386
func Convert_v1beta2_ControlPlaneComponent_To_v1beta1_ControlPlaneComponent(in *bootstrapv1.ControlPlaneComponent, out *ControlPlaneComponent, s apimachineryconversion.Scope) error {
379387
// Following fields require a custom conversions.
380388
out.ExtraArgs = bootstrapv1.ConvertFromArgs(in.ExtraArgs)
@@ -390,6 +398,11 @@ func Convert_v1beta2_LocalEtcd_To_v1beta1_LocalEtcd(in *bootstrapv1.LocalEtcd, o
390398
func Convert_v1beta2_NodeRegistrationOptions_To_v1beta1_NodeRegistrationOptions(in *bootstrapv1.NodeRegistrationOptions, out *NodeRegistrationOptions, s apimachineryconversion.Scope) error {
391399
// Following fields require a custom conversions.
392400
out.KubeletExtraArgs = bootstrapv1.ConvertFromArgs(in.KubeletExtraArgs)
401+
if in.Taints == nil {
402+
out.Taints = nil
403+
} else {
404+
out.Taints = *in.Taints
405+
}
393406
return autoConvert_v1beta2_NodeRegistrationOptions_To_v1beta1_NodeRegistrationOptions(in, out, s)
394407
}
395408

@@ -406,6 +419,14 @@ func Convert_v1beta1_APIServer_To_v1beta2_APIServer(in *APIServer, out *bootstra
406419
return autoConvert_v1beta1_APIServer_To_v1beta2_APIServer(in, out, s)
407420
}
408421

422+
func Convert_v1beta1_ControlPlaneComponent_To_v1beta2_ControllerManager(in *ControlPlaneComponent, out *bootstrapv1.ControllerManager, s apimachineryconversion.Scope) error {
423+
return Convert_v1beta1_ControlPlaneComponent_To_v1beta2_ControlPlaneComponent(in, &out.ControlPlaneComponent, s)
424+
}
425+
426+
func Convert_v1beta1_ControlPlaneComponent_To_v1beta2_Scheduler(in *ControlPlaneComponent, out *bootstrapv1.Scheduler, s apimachineryconversion.Scope) error {
427+
return Convert_v1beta1_ControlPlaneComponent_To_v1beta2_ControlPlaneComponent(in, &out.ControlPlaneComponent, s)
428+
}
429+
409430
func Convert_v1beta1_Discovery_To_v1beta2_Discovery(in *Discovery, out *bootstrapv1.Discovery, s apimachineryconversion.Scope) error {
410431
// Timeout has been removed in v1beta2
411432
return autoConvert_v1beta1_Discovery_To_v1beta2_Discovery(in, out, s)
@@ -432,6 +453,11 @@ func Convert_v1beta1_LocalEtcd_To_v1beta2_LocalEtcd(in *LocalEtcd, out *bootstra
432453

433454
func Convert_v1beta1_NodeRegistrationOptions_To_v1beta2_NodeRegistrationOptions(in *NodeRegistrationOptions, out *bootstrapv1.NodeRegistrationOptions, s apimachineryconversion.Scope) error {
434455
out.KubeletExtraArgs = bootstrapv1.ConvertToArgs(in.KubeletExtraArgs)
456+
if in.Taints == nil {
457+
out.Taints = nil
458+
} else {
459+
out.Taints = ptr.To(in.Taints)
460+
}
435461
return autoConvert_v1beta1_NodeRegistrationOptions_To_v1beta2_NodeRegistrationOptions(in, out, s)
436462
}
437463

api/bootstrap/kubeadm/v1beta1/conversion_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func KubeadmConfigFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
6565
spokeBootstrapTokenString,
6666
spokeBootstrapToken,
6767
hubKubeadmConfigSpec,
68+
hubNodeRegistrationOptions,
6869
}
6970
}
7071

@@ -78,6 +79,7 @@ func KubeadmConfigTemplateFuzzFuncs(_ runtimeserializer.CodecFactory) []interfac
7879
hubBootstrapTokenString,
7980
spokeBootstrapToken,
8081
hubKubeadmConfigSpec,
82+
hubNodeRegistrationOptions,
8183
}
8284
}
8385

@@ -128,6 +130,14 @@ func hubKubeadmConfigSpec(in *bootstrapv1.KubeadmConfigSpec, c randfill.Continue
128130
}
129131
}
130132

133+
func hubNodeRegistrationOptions(in *bootstrapv1.NodeRegistrationOptions, c randfill.Continue) {
134+
c.FillNoCustom(in)
135+
136+
if in.Taints != nil && *in.Taints == nil {
137+
in.Taints = nil
138+
}
139+
}
140+
131141
func spokeKubeadmConfigSpec(in *KubeadmConfigSpec, c randfill.Continue) {
132142
c.FillNoCustom(in)
133143

api/bootstrap/kubeadm/v1beta1/zz_generated.conversion.go

Lines changed: 26 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)