|
|
|
@ -19,6 +19,8 @@ import (
|
|
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
|
|
|
|
"k8s.io/client-go/dynamic"
|
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
|
toolswatch "k8s.io/client-go/tools/watch"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// KubernetesClient provides both typed and untyped interfaces to the
|
|
|
|
@ -101,17 +103,21 @@ func (w *Watcher) Watch(ctx context.Context) error {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts := metav1.ListOptions{Watch: true, FieldSelector: "metadata.name=" + w.params.Name}
|
|
|
|
|
watcher, err := w.client.Untyped.Resource(resourceID).Namespace(ns).Watch(ctx, opts)
|
|
|
|
|
watchFunc := func(_ metav1.ListOptions) (watch.Interface, error) {
|
|
|
|
|
return w.client.Untyped.
|
|
|
|
|
Resource(resourceID).
|
|
|
|
|
Namespace(ns).
|
|
|
|
|
Watch(ctx, metav1.ListOptions{Watch: true, FieldSelector: "metadata.name=" + w.params.Name})
|
|
|
|
|
}
|
|
|
|
|
retryWatcher, err := toolswatch.NewRetryWatcher("1", &cache.ListWatch{WatchFunc: watchFunc})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer watcher.Stop()
|
|
|
|
|
|
|
|
|
|
ticker := time.NewTicker(time.Second)
|
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
resultChan := watcher.ResultChan()
|
|
|
|
|
resultChan := retryWatcher.ResultChan()
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case evt, ok := <-resultChan:
|
|
|
|
@ -135,7 +141,7 @@ func (w *Watcher) Watch(ctx context.Context) error {
|
|
|
|
|
case watch.Deleted:
|
|
|
|
|
w.unwatchResource()
|
|
|
|
|
case watch.Error:
|
|
|
|
|
err := evt.Object.(error)
|
|
|
|
|
err := evt.Object.(*metav1.Status)
|
|
|
|
|
// TODO: remove panic and handle error
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
@ -167,7 +173,7 @@ func (w *Watcher) watchResource(ctx context.Context, resourceUID types.UID, podS
|
|
|
|
|
|
|
|
|
|
w.unwatchResource()
|
|
|
|
|
|
|
|
|
|
w.logger.Println("[Watcher] add podWatcher")
|
|
|
|
|
w.logger.Printf("[Watcher] add podWatcher, resourceUID = %s", resourceUID)
|
|
|
|
|
|
|
|
|
|
w.resourceUID = resourceUID
|
|
|
|
|
w.podSelector = podSelector
|
|
|
|
@ -189,7 +195,7 @@ func (w *Watcher) watchResource(ctx context.Context, resourceUID types.UID, podS
|
|
|
|
|
|
|
|
|
|
func (w *Watcher) unwatchResource() {
|
|
|
|
|
if w.podWatcher != nil {
|
|
|
|
|
w.logger.Println("[Watcher] remove podWatcher")
|
|
|
|
|
w.logger.Printf("[Watcher] remove podWatcher, resourceUID = %s", w.resourceUID)
|
|
|
|
|
w.podWatcher.Close()
|
|
|
|
|
w.podWatcher = nil
|
|
|
|
|
}
|
|
|
|
|