diff --git a/pkg/controllers/routing/bgp_policies.go b/pkg/controllers/routing/bgp_policies.go index a900520c..41ff5320 100644 --- a/pkg/controllers/routing/bgp_policies.go +++ b/pkg/controllers/routing/bgp_policies.go @@ -19,17 +19,12 @@ func (nrc *NetworkRoutingController) AddPolicies() error { return nil } - cidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride) - if err != nil { - return err - } - // creates prefix set to represent the assigned node's pod CIDR podCidrPrefixSet, err := table.NewPrefixSet(config.PrefixSet{ PrefixSetName: "podcidrprefixset", PrefixList: []config.Prefix{ { - IpPrefix: cidr, + IpPrefix: nrc.podCidr, }, }, }) diff --git a/pkg/controllers/routing/network_routes_controller.go b/pkg/controllers/routing/network_routes_controller.go index 590f4d2f..8a9c2be7 100644 --- a/pkg/controllers/routing/network_routes_controller.go +++ b/pkg/controllers/routing/network_routes_controller.go @@ -109,6 +109,7 @@ type NetworkRoutingController struct { pathPrepend bool localAddressList []string overrideNextHop bool + podCidr string nodeLister cache.Indexer svcLister cache.Indexer @@ -324,10 +325,7 @@ func (nrc *NetworkRoutingController) updateCNIConfig() { cidrlen, _ := cidr.Mask.Size() oldCidr := cidr.IP.String() + "/" + strconv.Itoa(cidrlen) - currentCidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride) - if err != nil { - glog.Fatalf("Failed to get pod CIDR from node spec. kube-router relies on kube-controller-manager to allocate pod CIDR for the node or an annotation `kube-router.io/pod-cidr`. Error: %v", err) - } + currentCidr := nrc.podCidr if len(cidr.IP) == 0 || strings.Compare(oldCidr, currentCidr) != 0 { err = utils.InsertPodCidrInCniSpec(nrc.cniConfFile, currentCidr) @@ -366,12 +364,8 @@ func (nrc *NetworkRoutingController) advertisePodRoute() error { if nrc.MetricsEnabled { metrics.ControllerBGPadvertisementsSent.Inc() } - cidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride) - if err != nil { - return err - } - cidrStr := strings.Split(cidr, "/") + cidrStr := strings.Split(nrc.podCidr, "/") subnet := cidrStr[0] cidrLen, _ := strconv.Atoi(cidrStr[1]) if nrc.isIpv6 { @@ -909,6 +903,13 @@ func NewNetworkRoutingController(clientset kubernetes.Interface, } } + cidr, err := utils.GetPodCidrFromNodeSpec(clientset, nrc.hostnameOverride) + if err != nil { + glog.Fatalf("Failed to get pod CIDR from node spec. kube-router relies on kube-controller-manager to allocate pod CIDR for the node or an annotation `kube-router.io/pod-cidr`. Error: %v", err) + return nil, fmt.Errorf("Failed to get pod CIDR details from Node.spec: %s", err.Error()) + } + nrc.podCidr = cidr + nrc.ipSetHandler, err = utils.NewIPSet(nrc.isIpv6) if err != nil { return nil, err diff --git a/pkg/controllers/routing/pbr.go b/pkg/controllers/routing/pbr.go index d497536f..e528e467 100644 --- a/pkg/controllers/routing/pbr.go +++ b/pkg/controllers/routing/pbr.go @@ -7,7 +7,6 @@ import ( "os/exec" "strings" - "github.com/cloudnativelabs/kube-router/pkg/utils" ) // setup a custom routing table that will be used for policy based routing to ensure traffic originating @@ -18,18 +17,13 @@ func (nrc *NetworkRoutingController) enablePolicyBasedRouting() error { return fmt.Errorf("Failed to update rt_tables file: %s", err) } - cidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride) - if err != nil { - return fmt.Errorf("Failed to get the pod CIDR allocated for the node: %s", err.Error()) - } - out, err := exec.Command("ip", "rule", "list").Output() if err != nil { return fmt.Errorf("Failed to verify if `ip rule` exists: %s", err.Error()) } - if !strings.Contains(string(out), cidr) { - err = exec.Command("ip", "rule", "add", "from", cidr, "lookup", customRouteTableID).Run() + if !strings.Contains(string(out), nrc.podCidr) { + err = exec.Command("ip", "rule", "add", "from", nrc.podCidr, "lookup", customRouteTableID).Run() if err != nil { return fmt.Errorf("Failed to add ip rule due to: %s", err.Error()) } @@ -44,20 +38,14 @@ func (nrc *NetworkRoutingController) disablePolicyBasedRouting() error { return fmt.Errorf("Failed to update rt_tables file: %s", err) } - cidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride) - if err != nil { - return fmt.Errorf("Failed to get the pod CIDR allocated for the node: %s", - err.Error()) - } - out, err := exec.Command("ip", "rule", "list").Output() if err != nil { return fmt.Errorf("Failed to verify if `ip rule` exists: %s", err.Error()) } - if strings.Contains(string(out), cidr) { - err = exec.Command("ip", "rule", "del", "from", cidr, "table", customRouteTableID).Run() + if strings.Contains(string(out), nrc.podCidr) { + err = exec.Command("ip", "rule", "del", "from", nrc.podCidr, "table", customRouteTableID).Run() if err != nil { return fmt.Errorf("Failed to delete ip rule: %s", err.Error()) }