-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmagefile.go
252 lines (191 loc) · 6.31 KB
/
magefile.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
//go:build mage
package main
import (
"fmt"
wait "github.com/DazEdword/observability-stack-otel/internal"
"github.com/bitfield/script"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
// Kind cluster management
type Kind mg.Namespace
type Prometheus mg.Namespace
type LGTM mg.Namespace
type Apps mg.Namespace
func Setup() error {
if err := sh.RunV("brew", "install", "mage", "kubectl", "kubectx", "kind", "kustomize", "txn2/tap/kubefwd", "helm", "jq", "golangci-lint"); err != nil {
return err
}
return nil
}
func All() error {
mg.Deps(Kind.CreateOlly, Kind.CreateApps)
// separated different install deps since they are designed in mage to run exactly once
mg.SerialDeps(Prometheus.InstallGlobal, Prometheus.DeployGlobal, LGTM.Deploy)
mg.SerialDeps(Prometheus.InstallWriter, Prometheus.DeployRemote, Apps.Deploy)
return nil
}
func DeleteAll() error {
mg.SerialDeps(Kind.DeleteApps, Kind.DeleteOlly)
return nil
}
func (Kind) CreateOlly() error {
if err := sh.RunV("kind", "create", "cluster", "--name", "observability-stack", "--config=deploy/ollyclusterconfig.yaml"); err != nil {
return err
}
return nil
}
func (Kind) DeleteOlly() error {
// TODO confirmation prompt
if err := sh.Run("kind", "delete", "cluster", "--name", "observability-stack"); err != nil {
return err
}
return nil
}
func (Kind) CreateApps() error {
if err := sh.RunV("kind", "create", "cluster", "--name", "demo-apps", "--config=deploy/appsclusterconfig.yaml"); err != nil {
return err
}
return nil
}
func (Kind) DeleteApps() error {
// TODO confirmation prompt
if err := sh.RunV("kind", "delete", "cluster", "--name", "demo-apps"); err != nil {
return err
}
return nil
}
func (Prometheus) InstallGlobal() error {
return prometheusInstall("kind-observability-stack")
}
func (Prometheus) InstallWriter() error {
return prometheusInstall("kind-demo-apps")
}
func prometheusInstall(context string) error {
// switch context
if err := sh.RunV("kubectx", context); err != nil {
return err
}
if err := sh.RunV("kubectl", "apply", "-f", "deploy/prometheus/namespace.yaml"); err != nil {
return err
}
if err := sh.RunV("kubectl", "create", "-f", "deploy/prometheus/operator/bundle.yaml"); err != nil {
return err
}
// pods might not be immediately available after creation, hence we attempt the wait with backoff
err := wait.ForPodWithConstantBackoff("app.kubernetes.io/name=prometheus-operator", "120s")
if err != nil {
return err
}
if err := sh.RunV("kubectl", "create", "-f", "deploy/prometheus/rbac.yaml"); err != nil {
return err
}
return nil
}
func (Prometheus) DeployGlobal() error {
if err := sh.RunV("kubectl", "create", "-f", "deploy/prometheus/global/instance.yaml"); err != nil {
return err
}
// pods might not be immediately available after creation, hence we attempt the wait with backoff
err := wait.ForPodWithConstantBackoff("app.kubernetes.io/instance=prometheus-global", "120s")
if err != nil {
return err
}
if err := sh.RunV("kubectl", "create", "-f", "deploy/prometheus/service.yaml"); err != nil {
return err
}
if err := sh.RunV("kubectl", "create", "-f", "deploy/prometheus/global/extservice.yaml"); err != nil {
return err
}
if err := sh.RunV("kubectl", "create", "-f", "deploy/prometheus/global/servicemonitor.yaml"); err != nil {
return err
}
return nil
}
func (Prometheus) DeployRemote() error {
if err := sh.RunV("kubectl", "create", "-f", "deploy/prometheus/writer/instance.yaml"); err != nil {
return err
}
// pods might not be immediately available after creation, hence we attempt the wait with backoff
err := wait.ForPodWithConstantBackoff("app.kubernetes.io/instance=prometheus-writer", "120s")
if err != nil {
return err
}
if err := sh.RunV("kubectl", "create", "-f", "deploy/prometheus/service.yaml"); err != nil {
return err
}
if err := sh.RunV("kubectl", "create", "-f", "deploy/prometheus/writer/extservice.yaml"); err != nil {
return err
}
if err := sh.RunV("kubectl", "create", "-f", "deploy/prometheus/writer/servicemonitor.yaml"); err != nil {
return err
}
return nil
}
func (Prometheus) Remove() error {
// todo remove existing custom resources in each namespace
if err := sh.RunV("kubectl", "delete", "-f", "deploy/prometheus/rbac.yaml"); err != nil {
return err
}
if err := sh.RunV("kubectl", "delete", "-f", "deploy/prometheus/operator/bundle.yaml"); err != nil {
return err
}
return nil
}
func (Prometheus) Forward() error {
// todo remove existing custom resources in each namespace
if err := sh.RunV("kubectl", "port-forward", "svc/prometheus", "9090"); err != nil {
return err
}
return nil
}
func (LGTM) Deploy() error {
if err := sh.RunV("helm", "repo", "add", "grafana", "https://grafana.github.io/helm-charts"); err != nil {
return err
}
if err := sh.RunV("helm", "upgrade", "-f", "deploy/lgtm/values.yaml", "observability-stack", "grafana/lgtm-distributed", "--create-namespace", "--namespace", "monitoring", "--install"); err != nil {
return err
}
return nil
}
func (LGTM) Forward() error {
// switch context
if err := sh.RunV("kubectx", "kind-observability-stack"); err != nil {
return err
}
password, err := script.Exec(`kubectl get secret --namespace monitoring observability-stack-grafana -o jsonpath="{.data.admin-password}"`).Exec("base64 --decode").String()
if err != nil {
return err
}
fmt.Println("Username:")
fmt.Printf("%s\n\n", "admin")
fmt.Println("Admin password:")
fmt.Printf("%s\n\n", password)
podName, err := script.Exec(`kubectl get pods --namespace monitoring -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=observability-stack" -o jsonpath="{.items[0].metadata.name}"`).String()
if err != nil {
return err
}
fmt.Printf("Forwarding pod %s\n\n", podName)
if err := sh.RunV("kubectl", "--namespace", "monitoring", "port-forward", podName, "3000"); err != nil {
return err
}
return nil
}
func (Apps) Deploy() error {
if err := sh.RunV("kubectl", "apply", "-f", "deploy/apps/sample/deployment.yaml"); err != nil {
return err
}
if err := sh.RunV("kubectl", "apply", "-f", "deploy/apps/sample/service.yaml"); err != nil {
return err
}
if err := sh.RunV("kubectl", "apply", "-f", "deploy/apps/sample/servicemonitor.yaml"); err != nil {
return err
}
return nil
}
func Lint() error {
if err := sh.RunV("golangci-lint", "run"); err != nil {
return err
}
return nil
}