Skip to content

Commit 8614da7

Browse files
committed
kubenet: remove code forcing bridge MAC address
1 parent 595998c commit 8614da7

File tree

2 files changed

+7
-73
lines changed

2 files changed

+7
-73
lines changed

pkg/kubelet/network/kubenet/kubenet_linux.go

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"io/ioutil"
2424
"net"
2525
"path/filepath"
26-
"strconv"
2726
"strings"
2827
"sync"
2928
"syscall"
@@ -58,12 +57,6 @@ const (
5857
// fallbackMTU is used if an MTU is not specified, and we cannot determine the MTU
5958
fallbackMTU = 1460
6059

61-
// private mac prefix safe to use
62-
// Universally administered and locally administered addresses are distinguished by setting the second-least-significant
63-
// bit of the first octet of the address. If it is 1, the address is locally administered. For example, for address 0a:00:00:00:00:00,
64-
// the first cotet is 0a(hex), the binary form of which is 00001010, where the second-least-significant bit is 1.
65-
privateMACPrefix = "0a:58"
66-
6760
// ebtables Chain to store dedup rules
6861
dedupChain = utilebtables.Chain("KUBE-DEDUP")
6962

@@ -326,22 +319,6 @@ func (plugin *kubenetNetworkPlugin) setup(namespace string, name string, id kube
326319
return fmt.Errorf("CNI plugin reported an invalid IPv4 address for container %v: %+v.", id, res.IP4)
327320
}
328321

329-
// Explicitly assign mac address to cbr0. If bridge mac address is not explicitly set will adopt the lowest MAC address of the attached veths.
330-
// TODO: Remove this once upstream cni bridge plugin handles this
331-
link, err := netlink.LinkByName(BridgeName)
332-
if err != nil {
333-
return fmt.Errorf("failed to lookup %q: %v", BridgeName, err)
334-
}
335-
macAddr, err := generateHardwareAddr(plugin.gateway)
336-
if err != nil {
337-
return err
338-
}
339-
glog.V(3).Infof("Configure %q mac address to %v", BridgeName, macAddr)
340-
err = netlink.LinkSetHardwareAddr(link, macAddr)
341-
if err != nil {
342-
return fmt.Errorf("Failed to configure %q mac address to %q: %v", BridgeName, macAddr, err)
343-
}
344-
345322
// Put the container bridge into promiscuous mode to force it to accept hairpin packets.
346323
// TODO: Remove this once the kernel bug (#20096) is fixed.
347324
// TODO: check and set promiscuous mode with netlink once vishvananda/netlink supports it
@@ -353,8 +330,14 @@ func (plugin *kubenetNetworkPlugin) setup(namespace string, name string, id kube
353330
return fmt.Errorf("Error setting promiscuous mode on %s: %v", BridgeName, err)
354331
}
355332
}
333+
334+
link, err := netlink.LinkByName(BridgeName)
335+
if err != nil {
336+
return fmt.Errorf("failed to lookup %q: %v", BridgeName, err)
337+
}
338+
356339
// configure the ebtables rules to eliminate duplicate packets by best effort
357-
plugin.syncEbtablesDedupRules(macAddr)
340+
plugin.syncEbtablesDedupRules(link.Attrs().HardwareAddr)
358341
}
359342

360343
plugin.podIPs[id] = ip4.String()
@@ -845,21 +828,3 @@ func (plugin *kubenetNetworkPlugin) syncEbtablesDedupRules(macAddr net.HardwareA
845828
return
846829
}
847830
}
848-
849-
// generateHardwareAddr generates 48 bit virtual mac addresses based on the IP input.
850-
func generateHardwareAddr(ip net.IP) (net.HardwareAddr, error) {
851-
if ip.To4() == nil {
852-
return nil, fmt.Errorf("generateHardwareAddr only support valid ipv4 address as input")
853-
}
854-
mac := privateMACPrefix
855-
sections := strings.Split(ip.String(), ".")
856-
for _, s := range sections {
857-
i, _ := strconv.Atoi(s)
858-
mac = mac + ":" + fmt.Sprintf("%02x", i)
859-
}
860-
hwAddr, err := net.ParseMAC(mac)
861-
if err != nil {
862-
return nil, fmt.Errorf("Failed to parse mac address %s generated based on ip %s due to: %v", mac, ip, err)
863-
}
864-
return hwAddr, nil
865-
}

pkg/kubelet/network/kubenet/kubenet_linux_test.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package kubenet
1818

1919
import (
2020
"fmt"
21-
"net"
2221

2322
"github.com/stretchr/testify/assert"
2423
"github.com/stretchr/testify/mock"
@@ -199,36 +198,6 @@ func TestInit_MTU(t *testing.T) {
199198
assert.Equal(t, 1, sysctl.Settings["net/bridge/bridge-nf-call-iptables"], "net/bridge/bridge-nf-call-iptables sysctl should have been set")
200199
}
201200

202-
func TestGenerateMacAddress(t *testing.T) {
203-
testCases := []struct {
204-
ip net.IP
205-
expectedMAC string
206-
}{
207-
{
208-
ip: net.ParseIP("10.0.0.2"),
209-
expectedMAC: privateMACPrefix + ":0a:00:00:02",
210-
},
211-
{
212-
ip: net.ParseIP("10.250.0.244"),
213-
expectedMAC: privateMACPrefix + ":0a:fa:00:f4",
214-
},
215-
{
216-
ip: net.ParseIP("172.17.0.2"),
217-
expectedMAC: privateMACPrefix + ":ac:11:00:02",
218-
},
219-
}
220-
221-
for _, tc := range testCases {
222-
mac, err := generateHardwareAddr(tc.ip)
223-
if err != nil {
224-
t.Errorf("Did not expect error: %v", err)
225-
}
226-
if mac.String() != tc.expectedMAC {
227-
t.Errorf("generated mac: %q, expecting: %q", mac.String(), tc.expectedMAC)
228-
}
229-
}
230-
}
231-
232201
// TestInvocationWithoutRuntime invokes the plugin without a runtime.
233202
// This is how kubenet is invoked from the cri.
234203
func TestTearDownWithoutRuntime(t *testing.T) {

0 commit comments

Comments
 (0)