Skip to content

Commit

Permalink
fix: use internal IP when node connect pod
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Sep 29, 2020
1 parent 1348a19 commit 9324491
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pkg/daemon/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@ type Controller struct {
iptable *iptables.IPTables
ipset *ipsets.IPSets

protocol string
protocol string
internalIP string
}

func getNodeInternalIP(node *v1.Node) string {
var nodeAddr string
for _, addr := range node.Status.Addresses {
if addr.Type == v1.NodeInternalIP {
nodeAddr = addr.Address
break
}
}
return nodeAddr
}

// NewController init a daemon controller
Expand Down Expand Up @@ -81,6 +93,7 @@ func NewController(config *Configuration, informerFactory informers.SharedInform
return nil, err
}
controller.protocol = util.CheckProtocol(node.Annotations[util.IpAddressAnnotation])
controller.internalIP = getNodeInternalIP(node)

if controller.protocol == kubeovnv1.ProtocolIPv4 {
iptable, err := iptables.NewWithProtocol(iptables.ProtocolIPv4)
Expand Down Expand Up @@ -120,7 +133,7 @@ func (c *Controller) enqueueSubnet(obj interface{}) {
c.subnetQueue.Add(key)
}

func (c *Controller) enqueueUpdateSubnet(old, new interface{}) {
func (c *Controller) enqueueUpdateSubnet(_, new interface{}) {
var key string
var err error
if key, err = cache.MetaNamespaceKeyFunc(new); err != nil {
Expand Down Expand Up @@ -220,7 +233,8 @@ func (c *Controller) reconcileRouters() error {
for _, r := range toAdd {
_, cidr, _ := net.ParseCIDR(r)
gw := net.ParseIP(gateway)
if err = netlink.RouteReplace(&netlink.Route{Dst: cidr, LinkIndex: nic.Attrs().Index, Scope: netlink.SCOPE_UNIVERSE, Gw: gw}); err != nil {
src := net.ParseIP(c.internalIP)
if err = netlink.RouteReplace(&netlink.Route{Dst: cidr, LinkIndex: nic.Attrs().Index, Scope: netlink.SCOPE_UNIVERSE, Gw: gw, Src: src}); err != nil {
klog.Errorf("failed to add route %v", err)
}
}
Expand Down

0 comments on commit 9324491

Please sign in to comment.