|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package autoscaling |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + autoscaling "k8s.io/api/autoscaling/v1" |
| 25 | + apiv1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1" |
| 28 | + "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/status" |
| 29 | + "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/test" |
| 30 | + "k8s.io/kubernetes/test/e2e/framework" |
| 31 | + podsecurity "k8s.io/pod-security-admission/api" |
| 32 | + |
| 33 | + ginkgo "github.com/onsi/ginkgo/v2" |
| 34 | + "github.com/onsi/gomega" |
| 35 | +) |
| 36 | + |
| 37 | +var _ = InPlaceE2eDescribe("In-Place", func() { |
| 38 | + |
| 39 | + f := framework.NewDefaultFramework("vertical-pod-autoscaling") |
| 40 | + f.NamespacePodSecurityEnforceLevel = podsecurity.LevelBaseline |
| 41 | + |
| 42 | + // TODO(jkyros): clean this up, some kind of helper, stash the InPlacePodVerticalScalingInUse function somewhere |
| 43 | + // useful |
| 44 | + // TODO(jkyros); the in-place tests check first to see if in-place is in use, and if it's not, there's nothing to test. I bet there's |
| 45 | + // precedent on how to test a gated feature with ginkgo, I should find out what it is |
| 46 | + var InPlacePodVerticalScalingNotInUse bool |
| 47 | + ginkgo.It("Should have InPlacePodVerticalScaling in-use", func() { |
| 48 | + |
| 49 | + ginkgo.By("Verifying the existence of container ResizePolicy") |
| 50 | + checkPod := &apiv1.Pod{} |
| 51 | + checkPod.Name = "inplace" |
| 52 | + checkPod.Namespace = f.Namespace.Name |
| 53 | + checkPod.Spec.Containers = append(checkPod.Spec.Containers, SetupHamsterContainer("100m", "10Mi")) |
| 54 | + _, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(context.Background(), checkPod, metav1.CreateOptions{}) |
| 55 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 56 | + checkPod, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(context.Background(), checkPod.Name, metav1.GetOptions{}) |
| 57 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 58 | + |
| 59 | + if !InPlacePodVerticalScalingInUse(checkPod) { |
| 60 | + InPlacePodVerticalScalingNotInUse = true |
| 61 | + ginkgo.Skip("InPlacePodVerticalScaling was not in use (containers had no ResizePolicy)") |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + ginkgo.It("In-place update pods when Admission Controller status available", func() { |
| 66 | + if InPlacePodVerticalScalingNotInUse { |
| 67 | + ginkgo.Skip("InPlacePodVerticalScaling was not in use (containers had no ResizePolicy)") |
| 68 | + } |
| 69 | + const statusUpdateInterval = 10 * time.Second |
| 70 | + |
| 71 | + ginkgo.By("Setting up the Admission Controller status") |
| 72 | + stopCh := make(chan struct{}) |
| 73 | + statusUpdater := status.NewUpdater( |
| 74 | + f.ClientSet, |
| 75 | + status.AdmissionControllerStatusName, |
| 76 | + status.AdmissionControllerStatusNamespace, |
| 77 | + statusUpdateInterval, |
| 78 | + "e2e test", |
| 79 | + ) |
| 80 | + defer func() { |
| 81 | + // Schedule a cleanup of the Admission Controller status. |
| 82 | + // Status is created outside the test namespace. |
| 83 | + ginkgo.By("Deleting the Admission Controller status") |
| 84 | + close(stopCh) |
| 85 | + err := f.ClientSet.CoordinationV1().Leases(status.AdmissionControllerStatusNamespace). |
| 86 | + Delete(context.TODO(), status.AdmissionControllerStatusName, metav1.DeleteOptions{}) |
| 87 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 88 | + }() |
| 89 | + statusUpdater.Run(stopCh) |
| 90 | + |
| 91 | + podList := setupPodsForUpscalingInPlace(f) |
| 92 | + if len(podList.Items[0].Spec.Containers[0].ResizePolicy) <= 0 { |
| 93 | + // Feature is probably not working here |
| 94 | + ginkgo.Skip("Skipping test, InPlacePodVerticalScaling not available") |
| 95 | + } |
| 96 | + |
| 97 | + initialPods := podList.DeepCopy() |
| 98 | + // 1. Take initial pod list |
| 99 | + // 2. Loop through and compare all the resource values |
| 100 | + // 3. When they change, it's good |
| 101 | + |
| 102 | + ginkgo.By("Waiting for pods to be in-place updated") |
| 103 | + |
| 104 | + //gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 105 | + err := WaitForPodsUpdatedWithoutEviction(f, initialPods, podList) |
| 106 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 107 | + }) |
| 108 | + |
| 109 | + ginkgo.It("Does not evict pods for downscaling in-place", func() { |
| 110 | + if InPlacePodVerticalScalingNotInUse { |
| 111 | + ginkgo.Skip("InPlacePodVerticalScaling was not in use (containers had no ResizePolicy)") |
| 112 | + } |
| 113 | + const statusUpdateInterval = 10 * time.Second |
| 114 | + |
| 115 | + ginkgo.By("Setting up the Admission Controller status") |
| 116 | + stopCh := make(chan struct{}) |
| 117 | + statusUpdater := status.NewUpdater( |
| 118 | + f.ClientSet, |
| 119 | + status.AdmissionControllerStatusName, |
| 120 | + status.AdmissionControllerStatusNamespace, |
| 121 | + statusUpdateInterval, |
| 122 | + "e2e test", |
| 123 | + ) |
| 124 | + defer func() { |
| 125 | + // Schedule a cleanup of the Admission Controller status. |
| 126 | + // Status is created outside the test namespace. |
| 127 | + ginkgo.By("Deleting the Admission Controller status") |
| 128 | + close(stopCh) |
| 129 | + err := f.ClientSet.CoordinationV1().Leases(status.AdmissionControllerStatusNamespace). |
| 130 | + Delete(context.TODO(), status.AdmissionControllerStatusName, metav1.DeleteOptions{}) |
| 131 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 132 | + }() |
| 133 | + statusUpdater.Run(stopCh) |
| 134 | + |
| 135 | + podList := setupPodsForDownscalingInPlace(f, nil) |
| 136 | + if len(podList.Items[0].Spec.Containers[0].ResizePolicy) <= 0 { |
| 137 | + // Feature is probably not working here |
| 138 | + ginkgo.Skip("Skipping test, InPlacePodVerticalScaling not available") |
| 139 | + } |
| 140 | + initialPods := podList.DeepCopy() |
| 141 | + |
| 142 | + ginkgo.By("Waiting for pods to be in-place downscaled") |
| 143 | + err := WaitForPodsUpdatedWithoutEviction(f, initialPods, podList) |
| 144 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 145 | + }) |
| 146 | + |
| 147 | + ginkgo.It("evicts pods when Admission Controller status available", func() { |
| 148 | + if InPlacePodVerticalScalingNotInUse { |
| 149 | + ginkgo.Skip("InPlacePodVerticalScaling was not in use (containers had no ResizePolicy)") |
| 150 | + } |
| 151 | + |
| 152 | + const statusUpdateInterval = 10 * time.Second |
| 153 | + |
| 154 | + ginkgo.By("Setting up the Admission Controller status") |
| 155 | + stopCh := make(chan struct{}) |
| 156 | + statusUpdater := status.NewUpdater( |
| 157 | + f.ClientSet, |
| 158 | + status.AdmissionControllerStatusName, |
| 159 | + status.AdmissionControllerStatusNamespace, |
| 160 | + statusUpdateInterval, |
| 161 | + "e2e test", |
| 162 | + ) |
| 163 | + defer func() { |
| 164 | + // Schedule a cleanup of the Admission Controller status. |
| 165 | + // Status is created outside the test namespace. |
| 166 | + ginkgo.By("Deleting the Admission Controller status") |
| 167 | + close(stopCh) |
| 168 | + err := f.ClientSet.CoordinationV1().Leases(status.AdmissionControllerStatusNamespace). |
| 169 | + Delete(context.TODO(), status.AdmissionControllerStatusName, metav1.DeleteOptions{}) |
| 170 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 171 | + }() |
| 172 | + statusUpdater.Run(stopCh) |
| 173 | + |
| 174 | + podList := setupPodsForUpscalingEviction(f) |
| 175 | + |
| 176 | + ginkgo.By("Waiting for pods to be evicted") |
| 177 | + err := WaitForPodsEvicted(f, podList) |
| 178 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 179 | + }) |
| 180 | + |
| 181 | + // TODO(jkyros): |
| 182 | + // - It falls back to eviction when in-place is Deferred for a minute (hard to fake, depends on node size) |
| 183 | + // - It falls back to eviction when in-place is Infeasible (easy to fake) |
| 184 | + // - It falls back to eviction when InProgress for more than an hour (maybe fake with annotation?) |
| 185 | + |
| 186 | +}) |
| 187 | + |
| 188 | +func setupPodsForUpscalingInPlace(f *framework.Framework) *apiv1.PodList { |
| 189 | + return setupPodsForInPlace(f, "100m", "100Mi", "200m", "200Mi", nil) |
| 190 | +} |
| 191 | + |
| 192 | +func setupPodsForDownscalingInPlace(f *framework.Framework, er []*vpa_types.EvictionRequirement) *apiv1.PodList { |
| 193 | + return setupPodsForInPlace(f, "500m", "500Mi", "600m", "600Mi", er) |
| 194 | +} |
| 195 | + |
| 196 | +func setupPodsForInPlace(f *framework.Framework, hamsterCPU, hamsterMemory, hamsterCPULimit, hamsterMemoryLimit string, er []*vpa_types.EvictionRequirement) *apiv1.PodList { |
| 197 | + controller := &autoscaling.CrossVersionObjectReference{ |
| 198 | + APIVersion: "apps/v1", |
| 199 | + Kind: "Deployment", |
| 200 | + Name: "hamster-deployment", |
| 201 | + } |
| 202 | + ginkgo.By(fmt.Sprintf("Setting up a hamster %v", controller.Kind)) |
| 203 | + // TODO(jkyros): I didn't want to mangle all the plumbing just yet |
| 204 | + //setupHamsterController(f, controller.Kind, hamsterCPU, hamsterMemory, defaultHamsterReplicas) |
| 205 | + |
| 206 | + // TODO(jkyros): we can't in-place scale without limits right now because of |
| 207 | + // https://github.com/kubernetes/kubernetes/blob/f4e246bc93ffb68b33ed67c7896c379efa4207e7/pkg/kubelet/kuberuntime/kuberuntime_manager.go#L550, |
| 208 | + // so if we want this to work, we need to add limits for now until we adjust that (assuming we can) |
| 209 | + SetupHamsterDeploymentWithLimits(f, hamsterCPU, hamsterMemory, hamsterCPULimit, hamsterMemoryLimit, defaultHamsterReplicas) |
| 210 | + podList, err := GetHamsterPods(f) |
| 211 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 212 | + |
| 213 | + ginkgo.By("Setting up a VPA CRD") |
| 214 | + containerName := GetHamsterContainerNameByIndex(0) |
| 215 | + vpaCRD := test.VerticalPodAutoscaler(). |
| 216 | + WithName("hamster-vpa"). |
| 217 | + WithNamespace(f.Namespace.Name). |
| 218 | + WithTargetRef(controller). |
| 219 | + WithUpdateMode(vpa_types.UpdateModeInPlaceOrRecreate). |
| 220 | + WithEvictionRequirements(er). |
| 221 | + WithContainer(containerName). |
| 222 | + AppendRecommendation( |
| 223 | + test.Recommendation(). |
| 224 | + WithContainer(containerName). |
| 225 | + WithTarget(containerName, "200m"). |
| 226 | + WithLowerBound(containerName, "200m"). |
| 227 | + WithUpperBound(containerName, "200m"). |
| 228 | + GetContainerResources()). |
| 229 | + Get() |
| 230 | + |
| 231 | + InstallVPA(f, vpaCRD) |
| 232 | + |
| 233 | + return podList |
| 234 | +} |
| 235 | + |
| 236 | +// InPlacePodVerticalScalingInUse returns true if pod spec is non-nil and ResizePolicy is set |
| 237 | +func InPlacePodVerticalScalingInUse(pod *apiv1.Pod) bool { |
| 238 | + if pod == nil { |
| 239 | + return false |
| 240 | + } |
| 241 | + for _, container := range pod.Spec.Containers { |
| 242 | + if len(container.ResizePolicy) > 0 { |
| 243 | + return true |
| 244 | + } |
| 245 | + } |
| 246 | + return false |
| 247 | +} |
0 commit comments