Skip to content

Commit e0cd516

Browse files
fix
Signed-off-by: Yaroslav Borbat <[email protected]>
1 parent cfe195d commit e0cd516

File tree

19 files changed

+596
-87
lines changed

19 files changed

+596
-87
lines changed

images/virtualization-artifact/cmd/virtualization-controller/main.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import (
5757
"github.com/deckhouse/virtualization-controller/pkg/controller/vmop"
5858
"github.com/deckhouse/virtualization-controller/pkg/controller/vmrestore"
5959
"github.com/deckhouse/virtualization-controller/pkg/controller/vmsnapshot"
60+
"github.com/deckhouse/virtualization-controller/pkg/controller/volumemigration"
6061
workloadupdater "github.com/deckhouse/virtualization-controller/pkg/controller/workload-updater"
6162
"github.com/deckhouse/virtualization-controller/pkg/featuregates"
6263
"github.com/deckhouse/virtualization-controller/pkg/logger"
@@ -389,12 +390,20 @@ func main() {
389390
os.Exit(1)
390391
}
391392

392-
if err = workloadupdater.SetupController(ctx, mgr, log, firmwareImage, controllerNamespace, virtControllerName); err != nil {
393+
workloadUpdaterLogger := logger.NewControllerLogger(workloadupdater.ControllerName, logLevel, logOutput, logDebugVerbosity, logDebugControllerList)
394+
if err = workloadupdater.SetupController(ctx, mgr, workloadUpdaterLogger, firmwareImage, controllerNamespace, virtControllerName); err != nil {
393395
log.Error(err.Error())
394396
os.Exit(1)
395397
}
396398

397-
if err = evacuation.SetupController(ctx, mgr, virtClient, log); err != nil {
399+
evacuationLogger := logger.NewControllerLogger(evacuation.ControllerName, logLevel, logOutput, logDebugVerbosity, logDebugControllerList)
400+
if err = evacuation.SetupController(ctx, mgr, virtClient, evacuationLogger); err != nil {
401+
log.Error(err.Error())
402+
os.Exit(1)
403+
}
404+
405+
volumeMigrationLogger := logger.NewControllerLogger(volumemigration.ControllerName, logLevel, logOutput, logDebugVerbosity, logDebugControllerList)
406+
if err = volumemigration.SetupController(ctx, mgr, volumeMigrationLogger); err != nil {
398407
log.Error(err.Error())
399408
os.Exit(1)
400409
}

images/virtualization-artifact/pkg/common/annotations/annotations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ const (
9696
AnnVMRestore = AnnAPIGroupV + "/vmrestore"
9797
// AnnVMOPEvacuation is an annotation on vmop that represents a vmop created by evacuation controller
9898
AnnVMOPEvacuation = AnnAPIGroupV + "/evacuation"
99+
// AnnVMOPVolumeMigration is an annotation on vmop that represents a vmop created by volume-migration controller
100+
AnnVMOPVolumeMigration = AnnAPIGroupV + "/volume-migration"
99101

100102
// LabelsPrefix is a prefix for virtualization-controller labels.
101103
LabelsPrefix = "virtualization.deckhouse.io"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 vd
18+
19+
import virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
20+
21+
func CurrentlyMountedVM(vd *virtv2.VirtualDisk) string {
22+
var currentlyMountedVM string
23+
for _, attachedVM := range vd.Status.AttachedToVirtualMachines {
24+
if attachedVM.Mounted {
25+
currentlyMountedVM = attachedVM.Name
26+
break
27+
}
28+
}
29+
return currentlyMountedVM
30+
}

images/virtualization-artifact/pkg/controller/evacuation/internal/handler/evacuation.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"log/slog"
23+
"time"
2324

2425
k8serrors "k8s.io/apimachinery/pkg/api/errors"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -67,9 +68,9 @@ func (h *EvacuationHandler) Handle(ctx context.Context, vm *v1alpha2.VirtualMach
6768

6869
log := logger.FromContext(ctx).With(logger.SlogHandler(nameEvacuationHandler))
6970

70-
needRequeue := false
71+
var requeueAfter time.Duration
7172
if err = h.removeFinalizerFromVMOPs(ctx, finishedVMOPs); err != nil {
72-
needRequeue = true
73+
requeueAfter = 100 * time.Millisecond
7374
if k8serrors.IsConflict(err) {
7475
log.Debug("Conflict occurred during handler execution", logger.SlogErr(err))
7576
} else {
@@ -81,7 +82,7 @@ func (h *EvacuationHandler) Handle(ctx context.Context, vm *v1alpha2.VirtualMach
8182
if err = h.cancelEvacuationForTerminatingVMOPs(ctx, migrationVMOPs, log); err != nil {
8283
return reconcile.Result{}, err
8384
}
84-
return reconcile.Result{Requeue: needRequeue}, nil
85+
return reconcile.Result{RequeueAfter: requeueAfter}, nil
8586
}
8687

8788
if !isVMNeedEvict(vm) || isVMMigrating(vm) {
@@ -94,7 +95,7 @@ func (h *EvacuationHandler) Handle(ctx context.Context, vm *v1alpha2.VirtualMach
9495
return reconcile.Result{}, err
9596
}
9697

97-
return reconcile.Result{Requeue: needRequeue}, nil
98+
return reconcile.Result{RequeueAfter: requeueAfter}, nil
9899
}
99100

100101
func (h *EvacuationHandler) Name() string {

images/virtualization-artifact/pkg/controller/reconciler/reconciler.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
"github.com/deckhouse/virtualization-controller/pkg/logger"
3232
)
3333

34+
var ErrStopReconciliation = errors.New("stop reconciliation")
35+
3436
type Handler[T client.Object] interface {
3537
Handle(ctx context.Context, obj T) (reconcile.Result, error)
3638
Name() string
@@ -77,12 +79,17 @@ func (r *BaseReconciler[H]) Reconcile(ctx context.Context) (reconcile.Result, er
7779
var result reconcile.Result
7880
var errs error
7981

82+
handlerLoop:
8083
for _, h := range r.handlers {
8184
log := logger.FromContext(ctx).With(logger.SlogHandler(reflect.TypeOf(h).Elem().Name()))
8285

8386
res, err := r.execute(ctx, h)
8487
switch {
8588
case err == nil: // OK.
89+
case errors.Is(err, ErrStopReconciliation):
90+
result = MergeResults(result, res)
91+
break handlerLoop
92+
8693
case k8serrors.IsConflict(err):
8794
log.Debug("Conflict occurred during handler execution", logger.SlogErr(err))
8895
result.RequeueAfter = 100 * time.Microsecond
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
"fmt"
22+
"time"
23+
24+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
25+
26+
"github.com/deckhouse/virtualization-controller/pkg/common/pwgen"
27+
"github.com/deckhouse/virtualization-controller/pkg/controller/reconciler"
28+
vdsupplements "github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/supplements"
29+
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
30+
)
31+
32+
type InitHandler struct{}
33+
34+
func NewInitHandler() *InitHandler {
35+
return &InitHandler{}
36+
}
37+
38+
func (h *InitHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (reconcile.Result, error) {
39+
// INIT PersistentVolumeClaim Name.
40+
// Required for correct work virtual disk supplements.
41+
// We should have different names for support migration volumes.
42+
// If the PVC name is empty, we should generate it and update the status immediately.
43+
if vd.Status.Target.PersistentVolumeClaim == "" {
44+
name := fmt.Sprintf("vd-%s-%s", vd.UID, pwgen.LowerAlpha(5))
45+
vdsupplements.SetPVCName(vd, name)
46+
return reconcile.Result{RequeueAfter: 100 * time.Millisecond}, reconciler.ErrStopReconciliation
47+
}
48+
return reconcile.Result{}, nil
49+
}

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131

3232
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
3333
"github.com/deckhouse/virtualization-controller/pkg/common/object"
34+
commonvd "github.com/deckhouse/virtualization-controller/pkg/common/vd"
3435
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
3536
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
3637
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition"
@@ -223,7 +224,7 @@ func (h InUseHandler) isVMActive(ctx context.Context, vm virtv2.VirtualMachine)
223224
}
224225

225226
func (h InUseHandler) updateAttachedVirtualMachinesStatus(vd *virtv2.VirtualDisk, usageMap map[string]bool) {
226-
currentlyMountedVM := currentlyMountedVM(vd)
227+
currentlyMountedVM := commonvd.CurrentlyMountedVM(vd)
227228

228229
attachedVMs := make([]virtv2.AttachedVirtualMachine, 0, len(usageMap))
229230
setAnyToTrue := false
@@ -312,14 +313,3 @@ func (h InUseHandler) checkUsageByCVI(ctx context.Context, vd *virtv2.VirtualDis
312313

313314
return false, nil
314315
}
315-
316-
func currentlyMountedVM(vd *virtv2.VirtualDisk) string {
317-
var currentlyMountedVM string
318-
for _, attachedVM := range vd.Status.AttachedToVirtualMachines {
319-
if attachedVM.Mounted {
320-
currentlyMountedVM = attachedVM.Name
321-
break
322-
}
323-
}
324-
return currentlyMountedVM
325-
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ import (
3131
"github.com/deckhouse/virtualization-controller/pkg/common/object"
3232
"github.com/deckhouse/virtualization-controller/pkg/common/patch"
3333
pvcspec "github.com/deckhouse/virtualization-controller/pkg/common/pvc"
34+
commonvd "github.com/deckhouse/virtualization-controller/pkg/common/vd"
3435
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
3536
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
3637
vdsupplements "github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/supplements"
38+
"github.com/deckhouse/virtualization-controller/pkg/featuregates"
3739
"github.com/deckhouse/virtualization-controller/pkg/logger"
3840
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
3941
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition"
@@ -68,6 +70,10 @@ func (h MigrationHandler) Handle(ctx context.Context, vd *virtv2.VirtualDisk) (r
6870
return reconcile.Result{}, nil
6971
}
7072

73+
if !featuregates.Default().Enabled(featuregates.VolumeMigration) {
74+
return reconcile.Result{}, nil
75+
}
76+
7177
action, err := h.getAction(ctx, vd)
7278
if err != nil {
7379
return reconcile.Result{}, err
@@ -96,7 +102,7 @@ const (
96102
)
97103

98104
func (h MigrationHandler) getAction(ctx context.Context, vd *virtv2.VirtualDisk) (action, error) {
99-
currentlyMountedVM := currentlyMountedVM(vd)
105+
currentlyMountedVM := commonvd.CurrentlyMountedVM(vd)
100106
if currentlyMountedVM == "" {
101107
return none, nil
102108
}

images/virtualization-artifact/pkg/controller/vd/internal/supplements/pvc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
package supplements
218

319
import "github.com/deckhouse/virtualization/api/core/v1alpha2"

images/virtualization-artifact/pkg/controller/vd/vd_controller.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ import (
2828
"sigs.k8s.io/controller-runtime/pkg/metrics"
2929

3030
"github.com/deckhouse/deckhouse/pkg/log"
31-
3231
"github.com/deckhouse/virtualization-controller/pkg/config"
3332
"github.com/deckhouse/virtualization-controller/pkg/controller/service"
3433
"github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal"
3534
intsvc "github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/service"
3635
"github.com/deckhouse/virtualization-controller/pkg/controller/vd/internal/source"
3736
"github.com/deckhouse/virtualization-controller/pkg/dvcr"
3837
"github.com/deckhouse/virtualization-controller/pkg/eventrecord"
39-
"github.com/deckhouse/virtualization-controller/pkg/featuregates"
4038
"github.com/deckhouse/virtualization-controller/pkg/logger"
4139
vdcolelctor "github.com/deckhouse/virtualization-controller/pkg/monitoring/metrics/vd"
4240
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
@@ -79,7 +77,9 @@ func NewController(
7977
sources.Set(virtv2.DataSourceTypeObjectRef, source.NewObjectRefDataSource(recorder, disk, mgr.GetClient()))
8078
sources.Set(virtv2.DataSourceTypeUpload, source.NewUploadDataSource(recorder, stat, uploader, disk, dvcr, mgr.GetClient()))
8179

82-
handlers := []Handler{
80+
reconciler := NewReconciler(
81+
mgr.GetClient(),
82+
internal.NewInitHandler(),
8383
internal.NewStorageClassReadyHandler(scService),
8484
internal.NewDatasourceReadyHandler(recorder, blank, sources),
8585
internal.NewLifeCycleHandler(recorder, blank, sources, mgr.GetClient()),
@@ -88,15 +88,8 @@ func NewController(
8888
internal.NewDeletionHandler(sources, mgr.GetClient()),
8989
internal.NewStatsHandler(stat, importer, uploader),
9090
internal.NewInUseHandler(mgr.GetClient()),
91-
}
92-
if featuregates.Default().Enabled(featuregates.VolumeMigration) {
93-
handlers = append(handlers, internal.NewMigrationHandler(mgr.GetClient(), scService, disk))
94-
}
95-
handlers = append(handlers, internal.NewProtectionHandler())
96-
97-
reconciler := NewReconciler(
98-
mgr.GetClient(),
99-
handlers...,
91+
internal.NewMigrationHandler(mgr.GetClient(), scService, disk),
92+
internal.NewProtectionHandler(),
10093
)
10194

10295
vdController, err := controller.New(ControllerName, mgr, controller.Options{

0 commit comments

Comments
 (0)