From 81b17469f49ab7b81a98b87889b95e228a6d238f Mon Sep 17 00:00:00 2001 From: Murali Reddy Date: Wed, 21 Nov 2018 11:47:30 +0530 Subject: [PATCH 1/2] prevent kubernetes node connecting to self by excluding the node IP from the list of the peers passed to weaver Fixes #3398 --- prog/kube-utils/main.go | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/prog/kube-utils/main.go b/prog/kube-utils/main.go index 1ebd098fe1..7121cf39ea 100644 --- a/prog/kube-utils/main.go +++ b/prog/kube-utils/main.go @@ -15,14 +15,15 @@ import ( "syscall" "time" + "github.com/vishvananda/netlink" + weaveapi "github.com/weaveworks/weave/api" + "github.com/weaveworks/weave/common" + "golang.org/x/sys/unix" api "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/cache" - - weaveapi "github.com/weaveworks/weave/api" - "github.com/weaveworks/weave/common" ) type nodeInfo struct { @@ -54,6 +55,10 @@ func getKubePeers(c kubernetes.Interface, includeWithNoIPAddr bool) ([]nodeInfo, // Fallback for cases where a Node has an ExternalIP but no InternalIP if internalIP != "" { + // exclude self from the list of peers this node will peer with + if isLocalNodeIP(internalIP) { + continue + } addresses = append(addresses, nodeInfo{name: peer.Name, addr: internalIP}) } else if externalIP != "" { addresses = append(addresses, nodeInfo{name: peer.Name, addr: externalIP}) @@ -64,6 +69,20 @@ func getKubePeers(c kubernetes.Interface, includeWithNoIPAddr bool) ([]nodeInfo, return addresses, nil } +// returns true if given IP matches with one of the local IP's +func isLocalNodeIP(ip string) bool { + addrs, err := netlink.AddrList(nil, unix.AF_INET) + if err != nil { + return false + } + for _, addr := range addrs { + if addr.Peer.IP.String() == ip { + return true + } + } + return false +} + // (minimal, incomplete) interface so weaver can be mocked for testing. type weaveClient interface { RmPeer(peerName string) (string, error) From d2a9a5129677a2994c50dbe79cd7a8e17289901e Mon Sep 17 00:00:00 2001 From: Murali Reddy Date: Tue, 4 Dec 2018 10:58:43 +0530 Subject: [PATCH 2/2] follow project conventions in ordering the imports --- prog/kube-utils/main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/prog/kube-utils/main.go b/prog/kube-utils/main.go index 7121cf39ea..ac87bbdbe7 100644 --- a/prog/kube-utils/main.go +++ b/prog/kube-utils/main.go @@ -16,14 +16,15 @@ import ( "time" "github.com/vishvananda/netlink" - weaveapi "github.com/weaveworks/weave/api" - "github.com/weaveworks/weave/common" "golang.org/x/sys/unix" api "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/cache" + + weaveapi "github.com/weaveworks/weave/api" + "github.com/weaveworks/weave/common" ) type nodeInfo struct {