@@ -18,16 +18,11 @@ package vmop
1818
1919import (
2020 "context"
21- "errors"
22- "fmt"
2321
24- corev1 "k8s.io/api/core/v1"
25- "k8s.io/apimachinery/pkg/types"
2622 "sigs.k8s.io/controller-runtime/pkg/client"
2723 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2824
2925 "github.com/deckhouse/deckhouse/pkg/log"
30- "github.com/deckhouse/virtualization-controller/pkg/common/object"
3126 "github.com/deckhouse/virtualization-controller/pkg/controller/validator"
3227 "github.com/deckhouse/virtualization/api/core/v1alpha2"
3328)
@@ -36,115 +31,7 @@ func NewValidator(c client.Client, log *log.Logger) admission.CustomValidator {
3631 return validator.NewValidator [* v1alpha2.VirtualMachineOperation ](log .
3732 With ("controller" , "vmop-controller" ).
3833 With ("webhook" , "validation" ),
39- ).WithCreateValidators (& deprecateMigrateValidator {}, NewLocalVirtualDiskValidator (c ))
40- }
41-
42- type LocalVirtualDiskValidator struct {
43- client client.Client
44- }
45-
46- func NewLocalVirtualDiskValidator (client client.Client ) * LocalVirtualDiskValidator {
47- return & LocalVirtualDiskValidator {client : client }
48- }
49-
50- func (v * LocalVirtualDiskValidator ) ValidateCreate (ctx context.Context , vmop * v1alpha2.VirtualMachineOperation ) (admission.Warnings , error ) {
51- if vmop .Spec .Type != v1alpha2 .VMOPTypeEvict && vmop .Spec .Type != v1alpha2 .VMOPTypeMigrate {
52- return nil , nil
53- }
54-
55- vm , err := object .FetchObject (ctx , types.NamespacedName {
56- Namespace : vmop .Namespace ,
57- Name : vmop .Spec .VirtualMachine ,
58- }, v .client , & v1alpha2.VirtualMachine {})
59- if err != nil {
60- return nil , fmt .Errorf ("failed to fetch virtual machine %s: %w" , vmop .Spec .VirtualMachine , err )
61- }
62-
63- if vm == nil {
64- return nil , nil
65- }
66-
67- var hasHotplugs bool
68- var hasRWO bool
69-
70- for _ , bdRef := range vm .Status .BlockDeviceRefs {
71- if bdRef .Hotplugged {
72- hasHotplugs = true
73- }
74-
75- switch bdRef .Kind {
76- case v1alpha2 .VirtualDiskKind :
77- var vd * v1alpha2.VirtualDisk
78- vd , err = object .FetchObject (ctx , types.NamespacedName {
79- Namespace : vm .Namespace ,
80- Name : bdRef .Name ,
81- }, v .client , & v1alpha2.VirtualDisk {})
82- if err != nil {
83- return nil , fmt .Errorf ("failed to fetch virtual disk %s: %w" , bdRef .Name , err )
84- }
85-
86- if vd == nil || vd .Status .Target .PersistentVolumeClaim == "" {
87- return nil , nil
88- }
89-
90- var isRWO bool
91- isRWO , err = v .isRWOPersistentVolumeClaim (ctx , vd .Status .Target .PersistentVolumeClaim , vm .Namespace )
92- if err != nil {
93- return nil , err
94- }
95-
96- hasRWO = hasRWO || isRWO
97- case v1alpha2 .VirtualImageKind :
98- var vi * v1alpha2.VirtualImage
99- vi , err = object .FetchObject (ctx , types.NamespacedName {
100- Namespace : vm .Namespace ,
101- Name : bdRef .Name ,
102- }, v .client , & v1alpha2.VirtualImage {})
103- if err != nil {
104- return nil , fmt .Errorf ("failed to fetch virtual image %s: %w" , bdRef .Name , err )
105- }
106-
107- if vi == nil || vi .Status .Target .PersistentVolumeClaim == "" {
108- return nil , nil
109- }
110-
111- var isRWO bool
112- isRWO , err = v .isRWOPersistentVolumeClaim (ctx , vi .Status .Target .PersistentVolumeClaim , vm .Namespace )
113- if err != nil {
114- return nil , err
115- }
116-
117- hasRWO = hasRWO || isRWO
118- }
119- }
120-
121- if hasRWO && hasHotplugs {
122- return nil , errors .New ("for now, migration of the rwo virtual disk is not allowed if the virtual machine has hot-plugged block devices" )
123- }
124-
125- return nil , nil
126- }
127-
128- func (v * LocalVirtualDiskValidator ) isRWOPersistentVolumeClaim (ctx context.Context , pvcName , pvcNamespace string ) (bool , error ) {
129- pvc , err := object .FetchObject (ctx , types.NamespacedName {
130- Namespace : pvcNamespace ,
131- Name : pvcName ,
132- }, v .client , & corev1.PersistentVolumeClaim {})
133- if err != nil {
134- return false , fmt .Errorf ("failed to fetch pvc %s: %w" , pvcName , err )
135- }
136-
137- if pvc == nil {
138- return false , nil
139- }
140-
141- for _ , mode := range pvc .Status .AccessModes {
142- if mode == corev1 .ReadWriteOnce {
143- return true , nil
144- }
145- }
146-
147- return false , nil
34+ ).WithCreateValidators (& deprecateMigrateValidator {})
14835}
14936
15037type deprecateMigrateValidator struct {}
0 commit comments