Skip to content

Commit dce5481

Browse files
authored
Allow passing VM interface name with CNI configuration (#178)
Allow passing VM interface name with CNI configuration
2 parents 5ce891b + f96c88a commit dce5481

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

cni/vmconf/vmconf.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ type StaticNetworkConf struct {
5353
// NetNSPath is the path to the bind-mounted network namespace in which the VM's
5454
// tap device was created and thus where the VM should execute.
5555
NetNSPath string
56+
// VMIfName (optional) is interface name to configure. If left blank, config
57+
// is applied to the first (default) interface.
58+
VMIfName string
5659

5760
// VMMacAddr is the mac address that callers should configure their VM to use internally.
5861
VMMacAddr string
@@ -117,9 +120,8 @@ func (c StaticNetworkConf) IPBootParam() string {
117120
// the "hostname" field actually just configures a hostname value for DHCP requests, thus no need to set it
118121
const dhcpHostname = ""
119122

120-
// TODO(sipsma) we are assuming there is only one network device
121-
// Just use the only network device present in the VM
122-
const device = ""
123+
// If blank, use the only network device present in the VM
124+
device := c.VMIfName
123125

124126
// Don't do any autoconfiguration (i.e. DHCP, BOOTP, RARP)
125127
const autoconfiguration = "off"

cni/vmconf/vmconf_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func TestIPBootParams(t *testing.T) {
6666
TapName: "taptaptap",
6767
NetNSPath: "/my/lil/netns",
6868
VMMacAddr: "00:11:22:33:44:55",
69+
VMIfName: "eth0",
6970
VMMTU: 1337,
7071
VMIPConfig: &current.IPConfig{
7172
Version: "4",
@@ -88,7 +89,7 @@ func TestIPBootParams(t *testing.T) {
8889
VMResolverOptions: []string{"choice", "is", "an", "illusion"},
8990
}
9091

91-
expectedIPBootParam := "10.0.0.2::10.0.0.1:255.255.255.0:::off:1.1.1.1:8.8.8.8:"
92+
expectedIPBootParam := "10.0.0.2::10.0.0.1:255.255.255.0::eth0:off:1.1.1.1:8.8.8.8:"
9293
actualIPBootParam := staticNetworkConf.IPBootParam()
9394
assert.Equal(t, expectedIPBootParam, actualIPBootParam)
9495
}

network.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func (networkInterfaces NetworkInterfaces) setupNetwork(
158158
IPAddr: vmNetConf.VMIPConfig.Address,
159159
Gateway: vmNetConf.VMIPConfig.Gateway,
160160
Nameservers: vmNetConf.VMNameservers,
161+
IfName: cniNetworkInterface.CNIConfiguration.VMIfName,
161162
}
162163
}
163164
}
@@ -241,6 +242,11 @@ type CNIConfiguration struct {
241242
// network device (which will by the one with "IfName").
242243
IfName string
243244

245+
// VMIfName (optional) sets the interface name in the VM. It is used
246+
// to correctly pass IP configuration obtained from the CNI to the VM kernel.
247+
// It can be left blank for VMs with single network interface.
248+
VMIfName string
249+
244250
// Args (optional) corresponds to the CNI_ARGS parameter as specified in
245251
// the CNI spec. It allows custom args to be passed to CNI plugins during
246252
// invocation.
@@ -490,7 +496,8 @@ func (staticConf StaticNetworkConfiguration) validate() error {
490496
// IPConfiguration specifies an IP, a gateway and DNS Nameservers that should be configured
491497
// automatically within the VM upon boot. It currently only supports IPv4 addresses.
492498
//
493-
// IPConfiguration can currently only be specified for VM's with a single network interface.
499+
// IPConfiguration can specify interface name, in that case config will be applied to the
500+
// specified interface, if IfName is left blank, config applies to VM with a single network interface.
494501
// The IPAddr and Gateway will be used to assign an IP a a default route for the VM's internal
495502
// interface.
496503
//
@@ -502,6 +509,7 @@ type IPConfiguration struct {
502509
IPAddr net.IPNet
503510
Gateway net.IP
504511
Nameservers []string
512+
IfName string
505513
}
506514

507515
func (ipConf IPConfiguration) validate() error {
@@ -528,6 +536,7 @@ func (conf IPConfiguration) ipBootParam() string {
528536
Address: conf.IPAddr,
529537
Gateway: conf.Gateway,
530538
},
539+
VMIfName: conf.IfName,
531540
}
532541

533542
return vmConf.IPBootParam()

network_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ func newCNIMachine(t *testing.T,
391391
CacheDir: cniCacheDir,
392392
NetworkName: networkName,
393393
IfName: ifName,
394+
VMIfName: "eth0",
394395
},
395396
}},
396397
VMID: vmID,
@@ -416,6 +417,10 @@ func startCNIMachine(t *testing.T, ctx context.Context, m *Machine) string {
416417
require.NotNil(t, ipConfig,
417418
"cni configuration should have updated network interface ip configuration")
418419

420+
require.Equal(t, m.Cfg.NetworkInterfaces[0].CNIConfiguration.VMIfName,
421+
staticConfig.IPConfiguration.IfName,
422+
"interface name should be propagated to static conf")
423+
419424
return ipConfig.IPAddr.IP.String()
420425
}
421426

0 commit comments

Comments
 (0)