-
Notifications
You must be signed in to change notification settings - Fork 13
/
watch_loop.go
46 lines (43 loc) · 1.11 KB
/
watch_loop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"log"
"strings"
"github.com/arschles/2017-KubeCon-EU/tpr"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/watch"
)
func runWatchLoop(
cl *kubernetes.Clientset,
openWatcher func() (watch.Interface, error),
) error {
// loop forever. inside of each loop iteration, open a new watcher, get its watch chan, and
// listen on that chan
for {
watcher, err := openWatcher()
if err != nil {
return err
}
defer watcher.Stop()
watchCh := watcher.ResultChan()
// receive on watchCh until it's closed
for evt := range watchCh {
log.Printf("received event %#v", evt)
backup, ok := evt.Object.(*tpr.Backup)
if !ok {
log.Printf("event was not a *Backup, skipping")
continue
}
log.Printf("backing up all %s resources", backup.ResourceType)
switch strings.ToLower(backup.ResourceType) {
case "pod", "pods":
if err := backupPods(cl); err != nil {
log.Printf("error backing up all pods (%s)", err)
}
default:
log.Printf("%s resources are not supported", backup.ResourceType)
}
}
log.Printf("Watch channel closed, reopening...")
}
return nil
}