Skip to content

Commit 7c3af58

Browse files
author
Likios
committed
fix: add CEL validation to require template field in RoleSpec
Add Kubernetes CEL (Common Expression Language) validation to ensure that when Template is optional (pointer type), users cannot create RoleBasedGroups without specifying a valid template. The validation rule ensures: 1. The template field must be present (has(self.template)) 2. The template must not be null (self.template != null) 3. The template must have at least one container (size(self.template.spec.containers) > 0) This prevents runtime errors where Pods would be created without any containers, which would fail to schedule. The CEL validation runs at the Kubernetes API server level when CRDs are applied, providing early feedback to users before the resource reaches the controller. Fixes the issue where Template pointer migration made the field optional without corresponding validation, potentially allowing invalid resources.
1 parent 67e62e9 commit 7c3af58

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

api/workloads/v1alpha1/rolebasedgroup_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ type RollingUpdate struct {
145145
}
146146

147147
// RoleSpec defines the specification for a role in the group
148+
// +kubebuilder:validation:XValidation:rule="has(self.template) && self.template != null && size(self.template.spec.containers) > 0",message="template must be specified and must have at least one container"
148149
type RoleSpec struct {
149150
// Unique identifier for the role
150151
// +kubebuilder:validation:Required

config/crd/bases/workloads.x-k8s.io_rolebasedgroups.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8925,6 +8925,11 @@ spec:
89258925
- name
89268926
- replicas
89278927
type: object
8928+
x-kubernetes-validations:
8929+
- message: template must be specified and must have at least one
8930+
container
8931+
rule: has(self.template) && self.template != null && size(self.template.spec.containers)
8932+
> 0
89288933
minItems: 1
89298934
type: array
89308935
x-kubernetes-list-map-keys:

config/crd/bases/workloads.x-k8s.io_rolebasedgroupsets.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9077,6 +9077,11 @@ spec:
90779077
- name
90789078
- replicas
90799079
type: object
9080+
x-kubernetes-validations:
9081+
- message: template must be specified and must have at least
9082+
one container
9083+
rule: has(self.template) && self.template != null && size(self.template.spec.containers)
9084+
> 0
90809085
minItems: 1
90819086
type: array
90829087
x-kubernetes-list-map-keys:

deploy/helm/rbgs/crds/workloads.x-k8s.io_rolebasedgroups.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8925,6 +8925,11 @@ spec:
89258925
- name
89268926
- replicas
89278927
type: object
8928+
x-kubernetes-validations:
8929+
- message: template must be specified and must have at least one
8930+
container
8931+
rule: has(self.template) && self.template != null && size(self.template.spec.containers)
8932+
> 0
89288933
minItems: 1
89298934
type: array
89308935
x-kubernetes-list-map-keys:

deploy/helm/rbgs/crds/workloads.x-k8s.io_rolebasedgroupsets.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9077,6 +9077,11 @@ spec:
90779077
- name
90789078
- replicas
90799079
type: object
9080+
x-kubernetes-validations:
9081+
- message: template must be specified and must have at least
9082+
one container
9083+
rule: has(self.template) && self.template != null && size(self.template.spec.containers)
9084+
> 0
90809085
minItems: 1
90819086
type: array
90829087
x-kubernetes-list-map-keys:

0 commit comments

Comments
 (0)