-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClusterService.go
471 lines (432 loc) · 15.2 KB
/
ClusterService.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* Copyright (c) 2020 Devtron Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package cluster
import (
"context"
"fmt"
"io/ioutil"
"os"
"time"
bean2 "github.com/devtron-labs/template-cron-job/api/bean"
"github.com/devtron-labs/template-cron-job/client/k8s/informer"
"github.com/devtron-labs/template-cron-job/internal/constants"
"github.com/devtron-labs/template-cron-job/internal/util"
"github.com/devtron-labs/template-cron-job/pkg/cluster/repository"
util2 "github.com/devtron-labs/template-cron-job/util"
"github.com/go-pg/pg"
"go.uber.org/zap"
)
type ClusterBean struct {
Id int `json:"id,omitempty" validate:"number"`
ClusterName string `json:"cluster_name,omitempty" validate:"required"`
ServerUrl string `json:"server_url,omitempty" validate:"url,required"`
PrometheusUrl string `json:"prometheus_url,omitempty" validate:"validate-non-empty-url"`
Active bool `json:"active"`
Config map[string]string `json:"config,omitempty" validate:"required"`
PrometheusAuth *PrometheusAuth `json:"prometheusAuth,omitempty"`
DefaultClusterComponent []*DefaultClusterComponent `json:"defaultClusterComponent"`
AgentInstallationStage int `json:"agentInstallationStage,notnull"` // -1=external, 0=not triggered, 1=progressing, 2=success, 3=fails
K8sVersion string `json:"k8sVersion"`
HasConfigOrUrlChanged bool `json:"-"`
}
type PrometheusAuth struct {
UserName string `json:"userName,omitempty"`
Password string `json:"password,omitempty"`
TlsClientCert string `json:"tlsClientCert,omitempty"`
TlsClientKey string `json:"tlsClientKey,omitempty"`
}
type DefaultClusterComponent struct {
ComponentName string `json:"name"`
AppId int `json:"appId"`
InstalledAppId int `json:"installedAppId,omitempty"`
EnvId int `json:"envId"`
EnvName string `json:"envName"`
Status string `json:"status"`
}
type ClusterService interface {
Save(parent context.Context, bean *ClusterBean, userId int32) (*ClusterBean, error)
FindOne(clusterName string) (*ClusterBean, error)
FindOneActive(clusterName string) (*ClusterBean, error)
FindAll() ([]*ClusterBean, error)
FindAllActive() ([]ClusterBean, error)
DeleteFromDb(bean *ClusterBean, userId int32) error
FindById(id int) (*ClusterBean, error)
FindByIds(id []int) ([]ClusterBean, error)
Update(ctx context.Context, bean *ClusterBean, userId int32) (*ClusterBean, error)
Delete(bean *ClusterBean, userId int32) error
FindAllForAutoComplete() ([]ClusterBean, error)
CreateGrafanaDataSource(clusterBean *ClusterBean, env *repository.Environment) (int, error)
GetClusterConfig(cluster *ClusterBean) (*util.ClusterConfig, error)
}
type ClusterServiceImpl struct {
clusterRepository repository.ClusterRepository
logger *zap.SugaredLogger
K8sUtil *util.K8sUtil
K8sInformerFactory informer.K8sInformerFactory
}
func NewClusterServiceImpl(repository repository.ClusterRepository, logger *zap.SugaredLogger,
K8sUtil *util.K8sUtil, K8sInformerFactory informer.K8sInformerFactory) *ClusterServiceImpl {
clusterService := &ClusterServiceImpl{
clusterRepository: repository,
logger: logger,
K8sUtil: K8sUtil,
K8sInformerFactory: K8sInformerFactory,
}
//go clusterService.buildInformer()
return clusterService
}
const DefaultClusterName = "default_cluster"
const TokenFilePath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
func (impl *ClusterServiceImpl) GetClusterConfig(cluster *ClusterBean) (*util.ClusterConfig, error) {
host := cluster.ServerUrl
configMap := cluster.Config
bearerToken := configMap["bearer_token"]
if cluster.Id == 1 && cluster.ClusterName == DefaultClusterName {
if _, err := os.Stat(TokenFilePath); os.IsNotExist(err) {
impl.logger.Errorw("no directory or file exists", "TOKEN_FILE_PATH", TokenFilePath, "err", err)
return nil, err
} else {
content, err := ioutil.ReadFile(TokenFilePath)
if err != nil {
impl.logger.Errorw("error on reading file", "err", err)
return nil, err
}
bearerToken = string(content)
}
}
clusterCfg := &util.ClusterConfig{Host: host, BearerToken: bearerToken}
return clusterCfg, nil
}
func (impl *ClusterServiceImpl) Save(parent context.Context, bean *ClusterBean, userId int32) (*ClusterBean, error) {
existingModel, err := impl.clusterRepository.FindOne(bean.ClusterName)
if err != nil && err != pg.ErrNoRows {
impl.logger.Error(err)
return nil, err
}
if existingModel.Id > 0 {
impl.logger.Errorw("error on fetching cluster, duplicate", "name", bean.ClusterName)
return nil, fmt.Errorf("cluster already exists")
}
model := &repository.Cluster{
ClusterName: bean.ClusterName,
Active: bean.Active,
ServerUrl: bean.ServerUrl,
Config: bean.Config,
PrometheusEndpoint: bean.PrometheusUrl,
}
if bean.PrometheusAuth != nil {
model.PUserName = bean.PrometheusAuth.UserName
model.PPassword = bean.PrometheusAuth.Password
model.PTlsClientCert = bean.PrometheusAuth.TlsClientCert
model.PTlsClientKey = bean.PrometheusAuth.TlsClientKey
}
model.CreatedBy = userId
model.UpdatedBy = userId
model.CreatedOn = time.Now()
model.UpdatedOn = time.Now()
cfg, err := impl.GetClusterConfig(bean)
if err != nil {
return nil, err
}
client, err := impl.K8sUtil.GetK8sDiscoveryClient(cfg)
if err != nil {
return nil, err
}
k8sServerVersion, err := client.ServerVersion()
if err != nil {
return nil, err
}
model.K8sVersion = k8sServerVersion.String()
err = impl.clusterRepository.Save(model)
if err != nil {
impl.logger.Errorw("error in saving cluster in db", "err", err)
err = &util.ApiError{
Code: constants.ClusterCreateDBFailed,
InternalMessage: "cluster creation failed in db",
UserMessage: fmt.Sprintf("requested by %d", userId),
}
}
bean.Id = model.Id
//on successful creation of new cluster, update informer cache for namespace group by cluster
//here sync for ea mode only
if util2.GetDevtronVersion().ServerMode != "FULL" {
impl.SyncNsInformer(bean)
}
return bean, err
}
func (impl *ClusterServiceImpl) FindOne(clusterName string) (*ClusterBean, error) {
model, err := impl.clusterRepository.FindOne(clusterName)
if err != nil {
return nil, err
}
bean := &ClusterBean{
Id: model.Id,
ClusterName: model.ClusterName,
ServerUrl: model.ServerUrl,
PrometheusUrl: model.PrometheusEndpoint,
AgentInstallationStage: model.AgentInstallationStage,
Active: model.Active,
Config: model.Config,
K8sVersion: model.K8sVersion,
}
return bean, nil
}
func (impl *ClusterServiceImpl) FindOneActive(clusterName string) (*ClusterBean, error) {
model, err := impl.clusterRepository.FindOneActive(clusterName)
if err != nil {
return nil, err
}
bean := &ClusterBean{
Id: model.Id,
ClusterName: model.ClusterName,
ServerUrl: model.ServerUrl,
PrometheusUrl: model.PrometheusEndpoint,
AgentInstallationStage: model.AgentInstallationStage,
Active: model.Active,
Config: model.Config,
K8sVersion: model.K8sVersion,
}
return bean, nil
}
func (impl *ClusterServiceImpl) FindAll() ([]*ClusterBean, error) {
model, err := impl.clusterRepository.FindAllActive()
if err != nil {
return nil, err
}
var beans []*ClusterBean
for _, m := range model {
beans = append(beans, &ClusterBean{
Id: m.Id,
ClusterName: m.ClusterName,
PrometheusUrl: m.PrometheusEndpoint,
AgentInstallationStage: m.AgentInstallationStage,
ServerUrl: m.ServerUrl,
Active: m.Active,
K8sVersion: m.K8sVersion,
})
}
return beans, nil
}
func (impl *ClusterServiceImpl) FindAllActive() ([]ClusterBean, error) {
model, err := impl.clusterRepository.FindAllActive()
if err != nil {
return nil, err
}
var beans []ClusterBean
for _, m := range model {
beans = append(beans, ClusterBean{
Id: m.Id,
ClusterName: m.ClusterName,
ServerUrl: m.ServerUrl,
Active: m.Active,
PrometheusUrl: m.PrometheusEndpoint,
AgentInstallationStage: m.AgentInstallationStage,
Config: m.Config,
K8sVersion: m.K8sVersion,
})
}
return beans, nil
}
func (impl *ClusterServiceImpl) FindById(id int) (*ClusterBean, error) {
model, err := impl.clusterRepository.FindById(id)
if err != nil {
return nil, err
}
bean := &ClusterBean{
Id: model.Id,
ClusterName: model.ClusterName,
ServerUrl: model.ServerUrl,
PrometheusUrl: model.PrometheusEndpoint,
AgentInstallationStage: model.AgentInstallationStage,
Active: model.Active,
Config: model.Config,
K8sVersion: model.K8sVersion,
}
prometheusAuth := &PrometheusAuth{
UserName: model.PUserName,
Password: model.PPassword,
TlsClientCert: model.PTlsClientCert,
TlsClientKey: model.PTlsClientKey,
}
bean.PrometheusAuth = prometheusAuth
return bean, nil
}
func (impl *ClusterServiceImpl) FindByIds(ids []int) ([]ClusterBean, error) {
models, err := impl.clusterRepository.FindByIds(ids)
if err != nil {
return nil, err
}
var beans []ClusterBean
for _, model := range models {
beans = append(beans, ClusterBean{
Id: model.Id,
ClusterName: model.ClusterName,
ServerUrl: model.ServerUrl,
PrometheusUrl: model.PrometheusEndpoint,
AgentInstallationStage: model.AgentInstallationStage,
Active: model.Active,
Config: model.Config,
K8sVersion: model.K8sVersion,
})
}
return beans, nil
}
func (impl *ClusterServiceImpl) Update(ctx context.Context, bean *ClusterBean, userId int32) (*ClusterBean, error) {
model, err := impl.clusterRepository.FindById(bean.Id)
if err != nil {
impl.logger.Error(err)
return nil, err
}
existingModel, err := impl.clusterRepository.FindOne(bean.ClusterName)
if err != nil && err != pg.ErrNoRows {
impl.logger.Error(err)
return nil, err
}
if existingModel.Id > 0 && model.Id != existingModel.Id {
impl.logger.Errorw("error on fetching cluster, duplicate", "name", bean.ClusterName)
return nil, fmt.Errorf("cluster already exists")
}
// check whether config modified or not, if yes create informer with updated config
dbConfig := model.Config["bearer_token"]
requestConfig := bean.Config["bearer_token"]
if bean.ServerUrl != model.ServerUrl || dbConfig != requestConfig {
bean.HasConfigOrUrlChanged = true
}
model.ClusterName = bean.ClusterName
model.ServerUrl = bean.ServerUrl
model.PrometheusEndpoint = bean.PrometheusUrl
if bean.PrometheusAuth != nil {
if bean.PrometheusAuth.UserName != "" {
model.PUserName = bean.PrometheusAuth.UserName
}
if bean.PrometheusAuth.Password != "" {
model.PPassword = bean.PrometheusAuth.Password
}
if bean.PrometheusAuth.TlsClientCert != "" {
model.PTlsClientCert = bean.PrometheusAuth.TlsClientCert
}
if bean.PrometheusAuth.TlsClientKey != "" {
model.PTlsClientKey = bean.PrometheusAuth.TlsClientKey
}
}
model.Active = bean.Active
model.Config = bean.Config
model.UpdatedBy = userId
model.UpdatedOn = time.Now()
if model.K8sVersion == "" {
cfg, err := impl.GetClusterConfig(bean)
if err != nil {
return nil, err
}
client, err := impl.K8sUtil.GetK8sDiscoveryClient(cfg)
if err != nil {
return nil, err
}
k8sServerVersion, err := client.ServerVersion()
if err != nil {
return nil, err
}
model.K8sVersion = k8sServerVersion.String()
}
err = impl.clusterRepository.Update(model)
if err != nil {
err = &util.ApiError{
Code: constants.ClusterUpdateDBFailed,
InternalMessage: "cluster update failed in db",
UserMessage: fmt.Sprintf("requested by %d", userId),
}
return bean, err
}
bean.Id = model.Id
//here sync for ea mode only
if bean.HasConfigOrUrlChanged && util2.GetDevtronVersion().ServerMode != "FULL" {
impl.SyncNsInformer(bean)
}
return bean, err
}
func (impl *ClusterServiceImpl) SyncNsInformer(bean *ClusterBean) {
requestConfig := bean.Config["bearer_token"]
//before creating new informer for cluster, close existing one
impl.K8sInformerFactory.CleanNamespaceInformer(bean.ClusterName)
//create new informer for cluster with new config
clusterInfo := &bean2.ClusterInfo{
ClusterId: bean.Id,
ClusterName: bean.ClusterName,
BearerToken: requestConfig,
ServerUrl: bean.ServerUrl,
}
impl.K8sInformerFactory.BuildInformer([]*bean2.ClusterInfo{clusterInfo})
}
func (impl *ClusterServiceImpl) Delete(bean *ClusterBean, userId int32) error {
model, err := impl.clusterRepository.FindById(bean.Id)
if err != nil {
return err
}
return impl.clusterRepository.Delete(model)
}
func (impl *ClusterServiceImpl) FindAllForAutoComplete() ([]ClusterBean, error) {
model, err := impl.clusterRepository.FindAll()
if err != nil {
return nil, err
}
var beans []ClusterBean
for _, m := range model {
beans = append(beans, ClusterBean{
Id: m.Id,
ClusterName: m.ClusterName,
})
}
return beans, nil
}
func (impl *ClusterServiceImpl) CreateGrafanaDataSource(clusterBean *ClusterBean, env *repository.Environment) (int, error) {
impl.logger.Errorw("CreateGrafanaDataSource not inplementd in ClusterServiceImpl")
return 0, fmt.Errorf("method not implemented")
}
func (impl *ClusterServiceImpl) buildInformer() {
models, err := impl.clusterRepository.FindAllActive()
if err != nil {
impl.logger.Errorw("error in fetching clusters", "err", err)
return
}
var clusterInfo []*bean2.ClusterInfo
for _, model := range models {
bearerToken := model.Config["bearer_token"]
clusterInfo = append(clusterInfo, &bean2.ClusterInfo{
ClusterId: model.Id,
ClusterName: model.ClusterName,
BearerToken: bearerToken,
ServerUrl: model.ServerUrl,
})
}
impl.K8sInformerFactory.BuildInformer(clusterInfo)
}
func (impl ClusterServiceImpl) DeleteFromDb(bean *ClusterBean, userId int32) error {
existingCluster, err := impl.clusterRepository.FindById(bean.Id)
if err != nil {
impl.logger.Errorw("No matching entry found for delete.", "id", bean.Id)
return err
}
deleteReq := existingCluster
deleteReq.UpdatedOn = time.Now()
deleteReq.UpdatedBy = userId
err = impl.clusterRepository.MarkClusterDeleted(deleteReq)
if err != nil {
impl.logger.Errorw("error in deleting cluster", "id", bean.Id, "err", err)
return err
}
return nil
}