Skip to content

Commit b8b8a43

Browse files
iamayushmAsh-exp
andauthored
fix: Unable to fetch manifest in case of ACD external listing (#6598)
* external argo fixes * removing unwanted param * nil handling * updating rest config --------- Co-authored-by: Asutosh Das <[email protected]>
1 parent 510f775 commit b8b8a43

File tree

7 files changed

+91
-69
lines changed

7 files changed

+91
-69
lines changed

api/k8s/application/k8sApplicationRestHandler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,8 @@ func (handler *K8sApplicationRestHandlerImpl) CreateEphemeralContainer(w http.Re
10251025
return
10261026
}
10271027
request.UserId = userId
1028+
request.ExternalArgoAppIdentifier = resourceRequestBean.ExternalArgoAppIdentifier
1029+
10281030
err = handler.k8sApplicationService.CreatePodEphemeralContainers(&request)
10291031
if err != nil {
10301032
handler.logger.Errorw("error occurred in creating ephemeral container", "err", err, "requestPayload", request)
@@ -1073,6 +1075,7 @@ func (handler *K8sApplicationRestHandlerImpl) DeleteEphemeralContainer(w http.Re
10731075
return
10741076
}
10751077
request.UserId = userId
1078+
request.ExternalArgoAppIdentifier = resourceRequestBean.ExternalArgoAppIdentifier
10761079
_, err = handler.k8sApplicationService.TerminatePodEphemeralContainer(request)
10771080
if err != nil {
10781081
handler.logger.Errorw("error occurred in terminating ephemeral container", "err", err, "requestPayload", request)
@@ -1156,6 +1159,7 @@ func (handler *K8sApplicationRestHandlerImpl) verifyRbacForAppRequests(token str
11561159
handler.logger.Errorw("error in decoding appId", "err", err, "appId", request.AppId)
11571160
return false, err
11581161
}
1162+
request.ExternalArgoAppIdentifier = argoAppIdentifier
11591163
request.ClusterId = argoAppIdentifier.ClusterId
11601164
request.ExternalArgoApplicationName = argoAppIdentifier.AppName
11611165
valid, err := handler.argoApplicationReadService.ValidateArgoResourceRequest(r.Context(), argoAppIdentifier, request.K8sRequest)

pkg/argoApplication/read/config/ArgoApplicationConfigService.go

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ type ArgoApplicationConfigServiceImpl struct {
2323
}
2424

2525
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)
2828
}
2929

3030
func NewArgoApplicationConfigServiceImpl(logger *zap.SugaredLogger,
@@ -37,19 +37,20 @@ func NewArgoApplicationConfigServiceImpl(logger *zap.SugaredLogger,
3737
}
3838
}
3939

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) {
4141
clusters, err := impl.clusterRepository.FindAllActive()
4242
var clusterWithApplicationObject clusterRepository.Cluster
4343
if err != nil {
4444
impl.logger.Errorw("error in getting all active clusters", "err", err)
4545
return nil, clusterWithApplicationObject, nil, err
4646
}
47-
clusterServerUrlIdMap := make(map[string]int, len(clusters))
47+
clusterServerUrlIdMap := make(map[string]*k8s.ClusterConfig, len(clusters))
4848
for _, cluster := range clusters {
4949
if cluster.Id == clusterId {
5050
clusterWithApplicationObject = cluster
5151
}
52-
clusterServerUrlIdMap[cluster.ServerUrl] = cluster.Id
52+
clusterBean := adapter.GetClusterBean(cluster)
53+
clusterServerUrlIdMap[cluster.ServerUrl] = clusterBean.GetClusterConfig()
5354
}
5455
if len(clusterWithApplicationObject.ErrorInConnecting) != 0 {
5556
return nil, clusterWithApplicationObject, nil, fmt.Errorf("error in connecting to cluster")
@@ -59,20 +60,20 @@ func (impl *ArgoApplicationConfigServiceImpl) GetClusterConfigFromAllClusters(cl
5960
return clusterConfig, clusterWithApplicationObject, clusterServerUrlIdMap, err
6061
}
6162

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)
6465
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)
6667
return nil, err
6768
}
6869
restConfig, err := impl.k8sUtil.GetRestConfigByCluster(clusterConfig)
6970
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)
7172
return nil, err
7273
}
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)
7475
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)
7677
return nil, err
7778
}
7879
restConfig, err = impl.GetServerConfigIfClusterIsNotAddedOnDevtron(resourceResp, restConfig, clusterWithApplicationObject, clusterServerUrlIdMap)
@@ -84,59 +85,63 @@ func (impl *ArgoApplicationConfigServiceImpl) GetRestConfigForExternalArgo(ctx c
8485
}
8586

8687
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) {
8889
var destinationServer string
8990
if resourceResp != nil && resourceResp.Manifest.Object != nil {
9091
_, _, destinationServer, _ =
9192
helper.GetHealthSyncStatusDestinationServerAndManagedResourcesForArgoK8sRawObject(resourceResp.Manifest.Object)
9293
}
93-
appDeployedOnClusterId := 0
9494
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)
10698
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)
108100
return nil, err
109101
}
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
121124
}
125+
break
122126
}
123127
}
124128
}
125129
}
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
139143
}
140144
}
145+
141146
return restConfig, nil
142147
}

pkg/cluster/environment/bean/ephemeralContainerBean.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package bean
22

3-
import "github.com/devtron-labs/devtron/pkg/cluster/repository"
3+
import (
4+
"github.com/devtron-labs/devtron/pkg/argoApplication/bean"
5+
"github.com/devtron-labs/devtron/pkg/cluster/repository"
6+
)
47

58
type EphemeralContainerRequest struct {
6-
BasicData *EphemeralContainerBasicData `json:"basicData"`
7-
AdvancedData *EphemeralContainerAdvancedData `json:"advancedData"`
8-
Namespace string `json:"namespace" validate:"required"`
9-
ClusterId int `json:"clusterId" validate:"gt=0"`
10-
PodName string `json:"podName" validate:"required"`
11-
ExternalArgoApplicationName string `json:"externalArgoApplicationName,omitempty"`
12-
UserId int32 `json:"-"`
9+
BasicData *EphemeralContainerBasicData `json:"basicData"`
10+
AdvancedData *EphemeralContainerAdvancedData `json:"advancedData"`
11+
Namespace string `json:"namespace" validate:"required"`
12+
ClusterId int `json:"clusterId" validate:"gt=0"`
13+
PodName string `json:"podName" validate:"required"`
14+
ExternalArgoApplicationName string `json:"externalArgoApplicationName,omitempty"`
15+
ExternalArgoApplicationNamespace string `json:"externalArgoApplicationNamespace,omitempty"`
16+
ExternalArgoAppIdentifier *bean.ArgoAppIdentifier `json:"externalArgoAppIdentifier"`
17+
UserId int32 `json:"-"`
1318
}
1419

1520
type EphemeralContainerAdvancedData struct {

pkg/k8s/K8sCommonService.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ func (impl *K8sCommonServiceImpl) UpdateResource(ctx context.Context, request *b
195195
func (impl *K8sCommonServiceImpl) GetRestConfigOfCluster(ctx context.Context, request *bean5.ResourceRequestBean) (*rest.Config, error) {
196196
//getting rest config by clusterId
197197
clusterId := request.ClusterId
198-
if len(request.ExternalArgoApplicationName) > 0 {
199-
restConfig, err := impl.argoApplicationConfigService.GetRestConfigForExternalArgo(ctx, clusterId, request.ExternalArgoApplicationName)
198+
if request.ExternalArgoAppIdentifier != nil {
199+
restConfig, err := impl.argoApplicationConfigService.GetRestConfigForExternalArgo(ctx, request.ExternalArgoAppIdentifier)
200200
if err != nil {
201-
impl.logger.Errorw("error in getting rest config", "err", err, "clusterId", clusterId, "externalArgoApplicationName", request.ExternalArgoApplicationName)
201+
impl.logger.Errorw("error in getting rest config", "err", err, "clusterId", clusterId, "externalArgoApplicationName", request.ExternalArgoAppIdentifier.AppName)
202202
return nil, err
203203
}
204204
return restConfig, nil
@@ -467,7 +467,7 @@ func (impl *K8sCommonServiceImpl) GetCoreClientByClusterId(clusterId int) (*kube
467467
}
468468

469469
func (impl *K8sCommonServiceImpl) GetCoreClientByClusterIdForExternalArgoApps(req *bean4.EphemeralContainerRequest) (*kubernetes.Clientset, *clientV1.CoreV1Client, error) {
470-
restConfig, err := impl.argoApplicationConfigService.GetRestConfigForExternalArgo(context.Background(), req.ClusterId, req.ExternalArgoApplicationName)
470+
restConfig, err := impl.argoApplicationConfigService.GetRestConfigForExternalArgo(context.Background(), req.ExternalArgoAppIdentifier)
471471
if err != nil {
472472
impl.logger.Errorw("error in getting rest config", "err", err, "clusterId", req.ClusterId, "externalArgoApplicationName", req.ExternalArgoApplicationName)
473473
}

pkg/k8s/application/k8sApplicationService.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ func (impl *K8sApplicationServiceImpl) ValidatePodLogsRequestQuery(r *http.Reque
263263
request.ClusterId = appIdentifier.ClusterId
264264
request.K8sRequest.ResourceIdentifier.Namespace = namespace
265265
request.AppId = appId
266+
request.ExternalArgoAppIdentifier = appIdentifier
266267
} else if request.AppType == bean3.HelmAppType {
267268
// For Helm App resources
268269
appIdentifier, err := impl.helmAppService.DecodeAppId(appId)
@@ -370,6 +371,8 @@ func (impl *K8sApplicationServiceImpl) ValidateTerminalRequestQuery(r *http.Requ
370371
resourceRequestBean.ClusterId = appIdentifier.ClusterId
371372
request.ClusterId = appIdentifier.ClusterId
372373
request.ExternalArgoApplicationName = appIdentifier.AppName
374+
request.ExternalArgoApplicationNamespace = appIdentifier.Namespace
375+
request.ExternalArgoAppIdentifier = appIdentifier
373376
}
374377
} else {
375378
// Validate Cluster Id
@@ -876,7 +879,7 @@ func (impl *K8sApplicationServiceImpl) CreatePodEphemeralContainers(req *bean5.E
876879
var clientSet *kubernetes.Clientset
877880
var v1Client *v1.CoreV1Client
878881
var err error
879-
if len(req.ExternalArgoApplicationName) > 0 {
882+
if req.ExternalArgoAppIdentifier != nil {
880883
clientSet, v1Client, err = impl.k8sCommonService.GetCoreClientByClusterIdForExternalArgoApps(req)
881884
if err != nil {
882885
impl.logger.Errorw("error in getting coreV1 client by clusterId", "err", err, "req", req)

pkg/k8s/bean/bean.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package bean
1919
import (
2020
"github.com/devtron-labs/common-lib/utils/k8s"
2121
helmBean "github.com/devtron-labs/devtron/api/helm-app/service/bean"
22+
bean3 "github.com/devtron-labs/devtron/pkg/argoApplication/bean"
2223
bean2 "github.com/devtron-labs/devtron/pkg/fluxApplication/bean"
2324
"github.com/devtron-labs/devtron/pkg/k8s/application/bean"
2425
)
@@ -33,6 +34,7 @@ type ResourceRequestBean struct {
3334
ClusterId int `json:"clusterId"` // clusterId is used when request is for direct cluster (not for helm release)
3435
ExternalArgoApplicationName string `json:"externalArgoApplicationName,omitempty"`
3536
ExternalFluxAppIdentifier *bean2.FluxAppIdentifier `json: "-"`
37+
ExternalArgoAppIdentifier *bean3.ArgoAppIdentifier `json:"-"`
3638
}
3739

3840
func (r *ResourceRequestBean) IsValidAppType() bool {

pkg/terminal/terminalSesion.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/caarlos0/env"
3737
"github.com/devtron-labs/common-lib/utils/k8s"
3838
"github.com/devtron-labs/devtron/internal/middleware"
39+
bean3 "github.com/devtron-labs/devtron/pkg/argoApplication/bean"
3940
"github.com/devtron-labs/devtron/pkg/argoApplication/read/config"
4041
"github.com/devtron-labs/devtron/pkg/cluster"
4142
"github.com/devtron-labs/devtron/pkg/cluster/bean"
@@ -396,9 +397,11 @@ type TerminalSessionRequest struct {
396397
EnvironmentId int
397398
AppId int
398399
//ClusterId is optional
399-
ClusterId int
400-
UserId int32
401-
ExternalArgoApplicationName string
400+
ClusterId int
401+
UserId int32
402+
ExternalArgoApplicationName string
403+
ExternalArgoApplicationNamespace string
404+
ExternalArgoAppIdentifier *bean3.ArgoAppIdentifier
402405
}
403406

404407
const CommandExecutionFailed = "Failed to Execute Command"
@@ -540,8 +543,8 @@ func (impl *TerminalSessionHandlerImpl) getClientSetAndRestConfigForTerminalConn
540543
var clusterConfig *k8s.ClusterConfig
541544
var restConfig *rest.Config
542545
var err error
543-
if len(req.ExternalArgoApplicationName) > 0 {
544-
restConfig, err = impl.argoApplicationConfigService.GetRestConfigForExternalArgo(context.Background(), req.ClusterId, req.ExternalArgoApplicationName)
546+
if req.ExternalArgoAppIdentifier != nil {
547+
restConfig, err = impl.argoApplicationConfigService.GetRestConfigForExternalArgo(context.Background(), req.ExternalArgoAppIdentifier)
545548
if err != nil {
546549
impl.logger.Errorw("error in getting rest config", "err", err, "clusterId", req.ClusterId, "externalArgoApplicationName", req.ExternalArgoApplicationName)
547550
return nil, nil, err
@@ -655,8 +658,8 @@ func (impl *TerminalSessionHandlerImpl) RunCmdInRemotePod(req *TerminalSessionRe
655658
func (impl *TerminalSessionHandlerImpl) saveEphemeralContainerTerminalAccessAudit(req *TerminalSessionRequest) error {
656659
var restConfig *rest.Config
657660
var err error
658-
if len(req.ExternalArgoApplicationName) > 0 {
659-
restConfig, err = impl.argoApplicationConfigService.GetRestConfigForExternalArgo(context.Background(), req.ClusterId, req.ExternalArgoApplicationName)
661+
if req.ExternalArgoAppIdentifier != nil {
662+
restConfig, err = impl.argoApplicationConfigService.GetRestConfigForExternalArgo(context.Background(), req.ExternalArgoAppIdentifier)
660663
if err != nil {
661664
impl.logger.Errorw("error in getting rest config", "err", err, "clusterId", req.ClusterId, "externalArgoApplicationName", req.ExternalArgoApplicationName)
662665
return err

0 commit comments

Comments
 (0)