-
Notifications
You must be signed in to change notification settings - Fork 527
/
Copy pathEnvironmentService.go
754 lines (707 loc) · 28.7 KB
/
EnvironmentService.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
* Copyright (c) 2020-2024. Devtron Inc.
*
* 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 environment
import (
"encoding/json"
"fmt"
"github.com/devtron-labs/devtron/client/grafana"
bean3 "github.com/devtron-labs/devtron/pkg/attributes/bean"
"github.com/devtron-labs/devtron/pkg/cluster"
bean4 "github.com/devtron-labs/devtron/pkg/cluster/bean"
adapter2 "github.com/devtron-labs/devtron/pkg/cluster/environment/adapter"
bean2 "github.com/devtron-labs/devtron/pkg/cluster/environment/bean"
"github.com/devtron-labs/devtron/pkg/cluster/environment/repository"
"github.com/devtron-labs/devtron/pkg/cluster/read"
"strconv"
"strings"
"sync"
"time"
util2 "github.com/devtron-labs/common-lib/utils/k8s"
repository2 "github.com/devtron-labs/devtron/internal/sql/repository"
"github.com/devtron-labs/devtron/pkg/auth/user"
"github.com/devtron-labs/devtron/pkg/auth/user/bean"
"github.com/devtron-labs/devtron/pkg/k8s/informer"
"github.com/devtron-labs/devtron/internal/util"
"github.com/go-pg/pg"
"github.com/pkg/errors"
"go.uber.org/zap"
)
type EnvironmentService interface {
FindOne(environment string) (*bean2.EnvironmentBean, error)
GetDataSourceName(*bean2.EnvironmentBean) (dataSourceData *bean2.DataSourceMetaData, err error)
Create(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error)
Update(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error)
GetAllActive() ([]bean2.EnvironmentBean, error)
GetAllActiveEnvironmentCount() (int, error)
Delete(deleteReq *bean2.EnvironmentBean, userId int32) error
FindClusterByEnvId(id int) (*bean4.ClusterBean, error)
// FindById provides an exposed struct of bean.EnvironmentBean;
// Usage - can be used for API responses;
FindById(id int) (*bean2.EnvironmentBean, error)
// GetExtendedEnvBeanById is used internally to get combined env and cluster data in bean.EnvironmentBean;
// Usage - used for intra-service communications only;
// For exposed view use FindById instead
GetExtendedEnvBeanById(id int) (*bean2.EnvironmentBean, error)
GetEnvironmentListForAutocomplete(isDeploymentTypeParam bool) ([]bean2.EnvironmentBean, error)
GetEnvironmentOnlyListForAutocomplete() ([]bean2.EnvironmentBean, error)
FindByIds(ids []*int) ([]*bean2.EnvironmentBean, error)
FindByNamespaceAndClusterName(namespaces string, clusterName string) (*repository.Environment, error)
FindOneByNamespaceAndClusterId(namespaces string, clusterId int) (*bean2.EnvironmentBean, error)
GetByClusterId(id int) ([]*bean2.EnvironmentBean, error)
GetCombinedEnvironmentListForDropDown(token string, isActionUserSuperAdmin bool, auth func(email string, object []string) map[string]bool) ([]*bean2.ClusterEnvDto, error)
GetCombinedEnvironmentListForDropDownByClusterIds(token string, clusterIds []int, auth func(token string, object string) bool) ([]*bean2.ClusterEnvDto, error)
HandleErrorInClusterConnections(clusters []*bean4.ClusterBean, respMap *sync.Map, clusterExistInDb bool)
GetDetailsById(envId int) (*repository.Environment, error)
}
type EnvironmentServiceImpl struct {
environmentRepository repository.EnvironmentRepository
logger *zap.SugaredLogger
clusterService cluster.ClusterService
K8sUtil *util2.K8sServiceImpl
k8sInformerFactory informer.K8sInformerFactory
//propertiesConfigService pipeline.PropertiesConfigService
userAuthService user.UserAuthService
attributesRepository repository2.AttributesRepository
clusterReadService read.ClusterReadService
grafanaClient grafana.GrafanaClient
}
func NewEnvironmentServiceImpl(environmentRepository repository.EnvironmentRepository,
clusterService cluster.ClusterService, logger *zap.SugaredLogger,
K8sUtil *util2.K8sServiceImpl, k8sInformerFactory informer.K8sInformerFactory,
// propertiesConfigService pipeline.PropertiesConfigService,
userAuthService user.UserAuthService, attributesRepository repository2.AttributesRepository,
clusterReadService read.ClusterReadService,
grafanaClient grafana.GrafanaClient) *EnvironmentServiceImpl {
return &EnvironmentServiceImpl{
environmentRepository: environmentRepository,
logger: logger,
clusterService: clusterService,
K8sUtil: K8sUtil,
k8sInformerFactory: k8sInformerFactory,
//propertiesConfigService: propertiesConfigService,
userAuthService: userAuthService,
attributesRepository: attributesRepository,
clusterReadService: clusterReadService,
grafanaClient: grafanaClient,
}
}
func (impl EnvironmentServiceImpl) GetDataSourceName(bean *bean2.EnvironmentBean) (*bean2.DataSourceMetaData, error) {
datasource := &bean2.DataSourceMetaData{}
if bean.DataSourceId == 0 || bean.PrometheusEndpoint == "" {
impl.logger.Debugw("grafana data source not configured for given", "dataSourceId", bean.DataSourceId)
return datasource, nil
} else {
impl.logger.Debugw("environment datasource", "datasource", bean.DataSourceId)
data, err := impl.grafanaClient.GetDatasource(bean.DataSourceId)
if err != nil {
impl.logger.Errorw("error in fetching datasource", "err", err)
return datasource, err
}
datasource.Name = data.Name
datasource.Id = bean.DataSourceId
return datasource, nil
}
}
func (impl EnvironmentServiceImpl) Create(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error) {
existingEnvs, err := impl.environmentRepository.FindByClusterId(mappings.ClusterId)
if err != nil && !util.IsErrNoRows(err) {
impl.logger.Errorw("error while fetch", "err", err)
return nil, err
}
err = impl.validateNamespaces(mappings.Namespace, existingEnvs)
if err != nil {
return nil, err
}
clusterBean, err := impl.clusterReadService.FindById(mappings.ClusterId)
if err != nil {
return nil, err
}
identifier := clusterBean.ClusterName + "__" + mappings.Namespace
model, err := impl.environmentRepository.FindByEnvNameOrIdentifierOrNamespace(mappings.ClusterId, mappings.Environment, identifier, mappings.Namespace)
if err != nil && err != pg.ErrNoRows {
impl.logger.Errorw("error in finding environment for update", "err", err)
return mappings, err
}
if model.Id > 0 {
impl.logger.Warnw("environment already exists for this cluster and namespace", "model", model)
return mappings, fmt.Errorf("environment already exists")
}
model = &repository.Environment{
Name: mappings.Environment,
ClusterId: mappings.ClusterId,
Active: mappings.Active,
Namespace: mappings.Namespace,
Default: mappings.Default,
Description: mappings.Description,
EnvironmentIdentifier: identifier,
}
model.CreatedBy = userId
model.UpdatedBy = userId
model.CreatedOn = time.Now()
model.UpdatedOn = time.Now()
err = impl.environmentRepository.Create(model)
if err != nil {
impl.logger.Errorw("error in saving environment", "err", err)
return mappings, err
}
if len(model.Namespace) > 0 {
cfg := clusterBean.GetClusterConfig()
if _, _, err := impl.K8sUtil.CreateNsIfNotExists(model.Namespace, cfg); err != nil {
impl.logger.Errorw("error in creating ns", "ns", model.Namespace, "err", err)
}
}
//ignore grafana if no prometheus url found
if len(clusterBean.PrometheusUrl) > 0 {
_, err = impl.clusterService.CreateGrafanaDataSource(clusterBean, model)
if err != nil {
impl.logger.Errorw("unable to create grafana data source", "env", model)
}
}
mappings.Id = model.Id
mappings.ClusterServerUrl = clusterBean.ServerUrl
return mappings, nil
}
func (impl EnvironmentServiceImpl) FindOne(environment string) (*bean2.EnvironmentBean, error) {
model, err := impl.environmentRepository.FindOne(environment)
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
return nil, err
}
bean := &bean2.EnvironmentBean{
Id: model.Id,
Environment: model.Name,
ClusterId: model.Cluster.Id,
Active: model.Active,
PrometheusEndpoint: model.Cluster.PrometheusEndpoint,
Namespace: model.Namespace,
Default: model.Default,
EnvironmentIdentifier: model.EnvironmentIdentifier,
Description: model.Description,
DataSourceId: model.GrafanaDatasourceId,
}
return bean, nil
}
func (impl EnvironmentServiceImpl) GetAllActive() ([]bean2.EnvironmentBean, error) {
models, err := impl.environmentRepository.FindAllActive()
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
}
var beans []bean2.EnvironmentBean
for _, model := range models {
beans = append(beans, bean2.EnvironmentBean{
Id: model.Id,
Environment: model.Name,
ClusterId: model.Cluster.Id,
ClusterName: model.Cluster.ClusterName,
Active: model.Active,
PrometheusEndpoint: model.Cluster.PrometheusEndpoint,
Namespace: model.Namespace,
Default: model.Default,
EnvironmentIdentifier: model.EnvironmentIdentifier,
Description: model.Description,
IsVirtualEnvironment: model.IsVirtualEnvironment,
})
}
return beans, nil
}
func (impl EnvironmentServiceImpl) GetAllActiveEnvironmentCount() (int, error) {
cnt, err := impl.environmentRepository.FindAllActiveEnvironmentCount()
return cnt, err
}
func (impl EnvironmentServiceImpl) FindById(id int) (*bean2.EnvironmentBean, error) {
model, err := impl.environmentRepository.FindById(id)
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
return nil, err
}
bean := &bean2.EnvironmentBean{
Id: model.Id,
Environment: model.Name,
ClusterId: model.Cluster.Id,
Active: model.Active,
PrometheusEndpoint: model.Cluster.PrometheusEndpoint,
Namespace: model.Namespace,
Default: model.Default,
EnvironmentIdentifier: model.EnvironmentIdentifier,
Description: model.Description,
IsVirtualEnvironment: model.IsVirtualEnvironment,
}
return bean, nil
}
func (impl EnvironmentServiceImpl) Update(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error) {
model, err := impl.environmentRepository.FindById(mappings.Id)
if err != nil {
impl.logger.Errorw("error in finding environment for update", "err", err)
return mappings, err
}
/*isNamespaceChange := false
if model.Namespace != mappings.Namespace {
isNamespaceChange = true
}*/
clusterBean, err := impl.clusterReadService.FindById(mappings.ClusterId)
if err != nil {
return nil, err
}
model.Name = mappings.Environment
model.Active = mappings.Active
model.Namespace = mappings.Namespace
model.Default = mappings.Default
model.UpdatedBy = userId
model.UpdatedOn = time.Now()
model.Description = mappings.Description
//namespace create if not exist
if len(model.Namespace) > 0 {
cfg := clusterBean.GetClusterConfig()
if _, _, err := impl.K8sUtil.CreateNsIfNotExists(model.Namespace, cfg); err != nil {
impl.logger.Errorw("error in creating ns", "ns", model.Namespace, "err", err)
}
}
//namespace changed, update it on chart env override config as well
/*if isNamespaceChange == true {
impl.logger.Debug("namespace has modified in request, it will update related config")
envPropertiesList, err := impl.propertiesConfigService.GetEnvironmentPropertiesById(mappings.Id)
if err != nil {
impl.logger.Error("failed to fetch chart environment override config", "err", err)
//TODO - atomic operation breaks, throw internal error codes
} else {
for _, envProperties := range envPropertiesList {
_, err := impl.propertiesConfigService.UpdateEnvironmentProperties(0, &pipeline.EnvironmentProperties{Id: envProperties.Id, Namespace: mappings.Namespace}, userId)
if err != nil {
impl.logger.Error("failed to update chart environment override config", "err", err)
//TODO - atomic operation breaks, throw internal error codes
}
}
}
}*/
grafanaDatasourceId := model.GrafanaDatasourceId
//grafana datasource create if not exist
if len(clusterBean.PrometheusUrl) > 0 && grafanaDatasourceId == 0 {
grafanaDatasourceId, err = impl.clusterService.CreateGrafanaDataSource(clusterBean, model)
if err != nil {
impl.logger.Errorw("unable to create grafana data source for missing env", "env", model)
}
}
model.GrafanaDatasourceId = grafanaDatasourceId
err = impl.environmentRepository.Update(model)
if err != nil {
impl.logger.Errorw("error in updating environment", "err", err)
return mappings, err
}
mappings.Id = model.Id
return mappings, nil
}
func (impl EnvironmentServiceImpl) GetExtendedEnvBeanById(id int) (*bean2.EnvironmentBean, error) {
model, err := impl.environmentRepository.FindById(id)
if err != nil {
impl.logger.Errorw("error in fetch environment by id", "err", err)
return nil, err
}
return adapter2.NewEnvironmentBean(model), nil
}
func (impl EnvironmentServiceImpl) FindClusterByEnvId(id int) (*bean4.ClusterBean, error) {
model, err := impl.environmentRepository.FindById(id)
if err != nil {
impl.logger.Errorw("fetch cluster by environment id", "err", err)
return nil, err
}
clusterBean := &bean4.ClusterBean{}
clusterBean.Id = model.Cluster.Id
clusterBean.ClusterName = model.Cluster.ClusterName
clusterBean.Active = model.Cluster.Active
clusterBean.ServerUrl = model.Cluster.ServerUrl
clusterBean.Config = model.Cluster.Config
return clusterBean, nil
}
var permittedDeploymentConfigString = []string{bean2.PIPELINE_DEPLOYMENT_TYPE_HELM, bean2.PIPELINE_DEPLOYMENT_TYPE_ACD}
func (impl EnvironmentServiceImpl) GetEnvironmentListForAutocomplete(isDeploymentTypeParam bool) ([]bean2.EnvironmentBean, error) {
models, err := impl.environmentRepository.FindAllActive()
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
}
var beans []bean2.EnvironmentBean
//Fetching deployment app type config values along with autocomplete api while creating CD pipeline
if isDeploymentTypeParam {
for _, model := range models {
var (
allowedDeploymentConfigString []string
)
deploymentConfig := make(map[string]map[string]bool)
deploymentConfigEnvLevel := make(map[string]bool)
deploymentConfigValues, _ := impl.attributesRepository.FindByKey(bean3.ENFORCE_DEPLOYMENT_TYPE_CONFIG)
//if empty config received(doesn't exist in table) which can't be parsed
if deploymentConfigValues.Value != "" {
if err = json.Unmarshal([]byte(deploymentConfigValues.Value), &deploymentConfig); err != nil {
return nil, err
}
deploymentConfigEnvLevel, _ = deploymentConfig[fmt.Sprintf("%d", model.Id)]
}
// if real config along with absurd values exist in table {"argo_cd": true, "helm": false, "absurd": false}",
if ok, filteredDeploymentConfig := impl.IsReceivedDeploymentTypeValid(deploymentConfigEnvLevel); ok {
allowedDeploymentConfigString = filteredDeploymentConfig
} else {
allowedDeploymentConfigString = permittedDeploymentConfigString
}
beans = append(beans, bean2.EnvironmentBean{
Id: model.Id,
Environment: model.Name,
Namespace: model.Namespace,
CdArgoSetup: model.Cluster.CdArgoSetup,
EnvironmentIdentifier: model.EnvironmentIdentifier,
ClusterName: model.Cluster.ClusterName,
Description: model.Description,
IsVirtualEnvironment: model.IsVirtualEnvironment,
AllowedDeploymentTypes: allowedDeploymentConfigString,
ClusterId: model.ClusterId,
})
}
} else {
for _, model := range models {
beans = append(beans, bean2.EnvironmentBean{
Id: model.Id,
Environment: model.Name,
Namespace: model.Namespace,
CdArgoSetup: model.Cluster.CdArgoSetup,
EnvironmentIdentifier: model.EnvironmentIdentifier,
ClusterName: model.Cluster.ClusterName,
Description: model.Description,
IsVirtualEnvironment: model.IsVirtualEnvironment,
ClusterId: model.ClusterId,
})
}
}
return beans, nil
}
func (impl EnvironmentServiceImpl) IsReceivedDeploymentTypeValid(deploymentConfig map[string]bool) (bool, []string) {
var (
filteredDeploymentConfig []string
flag bool
)
for key, value := range deploymentConfig {
for _, permitted := range permittedDeploymentConfigString {
if key == permitted {
//filtering only those deployment app types which are in permitted zone and are marked true
if value {
flag = true
filteredDeploymentConfig = append(filteredDeploymentConfig, key)
}
break
}
}
}
if !flag {
return false, nil
}
return true, filteredDeploymentConfig
}
func (impl EnvironmentServiceImpl) GetEnvironmentOnlyListForAutocomplete() ([]bean2.EnvironmentBean, error) {
models, err := impl.environmentRepository.FindAllActiveEnvOnlyDetails()
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
}
var beans []bean2.EnvironmentBean
for _, model := range models {
beans = append(beans, bean2.EnvironmentBean{
Id: model.Id,
Environment: model.Name,
Namespace: model.Namespace,
EnvironmentIdentifier: model.EnvironmentIdentifier,
ClusterId: model.ClusterId,
IsVirtualEnvironment: model.IsVirtualEnvironment,
})
}
return beans, nil
}
func (impl EnvironmentServiceImpl) validateNamespaces(namespace string, envs []*repository.Environment) error {
if len(envs) >= 1 {
if namespace == "" {
impl.logger.Errorw("namespace cannot be empty")
return errors.New("namespace cannot be empty")
}
for _, n := range envs {
if n.Namespace == "" {
impl.logger.Errorw("cannot create env as existing old envs have no namespace")
return errors.New("cannot create env as existing old envs have no namespace")
}
}
}
return nil
}
func (impl EnvironmentServiceImpl) FindByIds(ids []*int) ([]*bean2.EnvironmentBean, error) {
models, err := impl.environmentRepository.FindByIds(ids)
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
return nil, err
}
var beans []*bean2.EnvironmentBean
for _, model := range models {
beans = append(beans, &bean2.EnvironmentBean{
Id: model.Id,
Environment: model.Name,
Active: model.Active,
Namespace: model.Namespace,
Default: model.Default,
EnvironmentIdentifier: model.EnvironmentIdentifier,
ClusterId: model.ClusterId,
Description: model.Description,
IsVirtualEnvironment: model.IsVirtualEnvironment,
})
}
return beans, nil
}
func (impl EnvironmentServiceImpl) FindByNamespaceAndClusterName(namespaces string, clusterName string) (*repository.Environment, error) {
env, err := impl.environmentRepository.FindByNamespaceAndClusterName(namespaces, clusterName)
return env, err
}
func (impl EnvironmentServiceImpl) FindOneByNamespaceAndClusterId(namespaces string, clusterId int) (*bean2.EnvironmentBean, error) {
env, err := impl.environmentRepository.FindOneByNamespaceAndClusterId(namespaces, clusterId)
if err != nil {
impl.logger.Errorw("error in getting environment by namespace and cluster id", "namespaces", namespaces, "clusterId", clusterId)
return nil, err
}
return adapter2.NewEnvironmentBean(env), err
}
func (impl EnvironmentServiceImpl) GetByClusterId(id int) ([]*bean2.EnvironmentBean, error) {
models, err := impl.environmentRepository.FindByClusterId(id)
if err != nil {
impl.logger.Errorw("error in fetching environment", "err", err)
}
var beans []*bean2.EnvironmentBean
for _, model := range models {
beans = append(beans, &bean2.EnvironmentBean{
Id: model.Id,
Environment: model.Name,
Namespace: model.Namespace,
EnvironmentIdentifier: model.EnvironmentIdentifier,
Description: model.Description,
})
}
return beans, nil
}
func (impl EnvironmentServiceImpl) GetCombinedEnvironmentListForDropDown(token string, isActionUserSuperAdmin bool, auth func(email string, object []string) map[string]bool) ([]*bean2.ClusterEnvDto, error) {
var namespaceGroupByClusterResponse []*bean2.ClusterEnvDto
clusterModels, err := impl.clusterService.FindAllActive()
if err != nil {
impl.logger.Errorw("error in fetching clusters", "err", err)
return namespaceGroupByClusterResponse, err
}
clusterMap := make(map[string]int)
for _, item := range clusterModels {
clusterMap[item.ClusterName] = item.Id
}
models, err := impl.environmentRepository.FindAllActive()
if err != nil {
impl.logger.Errorw("error in fetching environments", "err", err)
return namespaceGroupByClusterResponse, err
}
rbacObject := make([]string, 0)
for _, model := range models {
rbacObject = append(rbacObject, model.EnvironmentIdentifier)
}
// auth enforcer applied here in batch
rbacObjectResult := auth(token, rbacObject)
uniqueComboMap := make(map[string]bool)
grantedEnvironmentMap := make(map[string][]*bean2.EnvDto)
for _, model := range models {
// isActionUserSuperAdmin tell that user is super admin or not. auth check skip for admin
if !isActionUserSuperAdmin {
isValidAuth := rbacObjectResult[model.EnvironmentIdentifier]
if !isValidAuth {
impl.logger.Debugw("authentication for env failed", "object", model.EnvironmentIdentifier)
continue
}
}
key := fmt.Sprintf("%s__%s", model.Cluster.ClusterName, model.Namespace)
groupKey := fmt.Sprintf("%s__%d", model.Cluster.ClusterName, model.ClusterId)
uniqueComboMap[key] = true
grantedEnvironmentMap[groupKey] = append(grantedEnvironmentMap[groupKey], &bean2.EnvDto{
EnvironmentId: model.Id,
EnvironmentName: model.Name,
Namespace: model.Namespace,
EnvironmentIdentifier: model.EnvironmentIdentifier,
Description: model.Description,
})
}
namespaceListGroupByClusters := impl.k8sInformerFactory.GetLatestNamespaceListGroupByCLuster()
rbacObject2 := make([]string, 0)
for clusterName, namespaces := range namespaceListGroupByClusters {
for namespace := range namespaces {
environmentIdentifier := fmt.Sprintf("%s__%s", clusterName, namespace)
rbacObject2 = append(rbacObject2, environmentIdentifier)
}
}
// auth enforcer applied here in batch
rbacObjectResult2 := auth(token, rbacObject)
for clusterName, namespaces := range namespaceListGroupByClusters {
clusterId := clusterMap[clusterName]
for namespace := range namespaces {
//deduplication for cluster and namespace combination
key := fmt.Sprintf("%s__%s", clusterName, namespace)
groupKey := fmt.Sprintf("%s__%d", clusterName, clusterId)
if _, ok := uniqueComboMap[key]; !ok {
environmentIdentifier := fmt.Sprintf("%s__%s", clusterName, namespace)
// isActionUserSuperAdmin tell that user is super admin or not. auth check skip for admin
if !isActionUserSuperAdmin {
isValidAuth := rbacObjectResult2[environmentIdentifier]
if !isValidAuth {
impl.logger.Debugw("authentication for env failed", "object", environmentIdentifier)
continue
}
}
grantedEnvironmentMap[groupKey] = append(grantedEnvironmentMap[groupKey], &bean2.EnvDto{
EnvironmentName: environmentIdentifier,
Namespace: namespace,
EnvironmentIdentifier: environmentIdentifier,
})
}
}
}
//final result builds here, namespace group by clusters
for k, v := range grantedEnvironmentMap {
clusterInfo := strings.Split(k, "__")
clusterId, err := strconv.Atoi(clusterInfo[1])
if err != nil {
clusterId = 0
}
namespaceGroupByClusterResponse = append(namespaceGroupByClusterResponse, &bean2.ClusterEnvDto{
ClusterName: clusterInfo[0],
ClusterId: clusterId,
Environments: v,
})
}
return namespaceGroupByClusterResponse, nil
}
func (impl EnvironmentServiceImpl) GetCombinedEnvironmentListForDropDownByClusterIds(token string, clusterIds []int, auth func(token string, object string) bool) ([]*bean2.ClusterEnvDto, error) {
var namespaceGroupByClusterResponse []*bean2.ClusterEnvDto
clusterModels, err := impl.clusterService.FindByIds(clusterIds)
if err != nil {
impl.logger.Errorw("error in fetching clusters", "err", err)
return namespaceGroupByClusterResponse, err
}
clusterMap := make(map[string]int)
for _, item := range clusterModels {
clusterMap[item.ClusterName] = item.Id
}
models, err := impl.environmentRepository.FindByClusterIds(clusterIds)
if err != nil {
impl.logger.Errorw("error in fetching environments", "err", err)
return namespaceGroupByClusterResponse, err
}
uniqueComboMap := make(map[string]bool)
grantedEnvironmentMap := make(map[string][]*bean2.EnvDto)
for _, model := range models {
// auth enforcer applied here
isValidAuth := auth(token, model.EnvironmentIdentifier)
if !isValidAuth {
impl.logger.Debugw("authentication for env failed", "object", model.EnvironmentIdentifier)
continue
}
key := fmt.Sprintf("%s__%s", model.Cluster.ClusterName, model.Namespace)
groupKey := fmt.Sprintf("%s__%d", model.Cluster.ClusterName, model.ClusterId)
uniqueComboMap[key] = true
grantedEnvironmentMap[groupKey] = append(grantedEnvironmentMap[groupKey], &bean2.EnvDto{
EnvironmentId: model.Id,
EnvironmentName: model.Name,
Namespace: model.Namespace,
EnvironmentIdentifier: model.EnvironmentIdentifier,
Description: model.Description,
})
}
namespaceListGroupByClusters := impl.k8sInformerFactory.GetLatestNamespaceListGroupByCLuster()
for clusterName, namespaces := range namespaceListGroupByClusters {
clusterId := clusterMap[clusterName]
if clusterId == 0 {
continue
}
for namespace := range namespaces {
//deduplication for cluster and namespace combination
key := fmt.Sprintf("%s__%s", clusterName, namespace)
groupKey := fmt.Sprintf("%s__%d", clusterName, clusterId)
if _, ok := uniqueComboMap[key]; !ok {
environmentIdentifier := fmt.Sprintf("%s__%s", clusterName, namespace)
// auth enforcer applied here
isValidAuth := auth(token, environmentIdentifier)
if !isValidAuth {
impl.logger.Debugw("authentication for env failed", "object", environmentIdentifier)
continue
}
grantedEnvironmentMap[groupKey] = append(grantedEnvironmentMap[groupKey], &bean2.EnvDto{
EnvironmentName: environmentIdentifier,
Namespace: namespace,
EnvironmentIdentifier: environmentIdentifier,
})
}
}
}
//final result builds here, namespace group by clusters
for k, v := range grantedEnvironmentMap {
clusterInfo := strings.Split(k, "__")
clusterId, err := strconv.Atoi(clusterInfo[1])
if err != nil {
clusterId = 0
}
namespaceGroupByClusterResponse = append(namespaceGroupByClusterResponse, &bean2.ClusterEnvDto{
ClusterName: clusterInfo[0],
ClusterId: clusterId,
Environments: v,
})
}
return namespaceGroupByClusterResponse, nil
}
func (impl EnvironmentServiceImpl) Delete(deleteReq *bean2.EnvironmentBean, userId int32) error {
existingEnv, err := impl.environmentRepository.FindById(deleteReq.Id)
if err != nil {
impl.logger.Errorw("No matching entry found for delete.", "id", deleteReq.Id)
return err
}
dbConnection := impl.environmentRepository.GetConnection()
tx, err := dbConnection.Begin()
if err != nil {
impl.logger.Errorw("error in establishing connection", "err", err)
return err
}
// Rollback tx on error.
defer tx.Rollback()
deleteRequest := existingEnv
deleteRequest.UpdatedOn = time.Now()
deleteRequest.UpdatedBy = userId
err = impl.environmentRepository.MarkEnvironmentDeleted(deleteRequest, tx)
if err != nil {
impl.logger.Errorw("error in deleting environment", "envId", deleteReq.Id, "envName", deleteReq.Environment)
return err
}
//deleting auth roles entries for this environment
err = impl.userAuthService.DeleteRoles(bean.ENV_TYPE, deleteRequest.Name, tx, existingEnv.EnvironmentIdentifier, "")
if err != nil {
impl.logger.Errorw("error in deleting auth roles", "err", err)
return err
}
err = tx.Commit()
if err != nil {
return err
}
return nil
}
func (impl EnvironmentServiceImpl) HandleErrorInClusterConnections(clusters []*bean4.ClusterBean, respMap *sync.Map, clusterExistInDb bool) {
impl.clusterService.HandleErrorInClusterConnections(clusters, respMap, clusterExistInDb)
}
func (impl EnvironmentServiceImpl) GetDetailsById(envId int) (*repository.Environment, error) {
envDetails, err := impl.environmentRepository.FindById(envId)
if err != nil {
impl.logger.Errorw("error encountered in GetDetailsById", "envId", envId, "err", err)
return nil, err
}
return envDetails, nil
}