Skip to content

Commit 5dfea63

Browse files
committed
remove mode from handlers
Signed-off-by: Daniil Antoshin <[email protected]>
1 parent f2eb6f8 commit 5dfea63

File tree

11 files changed

+33
-83
lines changed

11 files changed

+33
-83
lines changed

images/virtualization-artifact/pkg/controller/service/restorer/restorers/provisioner_restorer.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package restorer
1919
import (
2020
"bytes"
2121
"context"
22-
"errors"
2322
"fmt"
2423
"maps"
2524

@@ -35,13 +34,12 @@ import (
3534
)
3635

3736
type ProvisionerHandler struct {
38-
mode common.OperationMode
3937
secret *corev1.Secret
4038
client client.Client
4139
restoreUID string
4240
}
4341

44-
func NewProvisionerHandler(client client.Client, mode common.OperationMode, secretTmpl corev1.Secret, vmRestoreUID string) *ProvisionerHandler {
42+
func NewProvisionerHandler(client client.Client, secretTmpl corev1.Secret, vmRestoreUID string) *ProvisionerHandler {
4543
if secretTmpl.Annotations != nil {
4644
secretTmpl.Annotations[annotations.AnnVMRestore] = vmRestoreUID
4745
} else {
@@ -65,7 +63,6 @@ func NewProvisionerHandler(client client.Client, mode common.OperationMode, secr
6563
StringData: secretTmpl.StringData,
6664
Type: secretTmpl.Type,
6765
},
68-
mode: mode,
6966
client: client,
7067
restoreUID: vmRestoreUID,
7168
}
@@ -90,8 +87,7 @@ func (v *ProvisionerHandler) ValidateRestore(ctx context.Context) error {
9087
return nil
9188
}
9289

93-
// If kind is BestEffort, we don't care about the data, in this case, we just need to skip creating the secret
94-
if v.mode != common.BestEffortRestorerMode && !maps.EqualFunc(existed.Data, v.secret.Data, bytes.Equal) {
90+
if !maps.EqualFunc(existed.Data, v.secret.Data, bytes.Equal) {
9591
return fmt.Errorf("the provisioner secret %q %w", secretKey.Name, common.ErrAlreadyExistsAndHasDiff)
9692
}
9793

@@ -103,10 +99,6 @@ func (v *ProvisionerHandler) ValidateClone(ctx context.Context) error {
10399
}
104100

105101
func (v *ProvisionerHandler) ProcessRestore(ctx context.Context) error {
106-
if v.mode == common.DryRunMode {
107-
return errors.New("cannot Process with DryRun operation")
108-
}
109-
110102
err := v.ValidateRestore(ctx)
111103
if err != nil {
112104
return err

images/virtualization-artifact/pkg/controller/service/restorer/restorers/provisioner_restorer_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ import (
2828
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
2929

3030
"github.com/deckhouse/virtualization-controller/pkg/common/testutil"
31-
"github.com/deckhouse/virtualization-controller/pkg/controller/service/restorer/common"
3231
)
3332

3433
type ProvisionerTestArgs struct {
35-
mode common.OperationMode
36-
3734
secretExists bool
3835
secretDiffData bool
3936

@@ -108,7 +105,7 @@ var _ = Describe("ProvisionerRestorer", func() {
108105
Expect(fakeClient).ToNot(BeNil())
109106

110107
secret.Data = map[string][]byte{"data": []byte("data")}
111-
handler = NewProvisionerHandler(fakeClient, args.mode, secret, uid)
108+
handler = NewProvisionerHandler(fakeClient, secret, uid)
112109
Expect(handler).ToNot(BeNil())
113110

114111
err = handler.ValidateRestore(ctx)
@@ -128,8 +125,6 @@ var _ = Describe("ProvisionerRestorer", func() {
128125
Expect(secretCreated).To(Equal(args.shouldBeCreated))
129126
},
130127
Entry("secret exists; different data", ProvisionerTestArgs{
131-
mode: common.StrictRestoreMode,
132-
133128
secretExists: true,
134129
secretDiffData: true,
135130

@@ -139,8 +134,6 @@ var _ = Describe("ProvisionerRestorer", func() {
139134
shouldBeCreated: false,
140135
}),
141136
Entry("secret exists; same data", ProvisionerTestArgs{
142-
mode: common.StrictRestoreMode,
143-
144137
secretExists: true,
145138
secretDiffData: false,
146139

@@ -150,8 +143,6 @@ var _ = Describe("ProvisionerRestorer", func() {
150143
shouldBeCreated: false,
151144
}),
152145
Entry("secret doesn't exist", ProvisionerTestArgs{
153-
mode: common.StrictRestoreMode,
154-
155146
secretExists: false,
156147
secretDiffData: false,
157148

images/virtualization-artifact/pkg/controller/service/restorer/restorers/vd_restorer.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package restorer
1818

1919
import (
2020
"context"
21-
"errors"
2221
"fmt"
2322

2423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -32,13 +31,12 @@ import (
3231
)
3332

3433
type VirtualDiskHandler struct {
35-
mode common.OperationMode
3634
vd *v1alpha2.VirtualDisk
3735
client client.Client
3836
restoreUID string
3937
}
4038

41-
func NewVirtualDiskHandler(client client.Client, mode common.OperationMode, vdTmpl v1alpha2.VirtualDisk, vmRestoreUID string) *VirtualDiskHandler {
39+
func NewVirtualDiskHandler(client client.Client, vdTmpl v1alpha2.VirtualDisk, vmRestoreUID string) *VirtualDiskHandler {
4240
if vdTmpl.Annotations != nil {
4341
vdTmpl.Annotations[annotations.AnnVMRestore] = vmRestoreUID
4442
} else {
@@ -60,7 +58,6 @@ func NewVirtualDiskHandler(client client.Client, mode common.OperationMode, vdTm
6058
Spec: vdTmpl.Spec,
6159
Status: vdTmpl.Status,
6260
},
63-
mode: mode,
6461
client: client,
6562
restoreUID: vmRestoreUID,
6663
}
@@ -99,10 +96,6 @@ func (v *VirtualDiskHandler) ValidateClone(ctx context.Context) error {
9996
}
10097

10198
func (v *VirtualDiskHandler) ProcessRestore(ctx context.Context) error {
102-
if v.mode == common.DryRunMode {
103-
return errors.New("cannot Process with DryRun operation")
104-
}
105-
10699
err := v.ValidateRestore(ctx)
107100
if err != nil {
108101
return err

images/virtualization-artifact/pkg/controller/service/restorer/restorers/vd_restorer_test.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
2727

2828
"github.com/deckhouse/virtualization-controller/pkg/common/testutil"
29-
"github.com/deckhouse/virtualization-controller/pkg/controller/service/restorer/common"
3029
"github.com/deckhouse/virtualization/api/core/v1alpha2"
3130
)
3231

3332
type VirtualDiskTestArgs struct {
34-
mode common.OperationMode
35-
3633
diskExists bool
3734
diskUsedByDiffVM bool
3835

@@ -134,7 +131,7 @@ var _ = Describe("VirtualDiskRestorer", func() {
134131
Expect(fakeClient).ToNot(BeNil())
135132

136133
disk.Status.AttachedToVirtualMachines = []v1alpha2.AttachedVirtualMachine{{Name: vm, Mounted: true}}
137-
handler = NewVirtualDiskHandler(fakeClient, args.mode, disk, uid)
134+
handler = NewVirtualDiskHandler(fakeClient, disk, uid)
138135
Expect(handler).ToNot(BeNil())
139136

140137
err = handler.ValidateRestore(ctx)
@@ -155,8 +152,6 @@ var _ = Describe("VirtualDiskRestorer", func() {
155152
Expect(diskCreated).To(Equal(args.shouldBeCreated))
156153
},
157154
Entry("disk exists; used by different VM", VirtualDiskTestArgs{
158-
mode: common.StrictRestoreMode,
159-
160155
diskExists: true,
161156
diskUsedByDiffVM: true,
162157

@@ -167,8 +162,6 @@ var _ = Describe("VirtualDiskRestorer", func() {
167162
shouldBeCreated: false,
168163
}),
169164
Entry("disk exists; not used by different VM", VirtualDiskTestArgs{
170-
mode: common.StrictRestoreMode,
171-
172165
diskExists: true,
173166
diskUsedByDiffVM: false,
174167

@@ -179,8 +172,6 @@ var _ = Describe("VirtualDiskRestorer", func() {
179172
shouldBeCreated: true,
180173
}),
181174
Entry("disk doesn't exist", VirtualDiskTestArgs{
182-
mode: common.StrictRestoreMode,
183-
184175
diskExists: false,
185176
diskUsedByDiffVM: false,
186177

images/virtualization-artifact/pkg/controller/service/restorer/restorers/vm_restorer.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ import (
2828

2929
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
3030
"github.com/deckhouse/virtualization-controller/pkg/common/object"
31+
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
3132
"github.com/deckhouse/virtualization-controller/pkg/controller/service/restorer/common"
3233
"github.com/deckhouse/virtualization/api/core/v1alpha2"
34+
"github.com/deckhouse/virtualization/api/core/v1alpha2/vmcondition"
3335
)
3436

3537
const ReasonPVCNotFound = "PVC not found"
3638

3739
type VirtualMachineHandler struct {
38-
kind common.OperationMode
3940
vm *v1alpha2.VirtualMachine
4041
client client.Client
4142
restoreUID string
4243
}
4344

44-
func NewVirtualMachineHandler(client client.Client, kind common.OperationMode, vmTmpl v1alpha2.VirtualMachine, vmopRestoreUID string) *VirtualMachineHandler {
45+
func NewVirtualMachineHandler(client client.Client, vmTmpl v1alpha2.VirtualMachine, vmopRestoreUID string) *VirtualMachineHandler {
4546
if vmTmpl.Annotations != nil {
4647
vmTmpl.Annotations[annotations.AnnVMRestore] = vmopRestoreUID
4748
} else {
@@ -63,7 +64,6 @@ func NewVirtualMachineHandler(client client.Client, kind common.OperationMode, v
6364
},
6465
Spec: vmTmpl.Spec,
6566
},
66-
kind: kind,
6767
client: client,
6868
restoreUID: vmopRestoreUID,
6969
}
@@ -102,9 +102,18 @@ func (v *VirtualMachineHandler) ValidateRestore(ctx context.Context) error {
102102
}
103103

104104
if existed != nil {
105-
if value, ok := existed.Annotations[annotations.AnnVMRestore]; ok && value == v.restoreUID {
105+
if value, ok := existed.Annotations[annotations.AnnVMRestore]; ok && value != v.restoreUID {
106106
return nil
107107
}
108+
109+
cond, err := conditions.GetCondition(vmcondition.TypeRunning, existed.Status.Conditions)
110+
if err {
111+
return fmt.Errorf("failed to get the `VirtualMachine` %s condition: %w", existed.Name, err)
112+
}
113+
114+
if cond.Status != metav1.ConditionTrue {
115+
return fmt.Errorf("the `VirtualMachine` %s is not in maintenance mode", existed.Name)
116+
}
108117
}
109118

110119
return nil
@@ -115,6 +124,11 @@ func (v *VirtualMachineHandler) ValidateClone(ctx context.Context) error {
115124
}
116125

117126
func (v *VirtualMachineHandler) ProcessRestore(ctx context.Context) error {
127+
err := v.ValidateRestore(ctx)
128+
if err != nil {
129+
return err
130+
}
131+
118132
vmKey := types.NamespacedName{Namespace: v.vm.Namespace, Name: v.vm.Name}
119133
vm, err := object.FetchObject(ctx, vmKey, v.client, &v1alpha2.VirtualMachine{})
120134
if err != nil {

images/virtualization-artifact/pkg/controller/service/restorer/restorers/vm_restorer_test.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,10 @@ import (
2727

2828
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
2929
"github.com/deckhouse/virtualization-controller/pkg/common/testutil"
30-
"github.com/deckhouse/virtualization-controller/pkg/controller/service/restorer/common"
3130
"github.com/deckhouse/virtualization/api/core/v1alpha2"
3231
)
3332

3433
type VirtualMachineTestArgs struct {
35-
mode common.OperationMode
36-
3734
vmExists bool
3835
vmHasCorrectRestoreUID bool
3936
vmHasCorrectSpec bool
@@ -214,7 +211,7 @@ var _ = Describe("VirtualMachineRestorer", func() {
214211
Expect(err).ToNot(HaveOccurred())
215212
Expect(fakeClient).ToNot(BeNil())
216213

217-
handler = NewVirtualMachineHandler(fakeClient, args.mode, vm, restoreUID)
214+
handler = NewVirtualMachineHandler(fakeClient, vm, restoreUID)
218215
Expect(handler).ToNot(BeNil())
219216

220217
// Verify that restore annotation was added
@@ -250,8 +247,6 @@ var _ = Describe("VirtualMachineRestorer", func() {
250247
}
251248
},
252249
Entry("VM doesn't exist", VirtualMachineTestArgs{
253-
mode: common.StrictRestoreMode,
254-
255250
vmExists: false,
256251

257252
failValidation: false,
@@ -262,8 +257,6 @@ var _ = Describe("VirtualMachineRestorer", func() {
262257
shouldDeleteVMBDAs: true,
263258
}),
264259
Entry("VM exists with correct restore UID and correct spec", VirtualMachineTestArgs{
265-
mode: common.StrictRestoreMode,
266-
267260
vmExists: true,
268261
vmHasCorrectRestoreUID: true,
269262
vmHasCorrectSpec: true,
@@ -276,8 +269,6 @@ var _ = Describe("VirtualMachineRestorer", func() {
276269
shouldDeleteVMBDAs: true,
277270
}),
278271
Entry("VM exists with incorrect restore UID", VirtualMachineTestArgs{
279-
mode: common.StrictRestoreMode,
280-
281272
vmExists: true,
282273
vmHasCorrectRestoreUID: false,
283274
vmHasCorrectSpec: true,
@@ -290,8 +281,6 @@ var _ = Describe("VirtualMachineRestorer", func() {
290281
shouldDeleteVMBDAs: true,
291282
}),
292283
Entry("VM exists with incorrect spec", VirtualMachineTestArgs{
293-
mode: common.StrictRestoreMode,
294-
295284
vmExists: true,
296285
vmHasCorrectRestoreUID: true,
297286
vmHasCorrectSpec: false,
@@ -304,8 +293,6 @@ var _ = Describe("VirtualMachineRestorer", func() {
304293
shouldDeleteVMBDAs: true,
305294
}),
306295
Entry("VM exists with incorrect UID and spec", VirtualMachineTestArgs{
307-
mode: common.StrictRestoreMode,
308-
309296
vmExists: true,
310297
vmHasCorrectRestoreUID: false,
311298
vmHasCorrectSpec: false,
@@ -318,8 +305,6 @@ var _ = Describe("VirtualMachineRestorer", func() {
318305
shouldDeleteVMBDAs: true,
319306
}),
320307
Entry("VM exists with VMBDAs that should be deleted", VirtualMachineTestArgs{
321-
mode: common.StrictRestoreMode,
322-
323308
vmExists: true,
324309
vmHasCorrectRestoreUID: true,
325310
vmHasCorrectSpec: true,
@@ -334,8 +319,6 @@ var _ = Describe("VirtualMachineRestorer", func() {
334319
shouldDeleteVMBDAs: true,
335320
}),
336321
Entry("VM exists with VMBDAs that should not be deleted", VirtualMachineTestArgs{
337-
mode: common.StrictRestoreMode,
338-
339322
vmExists: true,
340323
vmHasCorrectRestoreUID: true,
341324
vmHasCorrectSpec: true,
@@ -389,7 +372,7 @@ var _ = Describe("VirtualMachineRestorer", func() {
389372
fakeClient, err = testutil.NewFakeClientWithInterceptorWithObjects(intercept)
390373
Expect(err).ToNot(HaveOccurred())
391374

392-
handler = NewVirtualMachineHandler(fakeClient, common.StrictRestoreMode, vm, restoreUID)
375+
handler = NewVirtualMachineHandler(fakeClient, vm, restoreUID)
393376
})
394377

395378
It("should override VM name", func() {

images/virtualization-artifact/pkg/controller/service/restorer/restorers/vmbda_restorer.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package restorer
1818

1919
import (
2020
"context"
21-
"errors"
2221
"fmt"
2322

2423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -32,13 +31,12 @@ import (
3231
)
3332

3433
type VMBlockDeviceAttachmentHandler struct {
35-
mode common.OperationMode
3634
vmbda *v1alpha2.VirtualMachineBlockDeviceAttachment
3735
client client.Client
3836
restoreUID string
3937
}
4038

41-
func NewVMBlockDeviceAttachmentHandler(client client.Client, mode common.OperationMode, vmbdaTmpl v1alpha2.VirtualMachineBlockDeviceAttachment, vmRestoreUID string) *VMBlockDeviceAttachmentHandler {
39+
func NewVMBlockDeviceAttachmentHandler(client client.Client, vmbdaTmpl v1alpha2.VirtualMachineBlockDeviceAttachment, vmRestoreUID string) *VMBlockDeviceAttachmentHandler {
4240
if vmbdaTmpl.Annotations != nil {
4341
vmbdaTmpl.Annotations[annotations.AnnVMRestore] = vmRestoreUID
4442
} else {
@@ -59,7 +57,6 @@ func NewVMBlockDeviceAttachmentHandler(client client.Client, mode common.Operati
5957
},
6058
Spec: vmbdaTmpl.Spec,
6159
},
62-
mode: mode,
6360
client: client,
6461
restoreUID: vmRestoreUID,
6562
}
@@ -104,10 +101,6 @@ func (v *VMBlockDeviceAttachmentHandler) ValidateClone(ctx context.Context) erro
104101
}
105102

106103
func (v *VMBlockDeviceAttachmentHandler) ProcessRestore(ctx context.Context) error {
107-
if v.mode == common.DryRunMode {
108-
return errors.New("cannot Process with DryRun operation")
109-
}
110-
111104
err := v.ValidateRestore(ctx)
112105
if err != nil {
113106
return err

0 commit comments

Comments
 (0)