Skip to content

Commit 71deab0

Browse files
committed
generate: add linux net device injection/removal.
Add AddLinuxNetDevice(), RemoveLinuxNetDeviceByHostName(), and RemoveLinuxNetDeviceByName() for injecting and removing linux network devices to and from the OCI Spec. Signed-off-by: Krisztian Litkey <[email protected]>
1 parent edf4cb3 commit 71deab0

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

generate/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ func (g *Generator) initConfigLinuxResourcesUnified() {
130130
}
131131
}
132132

133+
func (g *Generator) initConfigLinuxNetDevices() {
134+
g.initConfigLinux()
135+
if g.Config.Linux.NetDevices == nil {
136+
g.Config.Linux.NetDevices = map[string]rspec.LinuxNetDevice{}
137+
}
138+
}
139+
133140
func (g *Generator) initConfigSolaris() {
134141
g.initConfig()
135142
if g.Config.Solaris == nil {

generate/generate.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,31 @@ func (g *Generator) AddLinuxReadonlyPaths(path string) {
16601660
g.Config.Linux.ReadonlyPaths = append(g.Config.Linux.ReadonlyPaths, path)
16611661
}
16621662

1663+
// AddLinuxNetDevice adds a network device into g.Config.Linux.NetDevices.
1664+
func (g *Generator) AddLinuxNetDevice(hostIf string, netDev *rspec.LinuxNetDevice) {
1665+
g.initConfigLinuxNetDevices()
1666+
g.Config.Linux.NetDevices[hostIf] = *netDev
1667+
}
1668+
1669+
// RemoveLinuxNetDeviceByHostName removes any linux network device with a
1670+
// matching host interface name from g.Config.Linux.NetDevices.
1671+
func (g *Generator) RemoveLinuxNetDeviceByHostName(hostIf string) {
1672+
g.initConfigLinuxNetDevices()
1673+
delete(g.Config.Linux.NetDevices, hostIf)
1674+
}
1675+
1676+
// RemoveLinuxNetDeviceByName removes any linux network device with a
1677+
// matching name from g.Config.Linux.NetDevices.
1678+
func (g *Generator) RemoveLinuxNetDeviceByName(name string) {
1679+
g.initConfigLinuxNetDevices()
1680+
for hif, dev := range g.Config.Linux.NetDevices {
1681+
if dev.Name == name {
1682+
delete(g.Config.Linux.NetDevices, hif)
1683+
return
1684+
}
1685+
}
1686+
}
1687+
16631688
func addOrReplaceBlockIOThrottleDevice(tmpList []rspec.LinuxThrottleDevice, major int64, minor int64, rate uint64) []rspec.LinuxThrottleDevice {
16641689
throttleDevices := tmpList
16651690
for i, throttleDevice := range throttleDevices {

generate/generate_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"runtime"
77
"testing"
88

9+
rspec "github.com/opencontainers/runtime-spec/specs-go"
910
rfc2119 "github.com/opencontainers/runtime-tools/error"
1011
"github.com/opencontainers/runtime-tools/generate"
1112
"github.com/opencontainers/runtime-tools/specerror"
@@ -155,3 +156,33 @@ func TestMultipleEnvCaching(t *testing.T) {
155156
g.AddMultipleProcessEnv([]string{})
156157
assert.Equal(t, []string(nil), g.Config.Process.Env)
157158
}
159+
160+
func TestAddLinuxNetDevice(t *testing.T) {
161+
// Start with empty ENV and add a few
162+
g, err := generate.New("linux")
163+
if err != nil {
164+
t.Fatal(err)
165+
}
166+
expected := map[string]rspec.LinuxNetDevice{
167+
"eth0": {
168+
Name: "eno1",
169+
},
170+
"eth1": {
171+
Name: "eno2",
172+
},
173+
"eth2": {
174+
Name: "eno3",
175+
},
176+
}
177+
g.AddLinuxNetDevice("eth0", &rspec.LinuxNetDevice{Name: "eno1"})
178+
g.AddLinuxNetDevice("eth1", &rspec.LinuxNetDevice{Name: "eno2"})
179+
g.AddLinuxNetDevice("eth2", &rspec.LinuxNetDevice{Name: "eno3"})
180+
assert.Equal(t, expected, g.Config.Linux.NetDevices)
181+
182+
g.RemoveLinuxNetDeviceByHostName("eth0")
183+
delete(expected, "eth0")
184+
assert.Equal(t, expected, g.Config.Linux.NetDevices)
185+
g.RemoveLinuxNetDeviceByName("eno2")
186+
delete(expected, "eth1")
187+
assert.Equal(t, expected, g.Config.Linux.NetDevices)
188+
}

0 commit comments

Comments
 (0)