-
Notifications
You must be signed in to change notification settings - Fork 683
/
waiter.go
165 lines (144 loc) · 3.59 KB
/
waiter.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package kubeapply
import (
"fmt"
"log"
"time"
"github.com/datawire/ambassador/pkg/k8s"
)
// Waiter takes some YAML and waits for all of the resources described
// in it to be ready.
type Waiter struct {
watcher *k8s.Watcher
kinds map[k8s.ResourceType]map[string]struct{}
}
// NewWaiter constructs a Waiter object based on the supplied Watcher.
func NewWaiter(watcher *k8s.Watcher) (w *Waiter, err error) {
if watcher == nil {
cli, err := k8s.NewClient(nil)
if err != nil {
return nil, err
}
watcher = cli.Watcher()
}
return &Waiter{
watcher: watcher,
kinds: make(map[k8s.ResourceType]map[string]struct{}),
}, nil
}
func (w *Waiter) add(resource k8s.Resource) error {
resourceType, err := w.watcher.Client.ResolveResourceType(resource.QKind())
if err != nil {
return err
}
resourceName := resource.Name()
if resourceType.Namespaced {
namespace := resource.Namespace()
if namespace == "" {
namespace = w.watcher.Client.Namespace
}
resourceName += "." + namespace
}
if _, ok := w.kinds[resourceType]; !ok {
w.kinds[resourceType] = make(map[string]struct{})
}
w.kinds[resourceType][resourceName] = struct{}{}
return nil
}
// Scan calls LoadResources(path), and add all resources loaded to the
// Waiter.
func (w *Waiter) Scan(path string) (err error) {
resources, err := LoadResources(path)
for _, res := range resources {
err = w.add(res)
if err != nil {
return
}
}
return
}
func (w *Waiter) remove(kind k8s.ResourceType, name string) {
delete(w.kinds[kind], name)
}
func (w *Waiter) isEmpty() bool {
for _, names := range w.kinds {
if len(names) > 0 {
return false
}
}
return true
}
// Wait spews a bunch of crap on stdout, and waits for all of the
// Scan()ed resources to be ready. If they all become ready before
// deadline, then it returns true. If they don't become ready by
// then, then it bails early and returns false.
func (w *Waiter) Wait(deadline time.Time) bool {
start := time.Now()
printed := make(map[string]bool)
err := w.watcher.WatchQuery(k8s.Query{Kind: "events", Namespace: k8s.NamespaceAll}, func(watcher *k8s.Watcher) {
for _, r := range watcher.List("events") {
if lastStr, ok := r["lastTimestamp"].(string); ok {
last, err := time.Parse("2006-01-02T15:04:05Z", lastStr)
if err != nil {
log.Println(err)
continue
}
if last.Before(start) {
continue
}
}
if !printed[r.QName()] {
var name string
if obj, ok := r["involvedObject"].(map[string]interface{}); ok {
res := k8s.Resource(obj)
name = fmt.Sprintf("%s/%s", res.QKind(), res.QName())
} else {
name = r.QName()
}
fmt.Printf("event: %s %s\n", name, r["message"])
printed[r.QName()] = true
}
}
})
if err != nil {
panic(err)
}
listener := func(watcher *k8s.Watcher) {
for kind, names := range w.kinds {
for name := range names {
r := watcher.Get(kind.String(), name)
if Ready(r) {
if ReadyImplemented(r) {
fmt.Printf("ready: %s/%s\n", r.QKind(), r.QName())
} else {
fmt.Printf("ready: %s/%s (UNIMPLEMENTED)\n",
r.QKind(), r.QName())
}
w.remove(kind, name)
}
}
}
if w.isEmpty() {
watcher.Stop()
}
}
for k := range w.kinds {
err := w.watcher.WatchQuery(k8s.Query{Kind: k.String(), Namespace: k8s.NamespaceAll}, listener)
if err != nil {
panic(err)
}
}
w.watcher.Start()
go func() {
time.Sleep(time.Until(deadline))
w.watcher.Stop()
}()
w.watcher.Wait()
result := true
for kind, names := range w.kinds {
for name := range names {
fmt.Printf("not ready: %s/%s\n", kind, name)
result = false
}
}
return result
}