Add an extra loop when the pod watcher is closed unexpectedly. (#226)

This commit is contained in:
Robbie Zhang
2018-06-11 14:09:16 -07:00
committed by GitHub
parent 513cebe7b7
commit 22a4114890

View File

@@ -192,20 +192,24 @@ func (s *Server) registerNode() error {
// Run starts the server, registers it with Kubernetes and begins watching/reconciling the cluster. // Run starts the server, registers it with Kubernetes and begins watching/reconciling the cluster.
// Run will block until Stop is called or a SIGINT or SIGTERM signal is received. // Run will block until Stop is called or a SIGINT or SIGTERM signal is received.
func (s *Server) Run() error { func (s *Server) Run() error {
shouldStop := false
sig := make(chan os.Signal, 1) sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go func() { go func() {
<-sig <-sig
shouldStop = true
s.Stop() s.Stop()
}() }()
for {
opts := metav1.ListOptions{ opts := metav1.ListOptions{
FieldSelector: fields.OneTermEqualSelector("spec.nodeName", s.nodeName).String(), FieldSelector: fields.OneTermEqualSelector("spec.nodeName", s.nodeName).String(),
} }
pods, err := s.k8sClient.CoreV1().Pods(s.namespace).List(opts) pods, err := s.k8sClient.CoreV1().Pods(s.namespace).List(opts)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Failed to list pods", err)
} }
s.resourceManager.SetPods(pods) s.resourceManager.SetPods(pods)
s.reconcile() s.reconcile()
@@ -213,16 +217,24 @@ func (s *Server) Run() error {
opts.ResourceVersion = pods.ResourceVersion opts.ResourceVersion = pods.ResourceVersion
s.podWatcher, err = s.k8sClient.CoreV1().Pods(s.namespace).Watch(opts) s.podWatcher, err = s.k8sClient.CoreV1().Pods(s.namespace).Watch(opts)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("Failed to watch pods", err)
} }
loop:
for { for {
select { select {
case ev, ok := <-s.podWatcher.ResultChan(): case ev, ok := <-s.podWatcher.ResultChan():
if !ok { if !ok {
if shouldStop {
log.Println("Pod watcher is stopped.")
return nil return nil
} }
log.Println("Pod watcher connection is closed unexpectedly.")
break loop
}
log.Println("Pod watcher event is received:", ev.Type)
switch ev.Type { switch ev.Type {
case watch.Added: case watch.Added:
s.resourceManager.AddPod(ev.Object.(*corev1.Pod)) s.resourceManager.AddPod(ev.Object.(*corev1.Pod))
@@ -234,6 +246,9 @@ func (s *Server) Run() error {
s.reconcile() s.reconcile()
} }
} }
time.Sleep(5 * time.Second)
}
} }
// Stop shutsdown the server. // Stop shutsdown the server.