This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait.go
305 lines (279 loc) · 9.14 KB
/
wait.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package k8s
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"time"
"go.innotegrity.dev/zerolog"
"go.innotegrity.dev/zerolog/log"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// ConditionalWaiter stores information for executing a wait loop to ensure a specific condition is met for one
// or more resources.
type ConditionalWaiter struct {
resource *dynamicResource
waitError error
waitGroup *sync.WaitGroup
selectors []string
timeout uint
conditionName string
conditionValue string
}
// NewConditionalWaiter creates and initializes a new ConditionalWaiter object.
func NewConditionalWaiter(resource *dynamicResource, waitCondition WaitCondition,
waitGroup *sync.WaitGroup) *ConditionalWaiter {
w := &ConditionalWaiter{
resource: resource,
waitGroup: waitGroup,
timeout: waitCondition.Timeout,
selectors: waitCondition.Selectors,
conditionName: waitCondition.Condition,
conditionValue: "true",
}
if equalsIndex := strings.Index(w.conditionName, "="); equalsIndex != -1 {
w.conditionName = waitCondition.Condition[0:equalsIndex]
w.conditionValue = waitCondition.Condition[equalsIndex+1:]
}
return w
}
// Error returns the error associated with the object.
func (w *ConditionalWaiter) Error() error {
return w.waitError
}
// Run executes a loop waiting for the given condition to be true for all matching resources.
//
// Any errors that occur while the waiter is running can be retrieved by calling the waiter's Error()
// function.
//
// The following errors are possible with this function:
// ErrResourceWaitFailure
func (w *ConditionalWaiter) Run(ctx context.Context) {
logger := log.Logger
if l := zerolog.Ctx(ctx); l != nil {
logger = *l
}
logger = logger.With().
Str("kind", w.resource.gvk.Kind).
Str("group", w.resource.gvk.Group).
Str("version", w.resource.gvk.Version).Logger()
defer w.waitGroup.Done()
// create a nice representation of the resource for messages
var kind string
if w.resource.gvk.Group != "" {
kind = strings.ToLower(fmt.Sprintf("%s.%s", w.resource.gvk.Kind, w.resource.gvk.Group))
} else {
kind = strings.ToLower(fmt.Sprintf("%ss", w.resource.gvk.Kind))
}
// wait for objects
objName := w.resource.obj.GetName()
var subWaitGroup sync.WaitGroup
if objName == "" {
// lookup the resource based on selectors
selectors := strings.Join(w.selectors, ",")
var obj *unstructured.UnstructuredList
var err error
rounds := 0
for { // wait up to 10 seconds for a resource to be created
obj, err = w.resource.dr.List(context.Background(), metav1.ListOptions{
LabelSelector: selectors,
})
if err != nil {
e := &ErrResourceWaitFailure{
Kind: kind,
Selectors: selectors,
Err: err,
}
logger.Error().Err(e.Err).Str("label_selectors", selectors).Msg(e.Error())
w.waitError = e
return
}
if len(obj.Items) > 0 {
break
}
rounds++
if rounds <= 5 {
logger.Info().Msg("no matching resources found yet; retrying in 2 seconds...")
time.Sleep(time.Millisecond * 2000)
} else {
e := &ErrResourceWaitFailure{
Kind: kind,
Selectors: selectors,
Err: errors.New("maximum wait time exceeded for resource creation"),
}
logger.Error().Err(e.Err).Str("label_selectors", selectors).Msg(e.Error())
w.waitError = e
return
}
}
// add all matching resources to the list and wait for them
for _, item := range obj.Items {
subWaitGroup.Add(1)
go w.waitForObject(ctx, item.GetName(), &subWaitGroup)
}
} else {
subWaitGroup.Add(1)
go w.waitForObject(ctx, objName, &subWaitGroup)
}
subWaitGroup.Wait()
}
// isConditionMet determines whether or not the condition has been met for the given object.
func (w *ConditionalWaiter) isConditionMet(obj *unstructured.Unstructured) (bool, error) {
conditions, found, err := unstructured.NestedSlice(obj.Object, "status", "conditions")
if err != nil {
return false, err
}
if !found {
return false, nil
}
for _, conditionUncast := range conditions {
condition := conditionUncast.(map[string]interface{})
name, found, err := unstructured.NestedString(condition, "type")
if !found || err != nil || !strings.EqualFold(name, w.conditionName) {
continue
}
status, found, err := unstructured.NestedString(condition, "status")
if !found || err != nil {
continue
}
generation, found, _ := unstructured.NestedInt64(obj.Object, "metadata", "generation")
if found {
observedGeneration, found := getObservedGeneration(obj, condition)
if found && observedGeneration < generation {
return false, nil
}
}
return strings.EqualFold(status, w.conditionValue), nil
}
return false, nil
}
// waitForObject waits for the condition to be true for the resource with the given name.
//
// Any errors that occur while the waiter is running can be retrieved by calling the waiter's Error()
// function.
//
// The following errors are possible with this function:
// ErrResourceWaitFailure
func (w *ConditionalWaiter) waitForObject(ctx context.Context, name string, wg *sync.WaitGroup) {
logger := log.Logger
if l := zerolog.Ctx(ctx); l != nil {
logger = *l
}
logger = logger.With().
Str("kind", w.resource.gvk.Kind).
Str("group", w.resource.gvk.Group).
Str("version", w.resource.gvk.Version).
Str("name", name).
Logger()
defer wg.Done()
kind := gvkToString(w.resource.gvk)
logger.Info().Msgf("waiting for %s resource: %s", kind, name)
expires := time.Now().Add(time.Second * time.Duration(w.timeout))
for {
// lookup the object
obj, err := w.resource.dr.Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
e := &ErrResourceWaitFailure{Kind: kind, Name: name, Err: err}
logger.Error().Err(e.Err).Msg(e.Error())
w.waitError = e
return
}
// is the condition met
isMet, err := w.isConditionMet(obj)
if err != nil {
e := &ErrResourceWaitFailure{Kind: kind, Name: name, Err: err}
logger.Error().Err(e.Err).Msg(e.Error())
w.waitError = e
return
}
if isMet {
logger.Info().Msgf("finished waiting for %s resource: %s", kind, name)
break
}
// has the wait timed out
if time.Now().After(expires) {
e := &ErrResourceWaitFailure{Kind: kind, Name: name,
Err: errors.New("maximum wait time exceeded for resource condition"),
}
logger.Error().Err(e.Err).Msg(e.Error())
w.waitError = e
return
}
// wait for 1 second and try again
logger.Debug().Msgf("still waiting for %s resource: %s", kind, name)
time.Sleep(time.Second)
}
}
// getObservedGeneration returns the observedGeneration from the object.
func getObservedGeneration(obj *unstructured.Unstructured, condition map[string]interface{}) (int64, bool) {
conditionObservedGeneration, found, _ := unstructured.NestedInt64(condition, "observedGeneration")
if found {
return conditionObservedGeneration, true
}
statusObservedGeneration, found, _ := unstructured.NestedInt64(obj.Object, "status", "observedGeneration")
return statusObservedGeneration, found
}
// gvkToStirng converts a group/version/kind to a human-friendly string.
func gvkToString(gvk *schema.GroupVersionKind) string {
if gvk.Group != "" {
return strings.ToLower(fmt.Sprintf("%s.%s", gvk.Kind, gvk.Group))
}
return strings.ToLower(gvk.Kind)
}
// WaitCondition holds information on what resources we must wait on before continuing.
type WaitCondition struct {
Condition string `yaml:"condition"`
RawResource map[string]interface{} `yaml:"resource"`
Selectors []string `yaml:"selectors"`
Timeout uint `yaml:"timeout"`
ResourceDefinition []byte
}
type marshalledWaitCondition WaitCondition
// UnmarshalYAML handles parsing YAML into an object and setting sensible defaults.
//
// The following errors are returned by this function:
// ErrWaitConditionInvalid
func (c *WaitCondition) UnmarshalYAML(unmarshal func(interface{}) error) error {
// parse the data
var condition marshalledWaitCondition
if err := unmarshal(&condition); err != nil {
e := &ErrWaitConditionInvalid{Err: fmt.Errorf("failed to parse configuration: %s", err.Error())}
return e
}
// convert the raw resource data back into bytes that we can decode later
res, err := yaml.Marshal(condition.RawResource)
if err != nil {
e := &ErrWaitConditionInvalid{Err: fmt.Errorf("failed to encode resource data: %s", err.Error())}
return e
}
condition.ResourceDefinition = res
// make sure there is a non-empty condition
if condition.Condition == "" {
return &ErrWaitConditionInvalid{Err: errors.New("condition cannot be empty")}
}
// validate condition values
cond := strings.ToLower(condition.Condition)
validConditions := map[string]int{
"available": 1, // Deployment
"available=true": 1,
"available=false": 1,
"ready": 1, // Pod
"ready=true": 1,
"ready=false": 1,
"complete": 1, // Job
"complete=true": 1,
"complete=false": 1,
}
if _, ok := validConditions[cond]; !ok {
return &ErrWaitConditionInvalid{
Err: fmt.Errorf("'%s' is an unsupported condition", condition.Condition),
}
}
// copy values
*c = WaitCondition(condition)
return nil
}