-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
180 lines (149 loc) · 4.3 KB
/
main.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package main
import (
"bytes"
"context"
"fmt"
"io/fs"
"strings"
"text/template"
corev1 "k8s.io/client-go/applyconfigurations/core/v1"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/framework/kubernetes/kubetool"
"namespacelabs.dev/foundation/framework/provisioning"
"namespacelabs.dev/foundation/schema"
)
const (
id = "grafana.foundation.namespacelabs.dev"
configMapName = id
mountPath = "/etc/grafana"
)
var volumeName = strings.Replace(id, ".", "-", -1)
type tool struct{}
func main() {
h := provisioning.NewHandlers()
henv := h.MatchEnv(&schema.Environment{Runtime: "kubernetes"})
henv.HandleStack(tool{})
provisioning.Handle(h)
}
func (tool) Apply(ctx context.Context, r provisioning.StackRequest, out *provisioning.ApplyOutput) error {
kr, err := kubetool.FromRequest(r)
if err != nil {
return err
}
configs := map[string]string{}
items := []*kubedef.SpecExtension_Volume_ConfigMap_Item{}
dashboard := "default-dashboard.json"
data, err := fs.ReadFile(embeddedData, dashboard)
if err != nil {
return err
}
configs["dashboard"] = string(data)
items = append(items, &kubedef.SpecExtension_Volume_ConfigMap_Item{
Key: "dashboard",
Path: dashboard,
})
var b bytes.Buffer
if err := iniTmpl.Execute(&b, iniTmplArgs{MountPath: mountPath, DashboardPath: dashboard}); err != nil {
return fmt.Errorf("failed to generate configuration: %w", err)
}
grafanaIni := iniTmpl.Name()
configs[grafanaIni] = b.String()
items = append(items, &kubedef.SpecExtension_Volume_ConfigMap_Item{
Key: grafanaIni,
Path: grafanaIni,
})
for _, endpoint := range r.Stack.GetEndpoint() {
if !endpoint.HasKind("prometheus.io/endpoint") {
continue
}
if len(endpoint.GetPorts()) == 0 {
continue
}
port := endpoint.Ports[0].GetPort().GetContainerPort()
if port == 0 {
continue
}
host := endpoint.GetAllocatedName()
var b bytes.Buffer
if err := promTmpl.Execute(&b, promTmplArgs{Host: host, Port: port}); err != nil {
return fmt.Errorf("failed to generate configuration: %w", err)
}
cfgName := fmt.Sprintf("%s.yml", host)
configs[cfgName] = b.String()
items = append(items, &kubedef.SpecExtension_Volume_ConfigMap_Item{
Key: cfgName,
Path: fmt.Sprintf("provisioning/datasources/%s", cfgName),
})
}
out.Invocations = append(out.Invocations, kubedef.Apply{
Description: "Grafana ConfigMap",
SetNamespace: kr.CanSetNamespace,
Resource: corev1.ConfigMap(configMapName, kr.Namespace).WithData(configs),
})
out.Extensions = append(out.Extensions, kubedef.ExtendSpec{
With: &kubedef.SpecExtension{
Volume: []*kubedef.SpecExtension_Volume{{
Name: volumeName, // XXX generate unique names.
VolumeType: &kubedef.SpecExtension_Volume_ConfigMap_{
ConfigMap: &kubedef.SpecExtension_Volume_ConfigMap{
Name: configMapName,
Item: items,
},
},
}},
},
})
out.Extensions = append(out.Extensions, kubedef.ExtendContainer{
With: &kubedef.ContainerExtension{
VolumeMount: []*kubedef.ContainerExtension_VolumeMount{{
Name: volumeName,
ReadOnly: true,
MountPath: mountPath,
}},
},
})
return nil
}
func (tool) Delete(ctx context.Context, r provisioning.StackRequest, out *provisioning.DeleteOutput) error {
kr, err := kubetool.FromRequest(r)
if err != nil {
return err
}
out.Invocations = append(out.Invocations, kubedef.Delete{
Description: "Grafana ConfigMap",
Resource: "configmaps",
SetNamespace: kr.CanSetNamespace,
Namespace: kr.Namespace,
Name: configMapName,
})
return nil
}
type promTmplArgs struct {
Host string
Port int32
}
type iniTmplArgs struct {
MountPath string
DashboardPath string
}
var (
promTmpl = template.Must(template.New("prometheus.yml").Parse(`apiVersion: 1
datasources:
- name: Prometheus
type: prometheus
access: proxy
orgId: 1
url: http://{{.Host}}:{{.Port}}
isDefault: true
version: 1
editable: false
`))
iniTmpl = template.Must(template.New("grafana.ini").Parse(`[auth]
disable_login_form = false
[dashboards]
default_home_dashboard_path = {{.MountPath}}/{{.DashboardPath}}
`))
)