/
thanos.go
196 lines (181 loc) · 4.76 KB
/
thanos.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
package client
import (
"errors"
"fmt"
"log"
"github.com/infracloudio/krius/pkg/helm"
k "github.com/infracloudio/krius/pkg/kubeClient"
"gopkg.in/yaml.v2"
)
var thanosChartConfiguration = &helm.Config{
Repo: "bitnami",
Name: "thanos",
URL: "https://charts.bitnami.com/bitnami",
}
func NewThanosClient(thanosCluster *Cluster) (Client, error) {
thanosConfig, err := getConfig(thanosCluster.Data, "thanos")
if err != nil {
return nil, err
}
spec, _ := yaml.Marshal(thanosConfig)
var thanos Thanos
err = yaml.Unmarshal(spec, &thanos)
if err != nil {
return nil, err
}
return &thanos, nil
}
func (t *Thanos) PreflightChecks(clusterConfig *Config, clusterName string) ([]string, error) {
thanosErrs := []string{}
if clusterConfig.Order == 2 {
receiver := Receiver{}
if (t.Receiver) == receiver {
e := fmt.Sprintf("cluster.%s: %s,", clusterName, "Receiver not set")
thanosErrs = append(thanosErrs, e)
}
}
kubeClient, err := k.GetKubeClient(t.Namespace, clusterName)
if err != nil {
return nil, err
}
if t.Install {
err = kubeClient.CreateNSIfNotExist()
if err != nil {
e := fmt.Sprintf("cluster.%s: %s,", clusterName, err)
thanosErrs = append(thanosErrs, e)
return thanosErrs, nil // don't try to create secret, if error in creating namespace
}
} else {
// check namepsace exist
err := kubeClient.CheckNamespaceExist()
if err != nil {
e := fmt.Sprintf("cluster.%s: %s,", clusterName, err)
thanosErrs = append(thanosErrs, e)
return thanosErrs, nil
}
// check release exist
helmClient, err := createHelmClientObject(clusterName, t.Namespace, false, thanosChartConfiguration)
if err != nil {
thanosErrs = append(thanosErrs, err.Error())
}
helmClient.ChartName = "thanos"
helmClient.ReleaseName = t.Name
results, err := helmClient.ListDeployedReleases()
if err != nil {
thanosErrs = append(thanosErrs, err.Error())
}
exists := false
for _, v := range results {
if v.Name == helmClient.ReleaseName {
exists = true
}
}
if !exists {
e := fmt.Sprintf("cluster.%s: release %s does't exist", clusterName, t.Name)
thanosErrs = append(thanosErrs, e)
}
}
found := false
for _, v := range clusterConfig.ObjStoreConfigslist {
if v.Name == t.ObjStoreConfig {
found = true
secretSpec, err := createSecretforObjStore(v.Type, v.Config)
if err != nil {
return nil, err
}
err = kubeClient.CreateSecret(secretSpec, t.ObjStoreConfig)
if err != nil {
e := fmt.Sprintf("cluster.%s: %s,", t.Name, err)
thanosErrs = append(thanosErrs, e)
}
break
}
}
if !found {
e := fmt.Sprintf("cluster.%s: Bucket config doesn't exist,", clusterName)
thanosErrs = append(thanosErrs, e)
}
return thanosErrs, nil
}
func (t *Thanos) InstallClient(clusterName string, targets []string, debug bool) (string, error) {
helmClient, err := createHelmClientObject(clusterName, t.Namespace, debug, thanosChartConfiguration)
if err != nil {
return "", err
}
helmClient.ChartName = "thanos"
helmClient.ReleaseName = t.Name
exist, err := helmClient.AddRepo()
if err != nil {
return "", err
}
if !exist {
err = helmClient.UpdateRepo()
if err != nil {
return "", err
}
}
if len(t.Querier.Targets) == 0 {
t.Querier.Targets = targets
}
values, err := t.createThanosValuesMap()
if err != nil {
return "", err
}
if t.Install {
_, err = helmClient.InstallChart(values)
if err != nil {
return "", err
}
if t.Receiver.Name == "" { // sidecar mode
return "", nil
}
receiveEndpoint := getReceiveEndpoint(clusterName, t.Namespace, t.Name)
if len(receiveEndpoint) > 0 {
return receiveEndpoint[0], nil
}
return "", nil
}
_, err = helmClient.UpgradeChart(values)
if err != nil {
return "", err
}
if t.Receiver.Name == "" { // sidecar mode
return "", nil
}
receiveEndpoint := getReceiveEndpoint(clusterName, t.Namespace, t.Name)
if len(receiveEndpoint) > 0 {
return receiveEndpoint[0], nil
}
return "", nil
}
func (t *Thanos) UninstallClient(clusterName string) error {
helmClient, err := createHelmClientObject(clusterName, t.Namespace, false, thanosChartConfiguration)
if err != nil {
return err
}
helmClient.ChartName = "thanos"
helmClient.ReleaseName = t.Name
helmClient.Namespace = t.Namespace
// thanos is already installed, check the release exist & mode, then uninstall the chart
results, err := helmClient.ListDeployedReleases()
if err != nil {
return errors.New("helm list error")
}
exists := false
for _, v := range results {
if v.Name == helmClient.ReleaseName {
exists = true
}
}
if exists {
_, err = helmClient.UninstallChart()
if err != nil {
log.Printf("Error uninstalling thanos: %s", err)
return err
}
} else {
errMsg := fmt.Sprintf("Release %s doesn't exist", helmClient.ReleaseName)
return errors.New(errMsg)
}
return nil
}