Pass last resource version to restart watch
This commit is contained in:
+25
-14
@@ -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"
|
||||||
@@ -50,10 +51,11 @@ type (
|
|||||||
|
|
||||||
watchSubscription struct {
|
watchSubscription struct {
|
||||||
fission.Watch
|
fission.Watch
|
||||||
kubeWatch watch.Interface
|
kubeWatch watch.Interface
|
||||||
stopped *int32
|
lastResourceVersion string
|
||||||
kubernetesClient *kubernetes.Clientset
|
stopped *int32
|
||||||
publisher Publisher
|
kubernetesClient *kubernetes.Clientset
|
||||||
|
publisher Publisher
|
||||||
}
|
}
|
||||||
|
|
||||||
kubeWatcherRequest struct {
|
kubeWatcherRequest struct {
|
||||||
@@ -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) {
|
||||||
@@ -198,25 +205,27 @@ func (kw *KubeWatcher) removeWatch(w *fission.Watch) error {
|
|||||||
func MakeWatchSubscription(w *fission.Watch, kubeClient *kubernetes.Clientset, publisher Publisher) (*watchSubscription, error) {
|
func MakeWatchSubscription(w *fission.Watch, kubeClient *kubernetes.Clientset, publisher Publisher) (*watchSubscription, error) {
|
||||||
var stopped int32 = 0
|
var stopped int32 = 0
|
||||||
ws := &watchSubscription{
|
ws := &watchSubscription{
|
||||||
Watch: *w,
|
Watch: *w,
|
||||||
kubeWatch: nil,
|
kubeWatch: nil,
|
||||||
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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user