From 42e0fe9533eda34c13176d12f9f89c3b4d5535c2 Mon Sep 17 00:00:00 2001 From: Dhairya Arora Date: Sat, 5 Oct 2024 22:28:19 +0530 Subject: [PATCH] add basic validation for time duration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Basic validation for time duration can be done using kubebuilder markers to match the pattern "^(([1-9][0-9]*(\\.[0-9]+)?|0?\\.[0-9]+)(ns|us|µs|ms|s|m|h))+$", Signed-off-by: Dhairya Arora --- api/v1beta1/cluster_types.go | 18 ++++++++++++++++++ .../crd/bases/cluster.x-k8s.io_clusters.yaml | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/api/v1beta1/cluster_types.go b/api/v1beta1/cluster_types.go index d51fecb2924d..b3156d00cdb8 100644 --- a/api/v1beta1/cluster_types.go +++ b/api/v1beta1/cluster_types.go @@ -153,17 +153,23 @@ type ControlPlaneTopology struct { // NodeDrainTimeout is the total amount of time that the controller will spend on draining a node. // The default value is 0, meaning that the node can be drained without any time limitations. // NOTE: NodeDrainTimeout is different from `kubectl drain --timeout` + // +kubebuilder:validation:Pattern="^(([1-9][0-9]*(\\.[0-9]+)?|0?\\.[0-9]+)(ns|us|µs|ms|s|m|h))+$" + // +kubebuilder:validation:Type:=string // +optional NodeDrainTimeout *metav1.Duration `json:"nodeDrainTimeout,omitempty"` // NodeVolumeDetachTimeout is the total amount of time that the controller will spend on waiting for all volumes // to be detached. The default value is 0, meaning that the volumes can be detached without any time limitations. + // +kubebuilder:validation:Pattern="^(([1-9][0-9]*(\\.[0-9]+)?|0?\\.[0-9]+)(ns|us|µs|ms|s|m|h))+$" + // +kubebuilder:validation:Type:=string // +optional NodeVolumeDetachTimeout *metav1.Duration `json:"nodeVolumeDetachTimeout,omitempty"` // NodeDeletionTimeout defines how long the controller will attempt to delete the Node that the Machine // hosts after the Machine is marked for deletion. A duration of 0 will retry deletion indefinitely. // Defaults to 10 seconds. + // +kubebuilder:validation:Pattern="^(([1-9][0-9]*(\\.[0-9]+)?|0?\\.[0-9]+)(ns|us|µs|ms|s|m|h))+$" + // +kubebuilder:validation:Type:=string // +optional NodeDeletionTimeout *metav1.Duration `json:"nodeDeletionTimeout,omitempty"` @@ -226,17 +232,23 @@ type MachineDeploymentTopology struct { // NodeDrainTimeout is the total amount of time that the controller will spend on draining a node. // The default value is 0, meaning that the node can be drained without any time limitations. // NOTE: NodeDrainTimeout is different from `kubectl drain --timeout` + // +kubebuilder:validation:Pattern="^(([1-9][0-9]*(\\.[0-9]+)?|0?\\.[0-9]+)(ns|us|µs|ms|s|m|h))+$" + // +kubebuilder:validation:Type:=string // +optional NodeDrainTimeout *metav1.Duration `json:"nodeDrainTimeout,omitempty"` // NodeVolumeDetachTimeout is the total amount of time that the controller will spend on waiting for all volumes // to be detached. The default value is 0, meaning that the volumes can be detached without any time limitations. + // +kubebuilder:validation:Pattern="^(([1-9][0-9]*(\\.[0-9]+)?|0?\\.[0-9]+)(ns|us|µs|ms|s|m|h))+$" + // +kubebuilder:validation:Type:=string // +optional NodeVolumeDetachTimeout *metav1.Duration `json:"nodeVolumeDetachTimeout,omitempty"` // NodeDeletionTimeout defines how long the controller will attempt to delete the Node that the Machine // hosts after the Machine is marked for deletion. A duration of 0 will retry deletion indefinitely. // Defaults to 10 seconds. + // +kubebuilder:validation:Pattern="^(([1-9][0-9]*(\\.[0-9]+)?|0?\\.[0-9]+)(ns|us|µs|ms|s|m|h))+$" + // +kubebuilder:validation:Type:=string // +optional NodeDeletionTimeout *metav1.Duration `json:"nodeDeletionTimeout,omitempty"` @@ -303,17 +315,23 @@ type MachinePoolTopology struct { // NodeDrainTimeout is the total amount of time that the controller will spend on draining a node. // The default value is 0, meaning that the node can be drained without any time limitations. // NOTE: NodeDrainTimeout is different from `kubectl drain --timeout` + // +kubebuilder:validation:Pattern="^(([1-9][0-9]*(\\.[0-9]+)?|0?\\.[0-9]+)(ns|us|µs|ms|s|m|h))+$" + // +kubebuilder:validation:Type:=string // +optional NodeDrainTimeout *metav1.Duration `json:"nodeDrainTimeout,omitempty"` // NodeVolumeDetachTimeout is the total amount of time that the controller will spend on waiting for all volumes // to be detached. The default value is 0, meaning that the volumes can be detached without any time limitations. + // +kubebuilder:validation:Pattern="^(([1-9][0-9]*(\\.[0-9]+)?|0?\\.[0-9]+)(ns|us|µs|ms|s|m|h))+$" + // +kubebuilder:validation:Type:=string // +optional NodeVolumeDetachTimeout *metav1.Duration `json:"nodeVolumeDetachTimeout,omitempty"` // NodeDeletionTimeout defines how long the controller will attempt to delete the Node that the MachinePool // hosts after the MachinePool is marked for deletion. A duration of 0 will retry deletion indefinitely. // Defaults to 10 seconds. + // +kubebuilder:validation:Pattern="^(([1-9][0-9]*(\\.[0-9]+)?|0?\\.[0-9]+)(ns|us|µs|ms|s|m|h))+$" + // +kubebuilder:validation:Type:=string // +optional NodeDeletionTimeout *metav1.Duration `json:"nodeDeletionTimeout,omitempty"` diff --git a/config/crd/bases/cluster.x-k8s.io_clusters.yaml b/config/crd/bases/cluster.x-k8s.io_clusters.yaml index f8cc44b0464d..8d3e30c1f35a 100644 --- a/config/crd/bases/cluster.x-k8s.io_clusters.yaml +++ b/config/crd/bases/cluster.x-k8s.io_clusters.yaml @@ -1086,17 +1086,20 @@ spec: NodeDeletionTimeout defines how long the controller will attempt to delete the Node that the Machine hosts after the Machine is marked for deletion. A duration of 0 will retry deletion indefinitely. Defaults to 10 seconds. + pattern: ^(([1-9][0-9]*(\.[0-9]+)?|0?\.[0-9]+)(ns|us|µs|ms|s|m|h))+$ type: string nodeDrainTimeout: description: |- NodeDrainTimeout is the total amount of time that the controller will spend on draining a node. The default value is 0, meaning that the node can be drained without any time limitations. NOTE: NodeDrainTimeout is different from `kubectl drain --timeout` + pattern: ^(([1-9][0-9]*(\.[0-9]+)?|0?\.[0-9]+)(ns|us|µs|ms|s|m|h))+$ type: string nodeVolumeDetachTimeout: description: |- NodeVolumeDetachTimeout is the total amount of time that the controller will spend on waiting for all volumes to be detached. The default value is 0, meaning that the volumes can be detached without any time limitations. + pattern: ^(([1-9][0-9]*(\.[0-9]+)?|0?\.[0-9]+)(ns|us|µs|ms|s|m|h))+$ type: string replicas: description: |- @@ -1387,17 +1390,20 @@ spec: NodeDeletionTimeout defines how long the controller will attempt to delete the Node that the Machine hosts after the Machine is marked for deletion. A duration of 0 will retry deletion indefinitely. Defaults to 10 seconds. + pattern: ^(([1-9][0-9]*(\.[0-9]+)?|0?\.[0-9]+)(ns|us|µs|ms|s|m|h))+$ type: string nodeDrainTimeout: description: |- NodeDrainTimeout is the total amount of time that the controller will spend on draining a node. The default value is 0, meaning that the node can be drained without any time limitations. NOTE: NodeDrainTimeout is different from `kubectl drain --timeout` + pattern: ^(([1-9][0-9]*(\.[0-9]+)?|0?\.[0-9]+)(ns|us|µs|ms|s|m|h))+$ type: string nodeVolumeDetachTimeout: description: |- NodeVolumeDetachTimeout is the total amount of time that the controller will spend on waiting for all volumes to be detached. The default value is 0, meaning that the volumes can be detached without any time limitations. + pattern: ^(([1-9][0-9]*(\.[0-9]+)?|0?\.[0-9]+)(ns|us|µs|ms|s|m|h))+$ type: string replicas: description: |- @@ -1613,17 +1619,20 @@ spec: NodeDeletionTimeout defines how long the controller will attempt to delete the Node that the MachinePool hosts after the MachinePool is marked for deletion. A duration of 0 will retry deletion indefinitely. Defaults to 10 seconds. + pattern: ^(([1-9][0-9]*(\.[0-9]+)?|0?\.[0-9]+)(ns|us|µs|ms|s|m|h))+$ type: string nodeDrainTimeout: description: |- NodeDrainTimeout is the total amount of time that the controller will spend on draining a node. The default value is 0, meaning that the node can be drained without any time limitations. NOTE: NodeDrainTimeout is different from `kubectl drain --timeout` + pattern: ^(([1-9][0-9]*(\.[0-9]+)?|0?\.[0-9]+)(ns|us|µs|ms|s|m|h))+$ type: string nodeVolumeDetachTimeout: description: |- NodeVolumeDetachTimeout is the total amount of time that the controller will spend on waiting for all volumes to be detached. The default value is 0, meaning that the volumes can be detached without any time limitations. + pattern: ^(([1-9][0-9]*(\.[0-9]+)?|0?\.[0-9]+)(ns|us|µs|ms|s|m|h))+$ type: string replicas: description: |-