forked from kubernetes-retired/service-catalog
/
util.go
411 lines (349 loc) · 14.5 KB
/
util.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
Copyright 2017 The Kubernetes Authors.
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 util
import (
"context"
"fmt"
"testing"
"time"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/authentication/user"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
v1beta1servicecatalog "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset/typed/servicecatalog/v1beta1"
scfeatures "github.com/kubernetes-incubator/service-catalog/pkg/features"
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
utilfeature "k8s.io/apiserver/pkg/util/feature"
)
// WaitForBrokerCondition waits for the status of the named broker to contain
// a condition whose type and status matches the supplied one.
func WaitForBrokerCondition(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string, condition v1beta1.ServiceBrokerCondition) error {
// GetCatalog default timeout time is 60 seconds, so the wait here must be at least that (previously set to 30 seconds)
return wait.PollImmediate(500*time.Millisecond, 3*time.Minute,
func() (bool, error) {
glog.V(5).Infof("Waiting for broker %v condition %#v", name, condition)
broker, err := client.ClusterServiceBrokers().Get(name, metav1.GetOptions{})
if nil != err {
return false, fmt.Errorf("error getting Broker %v: %v", name, err)
}
if len(broker.Status.Conditions) == 0 {
return false, nil
}
glog.V(5).Infof("Conditions = %#v", broker.Status.Conditions)
for _, cond := range broker.Status.Conditions {
if condition.Type == cond.Type && condition.Status == cond.Status {
if condition.Reason == "" || condition.Reason == cond.Reason {
return true, nil
}
}
}
return false, nil
},
)
}
// WaitForBrokerToNotExist waits for the Broker with the given name to no
// longer exist.
func WaitForBrokerToNotExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for broker %v to not exist", name)
_, err := client.ClusterServiceBrokers().Get(name, metav1.GetOptions{})
if nil == err {
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
},
)
}
// WaitForClusterServiceClassToExist waits for the ClusterServiceClass with the given name
// to exist.
func WaitForClusterServiceClassToExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for serviceClass %v to exist", name)
_, err := client.ClusterServiceClasses().Get(name, metav1.GetOptions{})
if nil == err {
return true, nil
}
return false, nil
},
)
}
// WaitForClusterServicePlanToExist waits for the ClusterServicePlan
// with the given name to exist.
func WaitForClusterServicePlanToExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for ClusterServicePlan %v to exist", name)
_, err := client.ClusterServicePlans().Get(name, metav1.GetOptions{})
if nil == err {
return true, nil
}
return false, nil
},
)
}
// WaitForClusterServicePlanToNotExist waits for the ClusterServicePlan with the given name
// to not exist.
func WaitForClusterServicePlanToNotExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for ClusterServicePlan %q to not exist", name)
_, err := client.ClusterServicePlans().Get(name, metav1.GetOptions{})
if nil == err {
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
},
)
}
// WaitForClusterServiceClassToNotExist waits for the ClusterServiceClass with the given
// name to no longer exist.
func WaitForClusterServiceClassToNotExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for serviceClass %v to not exist", name)
_, err := client.ClusterServiceClasses().Get(name, metav1.GetOptions{})
if nil == err {
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
},
)
}
// WaitForInstanceCondition waits for the status of the named instance to
// contain a condition whose type and status matches the supplied one.
func WaitForInstanceCondition(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string, condition v1beta1.ServiceInstanceCondition) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for instance %v/%v condition %#v", namespace, name, condition)
instance, err := client.ServiceInstances(namespace).Get(name, metav1.GetOptions{})
if nil != err {
return false, fmt.Errorf("error getting Instance %v/%v: %v", namespace, name, err)
}
if len(instance.Status.Conditions) == 0 {
return false, nil
}
glog.V(5).Infof("Conditions = %#v", instance.Status.Conditions)
for _, cond := range instance.Status.Conditions {
if condition.Type == cond.Type && condition.Status == cond.Status {
if condition.Reason == "" || condition.Reason == cond.Reason {
return true, nil
}
}
}
return false, nil
},
)
}
// WaitForInstanceToNotExist waits for the Instance with the given name to no
// longer exist.
func WaitForInstanceToNotExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for instance %v/%v to not exist", namespace, name)
_, err := client.ServiceInstances(namespace).Get(name, metav1.GetOptions{})
if nil == err {
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
},
)
}
// WaitForInstanceProcessedGeneration waits for the status of the named instance to
// have the specified reconciled generation.
func WaitForInstanceProcessedGeneration(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string, processedGeneration int64) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for instance %v/%v to have processed generation of %v", namespace, name, processedGeneration)
instance, err := client.ServiceInstances(namespace).Get(name, metav1.GetOptions{})
if nil != err {
return false, fmt.Errorf("error getting Instance %v/%v: %v", namespace, name, err)
}
if instance.Status.ObservedGeneration >= processedGeneration &&
(isServiceInstanceReady(instance) || isServiceInstanceFailed(instance)) &&
!instance.Status.OrphanMitigationInProgress {
return true, nil
}
return false, nil
},
)
}
// isServiceInstanceConditionTrue returns whether the given instance has a given condition
// with status true.
func isServiceInstanceConditionTrue(instance *v1beta1.ServiceInstance, conditionType v1beta1.ServiceInstanceConditionType) bool {
for _, cond := range instance.Status.Conditions {
if cond.Type == conditionType {
return cond.Status == v1beta1.ConditionTrue
}
}
return false
}
// isServiceInstanceReady returns whether the given instance has a ready condition
// with status true.
func isServiceInstanceReady(instance *v1beta1.ServiceInstance) bool {
return isServiceInstanceConditionTrue(instance, v1beta1.ServiceInstanceConditionReady)
}
// isServiceInstanceFailed returns whether the instance has a failed condition with
// status true.
func isServiceInstanceFailed(instance *v1beta1.ServiceInstance) bool {
return isServiceInstanceConditionTrue(instance, v1beta1.ServiceInstanceConditionFailed)
}
// WaitForBindingCondition waits for the status of the named binding to contain
// a condition whose type and status matches the supplied one and then returns
// back the last binding condition of the same type requested during polling if found.
func WaitForBindingCondition(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string, condition v1beta1.ServiceBindingCondition) (*v1beta1.ServiceBindingCondition, error) {
var lastSeenCondition *v1beta1.ServiceBindingCondition
return lastSeenCondition, wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for binding %v/%v condition %#v", namespace, name, condition)
binding, err := client.ServiceBindings(namespace).Get(name, metav1.GetOptions{})
if nil != err {
return false, fmt.Errorf("error getting Binding %v/%v: %v", namespace, name, err)
}
if len(binding.Status.Conditions) == 0 {
return false, nil
}
glog.V(5).Infof("Conditions = %#v", binding.Status.Conditions)
for _, cond := range binding.Status.Conditions {
if condition.Type == cond.Type {
lastSeenCondition = &cond
}
if condition.Type == cond.Type && condition.Status == cond.Status {
if condition.Reason == "" || condition.Reason == cond.Reason {
return true, nil
}
}
}
return false, nil
},
)
}
// WaitForBindingToNotExist waits for the Binding with the given name to no
// longer exist.
func WaitForBindingToNotExist(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for binding %v/%v to not exist", namespace, name)
_, err := client.ServiceBindings(namespace).Get(name, metav1.GetOptions{})
if nil == err {
return false, nil
}
if errors.IsNotFound(err) {
return true, nil
}
return false, nil
},
)
}
// WaitForBindingReconciledGeneration waits for the status of the named binding to
// have the specified reconciled generation.
func WaitForBindingReconciledGeneration(client v1beta1servicecatalog.ServicecatalogV1beta1Interface, namespace, name string, reconciledGeneration int64) error {
return wait.PollImmediate(500*time.Millisecond, wait.ForeverTestTimeout,
func() (bool, error) {
glog.V(5).Infof("Waiting for binding %v/%v to have reconciled generation of %v", namespace, name, reconciledGeneration)
binding, err := client.ServiceBindings(namespace).Get(name, metav1.GetOptions{})
if nil != err {
return false, fmt.Errorf("error getting ServiceBinding %v/%v: %v", namespace, name, err)
}
if binding.Status.ReconciledGeneration == reconciledGeneration {
return true, nil
}
return false, nil
},
)
}
// AssertServiceInstanceCondition asserts that the instance's status contains
// the given condition type, status, and reason.
func AssertServiceInstanceCondition(t *testing.T, instance *v1beta1.ServiceInstance, conditionType v1beta1.ServiceInstanceConditionType, status v1beta1.ConditionStatus, reason ...string) {
foundCondition := false
for _, condition := range instance.Status.Conditions {
if condition.Type == conditionType {
foundCondition = true
if condition.Status != status {
t.Fatalf("%v condition had unexpected status; expected %v, got %v", conditionType, status, condition.Status)
}
if len(reason) == 1 && condition.Reason != reason[0] {
t.Fatalf("unexpected reason; expected %v, got %v", reason[0], condition.Reason)
}
}
}
if !foundCondition {
t.Fatalf("%v condition not found", conditionType)
}
}
// AssertServiceBindingCondition asserts that the binding's status contains
// the given condition type, status, and reason.
func AssertServiceBindingCondition(t *testing.T, binding *v1beta1.ServiceBinding, conditionType v1beta1.ServiceBindingConditionType, status v1beta1.ConditionStatus, reason ...string) {
foundCondition := false
for _, condition := range binding.Status.Conditions {
if condition.Type == conditionType {
foundCondition = true
if condition.Status != status {
t.Fatalf("%v condition had unexpected status; expected %v, got %v", conditionType, status, condition.Status)
}
if len(reason) == 1 && condition.Reason != reason[0] {
t.Fatalf("unexpected reason; expected %v, got %v", reason[0], condition.Reason)
}
}
}
if !foundCondition {
t.Fatalf("%v condition not found", conditionType)
}
}
// AssertServiceInstanceConditionFalseOrAbsent asserts that the instance's status
// either contains the given condition type with a status of False or does not
// contain the given condition.
func AssertServiceInstanceConditionFalseOrAbsent(t *testing.T, instance *v1beta1.ServiceInstance, conditionType v1beta1.ServiceInstanceConditionType) {
for _, condition := range instance.Status.Conditions {
if condition.Type == conditionType {
if e, a := v1beta1.ConditionFalse, condition.Status; e != a {
t.Fatalf("%v condition had unexpected status; expected %v, got %v", conditionType, e, a)
}
}
}
}
// EnableOriginatingIdentity enables the OriginatingIdentity feature gate. Returns
// the prior state of the gate.
func EnableOriginatingIdentity(t *testing.T, enabled bool) (previousState bool) {
prevOrigIdentEnablement := utilfeature.DefaultFeatureGate.Enabled(scfeatures.OriginatingIdentity)
if prevOrigIdentEnablement != enabled {
err := utilfeature.DefaultFeatureGate.Set(fmt.Sprintf("%v=%v", scfeatures.OriginatingIdentity, enabled))
if err != nil {
t.Fatalf("Failed to enable originating identity feature: %v", err)
}
}
return prevOrigIdentEnablement
}
// ContextWithUserName creates a Context with the specified userName
func ContextWithUserName(userName string) context.Context {
ctx := genericapirequest.NewContext()
userInfo := &user.DefaultInfo{
Name: userName,
}
return genericapirequest.WithUser(ctx, userInfo)
}