Skip to content
This repository was archived by the owner on Jun 20, 2024. It is now read-only.

Commit 024cfbe

Browse files
committed
prevent kubernetes node connecting to self by excluding the node IP from the list of the peers passed to weaver
Fixes #3398
1 parent 6e3f8a1 commit 024cfbe

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

prog/kube-utils/main.go

+22-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import (
1515
"syscall"
1616
"time"
1717

18+
"github.com/vishvananda/netlink"
19+
weaveapi "github.com/weaveworks/weave/api"
20+
"github.com/weaveworks/weave/common"
21+
"golang.org/x/sys/unix"
1822
api "k8s.io/apimachinery/pkg/apis/meta/v1"
1923
"k8s.io/client-go/informers"
2024
"k8s.io/client-go/kubernetes"
2125
"k8s.io/client-go/rest"
2226
"k8s.io/client-go/tools/cache"
23-
24-
weaveapi "github.com/weaveworks/weave/api"
25-
"github.com/weaveworks/weave/common"
2627
)
2728

2829
type nodeInfo struct {
@@ -54,6 +55,10 @@ func getKubePeers(c kubernetes.Interface, includeWithNoIPAddr bool) ([]nodeInfo,
5455

5556
// Fallback for cases where a Node has an ExternalIP but no InternalIP
5657
if internalIP != "" {
58+
// exclude self from the list of peers this node will peer with
59+
if isLocalNodeIp(internalIP) {
60+
continue
61+
}
5762
addresses = append(addresses, nodeInfo{name: peer.Name, addr: internalIP})
5863
} else if externalIP != "" {
5964
addresses = append(addresses, nodeInfo{name: peer.Name, addr: externalIP})
@@ -64,6 +69,20 @@ func getKubePeers(c kubernetes.Interface, includeWithNoIPAddr bool) ([]nodeInfo,
6469
return addresses, nil
6570
}
6671

72+
// returns true if given IP matches with one of the local IP's
73+
func isLocalNodeIP(ip string) bool {
74+
addrs, err := netlink.AddrList(nil, unix.AF_INET)
75+
if err != nil {
76+
return false
77+
}
78+
for _, addr := range addrs {
79+
if addr.Peer.IP.String() == ip {
80+
return true
81+
}
82+
}
83+
return false
84+
}
85+
6786
// (minimal, incomplete) interface so weaver can be mocked for testing.
6887
type weaveClient interface {
6988
RmPeer(peerName string) (string, error)

0 commit comments

Comments
 (0)