Skip to content

Commit 45785a4

Browse files
committed
Migrate Admission Controller Validation to CEL
Signed-off-by: Omer Aplatony <[email protected]>
1 parent 5cd491a commit 45785a4

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed

vertical-pod-autoscaler/deploy/vpa-v1-crd-gen.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ spec:
301301
required:
302302
- name
303303
type: object
304+
maxItems: 1
304305
type: array
305306
resourcePolicy:
306307
description: |-
@@ -324,7 +325,11 @@ spec:
324325
Name of the container or DefaultContainerResourcePolicy, in which
325326
case the policy is used by the containers that don't have their own
326327
policy specified.
328+
pattern: ^[a-zA-Z0-9-_]+$
327329
type: string
330+
x-kubernetes-validations:
331+
- message: ContainerName cannot be empty
332+
rule: size(self) > 0
328333
controlledResources:
329334
description: |-
330335
Specifies the type of recommendations that will be computed
@@ -366,13 +371,20 @@ spec:
366371
for the container. The default is no minimum.
367372
type: object
368373
mode:
374+
default: Auto
369375
description: Whether autoscaler is enabled for the container.
370376
The default is "Auto".
371377
enum:
372378
- Auto
373379
- "Off"
374380
type: string
375381
type: object
382+
x-kubernetes-validations:
383+
- message: ControlledValues shouldn't be specified if container
384+
scaling mode is off
385+
rule: '!has(self.mode) || !has(self.controlledValues) || self.mode
386+
!= ''Off'' || self.controlledValues != ''RequestsAndLimits'''
387+
maxItems: 100
376388
type: array
377389
type: object
378390
targetRef:
@@ -449,6 +461,7 @@ spec:
449461
pod eviction (pending other checks like PDB). Only positive values are
450462
allowed. Overrides global '--min-replicas' flag.
451463
format: int32
464+
minimum: 1
452465
type: integer
453466
updateMode:
454467
description: |-

vertical-pod-autoscaler/pkg/admission-controller/resource/vpa/handler.go

-32
Original file line numberDiff line numberDiff line change
@@ -112,25 +112,8 @@ func parseVPA(raw []byte) (*vpa_types.VerticalPodAutoscaler, error) {
112112

113113
// ValidateVPA checks the correctness of VPA Spec and returns an error if there is a problem.
114114
func ValidateVPA(vpa *vpa_types.VerticalPodAutoscaler, isCreate bool) error {
115-
if vpa.Spec.UpdatePolicy != nil {
116-
mode := vpa.Spec.UpdatePolicy.UpdateMode
117-
if mode == nil {
118-
return fmt.Errorf("UpdateMode is required if UpdatePolicy is used")
119-
}
120-
if _, found := possibleUpdateModes[*mode]; !found {
121-
return fmt.Errorf("unexpected UpdateMode value %s", *mode)
122-
}
123-
124-
if minReplicas := vpa.Spec.UpdatePolicy.MinReplicas; minReplicas != nil && *minReplicas <= 0 {
125-
return fmt.Errorf("MinReplicas has to be positive, got %v", *minReplicas)
126-
}
127-
}
128-
129115
if vpa.Spec.ResourcePolicy != nil {
130116
for _, policy := range vpa.Spec.ResourcePolicy.ContainerPolicies {
131-
if policy.ContainerName == "" {
132-
return fmt.Errorf("ContainerPolicies.ContainerName is required")
133-
}
134117
mode := policy.Mode
135118
if mode != nil {
136119
if _, found := possibleScalingModes[*mode]; !found {
@@ -152,23 +135,8 @@ func ValidateVPA(vpa *vpa_types.VerticalPodAutoscaler, isCreate bool) error {
152135
return fmt.Errorf("MaxAllowed: %v", err)
153136
}
154137
}
155-
ControlledValues := policy.ControlledValues
156-
if mode != nil && ControlledValues != nil {
157-
if *mode == vpa_types.ContainerScalingModeOff && *ControlledValues == vpa_types.ContainerControlledValuesRequestsAndLimits {
158-
return fmt.Errorf("ControlledValues shouldn't be specified if container scaling mode is off.")
159-
}
160-
}
161138
}
162139
}
163-
164-
if isCreate && vpa.Spec.TargetRef == nil {
165-
return fmt.Errorf("TargetRef is required. If you're using v1beta1 version of the API, please migrate to v1")
166-
}
167-
168-
if len(vpa.Spec.Recommenders) > 1 {
169-
return fmt.Errorf("The current version of VPA object shouldn't specify more than one recommenders.")
170-
}
171-
172140
return nil
173141
}
174142

vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1/types.go

+7
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ type VerticalPodAutoscalerSpec struct {
106106
// Recommender responsible for generating recommendation for this object.
107107
// List should be empty (then the default recommender will generate the
108108
// recommendation) or contain exactly one recommender.
109+
// +kubebuilder:validation:MaxItems=1
109110
// +optional
110111
Recommenders []*VerticalPodAutoscalerRecommenderSelector `json:"recommenders,omitempty" protobuf:"bytes,4,opt,name=recommenders"`
111112
}
@@ -142,6 +143,7 @@ type PodUpdatePolicy struct {
142143
// pod eviction (pending other checks like PDB). Only positive values are
143144
// allowed. Overrides global '--min-replicas' flag.
144145
// +optional
146+
// +kubebuilder:validation:Minimum=1
145147
MinReplicas *int32 `json:"minReplicas,omitempty" protobuf:"varint,2,opt,name=minReplicas"`
146148

147149
// EvictionRequirements is a list of EvictionRequirements that need to
@@ -183,18 +185,23 @@ type PodResourcePolicy struct {
183185
// +optional
184186
// +patchMergeKey=containerName
185187
// +patchStrategy=merge
188+
// +kubebuilder:validation:MaxItems=100
186189
ContainerPolicies []ContainerResourcePolicy `json:"containerPolicies,omitempty" patchStrategy:"merge" patchMergeKey:"containerName" protobuf:"bytes,1,rep,name=containerPolicies"`
187190
}
188191

189192
// ContainerResourcePolicy controls how autoscaler computes the recommended
190193
// resources for a specific container.
194+
// +kubebuilder:validation:XValidation:rule="!has(self.mode) || !has(self.controlledValues) || self.mode != 'Off' || self.controlledValues != 'RequestsAndLimits'",message="ControlledValues shouldn't be specified if container scaling mode is off"
191195
type ContainerResourcePolicy struct {
192196
// Name of the container or DefaultContainerResourcePolicy, in which
193197
// case the policy is used by the containers that don't have their own
194198
// policy specified.
199+
// +kubebuilder:validation:Pattern=`^[a-zA-Z0-9-_]+$`
200+
// +kubebuilder:validation:XValidation:rule="size(self) > 0",message="ContainerName cannot be empty"
195201
ContainerName string `json:"containerName,omitempty" protobuf:"bytes,1,opt,name=containerName"`
196202
// Whether autoscaler is enabled for the container. The default is "Auto".
197203
// +optional
204+
// +kubebuilder:default="Auto"
198205
Mode *ContainerScalingMode `json:"mode,omitempty" protobuf:"bytes,2,opt,name=mode"`
199206
// Specifies the minimal amount of resources that will be recommended
200207
// for the container. The default is no minimum.

0 commit comments

Comments
 (0)