Skip to content

Commit b60d176

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

File tree

2 files changed

+13
-32
lines changed

2 files changed

+13
-32
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ spec:
302302
- name
303303
type: object
304304
type: array
305+
maxItems: 1
305306
resourcePolicy:
306307
description: |-
307308
Controls how the autoscaler computes recommended resources.
@@ -325,6 +326,7 @@ spec:
325326
case the policy is used by the containers that don't have their own
326327
policy specified.
327328
type: string
329+
maxLength: 253
328330
controlledResources:
329331
description: |-
330332
Specifies the type of recommendations that will be computed
@@ -334,6 +336,7 @@ spec:
334336
description: ResourceName is the name identifying various
335337
resources in a ResourceList.
336338
type: string
339+
enum: ['RequestsAndLimits', 'RequestsOnly']
337340
type: array
338341
controlledValues:
339342
description: |-
@@ -373,7 +376,13 @@ spec:
373376
- "Off"
374377
type: string
375378
type: object
379+
x-kubernetes-validations:
380+
- rule: "size(self.containerName) > 0"
381+
message: "ContainerName cannot be empty"
382+
- rule: "!has(self.mode) || !has(self.controlledValues) || self.mode != 'Off' || self.controlledValues != 'RequestsAndLimits'"
383+
message: "ControlledValues shouldn't be specified if container scaling mode is off"
376384
type: array
385+
maxItems: 100
377386
type: object
378387
targetRef:
379388
description: |-
@@ -460,7 +469,11 @@ spec:
460469
- Recreate
461470
- Auto
462471
type: string
472+
default: "Auto"
463473
type: object
474+
x-kubernetes-validations:
475+
- rule: "!has(self.minReplicas) || self.minReplicas > 0"
476+
message: "MinReplicas has to be positive"
464477
required:
465478
- targetRef
466479
type: object

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

Lines changed: 0 additions & 32 deletions
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

0 commit comments

Comments
 (0)