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

Commit

Permalink
prevent kubernetes node connecting to self by excluding the node IP f…
Browse files Browse the repository at this point in the history
…rom the list of the peers passed to weaver

Fixes #3398
  • Loading branch information
murali-reddy committed Nov 21, 2018
1 parent 6e3f8a1 commit 024cfbe
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions prog/kube-utils/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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})
Expand All @@ -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)
Expand Down

0 comments on commit 024cfbe

Please sign in to comment.