Skip to content

⚠️ Add MaxLength & MinLength markers #11949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions .golangci-kal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,19 @@ issues:
text: "field Prefix should not use an int, int8 or int16. Use int32 or int64 depending on bounding requirements"
linters:
- kal
- path: "api/v1alpha1/*|api/v1alpha3/*"
# clusterctl and Runtime Hooks can be fixed once we bump their apiVersion.
- path: "cmd/clusterctl/api/v1alpha3|exp/runtime/hooks/api/v1alpha1"
text: "maxlength"
linters:
- kal
- path: "api/v1beta1/*"
text: "must have a maximum length, add (kubebuilder:validation:MaxLength|kubebuilder:validation:items:MaxLength) marker"
linters:
- kal
# controller-gen does not allow to add MaxItems to Schemaless fields
- path: "api/v1beta1/*"
text: "maxlength: field (AllOf|OneOf|AnyOf) must have a maximum items, add kubebuilder:validation:MaxItems marker"
linters:
- kal
# It does not make sense to add a maxItems marker on the *List structs as they are not used to generate CRD YAMLs.
# This exclude will be removed once https://github.com/JoelSpeed/kal/issues/38 is resolved.
- path: "api/v1beta1/*"
- path: "api/v1beta1/*|api/v1alpha1/*"
text: "maxlength: field Items must have a maximum items, add kubebuilder:validation:MaxItems marker"
linters:
- kal
Expand Down
33 changes: 30 additions & 3 deletions api/v1beta1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ type ClusterAvailabilityGate struct {
// Note: Both Cluster API conditions or conditions added by 3rd party controllers can be used as availability gates.
// +required
// +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])$`
// +kubebuilder:validation:MaxLength=316
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=316
ConditionType string `json:"conditionType"`

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

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

// version is the Kubernetes version of the cluster.
// +required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=256
Version string `json:"version"`

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

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

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

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

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

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

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

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

// value of the variable.
Expand Down Expand Up @@ -905,6 +924,8 @@ type ClusterNetwork struct {

// serviceDomain is the domain name for services.
// +optional
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
ServiceDomain string `json:"serviceDomain,omitempty"`
}

Expand All @@ -915,8 +936,10 @@ type ClusterNetwork struct {
// NetworkRanges represents ranges of network addresses.
type NetworkRanges struct {
// cidrBlocks is a list of CIDR blocks.
// +kubebuilder:validation:MaxItems=100
// +required
// +kubebuilder:validation:MaxItems=100
// +kubebuilder:validation:items:MinLength=1
// +kubebuilder:validation:items:MaxLength=43
CIDRBlocks []string `json:"cidrBlocks"`
}

Expand Down Expand Up @@ -952,11 +975,13 @@ type ClusterStatus struct {
// 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.
//
// +optional
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=10240
FailureMessage *string `json:"failureMessage,omitempty"`

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

// infrastructureReady is the state of the infrastructure provider.
Expand Down Expand Up @@ -1082,7 +1107,9 @@ func (c *ClusterStatus) GetTypedPhase() ClusterPhase {
// APIEndpoint represents a reachable Kubernetes API endpoint.
type APIEndpoint struct {
// host is the hostname on which the API server is serving.
// 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.
// +required
// +kubebuilder:validation:MaxLength=512
Host string `json:"host"`

// port is the port on which the API server is serving.
Expand Down
Loading