/
watcher.go
205 lines (172 loc) · 5.7 KB
/
watcher.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
// Copyright 2016-2022, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package watcher
import (
"context"
"fmt"
"math/rand"
"time"
logger "github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/dynamic"
)
// Predicate is a function that returns true when we can stop watching in `WatchUntil`.
type Predicate func(*unstructured.Unstructured) bool
// Retry is a function that returns nil when an operation that can fail some number of times has
// succeeded, and `RetryUntil` can stop retrying.
type Retry func(*unstructured.Unstructured, error) error
type pollFunc func() (*unstructured.Unstructured, error)
// ObjectWatcher will block and watch or retry some operation on an object until a timeout, or some
// condition is true.
type ObjectWatcher struct {
ctx context.Context
objName string
pollFunc pollFunc
}
// ForObject creates an `ObjectWatcher` to watch some object.
func ForObject(
ctx context.Context, clientForResource dynamic.ResourceInterface, name string,
) *ObjectWatcher {
return &ObjectWatcher{
ctx: ctx,
objName: name,
pollFunc: func() (*unstructured.Unstructured, error) {
obj, err := clientForResource.Get(ctx, name, metav1.GetOptions{})
if err != nil {
// Log the error.
logger.V(3).Infof("Received error polling for %q: %#v", name, err)
return nil, err
}
return obj, nil
},
}
}
// WatchUntil will block and watch an object until some predicate is true, or a timeout occurs.
func (ow *ObjectWatcher) WatchUntil(pred Predicate, timeout time.Duration) error {
untilTrue := func(obj *unstructured.Unstructured, err error) (bool, error) {
if err != nil {
// Error. Return error and true to stop watching.
return true, err
} else if pred(obj) {
// Success. Return true to stop watching.
return true, nil
}
// No error and predicate does not say to stop.
return false, nil
}
return ow.watch(untilTrue, timeout)
}
// RetryUntil will block and retry getting an object until the `Retry` operation succeeds (i.e.,
// does not return an error).
func (ow *ObjectWatcher) RetryUntil(r Retry, timeout time.Duration) error {
untilNoError := func(obj *unstructured.Unstructured, err error) (bool, error) {
if retryErr := r(obj, err); retryErr != nil {
rerr, isRetryable := retryErr.(*RetryError)
// Non-retryable error; return error and true to stop watching.
if !isRetryable {
return true, retryErr
}
logger.V(3).Infof("Retrying operation with message: %s", rerr.Error())
// Retryable error. Return false to continue watching.
return false, nil
}
// No error occurred. Return true to stop watching.
return true, nil
}
return ow.watch(untilNoError, timeout)
}
func (ow *ObjectWatcher) watch(
until func(*unstructured.Unstructured, error) (bool, error), timeout time.Duration,
) error {
timeoutCh := make(chan struct{})
go func() {
time.Sleep(timeout)
timeoutCh <- struct{}{}
}()
var obj *unstructured.Unstructured
results := make(chan result)
poll := func() {
var err error
obj, err = ow.pollFunc()
results <- result{Obj: obj, Err: err}
}
wait := 500 * time.Millisecond
for {
// Race between timeout and getting one Kubernetes object from the polling function.
go poll()
select {
case <-timeoutCh:
return timeoutErr(ow.objName, obj)
case <-ow.ctx.Done():
return cancellationErr(ow.objName, obj)
case res := <-results:
if stop, err := until(res.Obj, res.Err); err != nil {
return err
} else if stop {
return nil
}
// nolint:gosec
time.Sleep(wait + time.Duration(rand.Intn(int(float64(wait)*0.2))))
}
}
}
// --------------------------------------------------------------------------
// Helper utilities.
// --------------------------------------------------------------------------
type result struct {
Err error
Obj *unstructured.Unstructured
}
// --------------------------------------------------------------------------
// Errors.
//
// A collection of errors used to implement retry and watch logic.
// --------------------------------------------------------------------------
func timeoutErr(name string, obj *unstructured.Unstructured) error {
return &watchError{object: obj,
message: fmt.Sprintf("Timeout occurred polling for '%s'", name)}
}
func cancellationErr(name string, obj *unstructured.Unstructured) error {
return &watchError{object: obj,
message: fmt.Sprintf("Resource operation was cancelled for '%s'", name)}
}
type watchError struct {
object *unstructured.Unstructured
message string
}
var _ error = (*watchError)(nil)
func (we *watchError) Error() string {
return we.message
}
func (we *watchError) Object() *unstructured.Unstructured {
return we.object
}
// RetryError is the required return type of RetryFunc. It forces client code
// to choose whether or not a given error is retryable.
type RetryError struct {
Err error
Retryable bool
}
func (re RetryError) Error() string {
return re.Err.Error()
}
// RetryableError is a helper to create a RetryError that's retryable from a
// given error.
func RetryableError(err error) *RetryError {
if err == nil {
return nil
}
return &RetryError{Err: err, Retryable: true}
}