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
/
loki.go
83 lines (71 loc) · 2.66 KB
/
loki.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
package tools
import (
"encoding/json"
"fmt"
"github.com/KubeOperator/KubeOperator/pkg/constant"
"github.com/KubeOperator/KubeOperator/pkg/model"
)
type Loki struct {
Cluster *Cluster
Tool *model.ClusterTool
LocalHostName string
LocalRepositoryPort int
}
func NewLoki(cluster *Cluster, tool *model.ClusterTool) (*Loki, error) {
p := &Loki{
Tool: tool,
Cluster: cluster,
LocalHostName: constant.LocalRepositoryDomainName,
LocalRepositoryPort: cluster.helmRepoPort,
}
return p, nil
}
func (l Loki) setDefaultValue(toolDetail model.ClusterToolDetail, isInstall bool) {
imageMap := map[string]interface{}{}
_ = json.Unmarshal([]byte(toolDetail.Vars), &imageMap)
values := map[string]interface{}{}
_ = json.Unmarshal([]byte(l.Tool.Vars), &values)
values["loki.image.repository"] = fmt.Sprintf("%s:%d/%s", l.LocalHostName, l.LocalRepositoryPort, imageMap["loki_image_name"])
values["promtail.image.repository"] = fmt.Sprintf("%s:%d/%s", l.LocalHostName, l.LocalRepositoryPort, imageMap["promtail_image_name"])
values["loki.image.tag"] = imageMap["loki_image_tag"]
values["promtail.image.tag"] = imageMap["promtail_image_tag"]
if isInstall {
if _, ok := values["loki.persistence.size"]; ok {
values["loki.persistence.size"] = fmt.Sprintf("%vGi", values["loki.persistence.size"])
}
if va, ok := values["loki.persistence.enabled"]; ok {
if hasPers, _ := va.(bool); !hasPers {
delete(values, "loki.nodeSelector.kubernetes\\.io/hostname")
}
}
}
str, _ := json.Marshal(&values)
l.Tool.Vars = string(str)
}
func (l Loki) Install(toolDetail model.ClusterToolDetail) error {
l.setDefaultValue(toolDetail, true)
if err := installChart(l.Cluster.HelmClient, l.Tool, constant.LokiChartName, toolDetail.ChartVersion); err != nil {
return err
}
ingressItem := &Ingress{
name: constant.DefaultLokiIngressName,
url: constant.DefaultLokiIngress,
service: constant.DefaultLokiServiceName,
port: 3100,
version: l.Cluster.Version,
}
if err := createRoute(l.Cluster.Namespace, ingressItem, l.Cluster.KubeClient); err != nil {
return err
}
if err := waitForStatefulSetsRunning(l.Cluster.Namespace, constant.DefaultLokiStateSetsfulName, 1, l.Cluster.KubeClient); err != nil {
return err
}
return nil
}
func (l Loki) Upgrade(toolDetail model.ClusterToolDetail) error {
l.setDefaultValue(toolDetail, false)
return upgradeChart(l.Cluster.HelmClient, l.Tool, constant.LokiChartName, toolDetail.ChartVersion)
}
func (l Loki) Uninstall() error {
return uninstall(l.Cluster.Namespace, l.Tool, constant.DefaultLokiIngressName, l.Cluster.Version, l.Cluster.HelmClient, l.Cluster.KubeClient)
}