This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
machine_qsv1.go
366 lines (318 loc) · 12.4 KB
/
machine_qsv1.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
package environment
// The functions in this file were only used by the extended statefulset
// components tests, but might be used by others now. They were split off in
// preparation for standalone components.
import (
"context"
"encoding/json"
"os"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
clientscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
qstsv1a1 "code.cloudfoundry.org/quarks-statefulset/pkg/kube/apis/quarksstatefulset/v1alpha1"
"code.cloudfoundry.org/quarks-utils/testing/machine"
)
// GetStatefulSet gets a StatefulSet by namespace and name
func (m *Machine) GetStatefulSet(namespace string, name string) (*appsv1.StatefulSet, error) {
statefulSet, err := m.Clientset.AppsV1().StatefulSets(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
return &appsv1.StatefulSet{}, errors.Wrapf(err, "failed to query for statefulSet by name: %v", name)
}
return statefulSet, nil
}
// CreateQuarksStatefulSet creates a QuarksStatefulSet custom resource and returns a function to delete it
func (m *Machine) CreateQuarksStatefulSet(namespace string, ess qstsv1a1.QuarksStatefulSet) (*qstsv1a1.QuarksStatefulSet, machine.TearDownFunc, error) {
client := m.VersionedClientset.QuarksstatefulsetV1alpha1().QuarksStatefulSets(namespace)
d, err := client.Create(context.Background(), &ess, metav1.CreateOptions{})
return d, func() error {
pvcs, err := m.Clientset.CoreV1().PersistentVolumeClaims(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: labels.Set(map[string]string{
"test-run-reference": ess.Name,
}).String(),
})
if err != nil {
return err
}
err = client.Delete(context.Background(), ess.GetName(), metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
}
for _, pvc := range pvcs.Items {
err = m.Clientset.CoreV1().PersistentVolumeClaims(namespace).Delete(context.Background(), pvc.GetName(), metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
}
}
return nil
}, err
}
// GetQuarksStatefulSet gets a QuarksStatefulSet custom resource
func (m *Machine) GetQuarksStatefulSet(namespace string, name string) (*qstsv1a1.QuarksStatefulSet, error) {
client := m.VersionedClientset.QuarksstatefulsetV1alpha1().QuarksStatefulSets(namespace)
d, err := client.Get(context.Background(), name, metav1.GetOptions{})
return d, err
}
// UpdateQuarksStatefulSet updates a QuarksStatefulSet custom resource and returns a function to delete it
func (m *Machine) UpdateQuarksStatefulSet(namespace string, ess qstsv1a1.QuarksStatefulSet) (*qstsv1a1.QuarksStatefulSet, machine.TearDownFunc, error) {
client := m.VersionedClientset.QuarksstatefulsetV1alpha1().QuarksStatefulSets(namespace)
d, err := client.Update(context.Background(), &ess, metav1.UpdateOptions{})
return d, func() error {
err := client.Delete(context.Background(), ess.GetName(), metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
return err
}
return nil
}, err
}
// DeleteQuarksStatefulSet deletes a QuarksStatefulSet custom resource
func (m *Machine) DeleteQuarksStatefulSet(namespace string, name string) error {
client := m.VersionedClientset.QuarksstatefulsetV1alpha1().QuarksStatefulSets(namespace)
return client.Delete(context.Background(), name, metav1.DeleteOptions{})
}
// WaitForPodLabelToExist blocks until the specified label appears
func (m *Machine) WaitForPodLabelToExist(n string, podName string, label string) error {
return wait.PollImmediate(m.PollInterval, m.PollTimeout, func() (bool, error) {
return m.PodLabelToExist(n, podName, label)
})
}
// PodLabelToExist returns true if the label exist in the specified pod
func (m *Machine) PodLabelToExist(n string, podName string, label string) (bool, error) {
pod, err := m.Clientset.CoreV1().Pods(n).Get(context.Background(), podName, metav1.GetOptions{})
if err != nil {
return false, err
}
labels := pod.GetLabels()
if _, found := labels[label]; found {
return true, nil
}
return false, nil
}
// WaitForPodLabelToNotExist blocks until the specified label is not present
func (m *Machine) WaitForPodLabelToNotExist(n string, podName string, label string) error {
return wait.PollImmediate(m.PollInterval, m.PollTimeout, func() (bool, error) {
return m.PodLabelToNotExist(n, podName, label)
})
}
// PodLabelToNotExist returns true if the label does not exist
func (m *Machine) PodLabelToNotExist(n string, podName string, label string) (bool, error) {
pod, err := m.Clientset.CoreV1().Pods(n).Get(context.Background(), podName, metav1.GetOptions{})
if err != nil {
return false, err
}
labels := pod.GetLabels()
if _, found := labels[label]; !found {
return true, nil
}
return false, nil
}
// WaitForStatefulSetDelete blocks until the specified statefulSet is deleted
func (m *Machine) WaitForStatefulSetDelete(namespace string, name string) error {
return wait.PollImmediate(m.PollInterval, m.PollTimeout, func() (bool, error) {
found, err := m.StatefulSetExist(namespace, name)
return !found, err
})
}
// StatefulSetExist checks if the statefulSet exists
func (m *Machine) StatefulSetExist(namespace string, name string) (bool, error) {
_, err := m.Clientset.AppsV1().StatefulSets(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, errors.Wrapf(err, "failed to query for statefulSet by name: %s", name)
}
return true, nil
}
// WaitForStatefulSetNewGeneration blocks until at least one StatefulSet is
// found, which has a generation greater than currentVersion. It fails after
// the timeout.
func (m *Machine) WaitForStatefulSetNewGeneration(namespace string, name string, currentVersion int64) error {
return wait.PollImmediate(m.PollInterval, m.PollTimeout, func() (bool, error) {
return m.StatefulSetNewGeneration(namespace, name, currentVersion)
})
}
// WaitForQuarksStatefulSets blocks until at least one QuarksStatefulSet is found. It fails after the timeout.
func (m *Machine) WaitForQuarksStatefulSets(namespace string, labels string) error {
return wait.PollImmediate(m.PollInterval, m.PollTimeout, func() (bool, error) {
return m.QuarksStatefulSetExists(namespace, labels)
})
}
// WaitForPV blocks until the pv is running. It fails after the timeout.
func (m *Machine) WaitForPV(name string) error {
return wait.PollImmediate(m.PollInterval, m.PollTimeout, func() (bool, error) {
return m.PVAvailable(name)
})
}
// PVAvailable returns true if the pv by that name is in state available
func (m *Machine) PVAvailable(name string) (bool, error) {
pv, err := m.Clientset.CoreV1().PersistentVolumes().Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, errors.Wrapf(err, "failed to query for pv by name: %s", name)
}
if pv.Status.Phase == "Available" {
return true, nil
}
return false, nil
}
// WaitForPVsDelete blocks until the pv is deleted. It fails after the timeout.
func (m *Machine) WaitForPVsDelete(labels string) error {
return wait.PollImmediate(m.PollInterval, m.PollTimeout, func() (bool, error) {
return m.PVsDeleted(labels)
})
}
// PVsDeleted returns true if the all pvs are deleted
func (m *Machine) PVsDeleted(labels string) (bool, error) {
pvList, err := m.Clientset.CoreV1().PersistentVolumes().List(context.Background(), metav1.ListOptions{
LabelSelector: labels,
})
if err != nil {
return false, err
}
if len(pvList.Items) == 0 {
return true, nil
}
return false, nil
}
// WaitForPVCsDelete blocks until the pvc is deleted. It fails after the timeout.
func (m *Machine) WaitForPVCsDelete(namespace string) error {
return wait.PollImmediate(m.PollInterval, m.PollTimeout, func() (bool, error) {
return m.PVCsDeleted(namespace)
})
}
// PVCsDeleted returns true if the all pvs are deleted
func (m *Machine) PVCsDeleted(namespace string) (bool, error) {
pvcList, err := m.Clientset.CoreV1().PersistentVolumeClaims(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
return false, err
}
if len(pvcList.Items) == 0 {
return true, nil
}
return false, nil
}
// WaitForStatefulSet blocks until all statefulSet pods are running. It fails after the timeout.
func (m *Machine) WaitForStatefulSet(namespace string, name string) error {
return wait.PollImmediate(m.PollInterval, m.PollTimeout, func() (bool, error) {
return m.StatefulSetRunning(namespace, name)
})
}
// StatefulSetRunning returns true if the statefulSet by that name has all pods created
func (m *Machine) StatefulSetRunning(namespace string, name string) (bool, error) {
statefulSet, err := m.Clientset.AppsV1().StatefulSets(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, errors.Wrapf(err, "failed to query for statefulSet by name: %s", name)
}
if statefulSet.Status.ReadyReplicas == *statefulSet.Spec.Replicas {
return true, nil
}
return false, nil
}
// QuarksStatefulSetExists returns true if at least one ess selected by labels exists
func (m *Machine) QuarksStatefulSetExists(namespace string, labels string) (bool, error) {
esss, err := m.VersionedClientset.QuarksstatefulsetV1alpha1().QuarksStatefulSets(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: labels,
})
if err != nil {
return false, errors.Wrapf(err, "failed to query for ess by labels: %v", labels)
}
return len(esss.Items) > 0, nil
}
// StatefulSetNewGeneration returns true if StatefulSet has a new generation greater `generation`
func (m *Machine) StatefulSetNewGeneration(namespace string, name string, generation int64) (bool, error) {
client := m.Clientset.AppsV1().StatefulSets(namespace)
ss, err := client.Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
return false, errors.Wrapf(err, "failed to query for statefulSet by name: %v", name)
}
if ss.Status.ObservedGeneration > generation {
return true, nil
}
return false, nil
}
// GetNamespaceEvents exits as soon as an event reason and msg matches
func (m *Machine) GetNamespaceEvents(namespace, name, id, reason, msg string) (bool, error) {
eList, err := m.Clientset.CoreV1().Events(namespace).List(context.Background(), metav1.ListOptions{
FieldSelector: fields.Set{
"involvedObject.name": name,
"involvedObject.uid": id,
}.AsSelector().String(),
})
if err != nil {
return false, err
}
// Loop here due to the size
// of the events in the ns
for _, n := range eList.Items {
if n.Reason == reason && n.Message == msg {
return true, nil
}
}
return false, nil
}
// ExecPodCMD executes a cmd in a container
func (m *Machine) ExecPodCMD(client kubernetes.Interface, rc *rest.Config, pod *corev1.Pod, container string, command []string) (bool, error) {
req := client.CoreV1().RESTClient().Post().
Resource("pods").
Name(pod.Name).
Namespace(pod.Namespace).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Container: container,
Command: command,
Stdin: true,
Stdout: true,
Stderr: true,
}, clientscheme.ParameterCodec)
executor, err := remotecommand.NewSPDYExecutor(rc, "POST", req.URL())
if err != nil {
return false, errors.New("failed to initialize remote command executor")
}
if err = executor.Stream(remotecommand.StreamOptions{Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr, Tty: false}); err != nil {
return false, errors.Wrapf(err, "failed executing command in pod: %s, container: %s in namespace: %s",
pod.Name,
container,
pod.Namespace,
)
}
return true, nil
}
// PatchPod applies a patch into a specific pod
// operation can be of the form add,remove,replace
// See https://tools.ietf.org/html/rfc6902 for more information
func (m *Machine) PatchPod(namespace string, name string, o string, p string, v string) error {
payloadBytes, _ := json.Marshal(
[]struct {
Op string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`
}{{
Op: o,
Path: p,
Value: v,
}},
)
_, err := m.Clientset.CoreV1().Pods(namespace).Patch(
context.Background(),
name,
types.JSONPatchType,
payloadBytes,
metav1.PatchOptions{},
)
return err
}