Skip to content

Commit 3cc7064

Browse files
oilbeatercursoragent
authored andcommitted
refactor(ovn_leader_checker): improve readability and naming (kubeovn#6263)
- 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> (cherry picked from commit 7dfd023)
1 parent 335293a commit 3cc7064

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
}
@@ -278,7 +278,7 @@ func checkNorthdSvcExist(cfg *Configuration, namespace, svcName string) bool {
278278

279279
func checkNorthdEpAvailable(ip string) bool {
280280
address := util.JoinHostPort(ip, util.NBRaftPort)
281-
conn, err := net.DialTimeout("tcp", address, 3*time.Second)
281+
conn, err := net.DialTimeout("tcp", address, northdDialTimeout)
282282
if err != nil {
283283
klog.Errorf("failed to connect to northd leader %s, err: %v", ip, err)
284284
failCount++
@@ -455,12 +455,12 @@ func doOvnLeaderCheck(cfg *Configuration, podName, podNamespace string) {
455455
expectedAddrType = discoveryv1.AddressTypeIPv4
456456
}
457457

458-
if !cfg.ISICDBServer && !checkOvnIsAlive() {
458+
if !cfg.IsICDBServer && !checkOvnIsAlive() {
459459
klog.Errorf("ovn is not alive")
460460
return
461461
}
462462

463-
if !cfg.ISICDBServer {
463+
if !cfg.IsICDBServer {
464464
nbLeader := isDBLeader(cfg.localAddress, ovnnb.DatabaseName)
465465
sbLeader := isDBLeader(cfg.localAddress, ovnsb.DatabaseName)
466466
northdActive := checkNorthdActive()
@@ -539,8 +539,8 @@ func StartOvnLeaderCheck(cfg *Configuration) {
539539
podNamespace := os.Getenv(util.EnvPodNamespace)
540540
interval := time.Duration(cfg.ProbeInterval) * time.Second
541541
for {
542-
doOvnLeaderCheck(cfg, podName, podNamespace)
543542
time.Sleep(interval)
543+
doOvnLeaderCheck(cfg, podName, podNamespace)
544544
}
545545
}
546546

@@ -552,7 +552,7 @@ func getTSName(index int) string {
552552
}
553553

554554
func getTSCidr(index int) (string, error) {
555-
var proto, cidr string
555+
var proto string
556556
podIpsEnv := os.Getenv(util.EnvPodIPs)
557557
podIps := strings.Split(podIpsEnv, ",")
558558
if len(podIps) == 1 {
@@ -564,16 +564,15 @@ func getTSCidr(index int) (string, error) {
564564
} else if len(podIps) == 2 {
565565
proto = kubeovnv1.ProtocolDual
566566
}
567-
568567
switch proto {
569568
case kubeovnv1.ProtocolIPv4:
570-
cidr = fmt.Sprintf("169.254.%d.0/24", 100+index)
569+
return fmt.Sprintf("169.254.%d.0/24", 100+index), nil
571570
case kubeovnv1.ProtocolIPv6:
572-
cidr = fmt.Sprintf("fe80:a9fe:%02x::/112", 100+index)
571+
return fmt.Sprintf("fe80:a9fe:%02x::/112", 100+index), nil
573572
case kubeovnv1.ProtocolDual:
574-
cidr = fmt.Sprintf("169.254.%d.0/24,fe80:a9fe:%02x::/112", 100+index, 100+index)
573+
return fmt.Sprintf("169.254.%d.0/24,fe80:a9fe:%02x::/112", 100+index, 100+index), nil
575574
}
576-
return cidr, nil
575+
return "", fmt.Errorf("unsupported protocol %s", proto)
577576
}
578577

579578
func updateTS() error {

0 commit comments

Comments
 (0)