Skip to content

Commit

Permalink
Merge pull request kubevirt#2063 from qinqon/log_mac_ip
Browse files Browse the repository at this point in the history
Print MAC and IP at DHCP server start
  • Loading branch information
rmohr authored Mar 6, 2019
2 parents 35d8738 + 92f73ee commit 6ae065a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
12 changes: 12 additions & 0 deletions pkg/virt-launcher/virtwrap/network/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pkg/virt-launcher/virtwrap/network/generated_mock_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/virt-launcher/virtwrap/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/virt-launcher/virtwrap/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand All @@ -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())
})
Expand All @@ -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())
})
Expand Down
15 changes: 11 additions & 4 deletions pkg/virt-launcher/virtwrap/network/podinterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -341,6 +345,7 @@ func (b *BridgePodInterface) createBridge() error {
}

type MasqueradePodInterface struct {
vmi *v1.VirtualMachineInstance
vif *VIF
iface *v1.Interface
podNicLink netlink.Link
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -582,6 +588,7 @@ func (p *MasqueradePodInterface) createNatRules() error {
}

type SlirpPodInterface struct {
vmi *v1.VirtualMachineInstance
iface *v1.Interface
domain *api.Domain
podInterfaceNum int
Expand Down
11 changes: 6 additions & 5 deletions pkg/virt-launcher/virtwrap/network/podinterface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
})
})
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit 6ae065a

Please sign in to comment.