-
Notifications
You must be signed in to change notification settings - Fork 58
/
kubectl.go
191 lines (157 loc) · 4.27 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
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
// https://raw.githubusercontent.com/Azure/secrets-store-csi-driver-provider-azure/master/test/e2e/framework/exec/kubectl.go
package util
import (
"fmt"
"os/exec"
"strings"
"k8s.io/klog/v2"
)
// KubectlApply executes "kubectl apply" given a list of arguments.
func KubectlApply(kubeconfigPath, namespace string, args []string) error {
args = append([]string{
"apply",
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
fmt.Sprintf("--namespace=%s", namespace),
}, args...)
_, err := Kubectl(args)
return err
}
// HelmInstall executes "helm install" given a list of arguments.
func HelmInstall(kubeconfigPath, namespace string, args []string) error {
args = append([]string{
"install",
"eraser-e2e-test",
"--wait",
"--debug",
"--create-namespace",
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
fmt.Sprintf("--namespace=%s", namespace),
}, args...)
_, err := Helm(args)
return err
}
// HelmUninstall executes "helm uninstall" given a list of arguments.
func HelmUninstall(kubeconfigPath, namespace string, args []string) error {
args = append([]string{
"uninstall",
"eraser-e2e-test",
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
fmt.Sprintf("--namespace=%s", namespace),
}, args...)
_, err := Helm(args)
return err
}
// KubectlDelete executes "kubectl delete" given a list of arguments.
func KubectlDelete(kubeconfigPath, namespace string, args []string) error {
args = append([]string{
"delete",
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
fmt.Sprintf("--namespace=%s", namespace),
}, args...)
_, err := Kubectl(args)
return err
}
func KubectlExecCurl(kubeconfigPath, podName string, endpoint, namespace string) (string, error) {
args := []string{
"exec",
"-i",
podName,
"-n",
namespace,
"--",
"curl",
endpoint,
}
return Kubectl(args)
}
func KubectlWait(kubeconfigPath, podName, namespace string) (string, error) {
args := []string{
"wait",
"--for=condition=Ready",
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
"--timeout=120s",
"pod",
podName,
"-n",
namespace,
}
return Kubectl(args)
}
// KubectlLogs executes "kubectl logs" given a list of arguments.
func KubectlLogs(kubeconfigPath, podName, containerName, namespace string, extraArgs ...string) (string, error) {
args := []string{
"logs",
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
fmt.Sprintf("--namespace=%s", namespace),
podName,
}
if containerName != "" {
args = append(args, fmt.Sprintf("-c=%s", containerName))
}
args = append(args, extraArgs...)
return Kubectl(args)
}
// KubectlDescribe executes "kubectl describe" given a list of arguments.
func KubectlDescribe(kubeconfigPath, podName, namespace string) (string, error) {
args := []string{
"describe",
"pod",
podName,
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
fmt.Sprintf("--namespace=%s", namespace),
}
return Kubectl(args)
}
// KubectlDescribe executes "kubectl describe" given a list of arguments.
func KubectlCurlPod(kubeconfigPath, namespace string) (string, error) {
args := []string{
"run",
"temp",
"-n",
namespace,
"--image",
"curlimages/curl",
"--",
"tail",
"-f",
"/dev/null",
}
return Kubectl(args)
}
// KubectlGet executes "kubectl get" given a list of arguments.
func KubectlGet(kubeconfigPath string, otherArgs ...string) (string, error) {
args := []string{
fmt.Sprintf("--kubeconfig=%s", kubeconfigPath),
"get",
}
args = append(args, otherArgs...)
return Kubectl(args)
}
func Kubectl(args []string) (string, error) {
klog.Infof("kubectl %s", strings.Join(args, " "))
cmd := exec.Command("kubectl", args...)
stdoutStderr, err := cmd.CombinedOutput()
output := strings.TrimSpace(string(stdoutStderr))
if err != nil {
err = fmt.Errorf("%w: %s", err, output)
}
return output, err
}
func KubectlBackground(args []string) error {
klog.Infof("kubectl %s", strings.Join(args, " "))
cmd := exec.Command("kubectl", args...)
if err := cmd.Start(); err != nil {
return fmt.Errorf("failed to start cmd: %v", err)
}
return nil
}
func Helm(args []string) (string, error) {
klog.Infof("helm %s", strings.Join(args, " "))
cmd := exec.Command("helm", args...)
stdoutStderr, err := cmd.CombinedOutput()
output := strings.TrimSpace(string(stdoutStderr))
if err != nil {
err = fmt.Errorf("%w: %s", err, output)
}
return output, err
}