Skip to content

Commit 220c291

Browse files
committed
Add MaxLength markers to API fields
Signed-off-by: Stefan Büringer buringerst@vmware.com
1 parent 4bc5347 commit 220c291

30 files changed

+316
-6
lines changed

.golangci-kal.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ issues:
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
8581
# controller-gen does not allow to add MaxItems to Schemaless fields
8682
- path: "api/v1beta1/*"
8783
text: "maxlength: field (AllOf|OneOf|AnyOf) must have a maximum items, add kubebuilder:validation:MaxItems marker"

api/v1beta1/cluster_types.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ type ClusterAvailabilityGate struct {
519519
// Topology encapsulates the information of the managed resources.
520520
type Topology struct {
521521
// class is the name of the ClusterClass object to create the topology.
522+
// +kubebuilder:validation:MinLength=1
523+
// +kubebuilder:validation:MaxLength=253
522524
Class string `json:"class"`
523525

524526
// classNamespace is the namespace of the ClusterClass object to create the topology.
@@ -531,6 +533,7 @@ type Topology struct {
531533
ClassNamespace string `json:"classNamespace,omitempty"`
532534

533535
// version is the Kubernetes version of the cluster.
536+
// +kubebuilder:validation:MaxLength=256
534537
Version string `json:"version"`
535538

536539
// rolloutAfter performs a rollout of the entire cluster one component at a time,
@@ -647,17 +650,20 @@ type MachineDeploymentTopology struct {
647650
// class is the name of the MachineDeploymentClass used to create the set of worker nodes.
648651
// This should match one of the deployment classes defined in the ClusterClass object
649652
// mentioned in the `Cluster.Spec.Class` field.
653+
// +kubebuilder:validation:MaxLength=256
650654
Class string `json:"class"`
651655

652656
// name is the unique identifier for this MachineDeploymentTopology.
653657
// The value is used with other unique identifiers to create a MachineDeployment's Name
654658
// (e.g. cluster's name, etc). In case the name is greater than the allowed maximum length,
655659
// the values are hashed together.
660+
// +kubebuilder:validation:MaxLength=256
656661
Name string `json:"name"`
657662

658663
// failureDomain is the failure domain the machines will be created in.
659664
// Must match a key in the FailureDomains map stored on the cluster object.
660665
// +optional
666+
// +kubebuilder:validation:MaxLength=256
661667
FailureDomain *string `json:"failureDomain,omitempty"`
662668

663669
// replicas is the number of worker nodes belonging to this set.
@@ -750,18 +756,21 @@ type MachinePoolTopology struct {
750756
// class is the name of the MachinePoolClass used to create the pool of worker nodes.
751757
// This should match one of the deployment classes defined in the ClusterClass object
752758
// mentioned in the `Cluster.Spec.Class` field.
759+
// +kubebuilder:validation:MaxLength=256
753760
Class string `json:"class"`
754761

755762
// name is the unique identifier for this MachinePoolTopology.
756763
// The value is used with other unique identifiers to create a MachinePool's Name
757764
// (e.g. cluster's name, etc). In case the name is greater than the allowed maximum length,
758765
// the values are hashed together.
766+
// +kubebuilder:validation:MaxLength=256
759767
Name string `json:"name"`
760768

761769
// failureDomains is the list of failure domains the machine pool will be created in.
762770
// Must match a key in the FailureDomains map stored on the cluster object.
763771
// +optional
764772
// +kubebuilder:validation:MaxItems=100
773+
// +kubebuilder:validation:items:MaxLength=256
765774
FailureDomains []string `json:"failureDomains,omitempty"`
766775

767776
// nodeDrainTimeout is the total amount of time that the controller will spend on draining a node.
@@ -804,13 +813,15 @@ type MachinePoolTopology struct {
804813
// Variable definition in the ClusterClass `status` variables.
805814
type ClusterVariable struct {
806815
// name of the variable.
816+
// +kubebuilder:validation:MaxLength=256
807817
Name string `json:"name"`
808818

809819
// definitionFrom specifies where the definition of this Variable is from.
810820
//
811821
// Deprecated: This field is deprecated, must not be set anymore and is going to be removed in the next apiVersion.
812822
//
813823
// +optional
824+
// +kubebuilder:validation:MaxLength=256
814825
DefinitionFrom string `json:"definitionFrom,omitempty"`
815826

816827
// value of the variable.
@@ -875,6 +886,7 @@ type ClusterNetwork struct {
875886

876887
// serviceDomain is the domain name for services.
877888
// +optional
889+
// +kubebuilder:validation:MaxLength=253
878890
ServiceDomain string `json:"serviceDomain,omitempty"`
879891
}
880892

@@ -886,6 +898,7 @@ type ClusterNetwork struct {
886898
type NetworkRanges struct {
887899
// cidrBlocks is a list of CIDR blocks.
888900
// +kubebuilder:validation:MaxItems=100
901+
// +kubebuilder:validation:items:MaxLength=256
889902
CIDRBlocks []string `json:"cidrBlocks"`
890903
}
891904

@@ -921,11 +934,13 @@ type ClusterStatus struct {
921934
// Deprecated: This field is deprecated and is going to be removed in the next apiVersion. Please see https://github.com/kubernetes-sigs/cluster-api/blob/main/docs/proposals/20240916-improve-status-in-CAPI-resources.md for more details.
922935
//
923936
// +optional
937+
// +kubebuilder:validation:MaxLength=10240
924938
FailureMessage *string `json:"failureMessage,omitempty"`
925939

926940
// phase represents the current phase of cluster actuation.
927941
// E.g. Pending, Running, Terminating, Failed etc.
928942
// +optional
943+
// +kubebuilder:validation:MaxLength=50
929944
Phase string `json:"phase,omitempty"`
930945

931946
// infrastructureReady is the state of the infrastructure provider.
@@ -1051,6 +1066,7 @@ func (c *ClusterStatus) GetTypedPhase() ClusterPhase {
10511066
// APIEndpoint represents a reachable Kubernetes API endpoint.
10521067
type APIEndpoint struct {
10531068
// host is the hostname on which the API server is serving.
1069+
// +kubebuilder:validation:MaxLength=512
10541070
Host string `json:"host"`
10551071

10561072
// port is the port on which the API server is serving.

api/v1beta1/clusterclass_types.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ type ControlPlaneClassNamingStrategy struct {
214214
// * `.cluster.name`: The name of the cluster object.
215215
// * `.random`: A random alphanumeric string, without vowels, of length 5.
216216
// +optional
217+
// +kubebuilder:validation:MaxLength=512
217218
Template *string `json:"template,omitempty"`
218219
}
219220

@@ -227,6 +228,7 @@ type InfrastructureNamingStrategy struct {
227228
// * `.cluster.name`: The name of the cluster object.
228229
// * `.random`: A random alphanumeric string, without vowels, of length 5.
229230
// +optional
231+
// +kubebuilder:validation:MaxLength=512
230232
Template *string `json:"template,omitempty"`
231233
}
232234

@@ -255,6 +257,7 @@ type MachineDeploymentClass struct {
255257
// class denotes a type of worker node present in the cluster,
256258
// this name MUST be unique within a ClusterClass and can be referenced
257259
// in the Cluster to create a managed MachineDeployment.
260+
// +kubebuilder:validation:MaxLength=256
258261
Class string `json:"class"`
259262

260263
// template is a local struct containing a collection of templates for creation of
@@ -269,6 +272,7 @@ type MachineDeploymentClass struct {
269272
// Must match a key in the FailureDomains map stored on the cluster object.
270273
// NOTE: This value can be overridden while defining a Cluster.Topology using this MachineDeploymentClass.
271274
// +optional
275+
// +kubebuilder:validation:MaxLength=256
272276
FailureDomain *string `json:"failureDomain,omitempty"`
273277

274278
// namingStrategy allows changing the naming pattern used when creating the MachineDeployment.
@@ -350,6 +354,7 @@ type MachineDeploymentClassNamingStrategy struct {
350354
// * `.random`: A random alphanumeric string, without vowels, of length 5.
351355
// * `.machineDeployment.topologyName`: The name of the MachineDeployment topology (Cluster.spec.topology.workers.machineDeployments[].name).
352356
// +optional
357+
// +kubebuilder:validation:MaxLength=512
353358
Template *string `json:"template,omitempty"`
354359
}
355360

@@ -377,6 +382,7 @@ type MachineHealthCheckClass struct {
377382
// (b) there are at most 5 unhealthy machines
378383
// +optional
379384
// +kubebuilder:validation:Pattern=^\[[0-9]+-[0-9]+\]$
385+
// +kubebuilder:validation:MaxLength=32
380386
UnhealthyRange *string `json:"unhealthyRange,omitempty"`
381387

382388
// nodeStartupTimeout allows to set the maximum time for MachineHealthCheck
@@ -410,6 +416,7 @@ type MachinePoolClass struct {
410416
// class denotes a type of machine pool present in the cluster,
411417
// this name MUST be unique within a ClusterClass and can be referenced
412418
// in the Cluster to create a managed MachinePool.
419+
// +kubebuilder:validation:MaxLength=256
413420
Class string `json:"class"`
414421

415422
// template is a local struct containing a collection of templates for creation of
@@ -421,6 +428,7 @@ type MachinePoolClass struct {
421428
// NOTE: This value can be overridden while defining a Cluster.Topology using this MachinePoolClass.
422429
// +optional
423430
// +kubebuilder:validation:MaxItems=100
431+
// +kubebuilder:validation:items:MaxLength=256
424432
FailureDomains []string `json:"failureDomains,omitempty"`
425433

426434
// namingStrategy allows changing the naming pattern used when creating the MachinePool.
@@ -483,6 +491,7 @@ type MachinePoolClassNamingStrategy struct {
483491
// * `.random`: A random alphanumeric string, without vowels, of length 5.
484492
// * `.machinePool.topologyName`: The name of the MachinePool topology (Cluster.spec.topology.workers.machinePools[].name).
485493
// +optional
494+
// +kubebuilder:validation:MaxLength=512
486495
Template *string `json:"template,omitempty"`
487496
}
488497

@@ -495,6 +504,7 @@ func (m MachineHealthCheckClass) IsZero() bool {
495504
// be configured in the Cluster topology and used in patches.
496505
type ClusterClassVariable struct {
497506
// name of the variable.
507+
// +kubebuilder:validation:MaxLength=256
498508
Name string `json:"name"`
499509

500510
// required specifies if the variable is required.
@@ -549,6 +559,7 @@ type VariableSchema struct {
549559
// which are not supported in CAPI have been removed.
550560
type JSONSchemaProps struct {
551561
// description is a human-readable description of this variable.
562+
// +kubebuilder:validation:MaxLength=512
552563
Description string `json:"description,omitempty"`
553564

554565
// example is an example for this variable.
@@ -557,6 +568,7 @@ type JSONSchemaProps struct {
557568
// type is the type of the variable.
558569
// Valid values are: object, array, string, integer, number or boolean.
559570
// +optional
571+
// +kubebuilder:validation:MaxLength=16
560572
Type string `json:"type,omitempty"`
561573

562574
// properties specifies fields of an object.
@@ -593,6 +605,7 @@ type JSONSchemaProps struct {
593605
// NOTE: Can only be set if type is object.
594606
// +optional
595607
// +kubebuilder:validation:MaxItems=1000
608+
// +kubebuilder:validation:items:MaxLength=256
596609
Required []string `json:"required,omitempty"`
597610

598611
// items specifies fields of an array.
@@ -624,6 +637,7 @@ type JSONSchemaProps struct {
624637
// https://github.com/kubernetes/apiextensions-apiserver/blob/master/pkg/apiserver/validation/formats.go
625638
// NOTE: Can only be set if type is string.
626639
// +optional
640+
// +kubebuilder:validation:MaxLength=32
627641
Format string `json:"format,omitempty"`
628642

629643
// maxLength is the max length of a string variable.
@@ -639,6 +653,7 @@ type JSONSchemaProps struct {
639653
// pattern is the regex which a string variable must match.
640654
// NOTE: Can only be set if type is string.
641655
// +optional
656+
// +kubebuilder:validation:MaxLength=512
642657
Pattern string `json:"pattern,omitempty"`
643658

644659
// maximum is the maximum of an integer or number variable.
@@ -809,12 +824,14 @@ type ValidationRule struct {
809824
// skipped if an old value could not be found.
810825
//
811826
// +required
827+
// +kubebuilder:validation:MaxLength=512
812828
Rule string `json:"rule"`
813829
// message represents the message displayed when validation fails. The message is required if the Rule contains
814830
// line breaks. The message must not contain line breaks.
815831
// If unset, the message is "failed rule: {Rule}".
816832
// e.g. "must be a URL with the host matching spec.host"
817833
// +optional
834+
// +kubebuilder:validation:MaxLength=512
818835
Message string `json:"message,omitempty"`
819836
// messageExpression declares a CEL expression that evaluates to the validation failure message that is returned when this rule fails.
820837
// Since messageExpression is used as a failure message, it must evaluate to a string.
@@ -826,6 +843,7 @@ type ValidationRule struct {
826843
// Example:
827844
// "x must be less than max ("+string(self.max)+")"
828845
// +optional
846+
// +kubebuilder:validation:MaxLength=512
829847
MessageExpression string `json:"messageExpression,omitempty"`
830848
// reason provides a machine-readable validation failure reason that is returned to the caller when a request fails this validation rule.
831849
// The currently supported reasons are: "FieldValueInvalid", "FieldValueForbidden", "FieldValueRequired", "FieldValueDuplicate".
@@ -846,6 +864,7 @@ type ValidationRule struct {
846864
// For field name which contains special characters, use `['specialName']` to refer the field name.
847865
// e.g. for attribute `foo.34$` appears in a list `testList`, the fieldPath could be set to `.testList['foo.34$']`
848866
// +optional
867+
// +kubebuilder:validation:MaxLength=512
849868
FieldPath string `json:"fieldPath,omitempty"`
850869
}
851870

@@ -871,9 +890,11 @@ const (
871890
// ClusterClassPatch defines a patch which is applied to customize the referenced templates.
872891
type ClusterClassPatch struct {
873892
// name of the patch.
893+
// +kubebuilder:validation:MaxLength=256
874894
Name string `json:"name"`
875895

876896
// description is a human-readable description of this patch.
897+
// +kubebuilder:validation:MaxLength=512
877898
Description string `json:"description,omitempty"`
878899

879900
// enabledIf is a Go template to be used to calculate if a patch should be enabled.
@@ -882,6 +903,7 @@ type ClusterClassPatch struct {
882903
// be disabled.
883904
// If EnabledIf is not set, the patch will be enabled per default.
884905
// +optional
906+
// +kubebuilder:validation:MaxLength=256
885907
EnabledIf *string `json:"enabledIf,omitempty"`
886908

887909
// definitions define inline patches.
@@ -916,9 +938,11 @@ type PatchDefinition struct {
916938
// Note: The results of selection based on the individual fields are ANDed.
917939
type PatchSelector struct {
918940
// apiVersion filters templates by apiVersion.
941+
// +kubebuilder:validation:MaxLength=512
919942
APIVersion string `json:"apiVersion"`
920943

921944
// kind filters templates by kind.
945+
// +kubebuilder:validation:MaxLength=256
922946
Kind string `json:"kind"`
923947

924948
// matchResources selects templates based on where they are referenced.
@@ -956,6 +980,7 @@ type PatchSelectorMatchMachineDeploymentClass struct {
956980
// names selects templates by class names.
957981
// +optional
958982
// +kubebuilder:validation:MaxItems=100
983+
// +kubebuilder:validation:items:MaxLength=256
959984
Names []string `json:"names,omitempty"`
960985
}
961986

@@ -965,20 +990,23 @@ type PatchSelectorMatchMachinePoolClass struct {
965990
// names selects templates by class names.
966991
// +optional
967992
// +kubebuilder:validation:MaxItems=100
993+
// +kubebuilder:validation:items:MaxLength=256
968994
Names []string `json:"names,omitempty"`
969995
}
970996

971997
// JSONPatch defines a JSON patch.
972998
type JSONPatch struct {
973999
// op defines the operation of the patch.
9741000
// Note: Only `add`, `replace` and `remove` are supported.
1001+
// +kubebuilder:validation:MaxLength=8
9751002
Op string `json:"op"`
9761003

9771004
// path defines the path of the patch.
9781005
// Note: Only the spec of a template can be patched, thus the path has to start with /spec/.
9791006
// Note: For now the only allowed array modifications are `append` and `prepend`, i.e.:
9801007
// * for op: `add`: only index 0 (prepend) and - (append) are allowed
9811008
// * for op: `replace` or `remove`: no indexes are allowed
1009+
// +kubebuilder:validation:MaxLength=512
9821010
Path string `json:"path"`
9831011

9841012
// value defines the value of the patch.
@@ -1004,12 +1032,14 @@ type JSONPatchValue struct {
10041032
// variable is the variable to be used as value.
10051033
// Variable can be one of the variables defined in .spec.variables or a builtin variable.
10061034
// +optional
1035+
// +kubebuilder:validation:MaxLength=256
10071036
Variable *string `json:"variable,omitempty"`
10081037

10091038
// template is the Go template to be used to calculate the value.
10101039
// A template can reference variables defined in .spec.variables and builtin variables.
10111040
// Note: The template must evaluate to a valid YAML or JSON value.
10121041
// +optional
1042+
// +kubebuilder:validation:MaxLength=512
10131043
Template *string `json:"template,omitempty"`
10141044
}
10151045

@@ -1018,14 +1048,17 @@ type JSONPatchValue struct {
10181048
type ExternalPatchDefinition struct {
10191049
// generateExtension references an extension which is called to generate patches.
10201050
// +optional
1051+
// +kubebuilder:validation:MaxLength=512
10211052
GenerateExtension *string `json:"generateExtension,omitempty"`
10221053

10231054
// validateExtension references an extension which is called to validate the topology.
10241055
// +optional
1056+
// +kubebuilder:validation:MaxLength=512
10251057
ValidateExtension *string `json:"validateExtension,omitempty"`
10261058

10271059
// discoverVariablesExtension references an extension which is called to discover variables.
10281060
// +optional
1061+
// +kubebuilder:validation:MaxLength=512
10291062
DiscoverVariablesExtension *string `json:"discoverVariablesExtension,omitempty"`
10301063

10311064
// settings defines key value pairs to be passed to the extensions.
@@ -1079,6 +1112,7 @@ type ClusterClassV1Beta2Status struct {
10791112
// ClusterClassStatusVariable defines a variable which appears in the status of a ClusterClass.
10801113
type ClusterClassStatusVariable struct {
10811114
// name is the name of the variable.
1115+
// +kubebuilder:validation:MaxLength=256
10821116
Name string `json:"name"`
10831117

10841118
// definitionsConflict specifies whether or not there are conflicting definitions for a single variable name.
@@ -1095,6 +1129,7 @@ type ClusterClassStatusVariableDefinition struct {
10951129
// from specifies the origin of the variable definition.
10961130
// This will be `inline` for variables defined in the ClusterClass or the name of a patch defined in the ClusterClass
10971131
// for variables discovered from a DiscoverVariables runtime extensions.
1132+
// +kubebuilder:validation:MaxLength=256
10981133
From string `json:"from"`
10991134

11001135
// required specifies if the variable is required.

0 commit comments

Comments
 (0)