Skip to content

Commit 6dfed02

Browse files
authored
Merge pull request #32 from kubecube-io/fix-member-restart
Fix: when leader election fail, do not restart
2 parents a8c8de3 + c07cdf0 commit 6dfed02

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
bin
9+
10+
# Test binary, build with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Kubernetes Generated files - skip generated files, except for vendored files
17+
18+
!vendor/**/zz_generated.*
19+
20+
# editor and IDE paraphernalia
21+
.idea
22+
.vscode
23+
*.swp
24+
*.swo
25+
*~
26+

main.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ import (
3636
"kubecube-webconsole/handler"
3737
)
3838

39+
// leader flag
40+
var leader = false
41+
3942
func init() {
4043
clients.InitCubeClientSetWithOpts(nil)
4144
}
@@ -55,11 +58,7 @@ func main() {
5558
return
5659
}
5760

58-
// provide api for livenessProbe
59-
http.HandleFunc("/healthz", func(response http.ResponseWriter, request *http.Request) {
60-
logger.Debug("Health check")
61-
response.WriteHeader(http.StatusOK)
62-
})
61+
runAPIServer()
6362

6463
rl, err := resourcelock.New(resourcelock.ConfigMapsResourceLock,
6564
handler.LeaderElectionNamespace,
@@ -79,9 +78,10 @@ func main() {
7978
RetryPeriod: 2 * time.Second,
8079
Callbacks: leaderelection.LeaderCallbacks{
8180
OnStartedLeading: func(ctx context.Context) {
82-
run()
81+
leader = true
8382
},
8483
OnStoppedLeading: func() {
84+
leader = false
8585
glog.Infoln("leader election lost")
8686
},
8787
},
@@ -90,20 +90,29 @@ func main() {
9090
glog.Errorf("leader election fail, be member")
9191
}
9292
le.Run(context.Background())
93-
9493
}
9594

96-
func run() {
95+
func runAPIServer() {
96+
// provide api for livenessProbe
97+
http.HandleFunc("/healthz", func(response http.ResponseWriter, request *http.Request) {
98+
logger.Debug("Health check")
99+
response.WriteHeader(http.StatusOK)
100+
})
97101
http.Handle("/api/", handler.CreateHTTPAPIHandler())
98102
http.Handle("/api/sockjs/", handler.CreateAttachHandler("/api/sockjs"))
99103
// provide api for readinessProbe,avoid service flow into in-leader pod
100104
http.HandleFunc("/leader", func(response http.ResponseWriter, request *http.Request) {
101-
logger.Debug("This is leader")
102-
response.WriteHeader(http.StatusOK)
105+
statusCode := http.StatusOK
106+
if !leader {
107+
statusCode = http.StatusBadRequest
108+
}
109+
response.WriteHeader(statusCode)
103110
})
104111

105-
err := http.ListenAndServe(fmt.Sprintf(":%d", *handler.ServerPort), nil)
106-
if err != nil {
107-
logger.Critical("ListenAndServe failed,error msg: %s", err.Error())
108-
}
112+
go func() {
113+
err := http.ListenAndServe(fmt.Sprintf(":%d", *handler.ServerPort), nil)
114+
if err != nil {
115+
logger.Critical("ListenAndServe failed,error msg: %s", err.Error())
116+
}
117+
}()
109118
}

0 commit comments

Comments
 (0)