Skip to content

Commit

Permalink
Merge pull request #1785 from kgolab/kg-vpa-invalidate-v1beta1
Browse files Browse the repository at this point in the history
Forbid creating VPA objects without targetRef
  • Loading branch information
k8s-ci-robot authored Mar 12, 2019
2 parents 9733fe0 + 6ec53d4 commit 0e8e303
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions vertical-pod-autoscaler/pkg/admission-controller/logic/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ var (
}
)

func validateVPA(vpa *vpa_types.VerticalPodAutoscaler) error {
func validateVPA(vpa *vpa_types.VerticalPodAutoscaler, isCreate bool) error {
if vpa.Spec.UpdatePolicy != nil {
mode := vpa.Spec.UpdatePolicy.UpdateMode
if mode == nil {
Expand Down Expand Up @@ -178,16 +178,20 @@ func validateVPA(vpa *vpa_types.VerticalPodAutoscaler) error {
}
}

if isCreate && vpa.Spec.TargetRef == nil {
return fmt.Errorf("TargetRef is required. If you're using v1beta1 version of the API, please migrate to v1beta2.")
}

return nil
}

func getPatchesForVPADefaults(raw []byte) ([]patchRecord, error) {
func getPatchesForVPADefaults(raw []byte, isCreate bool) ([]patchRecord, error) {
vpa, err := parseVPA(raw)
if err != nil {
return nil, err
}

err = validateVPA(vpa)
err = validateVPA(vpa, isCreate)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -228,7 +232,7 @@ func (s *AdmissionServer) admit(data []byte) (*v1beta1.AdmissionResponse, metric
patches, err = s.getPatchesForPodResourceRequest(ar.Request.Object.Raw, ar.Request.Namespace)
resource = metrics_admission.Pod
case vpaResource:
patches, err = getPatchesForVPADefaults(ar.Request.Object.Raw)
patches, err = getPatchesForVPADefaults(ar.Request.Object.Raw, ar.Request.Operation == v1beta1.Create)
resource = metrics_admission.Vpa
// we don't let in problematic VPA objects - late validation
if err != nil {
Expand Down

0 comments on commit 0e8e303

Please sign in to comment.