From 92f73eeadfe63d391bb7ac46579edf56c547f0fd Mon Sep 17 00:00:00 2001 From: Quique Llorente Date: Wed, 27 Feb 2019 23:36:39 +0100 Subject: [PATCH] Print vmi nic information It also includes at String function to print VIF struct --- pkg/virt-launcher/virtwrap/network/common.go | 12 ++++++++++++ .../virtwrap/network/generated_mock_network.go | 8 ++++---- pkg/virt-launcher/virtwrap/network/network.go | 4 ++-- .../virtwrap/network/network_test.go | 6 +++--- .../virtwrap/network/podinterface.go | 15 +++++++++++---- .../virtwrap/network/podinterface_test.go | 11 ++++++----- 6 files changed, 38 insertions(+), 18 deletions(-) diff --git a/pkg/virt-launcher/virtwrap/network/common.go b/pkg/virt-launcher/virtwrap/network/common.go index 2db0c670dcf2..92562ed13b74 100644 --- a/pkg/virt-launcher/virtwrap/network/common.go +++ b/pkg/virt-launcher/virtwrap/network/common.go @@ -52,6 +52,18 @@ type VIF struct { Mtu uint16 } +func (vif VIF) String() string { + return fmt.Sprintf( + "VIF: { Name: %s, IP: %s, Mask: %s, MAC: %s, Gateway: %s, MTU: %d}", + vif.Name, + vif.IP.IP, + vif.IP.Mask, + vif.MAC, + vif.Gateway, + vif.Mtu, + ) +} + type NetworkHandler interface { LinkByName(name string) (netlink.Link, error) AddrList(link netlink.Link, family int) ([]netlink.Addr, error) diff --git a/pkg/virt-launcher/virtwrap/network/generated_mock_network.go b/pkg/virt-launcher/virtwrap/network/generated_mock_network.go index f005af893d67..7915a9b826ec 100644 --- a/pkg/virt-launcher/virtwrap/network/generated_mock_network.go +++ b/pkg/virt-launcher/virtwrap/network/generated_mock_network.go @@ -31,14 +31,14 @@ func (_m *MockNetworkInterface) EXPECT() *_MockNetworkInterfaceRecorder { return _m.recorder } -func (_m *MockNetworkInterface) Plug(iface *v1.Interface, network *v1.Network, domain *api.Domain, podInterfaceName string) error { - ret := _m.ctrl.Call(_m, "Plug", iface, network, domain, podInterfaceName) +func (_m *MockNetworkInterface) Plug(vmi *v1.VirtualMachineInstance, iface *v1.Interface, network *v1.Network, domain *api.Domain, podInterfaceName string) error { + ret := _m.ctrl.Call(_m, "Plug", vmi, iface, network, domain, podInterfaceName) ret0, _ := ret[0].(error) return ret0 } -func (_mr *_MockNetworkInterfaceRecorder) Plug(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { - return _mr.mock.ctrl.RecordCall(_mr.mock, "Plug", arg0, arg1, arg2, arg3) +func (_mr *_MockNetworkInterfaceRecorder) Plug(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "Plug", arg0, arg1, arg2, arg3, arg4) } func (_m *MockNetworkInterface) Unplug() { diff --git a/pkg/virt-launcher/virtwrap/network/network.go b/pkg/virt-launcher/virtwrap/network/network.go index 3e6269fc32fa..f03bfe1f909d 100644 --- a/pkg/virt-launcher/virtwrap/network/network.go +++ b/pkg/virt-launcher/virtwrap/network/network.go @@ -41,7 +41,7 @@ var NetworkInterfaceFactory = getNetworkClass var podInterfaceName = podInterface type NetworkInterface interface { - Plug(iface *v1.Interface, network *v1.Network, domain *api.Domain, podInterfaceName string) error + Plug(vmi *v1.VirtualMachineInstance, iface *v1.Interface, network *v1.Network, domain *api.Domain, podInterfaceName string) error Unplug() } @@ -80,7 +80,7 @@ func SetupNetworkInterfaces(vmi *v1.VirtualMachineInstance, domain *api.Domain) podInterfaceName = podInterface } - err = vif.Plug(&iface, network, domain, podInterfaceName) + err = vif.Plug(vmi, &iface, network, domain, podInterfaceName) if err != nil { return err } diff --git a/pkg/virt-launcher/virtwrap/network/network_test.go b/pkg/virt-launcher/virtwrap/network/network_test.go index ea85b7bb9608..f2f7ab7e1a34 100644 --- a/pkg/virt-launcher/virtwrap/network/network_test.go +++ b/pkg/virt-launcher/virtwrap/network/network_test.go @@ -51,7 +51,7 @@ var _ = Describe("Network", func() { iface := v1.DefaultNetworkInterface() defaultNet := v1.DefaultPodNetwork() - mockNetworkInterface.EXPECT().Plug(iface, defaultNet, domain, podInterface) + mockNetworkInterface.EXPECT().Plug(vm, iface, defaultNet, domain, podInterface) err := SetupNetworkInterfaces(vm, domain) Expect(err).To(BeNil()) }) @@ -78,7 +78,7 @@ var _ = Describe("Network", func() { } vm.Spec.Networks = []v1.Network{*cniNet} - mockNetworkInterface.EXPECT().Plug(iface, cniNet, domain, "net1") + mockNetworkInterface.EXPECT().Plug(vm, iface, cniNet, domain, "net1") err := SetupNetworkInterfaces(vm, domain) Expect(err).To(BeNil()) }) @@ -99,7 +99,7 @@ var _ = Describe("Network", func() { } vm.Spec.Networks = []v1.Network{*cniNet} - mockNetworkInterface.EXPECT().Plug(iface, cniNet, domain, genieInterfaceName) + mockNetworkInterface.EXPECT().Plug(vm, iface, cniNet, domain, genieInterfaceName) err := SetupNetworkInterfaces(vm, domain) Expect(err).To(BeNil()) }) diff --git a/pkg/virt-launcher/virtwrap/network/podinterface.go b/pkg/virt-launcher/virtwrap/network/podinterface.go index 52d30cc6399b..de9bb34360f4 100644 --- a/pkg/virt-launcher/virtwrap/network/podinterface.go +++ b/pkg/virt-launcher/virtwrap/network/podinterface.go @@ -59,7 +59,7 @@ func findInterfaceByName(ifaces []api.Interface, name string) (int, error) { } // Plug connect a Pod network device to the virtual machine -func (l *PodInterface) Plug(iface *v1.Interface, network *v1.Network, domain *api.Domain, podInterfaceName string) error { +func (l *PodInterface) Plug(vmi *v1.VirtualMachineInstance, iface *v1.Interface, network *v1.Network, domain *api.Domain, podInterfaceName string) error { precond.MustNotBeNil(domain) initHandler() @@ -68,7 +68,7 @@ func (l *PodInterface) Plug(iface *v1.Interface, network *v1.Network, domain *ap return nil } - driver, err := getBinding(iface, network, domain, podInterfaceName) + driver, err := getBinding(vmi, iface, network, domain, podInterfaceName) if err != nil { return err } @@ -107,7 +107,7 @@ func (l *PodInterface) Plug(iface *v1.Interface, network *v1.Network, domain *ap return nil } -func getBinding(iface *v1.Interface, network *v1.Network, domain *api.Domain, podInterfaceName string) (BindMechanism, error) { +func getBinding(vmi *v1.VirtualMachineInstance, iface *v1.Interface, network *v1.Network, domain *api.Domain, podInterfaceName string) (BindMechanism, error) { podInterfaceNum, err := findInterfaceByName(domain.Spec.Devices.Interfaces, iface.Name) if err != nil { return nil, err @@ -128,6 +128,7 @@ func getBinding(iface *v1.Interface, network *v1.Network, domain *api.Domain, po vif := &VIF{Name: podInterfaceName} populateMacAddress(vif, iface) return &BridgePodInterface{iface: iface, + vmi: vmi, vif: vif, domain: domain, podInterfaceNum: podInterfaceNum, @@ -138,6 +139,7 @@ func getBinding(iface *v1.Interface, network *v1.Network, domain *api.Domain, po vif := &VIF{Name: podInterfaceName} populateMacAddress(vif, iface) return &MasqueradePodInterface{iface: iface, + vmi: vmi, vif: vif, domain: domain, podInterfaceNum: podInterfaceNum, @@ -146,12 +148,13 @@ func getBinding(iface *v1.Interface, network *v1.Network, domain *api.Domain, po bridgeInterfaceName: fmt.Sprintf("k6t-%s", podInterfaceName)}, nil } if iface.Slirp != nil { - return &SlirpPodInterface{iface: iface, domain: domain, podInterfaceNum: podInterfaceNum}, nil + return &SlirpPodInterface{vmi: vmi, iface: iface, domain: domain, podInterfaceNum: podInterfaceNum}, nil } return nil, fmt.Errorf("Not implemented") } type BridgePodInterface struct { + vmi *v1.VirtualMachineInstance vif *VIF iface *v1.Interface podNicLink netlink.Link @@ -252,6 +255,7 @@ func (b *BridgePodInterface) preparePodNetworkInterfaces() error { func (b *BridgePodInterface) startDHCPServer() { // Start DHCP Server fakeServerAddr, _ := netlink.ParseAddr(fmt.Sprintf(bridgeFakeIP, b.podInterfaceNum)) + log.Log.Object(b.vmi).Infof("bridge pod interface: %s", b.vif) Handler.StartDHCP(b.vif, fakeServerAddr, b.bridgeInterfaceName, b.iface.DHCPOptions) } @@ -341,6 +345,7 @@ func (b *BridgePodInterface) createBridge() error { } type MasqueradePodInterface struct { + vmi *v1.VirtualMachineInstance vif *VIF iface *v1.Interface podNicLink netlink.Link @@ -438,6 +443,7 @@ func (p *MasqueradePodInterface) preparePodNetworkInterfaces() error { func (p *MasqueradePodInterface) startDHCPServer() { // Start DHCP Server + log.Log.Object(p.vmi).Infof("masquerade pod interface: %s", p.vif) Handler.StartDHCP(p.vif, p.gatewayAddr, p.bridgeInterfaceName, p.iface.DHCPOptions) } @@ -582,6 +588,7 @@ func (p *MasqueradePodInterface) createNatRules() error { } type SlirpPodInterface struct { + vmi *v1.VirtualMachineInstance iface *v1.Interface domain *api.Domain podInterfaceNum int diff --git a/pkg/virt-launcher/virtwrap/network/podinterface_test.go b/pkg/virt-launcher/virtwrap/network/podinterface_test.go index 773342dbf924..1bbb3cd0dc77 100644 --- a/pkg/virt-launcher/virtwrap/network/podinterface_test.go +++ b/pkg/virt-launcher/virtwrap/network/podinterface_test.go @@ -333,7 +333,7 @@ var _ = Describe("Pod Network", func() { vmi := newVMIBridgeInterface("testnamespace", "testVmName") api.SetObjectDefaults_Domain(domain) vmi.Spec.Domain.Devices.Interfaces[0].MacAddress = "de-ad-00-00-be-af" - driver, err := getBinding(&vmi.Spec.Domain.Devices.Interfaces[0], &vmi.Spec.Networks[0], domain, podInterface) + driver, err := getBinding(vmi, &vmi.Spec.Domain.Devices.Interfaces[0], &vmi.Spec.Networks[0], domain, podInterface) Expect(err).ToNot(HaveOccurred()) bridge, ok := driver.(*BridgePodInterface) Expect(ok).To(BeTrue()) @@ -354,8 +354,9 @@ var _ = Describe("Pod Network", func() { SRIOV: &v1.InterfaceSRIOV{}, }, } + vmi := newVMI("testnamespace", "testVmName") podiface := PodInterface{} - err := podiface.Plug(iface, net, domain, "fakeiface") + err := podiface.Plug(vmi, iface, net, domain, "fakeiface") Expect(err).ToNot(HaveOccurred()) }) }) @@ -376,7 +377,7 @@ var _ = Describe("Pod Network", func() { api.SetObjectDefaults_Domain(domain) - driver, err := getBinding(&vmi.Spec.Domain.Devices.Interfaces[0], &vmi.Spec.Networks[0], domain, podInterface) + driver, err := getBinding(vmi, &vmi.Spec.Domain.Devices.Interfaces[0], &vmi.Spec.Networks[0], domain, podInterface) Expect(err).ToNot(HaveOccurred()) TestRunPlug(driver) Expect(len(domain.Spec.Devices.Interfaces)).To(Equal(0)) @@ -391,7 +392,7 @@ var _ = Describe("Pod Network", func() { api.SetObjectDefaults_Domain(domain) vmi.Spec.Domain.Devices.Interfaces[0].MacAddress = "de-ad-00-00-be-af" - driver, err := getBinding(&vmi.Spec.Domain.Devices.Interfaces[0], &vmi.Spec.Networks[0], domain, podInterface) + driver, err := getBinding(vmi, &vmi.Spec.Domain.Devices.Interfaces[0], &vmi.Spec.Networks[0], domain, podInterface) Expect(err).ToNot(HaveOccurred()) TestRunPlug(driver) Expect(len(domain.Spec.Devices.Interfaces)).To(Equal(0)) @@ -417,7 +418,7 @@ var _ = Describe("Pod Network", func() { Name: "default", }}) - driver, err := getBinding(&vmi.Spec.Domain.Devices.Interfaces[0], &vmi.Spec.Networks[0], domain, podInterface) + driver, err := getBinding(vmi, &vmi.Spec.Domain.Devices.Interfaces[0], &vmi.Spec.Networks[0], domain, podInterface) Expect(err).ToNot(HaveOccurred()) TestRunPlug(driver) Expect(len(domain.Spec.Devices.Interfaces)).To(Equal(1))