Skip to content

Commit 459e52e

Browse files
authored
fix unhealthy on api server down (#813)
* fix router controller unhealthy on api server down * import glog * use NetworkRoutingController podCidr * fix undefind
1 parent 97c682e commit 459e52e

File tree

3 files changed

+15
-31
lines changed

3 files changed

+15
-31
lines changed

pkg/controllers/routing/bgp_policies.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,12 @@ func (nrc *NetworkRoutingController) AddPolicies() error {
1919
return nil
2020
}
2121

22-
cidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride)
23-
if err != nil {
24-
return err
25-
}
26-
2722
// creates prefix set to represent the assigned node's pod CIDR
2823
podCidrPrefixSet, err := table.NewPrefixSet(config.PrefixSet{
2924
PrefixSetName: "podcidrprefixset",
3025
PrefixList: []config.Prefix{
3126
{
32-
IpPrefix: cidr,
27+
IpPrefix: nrc.podCidr,
3328
},
3429
},
3530
})

pkg/controllers/routing/network_routes_controller.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ type NetworkRoutingController struct {
109109
pathPrepend bool
110110
localAddressList []string
111111
overrideNextHop bool
112+
podCidr string
112113

113114
nodeLister cache.Indexer
114115
svcLister cache.Indexer
@@ -324,10 +325,7 @@ func (nrc *NetworkRoutingController) updateCNIConfig() {
324325
cidrlen, _ := cidr.Mask.Size()
325326
oldCidr := cidr.IP.String() + "/" + strconv.Itoa(cidrlen)
326327

327-
currentCidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride)
328-
if err != nil {
329-
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)
330-
}
328+
currentCidr := nrc.podCidr
331329

332330
if len(cidr.IP) == 0 || strings.Compare(oldCidr, currentCidr) != 0 {
333331
err = utils.InsertPodCidrInCniSpec(nrc.cniConfFile, currentCidr)
@@ -366,12 +364,8 @@ func (nrc *NetworkRoutingController) advertisePodRoute() error {
366364
if nrc.MetricsEnabled {
367365
metrics.ControllerBGPadvertisementsSent.Inc()
368366
}
369-
cidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride)
370-
if err != nil {
371-
return err
372-
}
373367

374-
cidrStr := strings.Split(cidr, "/")
368+
cidrStr := strings.Split(nrc.podCidr, "/")
375369
subnet := cidrStr[0]
376370
cidrLen, _ := strconv.Atoi(cidrStr[1])
377371
if nrc.isIpv6 {
@@ -909,6 +903,13 @@ func NewNetworkRoutingController(clientset kubernetes.Interface,
909903
}
910904
}
911905

906+
cidr, err := utils.GetPodCidrFromNodeSpec(clientset, nrc.hostnameOverride)
907+
if err != nil {
908+
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)
909+
return nil, fmt.Errorf("Failed to get pod CIDR details from Node.spec: %s", err.Error())
910+
}
911+
nrc.podCidr = cidr
912+
912913
nrc.ipSetHandler, err = utils.NewIPSet(nrc.isIpv6)
913914
if err != nil {
914915
return nil, err

pkg/controllers/routing/pbr.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os/exec"
88
"strings"
99

10-
"github.com/cloudnativelabs/kube-router/pkg/utils"
1110
)
1211

1312
// 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 {
1817
return fmt.Errorf("Failed to update rt_tables file: %s", err)
1918
}
2019

21-
cidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride)
22-
if err != nil {
23-
return fmt.Errorf("Failed to get the pod CIDR allocated for the node: %s", err.Error())
24-
}
25-
2620
out, err := exec.Command("ip", "rule", "list").Output()
2721
if err != nil {
2822
return fmt.Errorf("Failed to verify if `ip rule` exists: %s", err.Error())
2923
}
3024

31-
if !strings.Contains(string(out), cidr) {
32-
err = exec.Command("ip", "rule", "add", "from", cidr, "lookup", customRouteTableID).Run()
25+
if !strings.Contains(string(out), nrc.podCidr) {
26+
err = exec.Command("ip", "rule", "add", "from", nrc.podCidr, "lookup", customRouteTableID).Run()
3327
if err != nil {
3428
return fmt.Errorf("Failed to add ip rule due to: %s", err.Error())
3529
}
@@ -44,20 +38,14 @@ func (nrc *NetworkRoutingController) disablePolicyBasedRouting() error {
4438
return fmt.Errorf("Failed to update rt_tables file: %s", err)
4539
}
4640

47-
cidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride)
48-
if err != nil {
49-
return fmt.Errorf("Failed to get the pod CIDR allocated for the node: %s",
50-
err.Error())
51-
}
52-
5341
out, err := exec.Command("ip", "rule", "list").Output()
5442
if err != nil {
5543
return fmt.Errorf("Failed to verify if `ip rule` exists: %s",
5644
err.Error())
5745
}
5846

59-
if strings.Contains(string(out), cidr) {
60-
err = exec.Command("ip", "rule", "del", "from", cidr, "table", customRouteTableID).Run()
47+
if strings.Contains(string(out), nrc.podCidr) {
48+
err = exec.Command("ip", "rule", "del", "from", nrc.podCidr, "table", customRouteTableID).Run()
6149
if err != nil {
6250
return fmt.Errorf("Failed to delete ip rule: %s", err.Error())
6351
}

0 commit comments

Comments
 (0)