Skip to content

Commit

Permalink
refactor: reduce duplicated function
Browse files Browse the repository at this point in the history
(cherry picked from commit 9eee6f9)
  • Loading branch information
oilbeater committed Mar 9, 2021
1 parent afe9a9f commit 35190e1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
13 changes: 1 addition & 12 deletions pkg/controller/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (c *Controller) handleAddNode(key string) error {
}

// There is only one nodeAddr temp
nodeAddr := getNodeInternalIP(node)
nodeAddr := util.GetNodeInternalIP(node)
for _, ip := range strings.Split(ipStr, ",") {
if util.CheckProtocol(nodeAddr) == util.CheckProtocol(ip) {
err = c.ovnClient.AddStaticRoute("", nodeAddr, ip, c.config.ClusterRouter)
Expand Down Expand Up @@ -337,17 +337,6 @@ func gatewayContains(gatewayNodeStr, gateway string) bool {
return false
}

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
}

func (c *Controller) createOrUpdateCrdIPs(key, ip, mac string) error {
v4IP, v6IP := util.SplitStringIP(ip)
ipCr, err := c.config.KubeOvnClient.KubeovnV1().IPs().Get(context.Background(), fmt.Sprintf("node-%s", key), metav1.GetOptions{})
Expand Down
13 changes: 1 addition & 12 deletions pkg/daemon/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,6 @@ type Controller struct {
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
func NewController(config *Configuration, podInformerFactory informers.SharedInformerFactory, nodeInformerFactory informers.SharedInformerFactory, kubeovnInformerFactory kubeovninformer.SharedInformerFactory) (*Controller, error) {
eventBroadcaster := record.NewBroadcaster()
Expand Down Expand Up @@ -102,7 +91,7 @@ func NewController(config *Configuration, podInformerFactory informers.SharedInf
return nil, err
}
controller.protocol = util.CheckProtocol(node.Annotations[util.IpAddressAnnotation])
controller.internalIP = getNodeInternalIP(node)
controller.internalIP = util.GetNodeInternalIP(node)

controller.iptable = make(map[string]*iptables.IPTables)
controller.ipset = make(map[string]*ipsets.IPSets)
Expand Down
14 changes: 14 additions & 0 deletions pkg/util/k8s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package util

import v1 "k8s.io/api/core/v1"

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
}

0 comments on commit 35190e1

Please sign in to comment.