Skip to content

Commit

Permalink
bridge: Support the mac parameter through Args
Browse files Browse the repository at this point in the history
Signed-off-by: Edward Haas <[email protected]>
  • Loading branch information
EdDev committed Jun 16, 2021
1 parent cf79c19 commit e630934
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions plugins/main/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ type NetConf struct {
PromiscMode bool `json:"promiscMode"`
Vlan int `json:"vlan"`
Mac string `json:"mac,omitempty"`
Args struct {
Cni BridgeArgs `json:"cni,omitempty"`
} `json:"args,omitempty"`
}

type BridgeArgs struct {
Mac string `json:"mac,omitempty"`
}

// MacEnvArgs represents CNI_ARGS
Expand Down Expand Up @@ -99,6 +106,10 @@ func loadNetConf(bytes []byte, envArgs string) (*NetConf, string, error) {
}
}

if mac := n.Args.Cni.Mac; mac != "" {
n.Mac = mac
}

return n, n.CNIVersion, nil
}

Expand Down
36 changes: 36 additions & 0 deletions plugins/main/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ type testCase struct {
runtimeConfig struct {
mac string
}
args struct {
cni struct {
mac string
}
}

// Unlike the parameters above, the following parameters
// are expected values to be checked against.
Expand Down Expand Up @@ -162,6 +167,13 @@ const (

mac = `,
"mac": %q`

argsFormat = `,
"args": {
"cni": {
"mac": %q
}
}`
)

// netConfJSON() generates a JSON network configuration string
Expand All @@ -177,6 +189,9 @@ func (tc testCase) netConfJSON(dataDir string) string {
if tc.mac != "" {
conf += fmt.Sprintf(mac, tc.mac)
}
if tc.args.cni.mac != "" {
conf += fmt.Sprintf(argsFormat, tc.args.cni.mac)
}

if !tc.isLayer2 {
conf += netDefault
Expand Down Expand Up @@ -2033,6 +2048,27 @@ var _ = Describe("bridge Operations", func() {
Expect(err).NotTo(HaveOccurred())
})

It(fmt.Sprintf("[%s] uses an explicit MAC addresses for the container iface (from Args)", ver), func() {
err := originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()

const expectedMac = "02:00:00:00:00:00"
tc := testCase{
cniVersion: ver,
subnet: "10.1.2.0/24",
mac: "02:00:00:00:01:23",
envArgs: "MAC=" + "02:00:00:00:04:56",

expectedMac: expectedMac,
}
tc.args.cni.mac = expectedMac
cmdAddDelTest(originalNS, targetNS, tc, dataDir)

return nil
})
Expect(err).NotTo(HaveOccurred())
})

It(fmt.Sprintf("[%s] checks ip release in case of error", ver), func() {
err := originalNS.Do(func(ns.NetNS) error {
defer GinkgoRecover()
Expand Down

0 comments on commit e630934

Please sign in to comment.