Skip to content

Commit 065c563

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

4 files changed

Lines changed: 111 additions & 0 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: 93 additions & 0 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"
@@ -543,6 +544,98 @@ func (b *KVVM) SetNetworkInterface(name string) {
543544
)
544545
}
545546

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

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

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

106106
kvvm.SetMetadata(vm.ObjectMeta)
107+
108+
kvvm.Resource.Spec.Template.Spec.Networks = []virtv1.Network{}
109+
kvvm.Resource.Spec.Template.Spec.Domain.Devices.Interfaces = []virtv1.Interface{}
107110
kvvm.SetNetworkInterface(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: 9 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,14 @@ func (h *SyncKvvmHandler) applyVMChangesToKVVM(ctx context.Context, s state.Virt
535536
return err
536537
}
537538

539+
if action == vmchange.ActionNone {
540+
vm := s.VirtualMachine().Current()
541+
ann := vm.Annotations[annotations.AnnAdditionalNetworkInterfaces]
542+
if ann != "" {
543+
action = vmchange.ActionApplyImmediate
544+
}
545+
}
546+
538547
switch action {
539548
case vmchange.ActionRestart:
540549
h.recorder.WithLogging(log).Event(current, corev1.EventTypeNormal, virtv2.ReasonVMChangesApplied, "Apply disruptive changes with restart")

0 commit comments

Comments
 (0)