Skip to content

Commit 310da17

Browse files
author
Roman Sysoev
committed
feat(vd): remove finalizer from unmounted vd
`vd-protection` will be removed from the virtual disk if the virtual machine is stopped. Signed-off-by: Roman Sysoev <roman.sysoev@flant.com>
1 parent 37b281d commit 310da17

2 files changed

Lines changed: 120 additions & 0 deletions

File tree

images/virtualization-artifact/pkg/controller/vd/internal/protection.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ func (h ProtectionHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (
4343
case len(vd.Status.AttachedToVirtualMachines) == 0:
4444
log.Debug("Allow virtual disk deletion")
4545
controllerutil.RemoveFinalizer(vd, virtv2.FinalizerVDProtection)
46+
case len(vd.Status.AttachedToVirtualMachines) != 0:
47+
unmounted := true
48+
for _, vm := range vd.Status.AttachedToVirtualMachines {
49+
if vm.Mounted {
50+
unmounted = false
51+
}
52+
}
53+
if unmounted {
54+
log.Debug("Allow virtual disk deletion")
55+
controllerutil.RemoveFinalizer(vd, virtv2.FinalizerVDProtection)
56+
}
4657
case vd.DeletionTimestamp == nil:
4758
log.Debug("Protect virtual disk from deletion")
4859
controllerutil.AddFinalizer(vd, virtv2.FinalizerVDProtection)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright 2025 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 internal
18+
19+
import (
20+
"context"
21+
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
25+
virtv1 "kubevirt.io/api/core/v1"
26+
27+
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
28+
. "github.com/onsi/ginkgo/v2"
29+
. "github.com/onsi/gomega"
30+
)
31+
32+
var _ = Describe("The protection handler test", func() {
33+
const vdProtection = "virtualization.deckhouse.io/vd-protection"
34+
35+
var (
36+
schema *runtime.Scheme
37+
ctx context.Context
38+
handler *ProtectionHandler
39+
)
40+
41+
BeforeEach(func() {
42+
schema = runtime.NewScheme()
43+
Expect(clientgoscheme.AddToScheme(schema)).To(Succeed())
44+
Expect(virtv2.AddToScheme(schema)).To(Succeed())
45+
Expect(virtv1.AddToScheme(schema)).To(Succeed())
46+
47+
ctx = context.TODO()
48+
})
49+
50+
Context("`VirtualDisk`", func() {
51+
When("has the `AttachedToVirtualMachines` status with the `Mounted` false value", func() {
52+
It("should remove the `vd-protection` finalizer from the `VirtualDisk`", func() {
53+
vd := &virtv2.VirtualDisk{
54+
ObjectMeta: metav1.ObjectMeta{
55+
Name: "test-virtual-disk",
56+
Namespace: "default",
57+
Finalizers: []string{
58+
vdProtection,
59+
},
60+
},
61+
Status: virtv2.VirtualDiskStatus{
62+
Conditions: []metav1.Condition{},
63+
AttachedToVirtualMachines: []virtv2.AttachedVirtualMachine{
64+
{
65+
Name: "test-virtual-machine",
66+
Mounted: false,
67+
},
68+
},
69+
},
70+
}
71+
72+
handler = &ProtectionHandler{}
73+
result, err := handler.Handle(ctx, vd)
74+
Expect(err).NotTo(HaveOccurred())
75+
Expect(result.IsZero()).To(BeTrue())
76+
Expect(vd.Finalizers).NotTo(ContainElement(vdProtection))
77+
})
78+
})
79+
80+
When("has the `AttachedToVirtualMachines` status with the `Mounted` true value", func() {
81+
It("should not remove the `vd-protection` finalizer from the `VirtualDisk`", func() {
82+
vd := &virtv2.VirtualDisk{
83+
ObjectMeta: metav1.ObjectMeta{
84+
Name: "test-virtual-disk",
85+
Namespace: "default",
86+
Finalizers: []string{
87+
vdProtection,
88+
},
89+
},
90+
Status: virtv2.VirtualDiskStatus{
91+
Conditions: []metav1.Condition{},
92+
AttachedToVirtualMachines: []virtv2.AttachedVirtualMachine{
93+
{
94+
Name: "test-virtual-machine",
95+
Mounted: true,
96+
},
97+
},
98+
},
99+
}
100+
101+
handler = &ProtectionHandler{}
102+
result, err := handler.Handle(ctx, vd)
103+
Expect(err).NotTo(HaveOccurred())
104+
Expect(result.IsZero()).To(BeTrue())
105+
Expect(vd.Finalizers).To(ContainElement(vdProtection))
106+
})
107+
})
108+
})
109+
})

0 commit comments

Comments
 (0)