@@ -23,8 +23,8 @@ type ArgoApplicationConfigServiceImpl struct {
23
23
}
24
24
25
25
type ArgoApplicationConfigService interface {
26
- GetRestConfigForExternalArgo (ctx context.Context , clusterId int , externalArgoApplicationName string ) (* rest.Config , error )
27
- GetClusterConfigFromAllClusters (clusterId int ) (* k8s.ClusterConfig , clusterRepository.Cluster , map [string ]int , error )
26
+ GetRestConfigForExternalArgo (ctx context.Context , externalArgoAppIdentifier * bean. ArgoAppIdentifier ) (* rest.Config , error )
27
+ GetClusterConfigFromAllClusters (clusterId int ) (* k8s.ClusterConfig , clusterRepository.Cluster , map [string ]* k8s. ClusterConfig , error )
28
28
}
29
29
30
30
func NewArgoApplicationConfigServiceImpl (logger * zap.SugaredLogger ,
@@ -37,19 +37,20 @@ func NewArgoApplicationConfigServiceImpl(logger *zap.SugaredLogger,
37
37
}
38
38
}
39
39
40
- func (impl * ArgoApplicationConfigServiceImpl ) GetClusterConfigFromAllClusters (clusterId int ) (* k8s.ClusterConfig , clusterRepository.Cluster , map [string ]int , error ) {
40
+ func (impl * ArgoApplicationConfigServiceImpl ) GetClusterConfigFromAllClusters (clusterId int ) (* k8s.ClusterConfig , clusterRepository.Cluster , map [string ]* k8s. ClusterConfig , error ) {
41
41
clusters , err := impl .clusterRepository .FindAllActive ()
42
42
var clusterWithApplicationObject clusterRepository.Cluster
43
43
if err != nil {
44
44
impl .logger .Errorw ("error in getting all active clusters" , "err" , err )
45
45
return nil , clusterWithApplicationObject , nil , err
46
46
}
47
- clusterServerUrlIdMap := make (map [string ]int , len (clusters ))
47
+ clusterServerUrlIdMap := make (map [string ]* k8s. ClusterConfig , len (clusters ))
48
48
for _ , cluster := range clusters {
49
49
if cluster .Id == clusterId {
50
50
clusterWithApplicationObject = cluster
51
51
}
52
- clusterServerUrlIdMap [cluster .ServerUrl ] = cluster .Id
52
+ clusterBean := adapter .GetClusterBean (cluster )
53
+ clusterServerUrlIdMap [cluster .ServerUrl ] = clusterBean .GetClusterConfig ()
53
54
}
54
55
if len (clusterWithApplicationObject .ErrorInConnecting ) != 0 {
55
56
return nil , clusterWithApplicationObject , nil , fmt .Errorf ("error in connecting to cluster" )
@@ -59,20 +60,20 @@ func (impl *ArgoApplicationConfigServiceImpl) GetClusterConfigFromAllClusters(cl
59
60
return clusterConfig , clusterWithApplicationObject , clusterServerUrlIdMap , err
60
61
}
61
62
62
- func (impl * ArgoApplicationConfigServiceImpl ) GetRestConfigForExternalArgo (ctx context.Context , clusterId int , externalArgoApplicationName string ) (* rest.Config , error ) {
63
- clusterConfig , clusterWithApplicationObject , clusterServerUrlIdMap , err := impl .GetClusterConfigFromAllClusters (clusterId )
63
+ func (impl * ArgoApplicationConfigServiceImpl ) GetRestConfigForExternalArgo (ctx context.Context , externalArgoAppIdentifier * bean. ArgoAppIdentifier ) (* rest.Config , error ) {
64
+ clusterConfig , clusterWithApplicationObject , clusterServerUrlIdMap , err := impl .GetClusterConfigFromAllClusters (externalArgoAppIdentifier . ClusterId )
64
65
if err != nil {
65
- impl .logger .Errorw ("error in getting cluster config" , "err" , err , "clusterId" , clusterId )
66
+ impl .logger .Errorw ("error in getting cluster config" , "err" , err , "clusterId" , externalArgoAppIdentifier . ClusterId )
66
67
return nil , err
67
68
}
68
69
restConfig , err := impl .k8sUtil .GetRestConfigByCluster (clusterConfig )
69
70
if err != nil {
70
- impl .logger .Errorw ("error in getting rest config" , "err" , err , "clusterId" , clusterId )
71
+ impl .logger .Errorw ("error in getting rest config" , "err" , err , "clusterId" , externalArgoAppIdentifier . ClusterId )
71
72
return nil , err
72
73
}
73
- resourceResp , err := impl .k8sUtil .GetResource (ctx , bean . DevtronCDNamespae , externalArgoApplicationName , bean .GvkForArgoApplication , restConfig )
74
+ resourceResp , err := impl .k8sUtil .GetResource (ctx , externalArgoAppIdentifier . Namespace , externalArgoAppIdentifier . AppName , bean .GvkForArgoApplication , restConfig )
74
75
if err != nil {
75
- impl .logger .Errorw ("not on external cluster" , "err" , err , "externalArgoApplicationName" , externalArgoApplicationName )
76
+ impl .logger .Errorw ("not on external cluster" , "err" , err , "externalArgoApplicationName" , externalArgoAppIdentifier . AppName , "externalArgoApplicationNamespace" , externalArgoAppIdentifier . Namespace )
76
77
return nil , err
77
78
}
78
79
restConfig , err = impl .GetServerConfigIfClusterIsNotAddedOnDevtron (resourceResp , restConfig , clusterWithApplicationObject , clusterServerUrlIdMap )
@@ -84,59 +85,63 @@ func (impl *ArgoApplicationConfigServiceImpl) GetRestConfigForExternalArgo(ctx c
84
85
}
85
86
86
87
func (impl * ArgoApplicationConfigServiceImpl ) GetServerConfigIfClusterIsNotAddedOnDevtron (resourceResp * k8s.ManifestResponse , restConfig * rest.Config ,
87
- clusterWithApplicationObject clusterRepository.Cluster , clusterServerUrlIdMap map [string ]int ) (* rest.Config , error ) {
88
+ clusterWithApplicationObject clusterRepository.Cluster , clusterServerUrlIdMap map [string ]* k8s. ClusterConfig ) (* rest.Config , error ) {
88
89
var destinationServer string
89
90
if resourceResp != nil && resourceResp .Manifest .Object != nil {
90
91
_ , _ , destinationServer , _ =
91
92
helper .GetHealthSyncStatusDestinationServerAndManagedResourcesForArgoK8sRawObject (resourceResp .Manifest .Object )
92
93
}
93
- appDeployedOnClusterId := 0
94
94
if destinationServer == k8sCommonBean .DefaultClusterUrl {
95
- appDeployedOnClusterId = clusterWithApplicationObject .Id
96
- } else if clusterIdFromMap , ok := clusterServerUrlIdMap [destinationServer ]; ok {
97
- appDeployedOnClusterId = clusterIdFromMap
98
- }
99
- var configOfClusterWhereAppIsDeployed * bean.ArgoClusterConfigObj
100
- if appDeployedOnClusterId < 1 {
101
- // cluster is not added on devtron, need to get server config from secret which argo-cd saved
102
- coreV1Client , err := impl .k8sUtil .GetCoreV1ClientByRestConfig (restConfig )
103
- secrets , err := coreV1Client .Secrets (bean .AllNamespaces ).List (context .Background (), v1.ListOptions {
104
- LabelSelector : labels .SelectorFromSet (labels.Set {"argocd.argoproj.io/secret-type" : "cluster" }).String (),
105
- })
95
+ return restConfig , nil
96
+ } else if clusterConfig , ok := clusterServerUrlIdMap [destinationServer ]; ok {
97
+ restConfig , err := impl .k8sUtil .GetRestConfigByCluster (clusterConfig )
106
98
if err != nil {
107
- impl .logger .Errorw ("error in getting resource list, secrets " , "err" , err )
99
+ impl .logger .Errorw ("error in GetRestConfigByCluster, GetServerConfigIfClusterIsNotAddedOnDevtron " , "err" , err , "serverUrl" , destinationServer )
108
100
return nil , err
109
101
}
110
- for _ , secret := range secrets .Items {
111
- if secret .Data != nil {
112
- if val , ok := secret .Data [bean .Server ]; ok {
113
- if string (val ) == destinationServer {
114
- if config , ok := secret .Data [bean .Config ]; ok {
115
- err = json .Unmarshal (config , & configOfClusterWhereAppIsDeployed )
116
- if err != nil {
117
- impl .logger .Errorw ("error in unmarshaling" , "err" , err )
118
- return nil , err
119
- }
120
- break
102
+ return restConfig , nil
103
+ }
104
+
105
+ var configOfClusterWhereAppIsDeployed * bean.ArgoClusterConfigObj
106
+ // cluster is not added on devtron, need to get server config from secret which argo-cd saved
107
+ coreV1Client , err := impl .k8sUtil .GetCoreV1ClientByRestConfig (restConfig )
108
+ secrets , err := coreV1Client .Secrets (bean .AllNamespaces ).List (context .Background (), v1.ListOptions {
109
+ LabelSelector : labels .SelectorFromSet (labels.Set {"argocd.argoproj.io/secret-type" : "cluster" }).String (),
110
+ })
111
+ if err != nil {
112
+ impl .logger .Errorw ("error in getting resource list, secrets" , "err" , err )
113
+ return nil , err
114
+ }
115
+ for _ , secret := range secrets .Items {
116
+ if secret .Data != nil {
117
+ if val , ok := secret .Data [bean .Server ]; ok {
118
+ if string (val ) == destinationServer {
119
+ if config , ok := secret .Data [bean .Config ]; ok {
120
+ err = json .Unmarshal (config , & configOfClusterWhereAppIsDeployed )
121
+ if err != nil {
122
+ impl .logger .Errorw ("error in unmarshaling" , "err" , err )
123
+ return nil , err
121
124
}
125
+ break
122
126
}
123
127
}
124
128
}
125
129
}
126
- if configOfClusterWhereAppIsDeployed != nil {
127
- restConfig , err = impl . k8sUtil . GetRestConfigByCluster ( & k8s. ClusterConfig {
128
- Host : destinationServer ,
129
- BearerToken : configOfClusterWhereAppIsDeployed . BearerToken ,
130
- InsecureSkipTLSVerify : configOfClusterWhereAppIsDeployed .TlsClientConfig . Insecure ,
131
- KeyData : configOfClusterWhereAppIsDeployed .TlsClientConfig .KeyData ,
132
- CAData : configOfClusterWhereAppIsDeployed .TlsClientConfig .CaData ,
133
- CertData : configOfClusterWhereAppIsDeployed .TlsClientConfig .CertData ,
134
- })
135
- if err != nil {
136
- impl . logger . Errorw ( "error in GetRestConfigByCluster, GetServerConfigIfClusterIsNotAddedOnDevtron" , " err" , err , "serverUrl" , destinationServer )
137
- return nil , err
138
- }
130
+ }
131
+ if configOfClusterWhereAppIsDeployed != nil {
132
+ restConfig , err = impl . k8sUtil . GetRestConfigByCluster ( & k8s. ClusterConfig {
133
+ Host : destinationServer ,
134
+ BearerToken : configOfClusterWhereAppIsDeployed .BearerToken ,
135
+ InsecureSkipTLSVerify : configOfClusterWhereAppIsDeployed .TlsClientConfig .Insecure ,
136
+ KeyData : configOfClusterWhereAppIsDeployed .TlsClientConfig .KeyData ,
137
+ CAData : configOfClusterWhereAppIsDeployed .TlsClientConfig .CaData ,
138
+ CertData : configOfClusterWhereAppIsDeployed . TlsClientConfig . CertData ,
139
+ })
140
+ if err != nil {
141
+ impl . logger . Errorw ( "error in GetRestConfigByCluster, GetServerConfigIfClusterIsNotAddedOnDevtron" , " err" , err , "serverUrl" , destinationServer )
142
+ return nil , err
139
143
}
140
144
}
145
+
141
146
return restConfig , nil
142
147
}
0 commit comments