-
Notifications
You must be signed in to change notification settings - Fork 66
/
kubernetes.go
374 lines (300 loc) · 9.54 KB
/
kubernetes.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
367
368
369
370
371
372
373
374
package installer
import (
"encoding/csv"
"fmt"
"os"
"path"
"regexp"
"strings"
cliConfig "github.com/kubeshop/tracetest/cli/config"
cliUI "github.com/kubeshop/tracetest/cli/ui"
)
var kubernetes = installer{
name: "kubernetes",
preChecks: []preChecker{
windowsGnuToolsChecker,
kubectlChecker,
helmChecker,
},
configs: []configurator{
configureKubernetes,
configureTracetest,
configureIngress,
configureDemoApp,
},
installFn: kubernetesInstaller,
}
func windowsGnuToolsChecker(ui cliUI.UI) {
if !isWindows() {
return
}
if commandExists("sed") {
ui.Println(ui.Green("✔ sed already installed"))
return
}
ui.Warning("I didn't find sed in your system")
option := ui.Select("What do you want to do?", []cliUI.Option{
{Text: "Install sed", Fn: installSed},
{Text: "Fix it manually", Fn: exitOption(
"Check the helm install docs on https://community.chocolatey.org/packages/sed",
)},
}, 0)
option.Fn(ui)
if commandExists("sed") {
ui.Println(ui.Green("✔ sed was successfully installed"))
} else {
ui.Exit(ui.Red("✘ sed could not be installed. Check output for errors. " + createIssueMsg))
}
}
func installSed(ui cliUI.UI) {
(cmd{
sudo: true,
notConfirmMsg: "No worries, you can try installing sed manually. See https://community.chocolatey.org/packages/sed",
installDocs: "https://community.chocolatey.org/packages/sed",
windows: "choco install sed",
}).exec(ui)
}
func kubernetesInstaller(config configuration, ui cliUI.UI) {
trackInstall("kubernetes", config, nil)
execCmdIgnoreErrors(kubectlCmd(config, "create namespace "+config.String("k8s.namespace")))
if !config.Bool("installer.only_tracetest") {
installCollector(config, ui)
}
installTracetest(config, ui)
}
func installCollector(config configuration, ui cliUI.UI) {
execCmd(
kubectlNamespaceCmd(config, "apply -f "+collectorYaml),
ui,
)
ui.Println(ui.Green("✔ collector ready"))
}
func installTracetest(conf configuration, ui cliUI.UI) {
setupHelmRepo(conf, ui)
installTracetestChart(conf, ui)
fixTracetestConfiguration(conf, ui)
if !conf.Bool("installer.only_tracetest") {
installOtelCollector(conf, ui)
}
execCmd(kubectlNamespaceCmd(conf, "delete pods -l app.kubernetes.io/name=tracetest"), ui)
if !conf.Bool("installer.only_tracetest") {
installDemo(conf, ui)
}
ui.Success("Install successful!")
ui.Println(fmt.Sprintf(`
To access tracetest:
%s
Then, use your browser to navigate to:
http://localhost:11633
Happy TraceTesting =)
`, kubectlNamespaceCmd(conf, "port-forward svc/tracetest 11633")))
}
func installDemo(conf configuration, ui cliUI.UI) {
helm := helmCmd(conf, "")
script := strings.ReplaceAll(demoScript, "#helm#", helm)
script = fmt.Sprintf(script, conf.String("tracetest.backend.endpoint.collector"))
execCmd(script, ui)
}
func installOtelCollector(conf configuration, ui cliUI.UI) {
cc := createTmpFile("collector-config", string(getCollectorConfigFileContents(ui, conf)), ui)
defer os.Remove(cc.Name())
cmdString := kubectlNamespaceCmd(conf,
"create configmap collector-config --from-file="+cc.Name()+" -o yaml --dry-run=client",
"| sed 's#"+path.Base(cc.Name())+"#collector.yaml#' |",
kubectlNamespaceCmd(conf, "replace -f -"),
)
execCmd(
cmdString,
ui,
)
execCmd(kubectlNamespaceCmd(conf, "delete pods -l app.kubernetes.io/name=otel-collector"), ui)
}
func fixTracetestConfiguration(conf configuration, ui cliUI.UI) {
c := getTracetestConfigFileContents("tracetest-postgresql", "tracetest", "not-secure-database-password", ui, conf)
ttc := createTmpFile("tracetest-config", string(c), ui)
defer os.Remove(ttc.Name())
p := getTracetestProvisionFileContents(ui, conf)
ttp := createTmpFile("tracetest-provisioning", string(p), ui)
defer os.Remove(ttp.Name())
execCmd(
kubectlNamespaceCmd(conf,
"create configmap tracetest --from-file="+ttc.Name()+" --from-file="+ttp.Name()+" -o yaml --dry-run=client",
"| sed 's#"+path.Base(ttc.Name())+"#config.yaml#'",
"| sed 's#"+path.Base(ttp.Name())+"#provisioning.yaml#' |",
kubectlNamespaceCmd(conf, "replace -f -"),
),
ui,
)
}
func installTracetestChart(conf configuration, ui cliUI.UI) {
cmd := []string{
"upgrade --install tracetest kubeshop/tracetest",
"--namespace " + conf.String("k8s.namespace") + " --create-namespace",
}
if cliConfig.Version == "dev" {
cmd = append(cmd, "--set image.tag=latest")
}
if os.Getenv("TRACETEST_DEV") != "" {
cmd = append(cmd, "--set env.tracetestDev=true")
}
execCmd(helmCmd(conf, cmd...), ui)
}
func setupHelmRepo(conf configuration, ui cliUI.UI) {
execCmd(
helmCmd(conf, "repo add --force-update kubeshop https://kubeshop.github.io/helm-charts"),
ui,
)
execCmd(
helmCmd(conf, "repo update"),
ui,
)
}
func helmCmd(config configuration, cmd ...string) string {
return fmt.Sprintf(
"helm --kubeconfig %s --kube-context %s %s",
config.String("k8s.kubeconfig"),
config.String("k8s.context"),
strings.Join(cmd, " "),
)
}
const (
collectorYaml = "https://raw.githubusercontent.com/kubeshop/tracetest/main/k8s/collector.yml"
demoScript = `
tmpdir=$(mktemp -d)
curl -L https://github.com/kubeshop/pokeshop/tarball/master | tar -xz --strip-components 1 -C $tmpdir
cd $tmpdir/helm-chart
#helm# dependency update
#helm# upgrade --install demo . \
--namespace demo --create-namespace \
-f values.yaml \
--set image.tag=latest \
--set image.pullPolicy=Always \
--set postgres.auth.username=ashketchum,postgres.auth.password=squirtle123,postgres.auth.database=pokeshop \
--set rabbitmq.auth.username=guest,rabbitmq.auth.password=guest,rabbitmq.auth.erlangCookie=secretcookie \
--set 'env[4].value=%s'
`
)
func createTmpFile(name, contents string, ui cliUI.UI) *os.File {
f, err := os.CreateTemp("", name)
if err != nil {
ui.Exit(fmt.Sprintf("Cannot create temp %s file: %s", name, err))
}
if _, err := f.Write([]byte(contents)); err != nil {
ui.Exit(fmt.Sprintf("Cannot write temp %s file: %s", name, err))
}
if err := f.Close(); err != nil {
ui.Exit(fmt.Sprintf("Cannot close temp %s file: %s", name, err))
}
return f
}
func kubectlNamespaceCmd(config configuration, cmd ...string) string {
ns := "--namespace " + config.String("k8s.namespace")
return kubectlCmd(config, append([]string{ns}, cmd...)...)
}
func kubectlCmd(config configuration, cmd ...string) string {
return fmt.Sprintf(
"kubectl --kubeconfig %s --context %s %s",
config.String("k8s.kubeconfig"),
config.String("k8s.context"),
strings.Join(cmd, " "),
)
}
type k8sContext struct {
name string
selected bool
}
func getK8sContexts(conf configuration, ui cliUI.UI) []k8sContext {
records, err := getKubernetesContextArray(conf.String("k8s.kubeconfig"))
if err != nil {
ui.Exit(fmt.Sprintf("cannot get kubectl contexts: %s", err.Error()))
}
results := []k8sContext{}
for _, r := range records {
results = append(results, k8sContext{
name: r[1],
selected: r[0] == "*",
})
}
return results
}
func getKubernetesContextArray(kubeconfig string) ([][]string, error) {
output := getCmdOutput(fmt.Sprintf(
`kubectl --kubeconfig %s config get-contexts --no-headers`,
kubeconfig,
))
// replace spaces with comma
spaceRegex := regexp.MustCompile(`[ ]+`)
newStringBytes := spaceRegex.ReplaceAll([]byte(output), []byte(","))
output = string(newStringBytes)
csvReader := csv.NewReader(strings.NewReader(output))
// Related to issue: https://github.com/kubeshop/tracetest/issues/2723
csvReader.FieldsPerRecord = -1 // Disable fields length validation
records, err := csvReader.ReadAll()
if err != nil {
return [][]string{}, err
}
return records, nil
}
func kubernetesContextExists(name string, contexts []k8sContext) bool {
for _, context := range contexts {
if context.name == name {
return true
}
}
return false
}
func getKubernetesContext(conf configuration, ui cliUI.UI) string {
contexts := getK8sContexts(conf, ui)
if len(contexts) == 0 {
ui.Exit(
"We didn't detect any kubectl contexts available. " +
"Make sure your kubectl tool is correctly configured and try again. \n" +
createIssueMsg,
)
}
if KubernetesContext != "" && kubernetesContextExists(KubernetesContext, contexts) {
ui.Println("On which kubectl context do you want to install Tracetest?")
ui.Println(fmt.Sprintf(" > %s", KubernetesContext))
return KubernetesContext
}
if len(contexts) == 1 {
ui.Println("On which kubectl context do you want to install Tracetest?")
ui.Println(fmt.Sprintf(" > %s", contexts[0].name))
return contexts[0].name
}
options := []cliUI.Option{}
defaultIndex := 0
for i, c := range contexts {
if c.selected {
defaultIndex = i
}
options = append(options, cliUI.Option{Text: c.name, Fn: func(ui cliUI.UI) {}})
}
selected := ui.Select("On which kubectl context do you want to install Tracetest?", options, defaultIndex)
return selected.Text
}
func configureKubernetes(conf configuration, ui cliUI.UI) configuration {
conf.set("k8s.kubeconfig", "${HOME}/.kube/config")
context := getKubernetesContext(conf, ui)
conf.set("k8s.context", context)
conf.set("k8s.namespace", "tracetest")
return conf
}
func configureIngress(conf configuration, ui cliUI.UI) configuration {
conf.set("k8s.ingress-host", "tracetest")
return conf
}
func helmChecker(ui cliUI.UI) {
if commandExists("helm") {
ui.Println(ui.Green("✔ helm already installed"))
return
}
ui.Exit("I didn't find helm in your system. Check the helm install docs on https://helm.sh/docs/intro/install/")
}
func kubectlChecker(ui cliUI.UI) {
if commandExists("kubectl") {
ui.Println(ui.Green("✔ kubectl already installed"))
return
}
ui.Exit("I didn't find kubectl in your system")
}