Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Yaroslav Borbat <[email protected]>
  • Loading branch information
yaroslavborbat committed Feb 3, 2025
1 parent a005749 commit 6435099
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

"github.com/deckhouse/deckhouse/pkg/log"

appconfig "github.com/deckhouse/virtualization-controller/pkg/config"
"github.com/deckhouse/virtualization-controller/pkg/controller/cvi"
"github.com/deckhouse/virtualization-controller/pkg/controller/indexer"
Expand All @@ -54,6 +55,7 @@ import (
"github.com/deckhouse/virtualization-controller/pkg/controller/vmrestore"
"github.com/deckhouse/virtualization-controller/pkg/controller/vmsnapshot"
"github.com/deckhouse/virtualization-controller/pkg/logger"
"github.com/deckhouse/virtualization-controller/pkg/migration"
"github.com/deckhouse/virtualization-controller/pkg/version"
"github.com/deckhouse/virtualization/api/client/kubeclient"
virtv2alpha1 "github.com/deckhouse/virtualization/api/core/v1alpha2"
Expand Down Expand Up @@ -223,6 +225,14 @@ func main() {
// Setup context to gracefully handle termination.
ctx := signals.SetupSignalHandler()

mCtrl := migration.NewController(mgr.GetClient(), log)
if err = mCtrl.Setup(); err != nil {
log.Error(err.Error())
os.Exit(1)
}

mCtrl.Run(ctx)

if err = indexer.IndexALL(ctx, mgr); err != nil {
log.Error(err.Error())
os.Exit(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/apiserver/pkg/registry/rest"
virtv1 "kubevirt.io/api/core/v1"

"github.com/deckhouse/virtualization-controller/pkg/controller/kvbuilder"
"github.com/deckhouse/virtualization-controller/pkg/tls/certmanager"
virtlisters "github.com/deckhouse/virtualization/api/client/generated/listers/core/v1alpha2"
"github.com/deckhouse/virtualization/api/subresources"
Expand Down Expand Up @@ -119,7 +120,7 @@ func (r AddVolumeREST) genMutateRequestHook(opts *subresources.VirtualMachineAdd
Disk: &virtv1.Disk{
Name: opts.Name,
DiskDevice: dd,
Serial: opts.Name,
Serial: kvbuilder.GenerateSerial(opts.Name),
},
}
switch opts.VolumeKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package kvbuilder

import (
"crypto/md5"
"encoding/hex"
"fmt"
"strings"

Expand Down Expand Up @@ -62,6 +64,13 @@ func GetOriginalDiskName(prefixedName string) (string, bool) {
return prefixedName, false
}

func GenerateSerial(input string) string {
hasher := md5.New()
hasher.Write([]byte(input))
hashInBytes := hasher.Sum(nil)
return hex.EncodeToString(hashInBytes)
}

type HotPlugDeviceSettings struct {
VolumeName string
PVCName string
Expand Down Expand Up @@ -139,7 +148,7 @@ func ApplyVirtualMachineSpec(
if err := kvvm.SetDisk(name, SetDiskOptions{
PersistentVolumeClaim: pointer.GetPointer(vi.Status.Target.PersistentVolumeClaim),
IsEphemeral: true,
Serial: name,
Serial: GenerateSerial(name),
BootOrder: bootOrder,
}); err != nil {
return err
Expand All @@ -148,7 +157,7 @@ func ApplyVirtualMachineSpec(
if err := kvvm.SetDisk(name, SetDiskOptions{
ContainerDisk: pointer.GetPointer(vi.Status.Target.RegistryURL),
IsCdrom: imageformat.IsISO(vi.Status.Format),
Serial: name,
Serial: GenerateSerial(name),
BootOrder: bootOrder,
}); err != nil {
return err
Expand All @@ -167,7 +176,7 @@ func ApplyVirtualMachineSpec(
if err := kvvm.SetDisk(name, SetDiskOptions{
ContainerDisk: pointer.GetPointer(cvi.Status.Target.RegistryURL),
IsCdrom: imageformat.IsISO(cvi.Status.Format),
Serial: name,
Serial: GenerateSerial(name),
BootOrder: bootOrder,
}); err != nil {
return err
Expand All @@ -186,7 +195,7 @@ func ApplyVirtualMachineSpec(
name := GenerateVMDDiskName(bd.Name)
if err := kvvm.SetDisk(name, SetDiskOptions{
PersistentVolumeClaim: pointer.GetPointer(vd.Status.Target.PersistentVolumeClaim),
Serial: name,
Serial: GenerateSerial(name),
BootOrder: bootOrder,
}); err != nil {
return err
Expand Down
13 changes: 12 additions & 1 deletion images/virtualization-artifact/pkg/logger/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ limitations under the License.

package logger

import "log/slog"
import (
"encoding/json"
"log/slog"
)

const (
errAttr = "err"
Expand Down Expand Up @@ -55,3 +58,11 @@ func SlogController(controller string) slog.Attr {
func SlogCollector(collector string) slog.Attr {
return slog.String(collectorAttr, collector)
}

func SlogTryJson(key string, i interface{}) slog.Attr {
bytes, err := json.Marshal(i)
if err == nil {
return slog.String(key, string(bytes))
}
return slog.Any(key, i)
}
106 changes: 106 additions & 0 deletions images/virtualization-artifact/pkg/migration/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright 2025 Flant JSC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package migration

import (
"context"
"sync"
"time"

"github.com/deckhouse/deckhouse/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/deckhouse/virtualization-controller/pkg/logger"
)

type constructor func(client client.Client, logger *log.Logger) (Migration, error)

var newMigrations = []constructor{
newQEMUMaxLength36,
}

type Migration interface {
Name() string
IsBlocking() bool
Migrate(ctx context.Context) error
}

type Controller struct {
client client.Client
log *log.Logger

blockingMigrations []Migration
nonBlockingMigrations []Migration
}

func NewController(client client.Client, log *log.Logger) *Controller {
return &Controller{
client: client,
log: log,
}
}

func (c *Controller) Setup() error {
for _, fn := range newMigrations {
m, err := fn(c.client, c.log)
if err != nil {
return err
}
if m.IsBlocking() {
c.blockingMigrations = append(c.blockingMigrations, m)
} else {
c.nonBlockingMigrations = append(c.nonBlockingMigrations, m)
}
}
return nil
}

func (c *Controller) Run(ctx context.Context) {
run := func(wg *sync.WaitGroup, migrations []Migration) {
for _, m := range migrations {
wg.Add(1)
lg := c.log.With("name", m.Name())
lg.Info("Running migration")

go func() {
defer lg.Info("Finished migration")
defer wg.Done()

for {
select {
case <-ctx.Done():
return
default:
if err := m.Migrate(ctx); err != nil {
lg.Error("Failed to run migration, retry after 5s...", logger.SlogErr(err))
time.Sleep(5 * time.Second)
continue
}
break
}
}
}()
}
}
unused := &sync.WaitGroup{}
used := &sync.WaitGroup{}

run(unused, c.nonBlockingMigrations)
run(used, c.blockingMigrations)
// Wait for blocked migrations only
used.Wait()
}
116 changes: 116 additions & 0 deletions images/virtualization-artifact/pkg/migration/qemu-max-length-36.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright 2025 Flant JSC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package migration

import (
"context"

"github.com/deckhouse/deckhouse/pkg/log"
virtv1 "kubevirt.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/deckhouse/virtualization-controller/pkg/controller/kvbuilder"
"github.com/deckhouse/virtualization-controller/pkg/logger"
)

const (
// https://github.com/qemu/qemu/commit/75997e182b695f2e3f0a2d649734952af5caf3ee
maxSerialLength = 36
qemuMaxLength36ControllerName = "qemu-max-length-36"
)

func newQEMUMaxLength36(client client.Client, logger *log.Logger) (Migration, error) {
return &qemuMaxLength36{
client: client,
logger: logger,
}, nil
}

type qemuMaxLength36 struct {
client client.Client
logger *log.Logger
}

func (r *qemuMaxLength36) Name() string {
return qemuMaxLength36ControllerName
}

func (r *qemuMaxLength36) IsBlocking() bool {
return true
}

func (r *qemuMaxLength36) Migrate(ctx context.Context) error {
kvvmList := &virtv1.VirtualMachineList{}
err := r.client.List(ctx, kvvmList)
if err != nil {
return err
}

for i := range kvvmList.Items {
kvvm := &kvvmList.Items[i]
if !r.needMigrate(&kvvm.Spec.Template.Spec) {
continue
}
copied := kvvm.DeepCopy()
r.mutateSpec(&copied.Spec.Template.Spec)
r.logger.Debug("Patch kvvm with new kvvmi spec", logger.SlogTryJson("newSpce", copied.Spec.Template.Spec))
if err = r.client.Patch(ctx, kvvm, client.MergeFrom(copied)); err != nil {
return err
}
}

kvvmiList := &virtv1.VirtualMachineInstanceList{}
err = r.client.List(ctx, kvvmiList)
if err != nil {
return err
}

for i := range kvvmiList.Items {
kvvmi := &kvvmiList.Items[i]
if !r.needMigrate(&kvvmi.Spec) {
continue
}
copied := kvvmi.DeepCopy()
r.mutateSpec(&copied.Spec)
r.logger.Debug("Patch kvvmi with new spec", logger.SlogTryJson("newSpce", copied.Spec))
if err = r.client.Patch(ctx, kvvmi, client.MergeFrom(copied)); err != nil {
return err
}
}

return nil
}

func (r *qemuMaxLength36) needMigrate(spec *virtv1.VirtualMachineInstanceSpec) bool {
for _, d := range spec.Domain.Devices.Disks {
serial := []rune(d.Serial)
if len(serial) > maxSerialLength {
return true
}
}
return false
}

func (r *qemuMaxLength36) mutateSpec(spec *virtv1.VirtualMachineInstanceSpec) {
for i := range spec.Domain.Devices.Disks {
d := &spec.Domain.Devices.Disks[i]
serial := []rune(d.Serial)
if len(serial) > maxSerialLength {
d.Serial = kvbuilder.GenerateSerial(d.Name)
}
}
}

0 comments on commit 6435099

Please sign in to comment.