Skip to content

Commit b5ded39

Browse files
committed
test(vm): set additional network interfaces
Signed-off-by: Isteb4k <dmitry.rakitin@flant.com>
1 parent 4dae847 commit b5ded39

4 files changed

Lines changed: 124 additions & 15 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ const (
7373
// AnnVmStartRequested is an annotation on KVVM that represents a request to start a virtual machine.
7474
AnnVmStartRequested = AnnAPIGroupV + "/vm-start-requested"
7575

76+
// AnnAdditionalNetworkInterfaces example: "name1:multus,name2:macvtap"
77+
AnnAdditionalNetworkInterfaces = AnnAPIGroup + "/additional-network-interfaces"
78+
7679
// AnnVmRestartRequested is an annotation on KVVM that represents a request to restart a virtual machine.
7780
AnnVmRestartRequested = AnnAPIGroupV + "/vm-restart-requested"
7881

images/virtualization-artifact/pkg/controller/kvbuilder/kvvm.go

Lines changed: 109 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package kvbuilder
1919
import (
2020
"fmt"
2121
"maps"
22+
"strings"
2223

2324
corev1 "k8s.io/api/core/v1"
2425
"k8s.io/apimachinery/pkg/api/resource"
@@ -519,22 +520,24 @@ func (b *KVVM) GetOSSettings() map[string]interface{} {
519520
}
520521
}
521522

522-
func (b *KVVM) SetNetworkInterface(name string) {
523-
devPreset := DeviceOptionsPresets.Find(b.opts.EnableParavirtualization)
524-
525-
net := virtv1.Network{
526-
Name: name,
527-
NetworkSource: virtv1.NetworkSource{
528-
Pod: &virtv1.PodNetwork{},
529-
},
523+
func (b *KVVM) SetNetworkInterface(vm *virtv2.VirtualMachine, name string) {
524+
hasMacvtap := strings.Contains(vm.GetAnnotations()[annotations.AnnAdditionalNetworkInterfaces], "macvtap")
525+
if !hasMacvtap {
526+
net := virtv1.Network{
527+
Name: name,
528+
NetworkSource: virtv1.NetworkSource{
529+
Pod: &virtv1.PodNetwork{},
530+
},
531+
}
532+
b.Resource.Spec.Template.Spec.Networks = array.SetArrayElem(
533+
b.Resource.Spec.Template.Spec.Networks, net,
534+
func(v1, v2 virtv1.Network) bool {
535+
return v1.Name == v2.Name
536+
}, true,
537+
)
530538
}
531-
b.Resource.Spec.Template.Spec.Networks = array.SetArrayElem(
532-
b.Resource.Spec.Template.Spec.Networks, net,
533-
func(v1, v2 virtv1.Network) bool {
534-
return v1.Name == v2.Name
535-
}, true,
536-
)
537539

540+
devPreset := DeviceOptionsPresets.Find(b.opts.EnableParavirtualization)
538541
iface := virtv1.Interface{
539542
Name: name,
540543
Model: devPreset.InterfaceModel,
@@ -548,6 +551,98 @@ func (b *KVVM) SetNetworkInterface(name string) {
548551
)
549552
}
550553

554+
func (b *KVVM) SetMacvtapInterfaces(vm *virtv2.VirtualMachine) {
555+
anis := vm.GetAnnotations()[annotations.AnnAdditionalNetworkInterfaces]
556+
if anis == "" {
557+
return
558+
}
559+
560+
var networks []string
561+
562+
for _, network := range strings.Split(anis, ",") {
563+
iname := strings.Split(network, ":")
564+
if iname[1] == "macvtap" {
565+
networks = append(networks, iname[0])
566+
}
567+
}
568+
569+
for _, network := range networks {
570+
net := virtv1.Network{
571+
Name: network,
572+
NetworkSource: virtv1.NetworkSource{
573+
Pod: &virtv1.PodNetwork{},
574+
},
575+
}
576+
b.Resource.Spec.Template.Spec.Networks = array.SetArrayElem(
577+
b.Resource.Spec.Template.Spec.Networks, net,
578+
func(v1, v2 virtv1.Network) bool {
579+
return v1.Name == v2.Name
580+
}, true,
581+
)
582+
583+
iface := virtv1.Interface{
584+
Name: network,
585+
Binding: &virtv1.PluginBinding{
586+
Name: "macvtap",
587+
},
588+
}
589+
590+
b.Resource.Spec.Template.Spec.Domain.Devices.Interfaces = array.SetArrayElem(
591+
b.Resource.Spec.Template.Spec.Domain.Devices.Interfaces, iface,
592+
func(v1, v2 virtv1.Interface) bool {
593+
return v1.Name == v2.Name
594+
}, true,
595+
)
596+
}
597+
}
598+
599+
func (b *KVVM) SetMultusInterfaces(vm *virtv2.VirtualMachine) {
600+
anis := vm.GetAnnotations()[annotations.AnnAdditionalNetworkInterfaces]
601+
if anis == "" {
602+
return
603+
}
604+
605+
var networks []string
606+
607+
for _, network := range strings.Split(anis, ",") {
608+
iname := strings.Split(network, ":")
609+
if iname[1] == "multus" {
610+
networks = append(networks, iname[0])
611+
}
612+
}
613+
614+
for _, network := range networks {
615+
net := virtv1.Network{
616+
Name: network,
617+
NetworkSource: virtv1.NetworkSource{
618+
Multus: &virtv1.MultusNetwork{
619+
NetworkName: network,
620+
},
621+
},
622+
}
623+
b.Resource.Spec.Template.Spec.Networks = array.SetArrayElem(
624+
b.Resource.Spec.Template.Spec.Networks, net,
625+
func(v1, v2 virtv1.Network) bool {
626+
return v1.Name == v2.Name
627+
}, true,
628+
)
629+
630+
iface := virtv1.Interface{
631+
Name: network,
632+
InterfaceBindingMethod: virtv1.InterfaceBindingMethod{
633+
Bridge: &virtv1.InterfaceBridge{},
634+
},
635+
}
636+
637+
b.Resource.Spec.Template.Spec.Domain.Devices.Interfaces = array.SetArrayElem(
638+
b.Resource.Spec.Template.Spec.Domain.Devices.Interfaces, iface,
639+
func(v1, v2 virtv1.Interface) bool {
640+
return v1.Name == v2.Name
641+
}, true,
642+
)
643+
}
644+
}
645+
551646
func (b *KVVM) SetBootloader(bootloader virtv2.BootloaderType) error {
552647
if b.Resource.Spec.Template.Spec.Domain.Firmware == nil {
553648
b.Resource.Spec.Template.Spec.Domain.Firmware = &virtv1.Firmware{}

images/virtualization-artifact/pkg/controller/kvbuilder/kvvm_utils.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ func ApplyVirtualMachineSpec(
104104
}
105105

106106
kvvm.SetMetadata(vm.ObjectMeta)
107-
kvvm.SetNetworkInterface(NetworkInterfaceName)
107+
108+
kvvm.Resource.Spec.Template.Spec.Networks = []virtv1.Network{}
109+
kvvm.Resource.Spec.Template.Spec.Domain.Devices.Interfaces = []virtv1.Interface{}
110+
kvvm.SetNetworkInterface(vm, NetworkInterfaceName)
111+
kvvm.SetMultusInterfaces(vm)
112+
kvvm.SetMacvtapInterfaces(vm)
113+
108114
kvvm.SetTablet("default-0")
109115
kvvm.SetNodeSelector(vm.Spec.NodeSelector, class.Spec.NodeSelector.MatchLabels)
110116
kvvm.SetTolerations(vm.Spec.Tolerations, class.Spec.Tolerations)

images/virtualization-artifact/pkg/controller/vm/internal/sync_kvvm.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"sigs.k8s.io/controller-runtime/pkg/client"
3030
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3131

32+
"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
3233
"github.com/deckhouse/virtualization-controller/pkg/common/object"
3334
vmutil "github.com/deckhouse/virtualization-controller/pkg/common/vm"
3435
"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
@@ -535,6 +536,10 @@ func (h *SyncKvvmHandler) applyVMChangesToKVVM(ctx context.Context, s state.Virt
535536
return err
536537
}
537538

539+
if s.VirtualMachine().Current().GetAnnotations()[annotations.AnnAdditionalNetworkInterfaces] != "" {
540+
action = vmchange.ActionRestart
541+
}
542+
538543
switch action {
539544
case vmchange.ActionRestart:
540545
h.recorder.WithLogging(log).Event(current, corev1.EventTypeNormal, virtv2.ReasonVMChangesApplied, "Apply disruptive changes with restart")

0 commit comments

Comments
 (0)