fix unhealthy on api server down (#813)

* fix router controller unhealthy on api server down

* import glog

* use  NetworkRoutingController  podCidr

* fix undefind
This commit is contained in:
wu0407 2020-02-17 04:26:21 +08:00 committed by GitHub
parent 97c682e6f2
commit 459e52eba2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 31 deletions

View File

@ -19,17 +19,12 @@ func (nrc *NetworkRoutingController) AddPolicies() error {
return nil 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 // creates prefix set to represent the assigned node's pod CIDR
podCidrPrefixSet, err := table.NewPrefixSet(config.PrefixSet{ podCidrPrefixSet, err := table.NewPrefixSet(config.PrefixSet{
PrefixSetName: "podcidrprefixset", PrefixSetName: "podcidrprefixset",
PrefixList: []config.Prefix{ PrefixList: []config.Prefix{
{ {
IpPrefix: cidr, IpPrefix: nrc.podCidr,
}, },
}, },
}) })

View File

@ -109,6 +109,7 @@ type NetworkRoutingController struct {
pathPrepend bool pathPrepend bool
localAddressList []string localAddressList []string
overrideNextHop bool overrideNextHop bool
podCidr string
nodeLister cache.Indexer nodeLister cache.Indexer
svcLister cache.Indexer svcLister cache.Indexer
@ -324,10 +325,7 @@ func (nrc *NetworkRoutingController) updateCNIConfig() {
cidrlen, _ := cidr.Mask.Size() cidrlen, _ := cidr.Mask.Size()
oldCidr := cidr.IP.String() + "/" + strconv.Itoa(cidrlen) oldCidr := cidr.IP.String() + "/" + strconv.Itoa(cidrlen)
currentCidr, err := utils.GetPodCidrFromNodeSpec(nrc.clientset, nrc.hostnameOverride) currentCidr := nrc.podCidr
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)
}
if len(cidr.IP) == 0 || strings.Compare(oldCidr, currentCidr) != 0 { if len(cidr.IP) == 0 || strings.Compare(oldCidr, currentCidr) != 0 {
err = utils.InsertPodCidrInCniSpec(nrc.cniConfFile, currentCidr) err = utils.InsertPodCidrInCniSpec(nrc.cniConfFile, currentCidr)
@ -366,12 +364,8 @@ func (nrc *NetworkRoutingController) advertisePodRoute() error {
if nrc.MetricsEnabled { if nrc.MetricsEnabled {
metrics.ControllerBGPadvertisementsSent.Inc() 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] subnet := cidrStr[0]
cidrLen, _ := strconv.Atoi(cidrStr[1]) cidrLen, _ := strconv.Atoi(cidrStr[1])
if nrc.isIpv6 { 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) nrc.ipSetHandler, err = utils.NewIPSet(nrc.isIpv6)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -7,7 +7,6 @@ import (
"os/exec" "os/exec"
"strings" "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 // 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) 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() out, err := exec.Command("ip", "rule", "list").Output()
if err != nil { if err != nil {
return fmt.Errorf("Failed to verify if `ip rule` exists: %s", err.Error()) return fmt.Errorf("Failed to verify if `ip rule` exists: %s", err.Error())
} }
if !strings.Contains(string(out), cidr) { if !strings.Contains(string(out), nrc.podCidr) {
err = exec.Command("ip", "rule", "add", "from", cidr, "lookup", customRouteTableID).Run() err = exec.Command("ip", "rule", "add", "from", nrc.podCidr, "lookup", customRouteTableID).Run()
if err != nil { if err != nil {
return fmt.Errorf("Failed to add ip rule due to: %s", err.Error()) 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) 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() out, err := exec.Command("ip", "rule", "list").Output()
if err != nil { if err != nil {
return fmt.Errorf("Failed to verify if `ip rule` exists: %s", return fmt.Errorf("Failed to verify if `ip rule` exists: %s",
err.Error()) err.Error())
} }
if strings.Contains(string(out), cidr) { if strings.Contains(string(out), nrc.podCidr) {
err = exec.Command("ip", "rule", "del", "from", cidr, "table", customRouteTableID).Run() err = exec.Command("ip", "rule", "del", "from", nrc.podCidr, "table", customRouteTableID).Run()
if err != nil { if err != nil {
return fmt.Errorf("Failed to delete ip rule: %s", err.Error()) return fmt.Errorf("Failed to delete ip rule: %s", err.Error())
} }