Pass last resource version to restart watch

This commit is contained in:
Soam Vasani
2017-03-28 10:01:11 +02:00
parent a71756762b
commit c9f63e8173
+16 -5
View File
@@ -25,6 +25,7 @@ import (
"log" "log"
"strings" "strings"
"sync/atomic" "sync/atomic"
"time"
"k8s.io/client-go/1.5/kubernetes" "k8s.io/client-go/1.5/kubernetes"
"k8s.io/client-go/1.5/pkg/api" "k8s.io/client-go/1.5/pkg/api"
@@ -51,6 +52,7 @@ type (
watchSubscription struct { watchSubscription struct {
fission.Watch fission.Watch
kubeWatch watch.Interface kubeWatch watch.Interface
lastResourceVersion string
stopped *int32 stopped *int32
kubernetesClient *kubernetes.Clientset kubernetesClient *kubernetes.Clientset
publisher Publisher publisher Publisher
@@ -137,11 +139,16 @@ func printKubernetesObject(obj runtime.Object, w io.Writer) error {
return err return err
} }
func createKubernetesWatch(kubeClient *kubernetes.Clientset, w *fission.Watch) (watch.Interface, error) { func createKubernetesWatch(kubeClient *kubernetes.Clientset, w *fission.Watch, resourceVersion string) (watch.Interface, error) {
var wi watch.Interface var wi watch.Interface
var err error var err error
var watchTimeoutSec int64 = 300
listOptions := api.ListOptions{} // TODO populate labelselector and fieldselector // TODO populate labelselector and fieldselector
listOptions := api.ListOptions{
ResourceVersion: resourceVersion,
TimeoutSeconds: &watchTimeoutSec,
}
// TODO handle the full list of types // TODO handle the full list of types
switch strings.ToUpper(w.ObjType) { switch strings.ToUpper(w.ObjType) {
@@ -203,20 +210,22 @@ func MakeWatchSubscription(w *fission.Watch, kubeClient *kubernetes.Clientset, p
stopped: &stopped, stopped: &stopped,
kubernetesClient: kubeClient, kubernetesClient: kubeClient,
publisher: publisher, publisher: publisher,
lastResourceVersion: "",
} }
err := ws.restartWatch() err := ws.restartWatch()
if err != nil { if err != nil {
return err return nil, err
} }
go ws.eventDispatchLoop() go ws.eventDispatchLoop()
return ws, nil
} }
func (ws *watchSubscription) restartWatch() error { func (ws *watchSubscription) restartWatch() error {
retries := 60 retries := 60
for { for {
wi, err := createKubernetesWatch(kubeClient, w) wi, err := createKubernetesWatch(ws.kubernetesClient, &ws.Watch, ws.lastResourceVersion)
if err != nil { if err != nil {
retries-- retries--
if retries > 0 { if retries > 0 {
@@ -227,7 +236,7 @@ func (ws *watchSubscription) restartWatch() error {
} }
} }
ws.kubeWatch = wi ws.kubeWatch = wi
return return nil
} }
} }
@@ -240,6 +249,8 @@ func (ws *watchSubscription) eventDispatchLoop() {
log.Println("Watch stopped", ws.Watch.Metadata.Name) log.Println("Watch stopped", ws.Watch.Metadata.Name)
break break
} }
// TODO: update ws.lastResourceVersion
ws.publisher.Publish(ev, ws.Watch.Target) ws.publisher.Publish(ev, ws.Watch.Target)
} }
if atomic.LoadInt32(ws.stopped) == 0 { if atomic.LoadInt32(ws.stopped) == 0 {