forked from gruntwork-io/terratest
/
kubectl.go
145 lines (124 loc) · 5.99 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
package k8s
import (
"net/url"
"os"
"github.com/stretchr/testify/require"
"github.com/wahlfeld/terratest/modules/shell"
"github.com/wahlfeld/terratest/modules/testing"
)
// RunKubectl will call kubectl using the provided options and args, failing the test on error.
func RunKubectl(t testing.TestingT, options *KubectlOptions, args ...string) {
require.NoError(t, RunKubectlE(t, options, args...))
}
// RunKubectlE will call kubectl using the provided options and args.
func RunKubectlE(t testing.TestingT, options *KubectlOptions, args ...string) error {
_, err := RunKubectlAndGetOutputE(t, options, args...)
return err
}
// RunKubectlAndGetOutputE will call kubectl using the provided options and args, returning the output of stdout and
// stderr.
func RunKubectlAndGetOutputE(t testing.TestingT, options *KubectlOptions, args ...string) (string, error) {
cmdArgs := []string{}
if options.ContextName != "" {
cmdArgs = append(cmdArgs, "--context", options.ContextName)
}
if options.ConfigPath != "" {
cmdArgs = append(cmdArgs, "--kubeconfig", options.ConfigPath)
}
if options.Namespace != "" {
cmdArgs = append(cmdArgs, "--namespace", options.Namespace)
}
cmdArgs = append(cmdArgs, args...)
command := shell.Command{
Command: "kubectl",
Args: cmdArgs,
Env: options.Env,
Logger: options.Logger,
}
return shell.RunCommandAndGetOutputE(t, command)
}
// KubectlDelete will take in a file path and delete it from the cluster targeted by KubectlOptions. If there are any
// errors, fail the test immediately.
func KubectlDelete(t testing.TestingT, options *KubectlOptions, configPath string) {
require.NoError(t, KubectlDeleteE(t, options, configPath))
}
// KubectlDeleteE will take in a file path and delete it from the cluster targeted by KubectlOptions.
func KubectlDeleteE(t testing.TestingT, options *KubectlOptions, configPath string) error {
return RunKubectlE(t, options, "delete", "-f", configPath)
}
// KubectlDeleteFromKustomize will take in a kustomization directory path and delete it from the cluster targeted by KubectlOptions. If there are any
// errors, fail the test immediately.
func KubectlDeleteFromKustomize(t testing.TestingT, options *KubectlOptions, configPath string) {
require.NoError(t, KubectlDeleteFromKustomizeE(t, options, configPath))
}
// KubectlDeleteFromKustomizeE will take in a kustomization directory path and delete it from the cluster targeted by KubectlOptions.
func KubectlDeleteFromKustomizeE(t testing.TestingT, options *KubectlOptions, configPath string) error {
return RunKubectlE(t, options, "delete", "-k", configPath)
}
// KubectlDeleteFromString will take in a kubernetes resource config as a string and delete it on the cluster specified
// by the provided kubectl options.
func KubectlDeleteFromString(t testing.TestingT, options *KubectlOptions, configData string) {
require.NoError(t, KubectlDeleteFromStringE(t, options, configData))
}
// KubectlDeleteFromStringE will take in a kubernetes resource config as a string and delete it on the cluster specified
// by the provided kubectl options. If it fails, this will return the error.
func KubectlDeleteFromStringE(t testing.TestingT, options *KubectlOptions, configData string) error {
tmpfile, err := StoreConfigToTempFileE(t, configData)
if err != nil {
return err
}
defer os.Remove(tmpfile)
return KubectlDeleteE(t, options, tmpfile)
}
// KubectlApply will take in a file path and apply it to the cluster targeted by KubectlOptions. If there are any
// errors, fail the test immediately.
func KubectlApply(t testing.TestingT, options *KubectlOptions, configPath string) {
require.NoError(t, KubectlApplyE(t, options, configPath))
}
// KubectlApplyE will take in a file path and apply it to the cluster targeted by KubectlOptions.
func KubectlApplyE(t testing.TestingT, options *KubectlOptions, configPath string) error {
return RunKubectlE(t, options, "apply", "-f", configPath)
}
// KubectlApplyFromKustomize will take in a kustomization directory path and apply it to the cluster targeted by KubectlOptions. If there are any
// errors, fail the test immediately.
func KubectlApplyFromKustomize(t testing.TestingT, options *KubectlOptions, configPath string) {
require.NoError(t, KubectlApplyFromKustomizeE(t, options, configPath))
}
// KubectlApplyFromKustomizeE will take in a kustomization directory path and apply it to the cluster targeted by KubectlOptions.
func KubectlApplyFromKustomizeE(t testing.TestingT, options *KubectlOptions, configPath string) error {
return RunKubectlE(t, options, "apply", "-k", configPath)
}
// KubectlApplyFromString will take in a kubernetes resource config as a string and apply it on the cluster specified
// by the provided kubectl options.
func KubectlApplyFromString(t testing.TestingT, options *KubectlOptions, configData string) {
require.NoError(t, KubectlApplyFromStringE(t, options, configData))
}
// KubectlApplyFromStringE will take in a kubernetes resource config as a string and apply it on the cluster specified
// by the provided kubectl options. If it fails, this will return the error.
func KubectlApplyFromStringE(t testing.TestingT, options *KubectlOptions, configData string) error {
tmpfile, err := StoreConfigToTempFileE(t, configData)
if err != nil {
return err
}
defer os.Remove(tmpfile)
return KubectlApplyE(t, options, tmpfile)
}
// StoreConfigToTempFile will store the provided config data to a temporary file created on the os and return the
// filename.
func StoreConfigToTempFile(t testing.TestingT, configData string) string {
out, err := StoreConfigToTempFileE(t, configData)
require.NoError(t, err)
return out
}
// StoreConfigToTempFileE will store the provided config data to a temporary file created on the os and return the
// filename, or error.
func StoreConfigToTempFileE(t testing.TestingT, configData string) (string, error) {
escapedTestName := url.PathEscape(t.Name())
tmpfile, err := os.CreateTemp("", escapedTestName)
if err != nil {
return "", err
}
defer tmpfile.Close()
_, err = tmpfile.WriteString(configData)
return tmpfile.Name(), err
}