-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckers.go
128 lines (105 loc) · 3.53 KB
/
checkers.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
package k8t
import (
"context"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
)
// enables the WaitFor() function to receive and execute custom
// checking functions, allowing the caller to define their own
// conditions for waiting on a specific resource or state to be reached.
type Checker func(context.Context, *Cluster) (bool, error)
// Optional options for WaitForWithOpts function
type WaitForOpts struct {
// here you can pass your context. It it's not set, the default
// context.Background() will be used
Context context.Context
// maximum duration for which the function will wait for the condition
// to be met. If it's not set, the default 2 minutes will be used
Timeout time.Duration
// represents the duration between invocations of the checker function
// If it's not set, the defauls 2 seconds will be used
Interval time.Duration
}
// simple, sort version of WaitForOpts with default values. The timeout is
// 2 minutes and interval is set to 2 seconds. Is't equivalent to:
//
// WaitForOpts(check, WaitForOpts{})
func (c *Cluster) WaitFor(check Checker) error {
return c.WaitForWithOpts(check, WaitForOpts{})
}
// the function will check if system met given condition.
// The checking is triggered every n seconds defined by interval in opts.
// The invocation of checking function continue until:
// - function ends with true
// - reach timeout limit defined in opts
//
// The function returns nil if the given checking function met the condition.
// If we reach the timeout or some error occured during checking, the function
// returns error.
func (c *Cluster) WaitForWithOpts(check Checker, opts WaitForOpts) error {
ctx := opts.Context
if ctx == nil {
ctx = context.Background()
}
timeout := opts.Timeout
if timeout == 0 {
timeout = 120 * time.Second
}
interval := opts.Interval
if interval == 0 {
interval = 2 * time.Second
}
err := wait.PollUntilContextTimeout(ctx, interval, timeout, true, func(innerCtx context.Context) (done bool, err error) {
return check(innerCtx, c)
})
return err
}
// check if resource of given kind and name exists in the given
// cluster, in the cluster's test namespace
func ResourceExist(apiVersion, kind, name string) Checker {
return func(ctx context.Context, c *Cluster) (bool, error) {
res, err := c.GetWithOpts(apiVersion, kind, name, GetOpts{Context: ctx})
if err != nil {
return false, err
}
if res == nil {
return false, nil
}
return true, nil
}
}
// check if resource of given kind and name doesn't exists in the given
// cluster, in the cluster's test namespace
func ResourceNotExist(apiVersion, kind, name string) Checker {
return func(ctx context.Context, c *Cluster) (bool, error) {
res, err := c.GetWithOpts(apiVersion, kind, name, GetOpts{Context: ctx})
if err != nil {
return true, nil
}
if res == nil {
return true, nil
}
return false, nil
}
}
// check if given pod is running, that means it's in `Running` phase.
// The function expect also namespace. If you provide empty string, then
// cluster's default test namespace will be used.
func PodIsRunning(namespace, name string) Checker {
return func(ctx context.Context, c *Cluster) (bool, error) {
podNamespace := namespace
if podNamespace == "" {
podNamespace = c.testNamespace
}
pod, err := c.k8sClient.CoreV1().Pods(podNamespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return false, err
}
if pod.Status.Phase == corev1.PodRunning {
return true, nil
}
return false, nil
}
}