Skip to content

Commit 5cfc702

Browse files
authored
⚠️ Fix required fields linter findings (#12558)
* Add omitempty/omitzero to required fields * Change Arg.Value from string to *string * Change Required fields in ClusterClass struct from bool to *bool * Change ResourceBinding.Applied from bool to *bool * Change Partition.Layout from bool to *bool Signed-off-by: Stefan Büringer [email protected] * Change LastRemediationStatus.RetryCount from int32 to *int32 * Change UnhealthyNodeConditions TimeoutSeconds from int32 to *int32 * Change IPAddressSpec.Prefix from int to *int32 Signed-off-by: Stefan Büringer [email protected] * Adjust KAL config to exclude requiredfields false positives * Fix compile issue after rebase --------- Signed-off-by: Stefan Büringer [email protected]
1 parent 122cac9 commit 5cfc702

File tree

96 files changed

+1171
-481
lines changed

Some content is hidden

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

96 files changed

+1171
-481
lines changed

.golangci-kal.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ linters:
9595
text: "field Conditions type Conditions must have a maximum items, add kubebuilder:validation:MaxItems marker"
9696
linters:
9797
- kubeapilinter
98+
- path: "api/core/v1beta2/condition_types.go"
99+
text: "requiredfields: field (Type|Status|LastTransitionTime) should have the omitempty tag"
100+
linters:
101+
- kubeapilinter
98102

99103
## Excludes for current clusterctl v1alpha3 and Runtime Hooks v1alpha1 apiVersions (can be fixed once we bump their apiVersion).
100104
# Note: The types in api/runtime/hooks/v1alpha1 are not CRDs, so e.g. SSA markers don't make sense there.
@@ -208,6 +212,10 @@ linters:
208212
text: "requiredfields: field .* is marked as required, but has the omitempty tag"
209213
linters:
210214
- kubeapilinter
215+
- path: "api/.*"
216+
text: "requiredfields: field (Applied|Value|Layout|RetryCount|Required|Prefix|TimeoutSeconds) is marked as required, should not be a pointer"
217+
linters:
218+
- kubeapilinter
211219

212220
# TODO: Excludes that should be removed once https://github.com/kubernetes-sigs/kube-api-linter/issues/132 will be fixed
213221
- path: "api/.*"
@@ -218,6 +226,7 @@ linters:
218226
text: "optionalfields: field (.*) is optional and has a valid zero value \\({}\\), but the validation is not complete \\(e.g. min properties/adding required fields\\). The field should be a pointer to allow the zero value to be set. If the zero value is not a valid use case, complete the validation and remove the pointer."
219227
linters:
220228
- kubeapilinter
229+
221230
issues:
222231
max-same-issues: 0
223232
max-issues-per-linter: 0

api/addons/v1beta1/conversion_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
28+
"k8s.io/utils/ptr"
2829
"sigs.k8s.io/randfill"
2930

3031
addonsv1 "sigs.k8s.io/cluster-api/api/addons/v1beta2"
@@ -75,11 +76,20 @@ func spokeClusterResourceSetStatus(in *ClusterResourceSetStatus, c randfill.Cont
7576

7677
func ClusterResourceSetBindingFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
7778
return []interface{}{
79+
hubResourceBinding,
7880
hubClusterResourceSetStatus,
7981
spokeClusterResourceSetBindingSpec,
8082
}
8183
}
8284

85+
func hubResourceBinding(in *addonsv1.ResourceBinding, c randfill.Continue) {
86+
c.FillNoCustom(in)
87+
88+
if in.Applied == nil {
89+
in.Applied = ptr.To(false) // Applied is a required field and nil does not round trip
90+
}
91+
}
92+
8393
func spokeClusterResourceSetBindingSpec(in *ClusterResourceSetBindingSpec, c randfill.Continue) {
8494
c.FillNoCustom(in)
8595

api/addons/v1beta1/zz_generated.conversion.go

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

api/addons/v1beta2/clusterresourceset_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ type ClusterResourceSetSpec struct {
5858
// It must match the Cluster labels. This field is immutable.
5959
// Label selector cannot be empty.
6060
// +required
61-
ClusterSelector metav1.LabelSelector `json:"clusterSelector"`
61+
ClusterSelector metav1.LabelSelector `json:"clusterSelector,omitempty,omitzero"`
6262

6363
// resources is a list of Secrets/ConfigMaps where each contains 1 or more resources to be applied to remote clusters.
6464
// +required
6565
// +listType=atomic
6666
// +kubebuilder:validation:MinItems=1
6767
// +kubebuilder:validation:MaxItems=100
68-
Resources []ResourceRef `json:"resources"`
68+
Resources []ResourceRef `json:"resources,omitempty"`
6969

7070
// strategy is the strategy to be used during applying resources. Defaults to ApplyOnce. This field is immutable.
7171
// +kubebuilder:validation:Enum=ApplyOnce;Reconcile

api/addons/v1beta2/clusterresourcesetbinding_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"reflect"
2121

2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/utils/ptr"
2324
)
2425

2526
// ResourceBinding shows the status of a resource that belongs to a ClusterResourceSet matched by the owner cluster of the ClusterResourceSetBinding object.
@@ -40,7 +41,7 @@ type ResourceBinding struct {
4041

4142
// applied is to track if a resource is applied to the cluster or not.
4243
// +required
43-
Applied bool `json:"applied"`
44+
Applied *bool `json:"applied,omitempty"`
4445
}
4546

4647
// ResourceSetBinding keeps info on all of the resources in a ClusterResourceSet.
@@ -61,7 +62,7 @@ type ResourceSetBinding struct {
6162
// IsApplied returns true if the resource is applied to the cluster by checking the cluster's binding.
6263
func (r *ResourceSetBinding) IsApplied(resourceRef ResourceRef) bool {
6364
resourceBinding := r.GetResource(resourceRef)
64-
return resourceBinding != nil && resourceBinding.Applied
65+
return resourceBinding != nil && ptr.Deref(resourceBinding.Applied, false)
6566
}
6667

6768
// GetResource returns a ResourceBinding for a resource ref if present.

api/addons/v1beta2/clusterresourcesetbinding_types_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/google/go-cmp/cmp"
2424
. "github.com/onsi/gomega"
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/utils/ptr"
2627
)
2728

2829
func TestIsResourceApplied(t *testing.T) {
@@ -43,13 +44,13 @@ func TestIsResourceApplied(t *testing.T) {
4344
Resources: []ResourceBinding{
4445
{
4546
ResourceRef: resourceRefApplySucceeded,
46-
Applied: true,
47+
Applied: ptr.To(true),
4748
Hash: "xyz",
4849
LastAppliedTime: metav1.Time{Time: time.Now().UTC()},
4950
},
5051
{
5152
ResourceRef: resourceRefApplyFailed,
52-
Applied: false,
53+
Applied: ptr.To(false),
5354
Hash: "",
5455
LastAppliedTime: metav1.Time{Time: time.Now().UTC()},
5556
},
@@ -106,7 +107,7 @@ func TestResourceSetBindingGetResourceBinding(t *testing.T) {
106107

107108
resourceRefApplyFailedBinding := ResourceBinding{
108109
ResourceRef: resourceRefApplyFailed,
109-
Applied: false,
110+
Applied: ptr.To(false),
110111
Hash: "",
111112
LastAppliedTime: metav1.Time{Time: time.Now().UTC()},
112113
}
@@ -115,7 +116,7 @@ func TestResourceSetBindingGetResourceBinding(t *testing.T) {
115116
Resources: []ResourceBinding{
116117
{
117118
ResourceRef: resourceRefApplySucceeded,
118-
Applied: true,
119+
Applied: ptr.To(true),
119120
Hash: "xyz",
120121
LastAppliedTime: metav1.Time{Time: time.Now().UTC()},
121122
},
@@ -161,15 +162,15 @@ func TestSetResourceBinding(t *testing.T) {
161162
Resources: []ResourceBinding{
162163
{
163164
ResourceRef: resourceRefApplyFailed,
164-
Applied: false,
165+
Applied: ptr.To(false),
165166
Hash: "",
166167
LastAppliedTime: metav1.Time{Time: time.Now().UTC()},
167168
},
168169
},
169170
}
170171
updateFailedResourceBinding := ResourceBinding{
171172
ResourceRef: resourceRefApplyFailed,
172-
Applied: true,
173+
Applied: ptr.To(true),
173174
Hash: "xyz",
174175
LastAppliedTime: metav1.Time{Time: time.Now().UTC()},
175176
}
@@ -179,7 +180,7 @@ func TestSetResourceBinding(t *testing.T) {
179180
Name: "newBinding",
180181
Kind: "Secret",
181182
},
182-
Applied: false,
183+
Applied: ptr.To(false),
183184
Hash: "xyz",
184185
LastAppliedTime: metav1.Time{Time: time.Now().UTC()},
185186
}

api/addons/v1beta2/zz_generated.deepcopy.go

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

api/bootstrap/kubeadm/v1beta1/conversion_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,50 @@ func hubKubeadmConfigSpec(in *bootstrapv1.KubeadmConfigSpec, c randfill.Continue
124124
if in.ClusterConfiguration.Etcd.Local.ExtraEnvs != nil && *in.ClusterConfiguration.Etcd.Local.ExtraEnvs == nil {
125125
in.ClusterConfiguration.Etcd.Local.ExtraEnvs = nil
126126
}
127+
128+
for i, arg := range in.ClusterConfiguration.APIServer.ExtraArgs {
129+
if arg.Value == nil {
130+
arg.Value = ptr.To("")
131+
}
132+
in.ClusterConfiguration.APIServer.ExtraArgs[i] = arg
133+
}
134+
for i, arg := range in.ClusterConfiguration.ControllerManager.ExtraArgs {
135+
if arg.Value == nil {
136+
arg.Value = ptr.To("")
137+
}
138+
in.ClusterConfiguration.ControllerManager.ExtraArgs[i] = arg
139+
}
140+
for i, arg := range in.ClusterConfiguration.Scheduler.ExtraArgs {
141+
if arg.Value == nil {
142+
arg.Value = ptr.To("")
143+
}
144+
in.ClusterConfiguration.Scheduler.ExtraArgs[i] = arg
145+
}
146+
for i, arg := range in.ClusterConfiguration.Etcd.Local.ExtraArgs {
147+
if arg.Value == nil {
148+
arg.Value = ptr.To("")
149+
}
150+
in.ClusterConfiguration.Etcd.Local.ExtraArgs[i] = arg
151+
}
152+
for i, arg := range in.InitConfiguration.NodeRegistration.KubeletExtraArgs {
153+
if arg.Value == nil {
154+
arg.Value = ptr.To("")
155+
}
156+
in.InitConfiguration.NodeRegistration.KubeletExtraArgs[i] = arg
157+
}
158+
for i, arg := range in.JoinConfiguration.NodeRegistration.KubeletExtraArgs {
159+
if arg.Value == nil {
160+
arg.Value = ptr.To("")
161+
}
162+
in.JoinConfiguration.NodeRegistration.KubeletExtraArgs[i] = arg
163+
}
164+
165+
for i, p := range in.DiskSetup.Partitions {
166+
if p.Layout == nil {
167+
p.Layout = ptr.To(false) // Layout is a required field and nil does not round trip
168+
}
169+
in.DiskSetup.Partitions[i] = p
170+
}
127171
}
128172

129173
func hubNodeRegistrationOptions(in *bootstrapv1.NodeRegistrationOptions, c randfill.Continue) {

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

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

api/bootstrap/kubeadm/v1beta2/conversion.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package v1beta2
1818

1919
import (
2020
"sort"
21+
22+
"k8s.io/utils/ptr"
2123
)
2224

2325
func (*KubeadmConfig) Hub() {}
@@ -36,11 +38,11 @@ func ConvertToArgs(in map[string]string) []Arg {
3638
}
3739
args := make([]Arg, 0, len(in))
3840
for k, v := range in {
39-
args = append(args, Arg{Name: k, Value: v})
41+
args = append(args, Arg{Name: k, Value: ptr.To(v)})
4042
}
4143
sort.Slice(args, func(i, j int) bool {
4244
if args[i].Name == args[j].Name {
43-
return args[i].Value < args[j].Value
45+
return ptr.Deref(args[i].Value, "") < ptr.Deref(args[j].Value, "")
4446
}
4547
return args[i].Name < args[j].Name
4648
})
@@ -56,7 +58,7 @@ func ConvertFromArgs(in []Arg) map[string]string {
5658
}
5759
args := make(map[string]string, len(in))
5860
for _, arg := range in {
59-
args[arg.Name] = arg.Value
61+
args[arg.Name] = ptr.Deref(arg.Value, "")
6062
}
6163
return args
6264
}

0 commit comments

Comments
 (0)