Skip to content

Commit 257b6f3

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#43661 from xiangpengzhao/revert-genmac
Automatic merge from submit-queue (batch tested with PRs 43661, 54062). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Fix kubernetes#43583 (kubenet: remove code forcing bridge MAC address) **What this PR does / why we need it**: *kubenet: remove code forcing bridge MAC address* **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes kubernetes#43583 **Special notes for your reviewer**: **Release note**: ```release-note ``` cc @dcbw @freehan
2 parents 1bea47a + 8614da7 commit 257b6f3

File tree

2 files changed

+7
-73
lines changed

2 files changed

+7
-73
lines changed

pkg/kubelet/network/kubenet/kubenet_linux.go

+7-42
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
"time"
@@ -59,12 +58,6 @@ const (
5958
// fallbackMTU is used if an MTU is not specified, and we cannot determine the MTU
6059
fallbackMTU = 1460
6160

62-
// private mac prefix safe to use
63-
// Universally administered and locally administered addresses are distinguished by setting the second-least-significant
64-
// 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,
65-
// the first cotet is 0a(hex), the binary form of which is 00001010, where the second-least-significant bit is 1.
66-
privateMACPrefix = "0a:58"
67-
6861
// ebtables Chain to store dedup rules
6962
dedupChain = utilebtables.Chain("KUBE-DEDUP")
7063

@@ -334,22 +327,6 @@ func (plugin *kubenetNetworkPlugin) setup(namespace string, name string, id kube
334327
return fmt.Errorf("CNI plugin reported an invalid IPv4 address for container %v: %+v.", id, res.IP4)
335328
}
336329

337-
// Explicitly assign mac address to cbr0. If bridge mac address is not explicitly set will adopt the lowest MAC address of the attached veths.
338-
// TODO: Remove this once upstream cni bridge plugin handles this
339-
link, err := netlink.LinkByName(BridgeName)
340-
if err != nil {
341-
return fmt.Errorf("failed to lookup %q: %v", BridgeName, err)
342-
}
343-
macAddr, err := generateHardwareAddr(plugin.gateway)
344-
if err != nil {
345-
return err
346-
}
347-
glog.V(3).Infof("Configure %q mac address to %v", BridgeName, macAddr)
348-
err = netlink.LinkSetHardwareAddr(link, macAddr)
349-
if err != nil {
350-
return fmt.Errorf("Failed to configure %q mac address to %q: %v", BridgeName, macAddr, err)
351-
}
352-
353330
// Put the container bridge into promiscuous mode to force it to accept hairpin packets.
354331
// TODO: Remove this once the kernel bug (#20096) is fixed.
355332
// TODO: check and set promiscuous mode with netlink once vishvananda/netlink supports it
@@ -361,8 +338,14 @@ func (plugin *kubenetNetworkPlugin) setup(namespace string, name string, id kube
361338
return fmt.Errorf("Error setting promiscuous mode on %s: %v", BridgeName, err)
362339
}
363340
}
341+
342+
link, err := netlink.LinkByName(BridgeName)
343+
if err != nil {
344+
return fmt.Errorf("failed to lookup %q: %v", BridgeName, err)
345+
}
346+
364347
// configure the ebtables rules to eliminate duplicate packets by best effort
365-
plugin.syncEbtablesDedupRules(macAddr)
348+
plugin.syncEbtablesDedupRules(link.Attrs().HardwareAddr)
366349
}
367350

368351
plugin.podIPs[id] = ip4.String()
@@ -833,21 +816,3 @@ func (plugin *kubenetNetworkPlugin) syncEbtablesDedupRules(macAddr net.HardwareA
833816
return
834817
}
835818
}
836-
837-
// generateHardwareAddr generates 48 bit virtual mac addresses based on the IP input.
838-
func generateHardwareAddr(ip net.IP) (net.HardwareAddr, error) {
839-
if ip.To4() == nil {
840-
return nil, fmt.Errorf("generateHardwareAddr only support valid ipv4 address as input")
841-
}
842-
mac := privateMACPrefix
843-
sections := strings.Split(ip.String(), ".")
844-
for _, s := range sections {
845-
i, _ := strconv.Atoi(s)
846-
mac = mac + ":" + fmt.Sprintf("%02x", i)
847-
}
848-
hwAddr, err := net.ParseMAC(mac)
849-
if err != nil {
850-
return nil, fmt.Errorf("Failed to parse mac address %s generated based on ip %s due to: %v", mac, ip, err)
851-
}
852-
return hwAddr, nil
853-
}

pkg/kubelet/network/kubenet/kubenet_linux_test.go

-31
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"
@@ -200,36 +199,6 @@ func TestInit_MTU(t *testing.T) {
200199
assert.Equal(t, 1, sysctl.Settings["net/bridge/bridge-nf-call-iptables"], "net/bridge/bridge-nf-call-iptables sysctl should have been set")
201200
}
202201

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

0 commit comments

Comments
 (0)