Skip to content

Commit 50167b9

Browse files
refactor(ovn_leader_checker): improve readability and naming
- Add northdDialTimeout constant for northd dial timeout - Rename Configuration.ISICDBServer to IsICDBServer - In getCmdExitCode: use if err := cmd.Run(); err != nil; log clear message when ProcessState is nil - In checkOvnIsAlive: use exitCode != 0 instead of err - In getTSCidr: return directly in switch and return error for unsupported protocol Signed-off-by: Mengxin Liu <liumengxinfly@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: Mengxin Liu <liumengxinfly@gmail.com>
1 parent 772531f commit 50167b9

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

pkg/ovn_leader_checker/ovn.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const (
4040
OvnNorthdPid = "/var/run/ovn/ovn-northd.pid"
4141
DefaultProbeInterval = 5
4242
MaxFailCount = 3
43+
northdDialTimeout = 3 * time.Second
4344
)
4445

4546
var failCount int
@@ -52,7 +53,7 @@ type Configuration struct {
5253
KubeClient kubernetes.Interface
5354
ProbeInterval int
5455
EnableCompact bool
55-
ISICDBServer bool
56+
IsICDBServer bool
5657
localAddress string
5758
remoteAddresses []string
5859
}
@@ -98,7 +99,7 @@ func ParseFlags() (*Configuration, error) {
9899
KubeConfigFile: *argKubeConfigFile,
99100
ProbeInterval: *argProbeInterval,
100101
EnableCompact: *argEnableCompact,
101-
ISICDBServer: *argIsICDBServer,
102+
IsICDBServer: *argIsICDBServer,
102103
localAddress: *localAddress,
103104
remoteAddresses: slices.DeleteFunc(*remoteAddresses, func(s string) bool { return s == *localAddress }),
104105
}
@@ -136,13 +137,12 @@ func KubeClientInit(cfg *Configuration) error {
136137
}
137138

138139
func getCmdExitCode(cmd *exec.Cmd) int {
139-
err := cmd.Run()
140-
if err != nil {
140+
if err := cmd.Run(); err != nil {
141141
klog.Errorf("getCmdExitCode run error %v", err)
142142
return -1
143143
}
144144
if cmd.ProcessState == nil {
145-
klog.Errorf("getCmdExitCode run error %v", err)
145+
klog.Errorf("getCmdExitCode: process state not available")
146146
return -1
147147
}
148148
status := cmd.ProcessState.Sys().(syscall.WaitStatus)
@@ -156,7 +156,7 @@ func checkOvnIsAlive() bool {
156156
components := [...]string{"northd", "ovnnb", "ovnsb"}
157157
for _, component := range components {
158158
cmd := exec.Command("/usr/share/ovn/scripts/ovn-ctl", "status_"+component) // #nosec G204
159-
if err := getCmdExitCode(cmd); err != 0 {
159+
if exitCode := getCmdExitCode(cmd); exitCode != 0 {
160160
klog.Errorf("CheckOvnIsAlive: %s is not alive", component)
161161
return false
162162
}
@@ -267,7 +267,7 @@ func checkNorthdSvcExist(cfg *Configuration, namespace, svcName string) bool {
267267

268268
func checkNorthdEpAvailable(ip string) bool {
269269
address := util.JoinHostPort(ip, util.NBRaftPort)
270-
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
270+
conn, err := net.DialTimeout("tcp", address, northdDialTimeout)
271271
if err != nil {
272272
klog.Errorf("failed to connect to northd leader %s, err: %v", ip, err)
273273
failCount++
@@ -377,12 +377,12 @@ func doOvnLeaderCheck(cfg *Configuration, podName, podNamespace string) {
377377
util.LogFatalAndExit(nil, "preValidChkCfg: invalid cfg")
378378
}
379379

380-
if !cfg.ISICDBServer && !checkOvnIsAlive() {
380+
if !cfg.IsICDBServer && !checkOvnIsAlive() {
381381
klog.Errorf("ovn is not alive")
382382
return
383383
}
384384

385-
if !cfg.ISICDBServer {
385+
if !cfg.IsICDBServer {
386386
nbLeader := isDBLeader(cfg.localAddress, ovnnb.DatabaseName)
387387
sbLeader := isDBLeader(cfg.localAddress, ovnsb.DatabaseName)
388388
northdActive := checkNorthdActive()
@@ -453,8 +453,8 @@ func StartOvnLeaderCheck(cfg *Configuration) {
453453
podNamespace := os.Getenv(util.EnvPodNamespace)
454454
interval := time.Duration(cfg.ProbeInterval) * time.Second
455455
for {
456-
doOvnLeaderCheck(cfg, podName, podNamespace)
457456
time.Sleep(interval)
457+
doOvnLeaderCheck(cfg, podName, podNamespace)
458458
}
459459
}
460460

@@ -466,7 +466,7 @@ func getTSName(index int) string {
466466
}
467467

468468
func getTSCidr(index int) (string, error) {
469-
var proto, cidr string
469+
var proto string
470470
podIpsEnv := os.Getenv(util.EnvPodIPs)
471471
podIps := strings.Split(podIpsEnv, ",")
472472
if len(podIps) == 1 {
@@ -478,16 +478,15 @@ func getTSCidr(index int) (string, error) {
478478
} else if len(podIps) == 2 {
479479
proto = kubeovnv1.ProtocolDual
480480
}
481-
482481
switch proto {
483482
case kubeovnv1.ProtocolIPv4:
484-
cidr = fmt.Sprintf("169.254.%d.0/24", 100+index)
483+
return fmt.Sprintf("169.254.%d.0/24", 100+index), nil
485484
case kubeovnv1.ProtocolIPv6:
486-
cidr = fmt.Sprintf("fe80:a9fe:%02x::/112", 100+index)
485+
return fmt.Sprintf("fe80:a9fe:%02x::/112", 100+index), nil
487486
case kubeovnv1.ProtocolDual:
488-
cidr = fmt.Sprintf("169.254.%d.0/24,fe80:a9fe:%02x::/112", 100+index, 100+index)
487+
return fmt.Sprintf("169.254.%d.0/24,fe80:a9fe:%02x::/112", 100+index, 100+index), nil
489488
}
490-
return cidr, nil
489+
return "", fmt.Errorf("unsupported protocol %s", proto)
491490
}
492491

493492
func updateTS() error {

0 commit comments

Comments
 (0)