Skip to content

Commit 53112f5

Browse files
sbueringercprivitere
authored andcommitted
⚠️ Add MaxLength & MinLength markers (kubernetes-sigs#11949)
* Add MaxLength markers to API fields Signed-off-by: Stefan Büringer [email protected] * Fix review findings * Fix review findings * Fix review findings * regen * Fix review findings * Fix review findings --------- Signed-off-by: Stefan Büringer [email protected]
1 parent 916f597 commit 53112f5

File tree

45 files changed

+1565
-31
lines changed

Some content is hidden

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

45 files changed

+1565
-31
lines changed

.golangci-kal.yml

+3-6
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,19 @@ 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/*"
77+
# clusterctl and Runtime Hooks can be fixed once we bump their apiVersion.
78+
- path: "cmd/clusterctl/api/v1alpha3|exp/runtime/hooks/api/v1alpha1"
7879
text: "maxlength"
7980
linters:
8081
- 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
8582
# controller-gen does not allow to add MaxItems to Schemaless fields
8683
- path: "api/v1beta1/*"
8784
text: "maxlength: field (AllOf|OneOf|AnyOf) must have a maximum items, add kubebuilder:validation:MaxItems marker"
8885
linters:
8986
- kal
9087
# It does not make sense to add a maxItems marker on the *List structs as they are not used to generate CRD YAMLs.
9188
# This exclude will be removed once https://github.com/JoelSpeed/kal/issues/38 is resolved.
92-
- path: "api/v1beta1/*"
89+
- path: "api/v1beta1/*|api/v1alpha1/*"
9390
text: "maxlength: field Items must have a maximum items, add kubebuilder:validation:MaxItems marker"
9491
linters:
9592
- kal

api/v1beta1/cluster_types.go

+30-3
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,8 @@ type ClusterAvailabilityGate struct {
524524
// Note: Both Cluster API conditions or conditions added by 3rd party controllers can be used as availability gates.
525525
// +required
526526
// +kubebuilder:validation:Pattern=`^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$`
527-
// +kubebuilder:validation:MaxLength=316
528527
// +kubebuilder:validation:MinLength=1
528+
// +kubebuilder:validation:MaxLength=316
529529
ConditionType string `json:"conditionType"`
530530

531531
// polarity of the conditionType specified in this availabilityGate.
@@ -542,6 +542,8 @@ type ClusterAvailabilityGate struct {
542542
type Topology struct {
543543
// class is the name of the ClusterClass object to create the topology.
544544
// +required
545+
// +kubebuilder:validation:MinLength=1
546+
// +kubebuilder:validation:MaxLength=253
545547
Class string `json:"class"`
546548

547549
// classNamespace is the namespace of the ClusterClass object to create the topology.
@@ -555,6 +557,8 @@ type Topology struct {
555557

556558
// version is the Kubernetes version of the cluster.
557559
// +required
560+
// +kubebuilder:validation:MinLength=1
561+
// +kubebuilder:validation:MaxLength=256
558562
Version string `json:"version"`
559563

560564
// rolloutAfter performs a rollout of the entire cluster one component at a time,
@@ -672,18 +676,24 @@ type MachineDeploymentTopology struct {
672676
// This should match one of the deployment classes defined in the ClusterClass object
673677
// mentioned in the `Cluster.Spec.Class` field.
674678
// +required
679+
// +kubebuilder:validation:MinLength=1
680+
// +kubebuilder:validation:MaxLength=256
675681
Class string `json:"class"`
676682

677683
// name is the unique identifier for this MachineDeploymentTopology.
678684
// The value is used with other unique identifiers to create a MachineDeployment's Name
679685
// (e.g. cluster's name, etc). In case the name is greater than the allowed maximum length,
680686
// the values are hashed together.
681687
// +required
688+
// +kubebuilder:validation:MinLength=1
689+
// +kubebuilder:validation:MaxLength=256
682690
Name string `json:"name"`
683691

684692
// failureDomain is the failure domain the machines will be created in.
685693
// Must match a key in the FailureDomains map stored on the cluster object.
686694
// +optional
695+
// +kubebuilder:validation:MinLength=1
696+
// +kubebuilder:validation:MaxLength=256
687697
FailureDomain *string `json:"failureDomain,omitempty"`
688698

689699
// replicas is the number of worker nodes belonging to this set.
@@ -777,19 +787,25 @@ type MachinePoolTopology struct {
777787
// This should match one of the deployment classes defined in the ClusterClass object
778788
// mentioned in the `Cluster.Spec.Class` field.
779789
// +required
790+
// +kubebuilder:validation:MinLength=1
791+
// +kubebuilder:validation:MaxLength=256
780792
Class string `json:"class"`
781793

782794
// name is the unique identifier for this MachinePoolTopology.
783795
// The value is used with other unique identifiers to create a MachinePool's Name
784796
// (e.g. cluster's name, etc). In case the name is greater than the allowed maximum length,
785797
// the values are hashed together.
786798
// +required
799+
// +kubebuilder:validation:MinLength=1
800+
// +kubebuilder:validation:MaxLength=256
787801
Name string `json:"name"`
788802

789803
// failureDomains is the list of failure domains the machine pool will be created in.
790804
// Must match a key in the FailureDomains map stored on the cluster object.
791805
// +optional
792806
// +kubebuilder:validation:MaxItems=100
807+
// +kubebuilder:validation:items:MinLength=1
808+
// +kubebuilder:validation:items:MaxLength=256
793809
FailureDomains []string `json:"failureDomains,omitempty"`
794810

795811
// nodeDrainTimeout is the total amount of time that the controller will spend on draining a node.
@@ -833,13 +849,16 @@ type MachinePoolTopology struct {
833849
type ClusterVariable struct {
834850
// name of the variable.
835851
// +required
852+
// +kubebuilder:validation:MinLength=1
853+
// +kubebuilder:validation:MaxLength=256
836854
Name string `json:"name"`
837855

838856
// definitionFrom specifies where the definition of this Variable is from.
839857
//
840858
// Deprecated: This field is deprecated, must not be set anymore and is going to be removed in the next apiVersion.
841859
//
842860
// +optional
861+
// +kubebuilder:validation:MaxLength=256
843862
DefinitionFrom string `json:"definitionFrom,omitempty"`
844863

845864
// value of the variable.
@@ -905,6 +924,8 @@ type ClusterNetwork struct {
905924

906925
// serviceDomain is the domain name for services.
907926
// +optional
927+
// +kubebuilder:validation:MinLength=1
928+
// +kubebuilder:validation:MaxLength=253
908929
ServiceDomain string `json:"serviceDomain,omitempty"`
909930
}
910931

@@ -915,8 +936,10 @@ type ClusterNetwork struct {
915936
// NetworkRanges represents ranges of network addresses.
916937
type NetworkRanges struct {
917938
// cidrBlocks is a list of CIDR blocks.
918-
// +kubebuilder:validation:MaxItems=100
919939
// +required
940+
// +kubebuilder:validation:MaxItems=100
941+
// +kubebuilder:validation:items:MinLength=1
942+
// +kubebuilder:validation:items:MaxLength=43
920943
CIDRBlocks []string `json:"cidrBlocks"`
921944
}
922945

@@ -952,11 +975,13 @@ type ClusterStatus struct {
952975
// 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.
953976
//
954977
// +optional
978+
// +kubebuilder:validation:MinLength=1
979+
// +kubebuilder:validation:MaxLength=10240
955980
FailureMessage *string `json:"failureMessage,omitempty"`
956981

957982
// phase represents the current phase of cluster actuation.
958-
// E.g. Pending, Running, Terminating, Failed etc.
959983
// +optional
984+
// +kubebuilder:validation:Enum=Pending;Provisioning;Provisioned;Deleting;Failed;Unknown
960985
Phase string `json:"phase,omitempty"`
961986

962987
// infrastructureReady is the state of the infrastructure provider.
@@ -1082,7 +1107,9 @@ func (c *ClusterStatus) GetTypedPhase() ClusterPhase {
10821107
// APIEndpoint represents a reachable Kubernetes API endpoint.
10831108
type APIEndpoint struct {
10841109
// host is the hostname on which the API server is serving.
1110+
// TODO: Can't set MinLength=1 for now, because this struct is not always used in pointer fields so today we have cases where host is set to an empty string.
10851111
// +required
1112+
// +kubebuilder:validation:MaxLength=512
10861113
Host string `json:"host"`
10871114

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

0 commit comments

Comments
 (0)