|
| 1 | +/* |
| 2 | +Copyright 2026 Flant JSC |
| 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 vm |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + policyv1 "k8s.io/api/policy/v1" |
| 25 | + "k8s.io/apimachinery/pkg/api/resource" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/labels" |
| 28 | + "k8s.io/utils/ptr" |
| 29 | + crclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + |
| 31 | + vmbuilder "github.com/deckhouse/virtualization-controller/pkg/builder/vm" |
| 32 | + "github.com/deckhouse/virtualization/api/core/v1alpha2" |
| 33 | + "github.com/deckhouse/virtualization/test/e2e/internal/framework" |
| 34 | + "github.com/deckhouse/virtualization/test/e2e/internal/object" |
| 35 | + "github.com/deckhouse/virtualization/test/e2e/internal/util" |
| 36 | +) |
| 37 | + |
| 38 | +var _ = Describe("HotplugPod", func() { |
| 39 | + var ( |
| 40 | + f = framework.NewFramework("hotplug-pod") |
| 41 | + vi *v1alpha2.VirtualImage |
| 42 | + ) |
| 43 | + |
| 44 | + BeforeEach(func() { |
| 45 | + f.Before() |
| 46 | + DeferCleanup(f.After) |
| 47 | + |
| 48 | + newVI := object.NewGeneratedHTTPVIUbuntu("hotplug-pod-", f.Namespace().Name) |
| 49 | + newVI, err := f.VirtClient().VirtualImages(f.Namespace().Name).Create(context.Background(), newVI, metav1.CreateOptions{}) |
| 50 | + Expect(err).NotTo(HaveOccurred()) |
| 51 | + f.DeferDelete(newVI) |
| 52 | + vi = newVI |
| 53 | + }) |
| 54 | + |
| 55 | + It("Should protect hotplug pod", func() { |
| 56 | + var ( |
| 57 | + vm *v1alpha2.VirtualMachine |
| 58 | + blank *v1alpha2.VirtualDisk |
| 59 | + ) |
| 60 | + By("Create VM", func() { |
| 61 | + root := object.NewVDFromVI("root", f.Namespace().Name, vi) |
| 62 | + blank = object.NewBlankVD("blank", f.Namespace().Name, nil, ptr.To(resource.MustParse("100Mi"))) |
| 63 | + Expect(f.CreateWithDeferredDeletion(context.Background(), root, blank)).To(Succeed()) |
| 64 | + |
| 65 | + var err error |
| 66 | + vm = object.NewMinimalVM("hotplug-pod-", f.Namespace().Name, vmbuilder.WithDisks(root)) |
| 67 | + vm, err = f.VirtClient().VirtualMachines(f.Namespace().Name).Create(context.Background(), vm, metav1.CreateOptions{}) |
| 68 | + Expect(err).NotTo(HaveOccurred()) |
| 69 | + f.DeferDelete(vm) |
| 70 | + }) |
| 71 | + |
| 72 | + By("Wait until VM agent is ready", func() { |
| 73 | + util.UntilVMAgentReady(crclient.ObjectKeyFromObject(vm), framework.LongTimeout) |
| 74 | + }) |
| 75 | + |
| 76 | + By("Attaching disk", func() { |
| 77 | + vmbda := object.NewVMBDAFromDisk(vm.Name, vm.Name, blank) |
| 78 | + Expect(f.CreateWithDeferredDeletion(context.Background(), vmbda)).To(Succeed()) |
| 79 | + util.UntilObjectPhase(string(v1alpha2.BlockDeviceAttachmentPhaseAttached), framework.MiddleTimeout, vmbda) |
| 80 | + }) |
| 81 | + |
| 82 | + By("Evict hp pod", func() { |
| 83 | + pods, err := f.KubeClient().CoreV1().Pods(f.Namespace().Name).List(context.Background(), metav1.ListOptions{ |
| 84 | + LabelSelector: labels.SelectorFromSet(map[string]string{ |
| 85 | + "kubevirt.internal.virtualization.deckhouse.io": "d8v-hotplug-disk", |
| 86 | + }).String(), |
| 87 | + }) |
| 88 | + Expect(err).NotTo(HaveOccurred()) |
| 89 | + Expect(pods.Items).To(HaveLen(1)) |
| 90 | + |
| 91 | + pod := pods.Items[0] |
| 92 | + |
| 93 | + err = f.KubeClient().CoreV1().Pods(pod.GetNamespace()).EvictV1(context.Background(), &policyv1.Eviction{ |
| 94 | + ObjectMeta: metav1.ObjectMeta{ |
| 95 | + Name: pod.GetName(), |
| 96 | + Namespace: pod.GetNamespace(), |
| 97 | + }, |
| 98 | + }) |
| 99 | + Expect(err).To(HaveOccurred()) |
| 100 | + Expect(err.Error()).To(ContainSubstring("cannot evict hotplug pod")) |
| 101 | + }) |
| 102 | + }) |
| 103 | +}) |
0 commit comments