Skip to content

Commit 2157a8d

Browse files
⚠️ Add additional MinProperties & MinItems validation across multiple APIs (#12538)
* More validation rules * Fix linter
1 parent 04a4841 commit 2157a8d

File tree

13 files changed

+254
-32
lines changed

13 files changed

+254
-32
lines changed

api/addons/v1beta2/clusterresourceset_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ type ClusterResourceSetSpec struct {
6161
ClusterSelector metav1.LabelSelector `json:"clusterSelector"`
6262

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

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

api/bootstrap/kubeadm/v1beta2/kubeadm_types.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type InitConfiguration struct {
7878
// This information IS NOT uploaded to the kubeadm cluster configmap, partly because of its sensitive nature
7979
// +optional
8080
// +listType=atomic
81+
// +kubebuilder:validation:MinItems=1
8182
// +kubebuilder:validation:MaxItems=100
8283
BootstrapTokens []BootstrapToken `json:"bootstrapTokens,omitempty"`
8384

@@ -101,6 +102,7 @@ type InitConfiguration struct {
101102
// This option takes effect only on Kubernetes >=1.22.0.
102103
// +optional
103104
// +listType=atomic
105+
// +kubebuilder:validation:MinItems=1
104106
// +kubebuilder:validation:MaxItems=50
105107
// +kubebuilder:validation:items:MinLength=1
106108
// +kubebuilder:validation:items:MaxLength=256
@@ -202,6 +204,7 @@ type APIServer struct {
202204
// extraVolumes is an extra set of host volumes, mounted to the control plane component.
203205
// +optional
204206
// +listType=atomic
207+
// +kubebuilder:validation:MinItems=1
205208
// +kubebuilder:validation:MaxItems=100
206209
ExtraVolumes []HostPathMount `json:"extraVolumes,omitempty"`
207210

@@ -210,12 +213,14 @@ type APIServer struct {
210213
// This option takes effect only on Kubernetes >=1.31.0.
211214
// +optional
212215
// +listType=atomic
216+
// +kubebuilder:validation:MinItems=1
213217
// +kubebuilder:validation:MaxItems=100
214218
ExtraEnvs []EnvVar `json:"extraEnvs,omitempty"`
215219

216220
// certSANs sets extra Subject Alternative Names for the API Server signing cert.
217221
// +optional
218222
// +listType=atomic
223+
// +kubebuilder:validation:MinItems=1
219224
// +kubebuilder:validation:MaxItems=100
220225
// +kubebuilder:validation:items:MinLength=1
221226
// +kubebuilder:validation:items:MaxLength=253
@@ -240,6 +245,7 @@ type ControllerManager struct {
240245
// extraVolumes is an extra set of host volumes, mounted to the control plane component.
241246
// +optional
242247
// +listType=atomic
248+
// +kubebuilder:validation:MinItems=1
243249
// +kubebuilder:validation:MaxItems=100
244250
ExtraVolumes []HostPathMount `json:"extraVolumes,omitempty"`
245251

@@ -248,6 +254,7 @@ type ControllerManager struct {
248254
// This option takes effect only on Kubernetes >=1.31.0.
249255
// +optional
250256
// +listType=atomic
257+
// +kubebuilder:validation:MinItems=1
251258
// +kubebuilder:validation:MaxItems=100
252259
ExtraEnvs []EnvVar `json:"extraEnvs,omitempty"`
253260
}
@@ -270,6 +277,7 @@ type Scheduler struct {
270277
// extraVolumes is an extra set of host volumes, mounted to the control plane component.
271278
// +optional
272279
// +listType=atomic
280+
// +kubebuilder:validation:MinItems=1
273281
// +kubebuilder:validation:MaxItems=100
274282
ExtraVolumes []HostPathMount `json:"extraVolumes,omitempty"`
275283

@@ -278,6 +286,7 @@ type Scheduler struct {
278286
// This option takes effect only on Kubernetes >=1.31.0.
279287
// +optional
280288
// +listType=atomic
289+
// +kubebuilder:validation:MinItems=1
281290
// +kubebuilder:validation:MaxItems=100
282291
ExtraEnvs []EnvVar `json:"extraEnvs,omitempty"`
283292
}
@@ -367,6 +376,7 @@ type NodeRegistrationOptions struct {
367376
// Value 'all' ignores errors from all checks.
368377
// +optional
369378
// +listType=atomic
379+
// +kubebuilder:validation:MinItems=1
370380
// +kubebuilder:validation:MaxItems=50
371381
// +kubebuilder:validation:items:MinLength=1
372382
// +kubebuilder:validation:items:MaxLength=512
@@ -416,6 +426,7 @@ type BootstrapToken struct {
416426
// for establishing bidirectional trust, but that can be changed here.
417427
// +optional
418428
// +listType=atomic
429+
// +kubebuilder:validation:MinItems=1
419430
// +kubebuilder:validation:MaxItems=100
420431
// +kubebuilder:validation:items:MinLength=1
421432
// +kubebuilder:validation:items:MaxLength=256
@@ -425,6 +436,7 @@ type BootstrapToken struct {
425436
// used for authentication
426437
// +optional
427438
// +listType=atomic
439+
// +kubebuilder:validation:MinItems=1
428440
// +kubebuilder:validation:MaxItems=100
429441
// +kubebuilder:validation:items:MinLength=1
430442
// +kubebuilder:validation:items:MaxLength=256
@@ -446,6 +458,7 @@ type Etcd struct {
446458
}
447459

448460
// LocalEtcd describes that kubeadm should run an etcd cluster locally.
461+
// +kubebuilder:validation:MinProperties=1
449462
type LocalEtcd struct {
450463
// ImageMeta allows to customize the container used for etcd
451464
ImageMeta `json:",inline"`
@@ -474,12 +487,14 @@ type LocalEtcd struct {
474487
// This option takes effect only on Kubernetes >=1.31.0.
475488
// +optional
476489
// +listType=atomic
490+
// +kubebuilder:validation:MinItems=1
477491
// +kubebuilder:validation:MaxItems=100
478492
ExtraEnvs []EnvVar `json:"extraEnvs,omitempty"`
479493

480494
// serverCertSANs sets extra Subject Alternative Names for the etcd server signing cert.
481495
// +optional
482496
// +listType=atomic
497+
// +kubebuilder:validation:MinItems=1
483498
// +kubebuilder:validation:MaxItems=100
484499
// +kubebuilder:validation:items:MinLength=1
485500
// +kubebuilder:validation:items:MaxLength=253
@@ -488,6 +503,7 @@ type LocalEtcd struct {
488503
// peerCertSANs sets extra Subject Alternative Names for the etcd peer signing cert.
489504
// +optional
490505
// +listType=atomic
506+
// +kubebuilder:validation:MinItems=1
491507
// +kubebuilder:validation:MaxItems=100
492508
// +kubebuilder:validation:items:MinLength=1
493509
// +kubebuilder:validation:items:MaxLength=253
@@ -560,6 +576,7 @@ type JoinConfiguration struct {
560576
// This option takes effect only on Kubernetes >=1.22.0.
561577
// +optional
562578
// +listType=atomic
579+
// +kubebuilder:validation:MinItems=1
563580
// +kubebuilder:validation:MaxItems=50
564581
// +kubebuilder:validation:items:MinLength=1
565582
// +kubebuilder:validation:items:MaxLength=256
@@ -605,6 +622,7 @@ type Discovery struct {
605622
}
606623

607624
// BootstrapTokenDiscovery is used to set the options for bootstrap token based discovery.
625+
// +kubebuilder:validation:MinProperties=1
608626
type BootstrapTokenDiscovery struct {
609627
// token is a token used to validate cluster information
610628
// fetched from the control-plane.
@@ -629,6 +647,7 @@ type BootstrapTokenDiscovery struct {
629647
// openssl x509 -pubkey -in ca.crt openssl rsa -pubin -outform der 2>&/dev/null | openssl dgst -sha256 -hex
630648
// +optional
631649
// +listType=atomic
650+
// +kubebuilder:validation:MinItems=1
632651
// +kubebuilder:validation:MaxItems=100
633652
// +kubebuilder:validation:items:MinLength=1
634653
// +kubebuilder:validation:items:MaxLength=512
@@ -678,6 +697,7 @@ type FileDiscoveryKubeConfig struct {
678697
// KubeConfigCluster contains information about how to communicate with a kubernetes cluster.
679698
//
680699
// Adapted from clientcmdv1.Cluster.
700+
// +kubebuilder:validation:MinProperties=1
681701
type KubeConfigCluster struct {
682702
// server is the address of the kubernetes cluster (https://hostname:port).
683703
//
@@ -729,6 +749,7 @@ type KubeConfigCluster struct {
729749
// Either authProvider or exec must be filled.
730750
//
731751
// Adapted from clientcmdv1.AuthInfo.
752+
// +kubebuilder:validation:MinProperties=1
732753
type KubeConfigUser struct {
733754
// authProvider specifies a custom authentication plugin for the kubernetes cluster.
734755
// +optional
@@ -767,6 +788,7 @@ type KubeConfigAuthExec struct {
767788
// args is the arguments to pass to the command when executing it.
768789
// +optional
769790
// +listType=atomic
791+
// +kubebuilder:validation:MinItems=1
770792
// +kubebuilder:validation:MaxItems=100
771793
// +kubebuilder:validation:items:MinLength=1
772794
// +kubebuilder:validation:items:MaxLength=512
@@ -777,6 +799,7 @@ type KubeConfigAuthExec struct {
777799
// to pass argument to the plugin.
778800
// +optional
779801
// +listType=atomic
802+
// +kubebuilder:validation:MinItems=1
780803
// +kubebuilder:validation:MaxItems=100
781804
Env []KubeConfigAuthExecEnv `json:"env,omitempty"`
782805

@@ -897,6 +920,7 @@ func NewBootstrapTokenString(token string) (*BootstrapTokenString, error) {
897920
}
898921

899922
// Patches contains options related to applying patches to components deployed by kubeadm.
923+
// +kubebuilder:validation:MinProperties=1
900924
type Patches struct {
901925
// directory is a path to a directory that contains files named "target[suffix][+patchtype].extension".
902926
// For example, "kube-apiserver0+merge.yaml" or just "etcd.json". "target" can be one of
@@ -935,6 +959,7 @@ type EnvVar struct {
935959
}
936960

937961
// Timeouts holds various timeouts that apply to kubeadm commands.
962+
// +kubebuilder:validation:MinProperties=1
938963
type Timeouts struct {
939964
// controlPlaneComponentHealthCheckSeconds is the amount of time to wait for a control plane
940965
// component, such as the API server, to be healthy during "kubeadm init" and "kubeadm join".

api/bootstrap/kubeadm/v1beta2/kubeadmconfig_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type KubeadmConfigSpec struct {
6767
// files specifies extra files to be passed to user_data upon creation.
6868
// +optional
6969
// +listType=atomic
70+
// +kubebuilder:validation:MinItems=1
7071
// +kubebuilder:validation:MaxItems=200
7172
Files []File `json:"files,omitempty"`
7273

@@ -77,6 +78,7 @@ type KubeadmConfigSpec struct {
7778
// mounts specifies a list of mount points to be setup.
7879
// +optional
7980
// +listType=atomic
81+
// +kubebuilder:validation:MinItems=1
8082
// +kubebuilder:validation:MaxItems=100
8183
Mounts []MountPoints `json:"mounts,omitempty"`
8284

@@ -85,6 +87,7 @@ type KubeadmConfigSpec struct {
8587
// once. This is typically run in the cloud-init.service systemd unit. This has no effect in Ignition.
8688
// +optional
8789
// +listType=atomic
90+
// +kubebuilder:validation:MinItems=1
8891
// +kubebuilder:validation:MaxItems=1000
8992
// +kubebuilder:validation:items:MinLength=1
9093
// +kubebuilder:validation:items:MaxLength=10240
@@ -95,6 +98,7 @@ type KubeadmConfigSpec struct {
9598
// the cloud-final.service systemd unit. In Ignition, this is prepended to /etc/kubeadm.sh.
9699
// +optional
97100
// +listType=atomic
101+
// +kubebuilder:validation:MinItems=1
98102
// +kubebuilder:validation:MaxItems=1000
99103
// +kubebuilder:validation:items:MinLength=1
100104
// +kubebuilder:validation:items:MaxLength=10240
@@ -105,6 +109,7 @@ type KubeadmConfigSpec struct {
105109
// the cloud-final.service systemd unit. In Ignition, this is appended to /etc/kubeadm.sh.
106110
// +optional
107111
// +listType=atomic
112+
// +kubebuilder:validation:MinItems=1
108113
// +kubebuilder:validation:MaxItems=1000
109114
// +kubebuilder:validation:items:MinLength=1
110115
// +kubebuilder:validation:items:MaxLength=10240
@@ -113,6 +118,7 @@ type KubeadmConfigSpec struct {
113118
// users specifies extra users to add
114119
// +optional
115120
// +listType=atomic
121+
// +kubebuilder:validation:MinItems=1
116122
// +kubebuilder:validation:MaxItems=100
117123
Users []User `json:"users,omitempty"`
118124

@@ -411,6 +417,7 @@ func (c *KubeadmConfigSpec) validateIgnition(pathPrefix *field.Path) field.Error
411417
}
412418

413419
// IgnitionSpec contains Ignition specific configuration.
420+
// +kubebuilder:validation:MinProperties=1
414421
type IgnitionSpec struct {
415422
// containerLinuxConfig contains CLC specific configuration.
416423
// +optional
@@ -420,6 +427,7 @@ type IgnitionSpec struct {
420427
// ContainerLinuxConfig contains CLC-specific configuration.
421428
//
422429
// We use a structured type here to allow adding additional fields, for example 'version'.
430+
// +kubebuilder:validation:MinProperties=1
423431
type ContainerLinuxConfig struct {
424432
// additionalConfig contains additional configuration to be merged with the Ignition
425433
// configuration generated by the bootstrapper controller. More info: https://coreos.github.io/ignition/operator-notes/#config-merging
@@ -762,6 +770,7 @@ type User struct {
762770
}
763771

764772
// NTP defines input for generated ntp in cloud-init.
773+
// +kubebuilder:validation:MinProperties=1
765774
type NTP struct {
766775
// servers specifies which NTP servers to use
767776
// +optional
@@ -777,6 +786,7 @@ type NTP struct {
777786
}
778787

779788
// DiskSetup defines input for generated disk_setup and fs_setup in cloud-init.
789+
// +kubebuilder:validation:MinProperties=1
780790
type DiskSetup struct {
781791
// partitions specifies the list of the partitions to setup.
782792
// +optional
@@ -863,6 +873,8 @@ type Filesystem struct {
863873
}
864874

865875
// MountPoints defines input for generated mounts in cloud-init.
876+
// +kubebuilder:validation:MinItems=1
877+
// +kubebuilder:validation:MaxItems=100
866878
// +kubebuilder:validation:items:MinLength=1
867879
// +kubebuilder:validation:items:MaxLength=512
868880
type MountPoints []string

api/core/v1beta2/clusterclass_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ type ClusterClassSpec struct {
123123
// in the Cluster topology and are then used in patches.
124124
// +optional
125125
// +listType=atomic
126+
// +kubebuilder:validation:MinItems=1
126127
// +kubebuilder:validation:MaxItems=1000
127128
Variables []ClusterClassVariable `json:"variables,omitempty"`
128129

@@ -131,6 +132,7 @@ type ClusterClassSpec struct {
131132
// Note: Patches will be applied in the order of the array.
132133
// +optional
133134
// +listType=atomic
135+
// +kubebuilder:validation:MinItems=1
134136
// +kubebuilder:validation:MaxItems=1000
135137
Patches []ClusterClassPatch `json:"patches,omitempty"`
136138
}
@@ -374,6 +376,7 @@ type WorkersClass struct {
374376
// +optional
375377
// +listType=map
376378
// +listMapKey=class
379+
// +kubebuilder:validation:MinItems=1
377380
// +kubebuilder:validation:MaxItems=100
378381
MachineDeployments []MachineDeploymentClass `json:"machineDeployments,omitempty"`
379382

@@ -382,6 +385,7 @@ type WorkersClass struct {
382385
// +optional
383386
// +listType=map
384387
// +listMapKey=class
388+
// +kubebuilder:validation:MinItems=1
385389
// +kubebuilder:validation:MaxItems=100
386390
MachinePools []MachinePoolClass `json:"machinePools,omitempty"`
387391
}
@@ -852,6 +856,7 @@ type VariableSchema struct {
852856
// JSONSchemaProps is a JSON-Schema following Specification Draft 4 (http://json-schema.org/).
853857
// This struct has been initially copied from apiextensionsv1.JSONSchemaProps, but all fields
854858
// which are not supported in CAPI have been removed.
859+
// +kubebuilder:validation:MinProperties=1
855860
type JSONSchemaProps struct {
856861
// description is a human-readable description of this variable.
857862
// +optional
@@ -903,6 +908,7 @@ type JSONSchemaProps struct {
903908
// NOTE: Can only be set if type is object.
904909
// +optional
905910
// +listType=atomic
911+
// +kubebuilder:validation:MinItems=1
906912
// +kubebuilder:validation:MaxItems=1000
907913
// +kubebuilder:validation:items:MinLength=1
908914
// +kubebuilder:validation:items:MaxLength=256
@@ -1004,6 +1010,7 @@ type JSONSchemaProps struct {
10041010
// +optional
10051011
// +listType=map
10061012
// +listMapKey=rule
1013+
// +kubebuilder:validation:MinItems=1
10071014
// +kubebuilder:validation:MaxItems=100
10081015
XValidations []ValidationRule `json:"x-kubernetes-validations,omitempty"`
10091016

@@ -1063,6 +1070,7 @@ type JSONSchemaProps struct {
10631070

10641071
// VariableSchemaMetadata is the metadata of a variable or a nested field within a variable.
10651072
// It can be used to add additional data for higher level tools.
1073+
// +kubebuilder:validation:MinProperties=1
10661074
type VariableSchemaMetadata struct {
10671075
// labels is a map of string keys and values that can be used to organize and categorize
10681076
// (scope and select) variables.

0 commit comments

Comments
 (0)