This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
helm.go
237 lines (193 loc) · 6.38 KB
/
helm.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
package grafana
import (
"path/filepath"
"sort"
toolsetsv1beta2 "github.com/caos/orbos/internal/operator/boom/api/v1beta2"
"github.com/caos/orbos/internal/operator/boom/application/applications/grafana/admin"
"github.com/caos/orbos/internal/operator/boom/application/applications/grafana/auth"
"github.com/caos/orbos/internal/operator/boom/application/applications/grafana/config"
"github.com/caos/orbos/internal/operator/boom/application/applications/grafana/helm"
"github.com/caos/orbos/internal/operator/boom/templator/helm/chart"
"github.com/caos/orbos/internal/utils/helper"
"github.com/caos/orbos/internal/utils/kubectl"
"github.com/caos/orbos/internal/utils/kustomize"
"github.com/caos/orbos/mntr"
)
func (g *Grafana) HelmMutate(monitor mntr.Monitor, toolsetCRDSpec *toolsetsv1beta2.ToolsetSpec, resultFilePath string) error {
if toolsetCRDSpec.KubeMetricsExporter != nil && toolsetCRDSpec.KubeMetricsExporter.Deploy &&
toolsetCRDSpec.MetricsPersisting != nil && (toolsetCRDSpec.MetricsPersisting.Metrics == nil || toolsetCRDSpec.MetricsPersisting.Metrics.KubeStateMetrics) {
if err := helper.DeleteFirstResourceFromYaml(resultFilePath, "v1", "ConfigMap", "grafana-persistentvolumesusage"); err != nil {
return err
}
}
return nil
}
func (g *Grafana) HelmPreApplySteps(monitor mntr.Monitor, spec *toolsetsv1beta2.ToolsetSpec) ([]interface{}, error) {
config := config.New(spec)
folders := make([]string, 0)
for _, provider := range config.DashboardProviders {
folders = append(folders, provider.Folder)
}
outs, err := getKustomizeOutput(folders)
if err != nil {
return nil, err
}
ret := make([]interface{}, len(outs))
for k, v := range outs {
ret[k] = v
}
if spec.Monitoring != nil && spec.Monitoring.Admin != nil {
ret = append(ret, admin.GetSecrets(spec.Monitoring.Admin)...)
}
return ret, nil
}
type ProviderSorter []*helm.Provider
func (a ProviderSorter) Len() int { return len(a) }
func (a ProviderSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ProviderSorter) Less(i, j int) bool { return a[i].Name < a[j].Name }
type AlphaSorter []string
func (a AlphaSorter) Len() int { return len(a) }
func (a AlphaSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a AlphaSorter) Less(i, j int) bool { return a[i] < a[j] }
func (g *Grafana) SpecToHelmValues(monitor mntr.Monitor, toolset *toolsetsv1beta2.ToolsetSpec) interface{} {
version, err := kubectl.NewVersion().GetKubeVersion(monitor)
if err != nil {
return nil
}
values := helm.DefaultValues(g.GetImageTags())
conf := config.New(toolset)
values.KubeTargetVersionOverride = version
providers := make([]*helm.Provider, 0)
dashboards := make(map[string]string, 0)
datasources := make([]*helm.Datasource, 0)
//internal datasources
if conf.Datasources != nil {
for _, datasource := range conf.Datasources {
valuesDatasource := &helm.Datasource{
Name: datasource.Name,
Type: datasource.Type,
URL: datasource.Url,
Access: datasource.Access,
IsDefault: datasource.IsDefault,
}
datasources = append(datasources, valuesDatasource)
}
}
//internal dashboards
if conf.DashboardProviders != nil {
for _, provider := range conf.DashboardProviders {
sort.Sort(AlphaSorter(provider.ConfigMaps))
for _, configmap := range provider.ConfigMaps {
providers = append(providers, getProvider(configmap))
dashboards[configmap] = configmap
}
}
}
if len(providers) > 0 {
sort.Sort(ProviderSorter(providers))
values.Grafana.DashboardProviders = &helm.DashboardProviders{
Providers: &helm.Providersyaml{
APIVersion: 1,
Providers: providers,
},
}
values.Grafana.DashboardsConfigMaps = dashboards
}
if len(datasources) > 0 {
values.Grafana.AdditionalDataSources = datasources
}
spec := toolset.Monitoring
if spec == nil {
return values
}
if spec.Admin != nil {
values.Grafana.Admin = admin.GetConfig(spec.Admin)
}
if spec.Storage != nil {
values.Grafana.Persistence.Enabled = true
values.Grafana.Persistence.Size = spec.Storage.Size
values.Grafana.Persistence.StorageClassName = spec.Storage.StorageClass
if spec.Storage.AccessModes != nil {
values.Grafana.Persistence.AccessModes = spec.Storage.AccessModes
}
}
if spec.Network != nil && spec.Network.Domain != "" {
values.Grafana.Env["GF_SERVER_DOMAIN"] = spec.Network.Domain
if spec.Auth != nil {
if spec.Auth.Google != nil {
google, err := auth.GetGoogleAuthConfig(spec.Auth.Google)
if err == nil && google != nil {
values.Grafana.Ini.AuthGoogle = google
}
}
if spec.Auth.Github != nil {
github, err := auth.GetGithubAuthConfig(spec.Auth.Github)
if err == nil && github != nil {
values.Grafana.Ini.AuthGithub = github
}
}
if spec.Auth.Gitlab != nil {
gitlab, err := auth.GetGitlabAuthConfig(spec.Auth.Gitlab)
if err == nil && gitlab != nil {
values.Grafana.Ini.AuthGitlab = gitlab
}
}
if spec.Auth.GenericOAuth != nil {
generic, err := auth.GetGenericOAuthConfig(spec.Auth.GenericOAuth)
if err == nil && generic != nil {
values.Grafana.Ini.AuthGeneric = generic
}
}
}
}
if spec.Plugins != nil && len(spec.Plugins) > 0 {
values.Grafana.Plugins = append(values.Grafana.Plugins, spec.Plugins...)
}
if spec.NodeSelector != nil {
for k, v := range spec.NodeSelector {
values.Grafana.NodeSelector[k] = v
}
}
if spec.Tolerations != nil {
for _, tol := range spec.Tolerations {
values.Grafana.Tolerations = append(values.Grafana.Tolerations, tol)
}
}
if spec.Resources != nil {
values.Grafana.Resources = spec.Resources
}
return values
}
func getKustomizeOutput(folders []string) ([]string, error) {
ret := make([]string, len(folders))
for n, folder := range folders {
cmd, err := kustomize.New(folder)
if err != nil {
return nil, err
}
execcmd := cmd.Build()
out, err := execcmd.Output()
if err != nil {
return nil, err
}
ret[n] = string(out)
}
return ret, nil
}
func getProvider(configmapName string) *helm.Provider {
return &helm.Provider{
Name: configmapName,
Type: "file",
DisableDeletion: false,
Editable: false,
Options: map[string]string{
"path": filepath.Join("/var/lib/grafana/dashboards", configmapName),
},
}
}
func (g *Grafana) GetChartInfo() *chart.Chart {
return helm.GetChartInfo()
}
func (g *Grafana) GetImageTags() map[string]string {
return helm.GetImageTags()
}