-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkubectl.go
83 lines (66 loc) · 1.7 KB
/
kubectl.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
package testhelper
import (
"os/exec"
"testing"
)
const (
kubectl = "kubectl"
)
// Operation represents a kubectl operation type, i.e. apply or delete.
type Operation string
const (
// Apply is the apply kubectl operation.
Apply Operation = "apply"
// Delete is the delete kubectl operation.
Delete Operation = "delete"
// Create is the create kubectl operation.
Create Operation = "create"
// Get is the get kubectl operation.
Get Operation = "get"
)
func kubectlNamespace(t *testing.T, operation Operation, namespace string) {
t.Helper()
cmd := exec.Command(kubectl, string(operation), "namespace", namespace) //nolint:gosec
err := cmd.Run()
if err != nil {
t.Fatalf("error executing kubectl command, error: '%s'", err)
}
}
// KubectlCreateNamespace execs a kubectl create namespace.
func KubectlCreateNamespace(t *testing.T, namespace string) {
t.Helper()
kubectlNamespace(t, Create, namespace)
}
// KubectlDeleteNamespace execs a kubectl delete namespace.
func KubectlDeleteNamespace(t *testing.T, namespace string) {
t.Helper()
kubectlNamespace(t, Delete, namespace)
}
// KubectlFileOp execs a kubectl operation on a file (i.e. apply/delete).
func KubectlFileOp(t *testing.T, operation Operation, namespace, fileName string) {
t.Helper()
cmd := exec.Command( //nolint:gosec
kubectl,
string(operation),
"--namespace",
namespace,
"-f",
fileName,
)
_ = Execute(t, cmd)
}
// KubectlGetOp runs get on the given object, returning the yaml output.
func KubectlGetOp(t *testing.T, kind, namespace, name string) []byte {
t.Helper()
cmd := exec.Command( //nolint:gosec
kubectl,
string(Get),
kind,
"--namespace",
namespace,
name,
"-o",
"yaml",
)
return Execute(t, cmd)
}