Skip to content

Commit

Permalink
bridge: Support the mac parameter through RuntimeConfig
Browse files Browse the repository at this point in the history
To use RuntimeConfig, the network configuration should include the
`capabilities` field with `mac` specified (`"capabilities": {"mac": true}`).

Signed-off-by: Edward Haas <[email protected]>
  • Loading branch information
EdDev committed Jun 16, 2021
1 parent e630934 commit d1bcb40
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions plugins/main/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +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"`
RuntimeConfig struct {
Mac string `json:"mac,omitempty"`
} `json:"runtimeConfig,omit"`
}

type BridgeArgs struct {
Expand Down Expand Up @@ -110,6 +114,10 @@ func loadNetConf(bytes []byte, envArgs string) (*NetConf, string, error) {
n.Mac = mac
}

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

return n, n.CNIVersion, nil
}

Expand Down
30 changes: 30 additions & 0 deletions plugins/main/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ const (
"mac": %q
}
}`

runtimeConfig = `,
"RuntimeConfig": {
"mac": %q
}`
)

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

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

It(fmt.Sprintf("[%s] uses an explicit MAC addresses for the container iface (from RuntimeConfig)", 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 = "02:00:00:00:07:89"
tc.runtimeConfig.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 d1bcb40

Please sign in to comment.