Skip to content

Commit 346f861

Browse files
authored
Merge pull request #11934 from sbueringer/pr-add-max-items
⚠️ Add MaxItems markers to API fields
2 parents 4a2e16a + 4bc5347 commit 346f861

19 files changed

+245
-1
lines changed

.golangci-kal.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,25 @@ issues:
7474
text: "field Prefix should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
7575
linters:
7676
- kal
77-
- path: "api/v1alpha1/*|api/v1alpha3/*|api/v1beta1/*"
77+
- path: "api/v1alpha1/*|api/v1alpha3/*"
7878
text: "maxlength"
7979
linters:
8080
- kal
81+
- path: "api/v1beta1/*"
82+
text: "must have a maximum length, add (kubebuilder:validation:MaxLength|kubebuilder:validation:items:MaxLength) marker"
83+
linters:
84+
- kal
85+
# controller-gen does not allow to add MaxItems to Schemaless fields
86+
- path: "api/v1beta1/*"
87+
text: "maxlength: field (AllOf|OneOf|AnyOf) must have a maximum items, add kubebuilder:validation:MaxItems marker"
88+
linters:
89+
- kal
90+
# It does not make sense to add a maxItems marker on the *List structs as they are not used to generate CRD YAMLs.
91+
# This exclude will be removed once https://github.com/JoelSpeed/kal/issues/38 is resolved.
92+
- path: "api/v1beta1/*"
93+
text: "maxlength: field Items must have a maximum items, add kubebuilder:validation:MaxItems marker"
94+
linters:
95+
- kal
8196
- path: "api/v1alpha1/*|api/v1beta1/*"
8297
text: "nobools"
8398
linters:

api/v1beta1/cluster_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ type Topology struct {
578578
// +optional
579579
// +listType=map
580580
// +listMapKey=name
581+
// +kubebuilder:validation:MaxItems=1000
581582
Variables []ClusterVariable `json:"variables,omitempty"`
582583
}
583584

@@ -646,12 +647,14 @@ type WorkersTopology struct {
646647
// +optional
647648
// +listType=map
648649
// +listMapKey=name
650+
// +kubebuilder:validation:MaxItems=2000
649651
MachineDeployments []MachineDeploymentTopology `json:"machineDeployments,omitempty"`
650652

651653
// machinePools is a list of machine pools in the cluster.
652654
// +optional
653655
// +listType=map
654656
// +listMapKey=name
657+
// +kubebuilder:validation:MaxItems=2000
655658
MachinePools []MachinePoolTopology `json:"machinePools,omitempty"`
656659
}
657660

@@ -780,6 +783,7 @@ type MachinePoolTopology struct {
780783
// failureDomains is the list of failure domains the machine pool will be created in.
781784
// Must match a key in the FailureDomains map stored on the cluster object.
782785
// +optional
786+
// +kubebuilder:validation:MaxItems=100
783787
FailureDomains []string `json:"failureDomains,omitempty"`
784788

785789
// nodeDrainTimeout is the total amount of time that the controller will spend on draining a node.
@@ -847,6 +851,7 @@ type ControlPlaneVariables struct {
847851
// +optional
848852
// +listType=map
849853
// +listMapKey=name
854+
// +kubebuilder:validation:MaxItems=1000
850855
Overrides []ClusterVariable `json:"overrides,omitempty"`
851856
}
852857

@@ -856,6 +861,7 @@ type MachineDeploymentVariables struct {
856861
// +optional
857862
// +listType=map
858863
// +listMapKey=name
864+
// +kubebuilder:validation:MaxItems=1000
859865
Overrides []ClusterVariable `json:"overrides,omitempty"`
860866
}
861867

@@ -865,6 +871,7 @@ type MachinePoolVariables struct {
865871
// +optional
866872
// +listType=map
867873
// +listMapKey=name
874+
// +kubebuilder:validation:MaxItems=1000
868875
Overrides []ClusterVariable `json:"overrides,omitempty"`
869876
}
870877

@@ -900,6 +907,7 @@ type ClusterNetwork struct {
900907
// NetworkRanges represents ranges of network addresses.
901908
type NetworkRanges struct {
902909
// cidrBlocks is a list of CIDR blocks.
910+
// +kubebuilder:validation:MaxItems=100
903911
CIDRBlocks []string `json:"cidrBlocks"`
904912
}
905913

api/v1beta1/clusterclass_types.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,14 @@ type ClusterClassSpec struct {
122122
// variables defines the variables which can be configured
123123
// in the Cluster topology and are then used in patches.
124124
// +optional
125+
// +kubebuilder:validation:MaxItems=1000
125126
Variables []ClusterClassVariable `json:"variables,omitempty"`
126127

127128
// patches defines the patches which are applied to customize
128129
// referenced templates of a ClusterClass.
129130
// Note: Patches will be applied in the order of the array.
130131
// +optional
132+
// +kubebuilder:validation:MaxItems=1000
131133
Patches []ClusterClassPatch `json:"patches,omitempty"`
132134
}
133135

@@ -235,13 +237,15 @@ type WorkersClass struct {
235237
// +optional
236238
// +listType=map
237239
// +listMapKey=class
240+
// +kubebuilder:validation:MaxItems=100
238241
MachineDeployments []MachineDeploymentClass `json:"machineDeployments,omitempty"`
239242

240243
// machinePools is a list of machine pool classes that can be used to create
241244
// a set of worker nodes.
242245
// +optional
243246
// +listType=map
244247
// +listMapKey=class
248+
// +kubebuilder:validation:MaxItems=100
245249
MachinePools []MachinePoolClass `json:"machinePools,omitempty"`
246250
}
247251

@@ -356,6 +360,7 @@ type MachineHealthCheckClass struct {
356360
// logical OR, i.e. if any of the conditions is met, the node is unhealthy.
357361
//
358362
// +optional
363+
// +kubebuilder:validation:MaxItems=100
359364
UnhealthyConditions []UnhealthyCondition `json:"unhealthyConditions,omitempty"`
360365

361366
// maxUnhealthy specifies the maximum number of unhealthy machines allowed.
@@ -415,6 +420,7 @@ type MachinePoolClass struct {
415420
// Must match a key in the FailureDomains map stored on the cluster object.
416421
// NOTE: This value can be overridden while defining a Cluster.Topology using this MachinePoolClass.
417422
// +optional
423+
// +kubebuilder:validation:MaxItems=100
418424
FailureDomains []string `json:"failureDomains,omitempty"`
419425

420426
// namingStrategy allows changing the naming pattern used when creating the MachinePool.
@@ -586,6 +592,7 @@ type JSONSchemaProps struct {
586592
// required specifies which fields of an object are required.
587593
// NOTE: Can only be set if type is object.
588594
// +optional
595+
// +kubebuilder:validation:MaxItems=1000
589596
Required []string `json:"required,omitempty"`
590597

591598
// items specifies fields of an array.
@@ -667,6 +674,7 @@ type JSONSchemaProps struct {
667674
// enum is the list of valid values of the variable.
668675
// NOTE: Can be set for all types.
669676
// +optional
677+
// +kubebuilder:validation:MaxItems=100
670678
Enum []apiextensionsv1.JSON `json:"enum,omitempty"`
671679

672680
// default is the default value of the variable.
@@ -678,6 +686,7 @@ type JSONSchemaProps struct {
678686
// +optional
679687
// +listType=map
680688
// +listMapKey=rule
689+
// +kubebuilder:validation:MaxItems=100
681690
XValidations []ValidationRule `json:"x-kubernetes-validations,omitempty"`
682691

683692
// x-metadata is the metadata of a variable or a nested field within a variable.
@@ -879,6 +888,7 @@ type ClusterClassPatch struct {
879888
// Note: Patches will be applied in the order of the array.
880889
// Note: Exactly one of Definitions or External must be set.
881890
// +optional
891+
// +kubebuilder:validation:MaxItems=100
882892
Definitions []PatchDefinition `json:"definitions,omitempty"`
883893

884894
// external defines an external patch.
@@ -895,6 +905,7 @@ type PatchDefinition struct {
895905
// jsonPatches defines the patches which should be applied on the templates
896906
// matching the selector.
897907
// Note: Patches will be applied in the order of the array.
908+
// +kubebuilder:validation:MaxItems=100
898909
JSONPatches []JSONPatch `json:"jsonPatches"`
899910
}
900911

@@ -944,6 +955,7 @@ type PatchSelectorMatch struct {
944955
type PatchSelectorMatchMachineDeploymentClass struct {
945956
// names selects templates by class names.
946957
// +optional
958+
// +kubebuilder:validation:MaxItems=100
947959
Names []string `json:"names,omitempty"`
948960
}
949961

@@ -952,6 +964,7 @@ type PatchSelectorMatchMachineDeploymentClass struct {
952964
type PatchSelectorMatchMachinePoolClass struct {
953965
// names selects templates by class names.
954966
// +optional
967+
// +kubebuilder:validation:MaxItems=100
955968
Names []string `json:"names,omitempty"`
956969
}
957970

@@ -1035,6 +1048,7 @@ type LocalObjectTemplate struct {
10351048
type ClusterClassStatus struct {
10361049
// variables is a list of ClusterClassStatusVariable that are defined for the ClusterClass.
10371050
// +optional
1051+
// +kubebuilder:validation:MaxItems=1000
10381052
Variables []ClusterClassStatusVariable `json:"variables,omitempty"`
10391053

10401054
// conditions defines current observed state of the ClusterClass.
@@ -1072,6 +1086,7 @@ type ClusterClassStatusVariable struct {
10721086
DefinitionsConflict bool `json:"definitionsConflict"`
10731087

10741088
// definitions is a list of definitions for a variable.
1089+
// +kubebuilder:validation:MaxItems=100
10751090
Definitions []ClusterClassStatusVariableDefinition `json:"definitions"`
10761091
}
10771092

api/v1beta1/machinehealthcheck_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type MachineHealthCheckSpec struct {
6363
// logical OR, i.e. if any of the conditions is met, the node is unhealthy.
6464
//
6565
// +optional
66+
// +kubebuilder:validation:MaxItems=100
6667
UnhealthyConditions []UnhealthyCondition `json:"unhealthyConditions,omitempty"`
6768

6869
// maxUnhealthy specifies the maximum number of unhealthy machines allowed.
@@ -165,6 +166,7 @@ type MachineHealthCheckStatus struct {
165166

166167
// targets shows the current list of machines the machine health check is watching
167168
// +optional
169+
// +kubebuilder:validation:MaxItems=10000
168170
Targets []string `json:"targets,omitempty"`
169171

170172
// conditions defines current service state of the MachineHealthCheck.

bootstrap/kubeadm/api/v1beta1/kubeadm_types.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type InitConfiguration struct {
3838
// bootstrapTokens is respected at `kubeadm init` time and describes a set of Bootstrap Tokens to create.
3939
// This information IS NOT uploaded to the kubeadm cluster configmap, partly because of its sensitive nature
4040
// +optional
41+
// +kubebuilder:validation:MaxItems=100
4142
BootstrapTokens []BootstrapToken `json:"bootstrapTokens,omitempty"`
4243

4344
// nodeRegistration holds fields that relate to registering the new control-plane node to the cluster.
@@ -59,6 +60,7 @@ type InitConfiguration struct {
5960
// The list of phases can be obtained with the "kubeadm init --help" command.
6061
// This option takes effect only on Kubernetes >=1.22.0.
6162
// +optional
63+
// +kubebuilder:validation:MaxItems=50
6264
SkipPhases []string `json:"skipPhases,omitempty"`
6365

6466
// patches contains options related to applying patches to components deployed by kubeadm during
@@ -156,12 +158,14 @@ type ControlPlaneComponent struct {
156158

157159
// extraVolumes is an extra set of host volumes, mounted to the control plane component.
158160
// +optional
161+
// +kubebuilder:validation:MaxItems=100
159162
ExtraVolumes []HostPathMount `json:"extraVolumes,omitempty"`
160163

161164
// extraEnvs is an extra set of environment variables to pass to the control plane component.
162165
// Environment variables passed using ExtraEnvs will override any existing environment variables, or *_proxy environment variables that kubeadm adds by default.
163166
// This option takes effect only on Kubernetes >=1.31.0.
164167
// +optional
168+
// +kubebuilder:validation:MaxItems=100
165169
ExtraEnvs []EnvVar `json:"extraEnvs,omitempty"`
166170
}
167171

@@ -171,6 +175,7 @@ type APIServer struct {
171175

172176
// certSANs sets extra Subject Alternative Names for the API Server signing cert.
173177
// +optional
178+
// +kubebuilder:validation:MaxItems=100
174179
CertSANs []string `json:"certSANs,omitempty"`
175180

176181
// timeoutForControlPlane controls the timeout that we use for API server to appear
@@ -245,6 +250,7 @@ type NodeRegistrationOptions struct {
245250
// it will be defaulted to []v1.Taint{'node-role.kubernetes.io/master=""'}. If you don't want to taint your control-plane node, set this field to an
246251
// empty slice, i.e. `taints: []` in the YAML file. This field is solely used for Node registration.
247252
// +optional
253+
// +kubebuilder:validation:MaxItems=100
248254
Taints []corev1.Taint `json:"taints,omitempty"`
249255

250256
// kubeletExtraArgs passes through extra arguments to the kubelet. The arguments here are passed to the kubelet command line via the environment file
@@ -255,6 +261,7 @@ type NodeRegistrationOptions struct {
255261

256262
// ignorePreflightErrors provides a slice of pre-flight errors to be ignored when the current node is registered.
257263
// +optional
264+
// +kubebuilder:validation:MaxItems=50
258265
IgnorePreflightErrors []string `json:"ignorePreflightErrors,omitempty"`
259266

260267
// imagePullPolicy specifies the policy for image pulling
@@ -361,10 +368,12 @@ type BootstrapToken struct {
361368
// usages describes the ways in which this token can be used. Can by default be used
362369
// for establishing bidirectional trust, but that can be changed here.
363370
// +optional
371+
// +kubebuilder:validation:MaxItems=100
364372
Usages []string `json:"usages,omitempty"`
365373
// groups specifies the extra groups that this token will authenticate as when/if
366374
// used for authentication
367375
// +optional
376+
// +kubebuilder:validation:MaxItems=100
368377
Groups []string `json:"groups,omitempty"`
369378
}
370379

@@ -401,20 +410,25 @@ type LocalEtcd struct {
401410
// Environment variables passed using ExtraEnvs will override any existing environment variables, or *_proxy environment variables that kubeadm adds by default.
402411
// This option takes effect only on Kubernetes >=1.31.0.
403412
// +optional
413+
// +kubebuilder:validation:MaxItems=100
404414
ExtraEnvs []EnvVar `json:"extraEnvs,omitempty"`
405415

406416
// serverCertSANs sets extra Subject Alternative Names for the etcd server signing cert.
407417
// +optional
418+
// +kubebuilder:validation:MaxItems=100
408419
ServerCertSANs []string `json:"serverCertSANs,omitempty"`
420+
409421
// peerCertSANs sets extra Subject Alternative Names for the etcd peer signing cert.
410422
// +optional
423+
// +kubebuilder:validation:MaxItems=100
411424
PeerCertSANs []string `json:"peerCertSANs,omitempty"`
412425
}
413426

414427
// ExternalEtcd describes an external etcd cluster.
415428
// Kubeadm has no knowledge of where certificate files live and they must be supplied.
416429
type ExternalEtcd struct {
417430
// endpoints of etcd members. Required for ExternalEtcd.
431+
// +kubebuilder:validation:MaxItems=50
418432
Endpoints []string `json:"endpoints"`
419433

420434
// caFile is an SSL Certificate Authority file used to secure etcd communication.
@@ -463,6 +477,7 @@ type JoinConfiguration struct {
463477
// The list of phases can be obtained with the "kubeadm init --help" command.
464478
// This option takes effect only on Kubernetes >=1.22.0.
465479
// +optional
480+
// +kubebuilder:validation:MaxItems=50
466481
SkipPhases []string `json:"skipPhases,omitempty"`
467482

468483
// patches contains options related to applying patches to components deployed by kubeadm during
@@ -520,6 +535,7 @@ type BootstrapTokenDiscovery struct {
520535
// ASN.1. These hashes can be calculated using, for example, OpenSSL:
521536
// openssl x509 -pubkey -in ca.crt openssl rsa -pubin -outform der 2>&/dev/null | openssl dgst -sha256 -hex
522537
// +optional
538+
// +kubebuilder:validation:MaxItems=100
523539
CACertHashes []string `json:"caCertHashes,omitempty"`
524540

525541
// unsafeSkipCAVerification allows token-based discovery
@@ -636,12 +652,14 @@ type KubeConfigAuthExec struct {
636652

637653
// args is the arguments to pass to the command when executing it.
638654
// +optional
655+
// +kubebuilder:validation:MaxItems=100
639656
Args []string `json:"args,omitempty"`
640657

641658
// env defines additional environment variables to expose to the process. These
642659
// are unioned with the host's environment, as well as variables client-go uses
643660
// to pass argument to the plugin.
644661
// +optional
662+
// +kubebuilder:validation:MaxItems=100
645663
Env []KubeConfigAuthExecEnv `json:"env,omitempty"`
646664

647665
// apiVersion is preferred input version of the ExecInfo. The returned ExecCredentials MUST use

0 commit comments

Comments
 (0)