diff --git a/plugins/main/bridge/bridge.go b/plugins/main/bridge/bridge.go index 4fe52be49..03d14e7ee 100644 --- a/plugins/main/bridge/bridge.go +++ b/plugins/main/bridge/bridge.go @@ -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 @@ -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 } diff --git a/plugins/main/bridge/bridge_test.go b/plugins/main/bridge/bridge_test.go index 691790439..f6c738b3a 100644 --- a/plugins/main/bridge/bridge_test.go +++ b/plugins/main/bridge/bridge_test.go @@ -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. @@ -162,6 +167,13 @@ const ( mac = `, "mac": %q` + + argsFormat = `, + "args": { + "cni": { + "mac": %q + } + }` ) // netConfJSON() generates a JSON network configuration string @@ -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 @@ -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()