@@ -20,6 +20,7 @@ package app
20
20
import (
21
21
"encoding/json"
22
22
"fmt"
23
+ bean3 "github.com/devtron-labs/devtron/pkg/deployment/manifest/bean"
23
24
"regexp"
24
25
"strconv"
25
26
"strings"
@@ -48,7 +49,7 @@ type AppCrudOperationService interface {
48
49
FindAll () ([]* bean.AppLabelDto , error )
49
50
GetAppMetaInfo (appId int , installedAppId int , envId int ) (* bean.AppMetaInfoDto , error )
50
51
GetHelmAppMetaInfo (appId string ) (* bean.AppMetaInfoDto , error )
51
- GetLabelsByAppIdForDeployment (appId int ) ([]byte , error )
52
+ GetAppLabelsForDeployment (appId int , appName , envName string ) ([]byte , error )
52
53
GetLabelsByAppId (appId int ) (map [string ]string , error )
53
54
UpdateApp (request * bean.CreateAppDTO ) (* bean.CreateAppDTO , error )
54
55
UpdateProjectForApps (request * bean.UpdateProjectBulkAppsRequest ) (* bean.UpdateProjectBulkAppsRequest , error )
@@ -491,14 +492,13 @@ func (impl AppCrudOperationServiceImpl) GetHelmAppMetaInfo(appId string) (*bean.
491
492
return info , nil
492
493
}
493
494
494
- func (impl AppCrudOperationServiceImpl ) GetLabelsByAppIdForDeployment (appId int ) ([] byte , error ) {
495
- appLabelJson := & bean. AppLabelsJsonForDeployment {}
495
+ func (impl AppCrudOperationServiceImpl ) getLabelsByAppIdForDeployment (appId int ) (map [ string ] string , error ) {
496
+ labelsDto := make ( map [ string ] string )
496
497
labels , err := impl .appLabelRepository .FindAllByAppId (appId )
497
498
if err != nil && err != pg .ErrNoRows {
498
499
impl .logger .Errorw ("error in getting app labels by appId" , "err" , err , "appId" , appId )
499
- return nil , err
500
+ return labelsDto , err
500
501
}
501
- labelsDto := make (map [string ]string )
502
502
for _ , label := range labels {
503
503
labelKey := strings .TrimSpace (label .Key )
504
504
labelValue := strings .TrimSpace (label .Value )
@@ -524,14 +524,47 @@ func (impl AppCrudOperationServiceImpl) GetLabelsByAppIdForDeployment(appId int)
524
524
525
525
labelsDto [labelKey ] = labelValue
526
526
}
527
- appLabelJson .Labels = labelsDto
527
+ return labelsDto , nil
528
+ }
529
+
530
+ func (impl AppCrudOperationServiceImpl ) getExtraAppLabelsToPropagate (appId int , appName , envName string ) (map [string ]string , error ) {
531
+ appMetaInfo , err := impl .appRepository .FindAppAndProjectByAppId (appId )
532
+ if err != nil {
533
+ impl .logger .Errorw ("error in finding app and project by appId" , "appId" , appId , "err" , err )
534
+ return nil , err
535
+ }
536
+ return map [string ]string {
537
+ bean3 .AppNameDevtronLabel : appName ,
538
+ bean3 .EnvNameDevtronLabel : envName ,
539
+ bean3 .ProjectNameDevtronLabel : appMetaInfo .Team .Name ,
540
+ }, nil
541
+ }
542
+
543
+ func (impl AppCrudOperationServiceImpl ) GetAppLabelsForDeployment (appId int , appName , envName string ) ([]byte , error ) {
544
+ appLabelJson := & bean.AppLabelsJsonForDeployment {}
545
+ appLabelsMapFromDb , err := impl .getLabelsByAppIdForDeployment (appId )
546
+ if err != nil {
547
+ impl .logger .Errorw ("error in getting app labels from db using appId" , "appId" , appId , "err" , err )
548
+ return nil , err
549
+ }
550
+ extraAppLabelsToPropagate , err := impl .getExtraAppLabelsToPropagate (appId , appName , envName )
551
+ if err != nil {
552
+ impl .logger .Errorw ("error in getting extra app labels to propagate" , "appName" , appName , "envName" , envName , "err" , err )
553
+ return nil , err
554
+ }
555
+ //when app labels are provided by the user and share the same label key names as those in the extraAppLabelsToPropagate map,
556
+ //priority will be given to the user-provided label keys.
557
+ mergedAppLabels := MergeChildMapToParentMap (appLabelsMapFromDb , extraAppLabelsToPropagate )
558
+
559
+ appLabelJson .Labels = mergedAppLabels
528
560
appLabelByte , err := json .Marshal (appLabelJson )
529
561
if err != nil {
530
562
impl .logger .Errorw ("error in marshaling appLabels json" , "err" , err , "appLabelJson" , appLabelJson )
531
563
return nil , err
532
564
}
533
565
return appLabelByte , nil
534
566
}
567
+
535
568
func (impl AppCrudOperationServiceImpl ) GetLabelsByAppId (appId int ) (map [string ]string , error ) {
536
569
labels , err := impl .appLabelRepository .FindAllByAppId (appId )
537
570
if err != nil {
0 commit comments