Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ import (

"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/admission-controller/resource"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/features"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/metrics/admission"
)

var (
possibleUpdateModes = map[vpa_types.UpdateMode]interface{}{
vpa_types.UpdateModeOff: struct{}{},
vpa_types.UpdateModeInitial: struct{}{},
vpa_types.UpdateModeRecreate: struct{}{},
vpa_types.UpdateModeAuto: struct{}{},
vpa_types.UpdateModeOff: struct{}{},
vpa_types.UpdateModeInitial: struct{}{},
vpa_types.UpdateModeRecreate: struct{}{},
vpa_types.UpdateModeAuto: struct{}{},
vpa_types.UpdateModeInPlaceOrRecreate: struct{}{},
}

possibleScalingModes = map[vpa_types.ContainerScalingMode]interface{}{
Expand Down Expand Up @@ -121,6 +123,9 @@ func ValidateVPA(vpa *vpa_types.VerticalPodAutoscaler, isCreate bool) error {
if _, found := possibleUpdateModes[*mode]; !found {
return fmt.Errorf("unexpected UpdateMode value %s", *mode)
}
if (*mode == vpa_types.UpdateModeInPlaceOrRecreate) && !features.Enabled(features.InPlaceOrRecreate) && isCreate {
return fmt.Errorf("in order to use UpdateMode %s, you must enable feature gate %s in the admission-controller args", vpa_types.UpdateModeInPlaceOrRecreate, features.InPlaceOrRecreate)
}

if minReplicas := vpa.Spec.UpdatePolicy.MinReplicas; minReplicas != nil && *minReplicas <= 0 {
return fmt.Errorf("MinReplicas has to be positive, got %v", *minReplicas)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import (
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

featuregatetesting "k8s.io/component-base/featuregate/testing"

vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/features"
)

const (
Expand All @@ -42,11 +45,13 @@ func TestValidateVPA(t *testing.T) {
validScalingMode := vpa_types.ContainerScalingModeAuto
scalingModeOff := vpa_types.ContainerScalingModeOff
controlledValuesRequestsAndLimits := vpa_types.ContainerControlledValuesRequestsAndLimits
inPlaceOrRecreateUpdateMode := vpa_types.UpdateModeInPlaceOrRecreate
tests := []struct {
name string
vpa vpa_types.VerticalPodAutoscaler
isCreate bool
expectError error
name string
vpa vpa_types.VerticalPodAutoscaler
isCreate bool
expectError error
inPlaceOrRecreateFeatureGateDisabled bool
}{
{
name: "empty update",
Expand Down Expand Up @@ -78,6 +83,42 @@ func TestValidateVPA(t *testing.T) {
},
expectError: fmt.Errorf("unexpected UpdateMode value bad"),
},
{
name: "creating VPA with InPlaceOrRecreate update mode not allowed by disabled feature gate",
vpa: vpa_types.VerticalPodAutoscaler{
Spec: vpa_types.VerticalPodAutoscalerSpec{
UpdatePolicy: &vpa_types.PodUpdatePolicy{
UpdateMode: &inPlaceOrRecreateUpdateMode,
},
},
},
isCreate: true,
inPlaceOrRecreateFeatureGateDisabled: true,
expectError: fmt.Errorf("in order to use UpdateMode %s, you must enable feature gate %s in the admission-controller args", vpa_types.UpdateModeInPlaceOrRecreate, features.InPlaceOrRecreate),
},
{
name: "updating VPA with InPlaceOrRecreate update mode allowed by disabled feature gate",
vpa: vpa_types.VerticalPodAutoscaler{
Spec: vpa_types.VerticalPodAutoscalerSpec{
UpdatePolicy: &vpa_types.PodUpdatePolicy{
UpdateMode: &inPlaceOrRecreateUpdateMode,
},
},
},
isCreate: false,
inPlaceOrRecreateFeatureGateDisabled: true,
expectError: nil,
},
{
name: "InPlaceOrRecreate update mode enabled by feature gate",
vpa: vpa_types.VerticalPodAutoscaler{
Spec: vpa_types.VerticalPodAutoscalerSpec{
UpdatePolicy: &vpa_types.PodUpdatePolicy{
UpdateMode: &inPlaceOrRecreateUpdateMode,
},
},
},
},
{
name: "zero minReplicas",
vpa: vpa_types.VerticalPodAutoscaler{
Expand Down Expand Up @@ -282,6 +323,9 @@ func TestValidateVPA(t *testing.T) {
}
for _, tc := range tests {
t.Run(fmt.Sprintf("test case: %s", tc.name), func(t *testing.T) {
if !tc.inPlaceOrRecreateFeatureGateDisabled {
featuregatetesting.SetFeatureGateDuringTest(t, features.MutableFeatureGate, features.InPlaceOrRecreate, true)
}
err := ValidateVPA(&tc.vpa, tc.isCreate)
if tc.expectError == nil {
assert.NoError(t, err)
Expand Down
Loading