Skip to content

Commit

Permalink
add available check for northd enpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed May 16, 2023
1 parent f7a80c9 commit 94e7463
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions pkg/ovn_leader_checker/ovn.go
Expand Up @@ -31,9 +31,13 @@ const (
EnvPodName = "POD_NAME"
EnvPodNameSpace = "POD_NAMESPACE"
OvnNorthdPid = "/var/run/ovn/ovn-northd.pid"
DefaultProbeInterval = 15
DefaultProbeInterval = 5
OvnNorthdPort = "6643"
MaxFailCount = 3
)

var failCount int

// Configuration is the controller conf
type Configuration struct {
KubeConfigFile string
Expand Down Expand Up @@ -205,14 +209,16 @@ func checkNorthdActive() bool {
}

func stealLock() {
podIP := os.Getenv("POD_IP")

var command []string
if os.Getenv(EnvSSL) == "false" {
command = []string{
"-v",
"-t",
"1",
"steal",
"tcp:127.0.0.1:6642",
fmt.Sprintf("tcp:%s:6642", podIP),
"ovn_northd",
}
} else {
Expand All @@ -227,7 +233,7 @@ func stealLock() {
"-C",
"/var/run/tls/cacert",
"steal",
"ssl:127.0.0.1:6642",
fmt.Sprintf("ssl:%s:6642", podIP),
"ovn_northd",
}
}
Expand Down Expand Up @@ -268,7 +274,24 @@ func checkNorthdSvcExist(cfg *Configuration, namespace, svcName string) bool {
return true
}

func checkNorthdSvcValidIP(cfg *Configuration, namespace, epName string) bool {
func checkNorthdEpAvailable(ip string) bool {
address := net.JoinHostPort(ip, OvnNorthdPort)
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
if err != nil {
klog.Errorf("failed to connect to northd leader %s, err: %v", ip, err)
failCount++
if failCount >= MaxFailCount {
return false
}
} else {
failCount = 0
klog.V(5).Infof("succeed to connect to northd leader %s", ip)
_ = conn.Close()
}
return true
}

func checkNorthdEpAlive(cfg *Configuration, namespace, epName string) bool {
eps, err := cfg.KubeClient.CoreV1().Endpoints(namespace).Get(context.Background(), epName, metav1.GetOptions{})
if err != nil {
klog.Errorf("get ep %v namespace %v error %v", epName, namespace, err)
Expand All @@ -284,9 +307,9 @@ func checkNorthdSvcValidIP(cfg *Configuration, namespace, epName string) bool {
klog.V(5).Infof("epName %v has no address assigned", epName)
return false
}

klog.V(5).Infof("epName %v address assigned %+v", epName, eps.Subsets[0].Addresses[0].IP)
return true

return checkNorthdEpAvailable(eps.Subsets[0].Addresses[0].IP)
}

func updatePodLabels(labels map[string]string, key string, isLeader bool) {
Expand Down Expand Up @@ -351,7 +374,7 @@ func doOvnLeaderCheck(cfg *Configuration, podName string, podNamespace string) {
return
}
if sbLeader && checkNorthdSvcExist(cfg, podNamespace, "ovn-northd") {
if !checkNorthdSvcValidIP(cfg, podNamespace, "ovn-northd") {
if !checkNorthdEpAlive(cfg, podNamespace, "ovn-northd") {
klog.Warning("no available northd leader, try to release the lock")
stealLock()
}
Expand Down

0 comments on commit 94e7463

Please sign in to comment.