This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 875
/
grafana.go
118 lines (102 loc) · 4.79 KB
/
grafana.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
package tools
import (
"encoding/json"
"fmt"
"github.com/KubeOperator/KubeOperator/pkg/constant"
"github.com/KubeOperator/KubeOperator/pkg/model"
)
type Grafana struct {
Cluster *Cluster
Tool *model.ClusterTool
LocalHostName string
LocalRepositoryPort int
prometheusNs string
lokiNs string
}
func NewGrafana(cluster *Cluster, tool *model.ClusterTool, prometheusNs, lokiNs string) (*Grafana, error) {
p := &Grafana{
Tool: tool,
Cluster: cluster,
LocalHostName: constant.LocalRepositoryDomainName,
LocalRepositoryPort: cluster.helmRepoPort,
prometheusNs: prometheusNs,
lokiNs: lokiNs,
}
return p, nil
}
func (g Grafana) setDefaultValue(toolDetail model.ClusterToolDetail, isInstall bool) {
imageMap := map[string]interface{}{}
_ = json.Unmarshal([]byte(toolDetail.Vars), &imageMap)
values := map[string]interface{}{}
_ = json.Unmarshal([]byte(g.Tool.Vars), &values)
values["image.repository"] = fmt.Sprintf("%s:%d/%s", g.LocalHostName, g.LocalRepositoryPort, imageMap["grafana_image_name"])
values["image.tag"] = imageMap["grafana_image_tag"]
values["initChownData.enabled"] = true
values["initChownData.image.repository"] = fmt.Sprintf("%s:%d/%s", g.LocalHostName, g.LocalRepositoryPort, imageMap["busybox_image_name"])
values["initChownData.image.tag"] = imageMap["busybox_image_tag"]
values["downloadDashboardsImage.repository"] = fmt.Sprintf("%s:%d/%s", g.LocalHostName, g.LocalRepositoryPort, imageMap["curl_image_name"])
values["downloadDashboardsImage.tag"] = imageMap["curl_image_tag"]
if isInstall {
values["grafana\\.ini.server.root_url"] = "%(protocol)s://%(domain)s:%(http_port)s/proxy/grafana/" + g.Cluster.Name + "/"
values["grafana\\.ini.server.serve_from_sub_path"] = true
values["datasources.datasources\\.yaml.apiVersion"] = 1
if len(g.prometheusNs) != 0 {
values["datasources.datasources\\.yaml.datasources[0].name"] = "MYDS_Prometheus"
values["datasources.datasources\\.yaml.datasources[0].type"] = "prometheus"
values["datasources.datasources\\.yaml.datasources[0].url"] = "http://prometheus-server." + g.prometheusNs
values["datasources.datasources\\.yaml.datasources[0].access"] = "proxy"
values["datasources.datasources\\.yaml.datasources[0].isDefault"] = true
}
if len(g.lokiNs) != 0 {
values["datasources.datasources\\.yaml.datasources[1].name"] = "Loki"
values["datasources.datasources\\.yaml.datasources[1].type"] = "loki"
values["datasources.datasources\\.yaml.datasources[1].url"] = "http://loki." + g.lokiNs + ":3100"
values["datasources.datasources\\.yaml.datasources[1].access"] = "proxy"
}
values["dashboardProviders.dashboardproviders\\.yaml.apiVersion"] = 1
values["dashboardProviders.dashboardproviders\\.yaml.providers[0].name"] = "default"
values["dashboardProviders.dashboardproviders\\.yaml.providers[0].orgId"] = 1
values["dashboardProviders.dashboardproviders\\.yaml.providers[0].type"] = "file"
values["dashboardProviders.dashboardproviders\\.yaml.providers[0].disableDeletion"] = false
values["dashboardProviders.dashboardproviders\\.yaml.providers[0].editable"] = true
values["dashboardProviders.dashboardproviders\\.yaml.providers[0].options.path"] = "/var/lib/grafana/dashboards/default"
values["dashboards.default.custom-dashboard.file"] = "dashboards/custom-dashboard.json"
if _, ok := values["persistence.size"]; ok {
values["persistence.size"] = fmt.Sprintf("%vGi", values["persistence.size"])
}
if va, ok := values["persistence.enabled"]; ok {
if hasPers, _ := va.(bool); !hasPers {
delete(values, "nodeSelector.kubernetes\\.io/hostname")
}
}
}
str, _ := json.Marshal(&values)
g.Tool.Vars = string(str)
}
func (g Grafana) Install(toolDetail model.ClusterToolDetail) error {
g.setDefaultValue(toolDetail, true)
if err := installChart(g.Cluster.HelmClient, g.Tool, constant.GrafanaChartName, toolDetail.ChartVersion); err != nil {
return err
}
ingressItem := &Ingress{
name: constant.DefaultGrafanaIngressName,
url: constant.DefaultGrafanaIngress,
service: constant.DefaultGrafanaServiceName,
port: 80,
version: g.Cluster.Version,
}
if err := createRoute(g.Cluster.Namespace, ingressItem, g.Cluster.KubeClient); err != nil {
return err
}
if err := waitForRunning(g.Cluster.Namespace, constant.DefaultGrafanaDeploymentName, 1, g.Cluster.KubeClient); err != nil {
return err
}
return nil
}
func (g Grafana) Upgrade(toolDetail model.ClusterToolDetail) error {
g.setDefaultValue(toolDetail, false)
return upgradeChart(g.Cluster.HelmClient, g.Tool, constant.GrafanaChartName, toolDetail.ChartVersion)
}
func (g Grafana) Uninstall() error {
return uninstall(g.Cluster.Namespace, g.Tool, constant.DefaultGrafanaIngressName, g.Cluster.Version, g.Cluster.HelmClient, g.Cluster.KubeClient)
}