forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_util.go
99 lines (89 loc) · 3.73 KB
/
wait_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
// Copyright 2018 The Operator-SDK 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 e2eutil
import (
"context"
"testing"
"time"
"github.com/operator-framework/operator-sdk/pkg/test"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// WaitForDeployment checks to see if a given deployment has a certain number of available replicas after a specified amount of time
// If the deployment does not have the required number of replicas after 5 * retries seconds, the function returns an error
// This can be used in multiple ways, like verifying that a required resource is ready before trying to use it, or to test
// failure handling, like simulated in SimulatePodFail.
func WaitForDeployment(t *testing.T, kubeclient kubernetes.Interface, namespace, name string, replicas int, retryInterval, timeout time.Duration) error {
return waitForDeployment(t, kubeclient, namespace, name, replicas, retryInterval, timeout, false)
}
// WaitForOperatorDeployment has the same functionality as WaitForDeployment but will no wait for the deployment if the
// test was run with a locally run operator (--up-local flag)
func WaitForOperatorDeployment(t *testing.T, kubeclient kubernetes.Interface, namespace, name string, replicas int, retryInterval, timeout time.Duration) error {
return waitForDeployment(t, kubeclient, namespace, name, replicas, retryInterval, timeout, true)
}
func waitForDeployment(t *testing.T, kubeclient kubernetes.Interface, namespace, name string, replicas int, retryInterval, timeout time.Duration, isOperator bool) error {
if isOperator && test.Global.LocalOperator {
t.Log("Operator is running locally; skip waitForDeployment")
return nil
}
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
deployment, err := kubeclient.AppsV1().Deployments(namespace).Get(name, metav1.GetOptions{IncludeUninitialized: true})
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of %s deployment\n", name)
return false, nil
}
return false, err
}
if int(deployment.Status.AvailableReplicas) == replicas {
return true, nil
}
t.Logf("Waiting for full availability of %s deployment (%d/%d)\n", name, deployment.Status.AvailableReplicas, replicas)
return false, nil
})
if err != nil {
return err
}
t.Logf("Deployment available (%d/%d)\n", replicas, replicas)
return nil
}
func WaitForDeletion(t *testing.T, dynclient client.Client, obj runtime.Object, retryInterval, timeout time.Duration) error {
key, err := client.ObjectKeyFromObject(obj)
if err != nil {
return err
}
kind := obj.GetObjectKind().GroupVersionKind().Kind
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err = wait.Poll(retryInterval, timeout, func() (done bool, err error) {
err = dynclient.Get(ctx, key, obj)
if apierrors.IsNotFound(err) {
return true, nil
}
if err != nil {
return false, err
}
t.Logf("Waiting for %s %s to be deleted\n", kind, key)
return false, nil
})
if err != nil {
return err
}
t.Logf("%s %s was deleted\n", kind, key)
return nil
}