-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch.go
50 lines (44 loc) · 1.34 KB
/
watch.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
47
48
49
50
package client
import (
"context"
"github.com/wwitzel3/k8s-resource-client/pkg/cache"
"github.com/wwitzel3/k8s-resource-client/pkg/resource"
"go.uber.org/zap"
)
// WatchResource creates a watch for the Resource in the provided namespaces.
// To watch across all namespaces you can pass in metav1.NamespaceAll.
func WatchResource(ctx context.Context, client *Client, res resource.Resource, queueEvents bool, namespaces []string) ([]cache.ResourceLister, error) {
if hasNamespaceAll(namespaces) {
w, err := client.watcher.Watch(ctx, "", res, queueEvents)
if err != nil {
return nil, err
}
return []cache.ResourceLister{w}, nil
}
watchDetails := make([]cache.ResourceLister, len(namespaces))
for i, ns := range namespaces {
client.Logger.Info("creating watch",
zap.String("resource", res.Key()),
zap.String("namespace", ns),
)
w, err := client.watcher.Watch(ctx, ns, res, queueEvents)
if err != nil {
return nil, err
}
watchDetails[i] = w
}
return watchDetails, nil
}
func WatchAllResources(ctx context.Context, client *Client, queueEvents bool, namespaces []string) {
for _, res := range cache.Resources.Get("namespace") {
WatchResource(ctx, client, res, queueEvents, namespaces)
}
}
func hasNamespaceAll(namespaces []string) bool {
for _, ns := range namespaces {
if ns == "" {
return true
}
}
return false
}