Skip to content

Commit 69a88d8

Browse files
committed
fix: nil handling updated
1 parent 1e639e4 commit 69a88d8

File tree

4 files changed

+56
-47
lines changed

4 files changed

+56
-47
lines changed

Diff for: api/argoApplication/ArgoApplicationRestHandler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (handler *ArgoApplicationRestHandlerImpl) GetApplicationDetail(w http.Respo
104104
return
105105
}
106106
}
107-
resp, err := handler.readService.GetAppDetail(resourceName, namespace, clusterId)
107+
resp, err := handler.readService.GetAppDetailEA(r.Context(), resourceName, namespace, clusterId)
108108
if err != nil {
109109
handler.logger.Errorw("error in getting argo application app detail", "err", err, "resourceName", resourceName, "clusterId", clusterId)
110110
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)

Diff for: api/k8s/application/k8sApplicationRestHandler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func (handler *K8sApplicationRestHandlerImpl) GetHostUrlsByBatch(w http.Response
303303
}
304304
//RBAC enforcer Ends
305305

306-
appDetail, err := handler.argoApplicationReadService.GetAppDetail(appIdentifier.AppName, appIdentifier.Namespace, appIdentifier.ClusterId)
306+
appDetail, err := handler.argoApplicationReadService.GetAppDetailEA(r.Context(), appIdentifier.AppName, appIdentifier.Namespace, appIdentifier.ClusterId)
307307
if err != nil {
308308
apiError := clientErrors.ConvertToApiError(err)
309309
if apiError != nil {

Diff for: pkg/argoApplication/ArgoApplicationServiceExtended.go

+15-11
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ func (c *ArgoApplicationServiceExtendedImpl) GetResourceTree(ctx context.Context
186186

187187
func (c *ArgoApplicationServiceExtendedImpl) getArgoResourceTreeAndPodMetadata(ctx context.Context, asc application2.ApplicationServiceClient, acdQueryRequest *bean.AcdClientQueryRequest) (*v1alpha1.ApplicationTree, []*argoApplication.PodMetadata, error) {
188188
if acdQueryRequest.Mode.IsDeclarative() {
189-
argoResourceTree, podMetadataList, err := c.getResourceTreeUsingCache(ctx, acdQueryRequest)
189+
argoResourceTree, podMetadataList, err := c.getResourceTreeUsingK8sClient(ctx, acdQueryRequest)
190190
if err != nil {
191191
c.logger.Errorw("Error in getArgoResourceTreeAndPodMetadata, calling fallback function to get from argo", "acdQueryRequest", acdQueryRequest, "err", err)
192192
} else {
@@ -195,6 +195,10 @@ func (c *ArgoApplicationServiceExtendedImpl) getArgoResourceTreeAndPodMetadata(c
195195
}
196196

197197
//fallback
198+
return c.getResourceTreeUsingArgoClient(ctx, asc, acdQueryRequest)
199+
}
200+
201+
func (c *ArgoApplicationServiceExtendedImpl) getResourceTreeUsingArgoClient(ctx context.Context, asc application2.ApplicationServiceClient, acdQueryRequest *bean.AcdClientQueryRequest) (*v1alpha1.ApplicationTree, []*argoApplication.PodMetadata, error) {
198202
//all the apps deployed via argo are fetching status from here
199203
argoCtx, cancel := context.WithTimeout(ctx, argoApplication.TimeoutSlow)
200204
defer cancel()
@@ -208,7 +212,16 @@ func (c *ArgoApplicationServiceExtendedImpl) getArgoResourceTreeAndPodMetadata(c
208212
return rtResp, podMetadata, nil
209213
}
210214

211-
func (c *ArgoApplicationServiceExtendedImpl) getArgoResourceTreeResponse(ctx context.Context, clusterConfig *k8s.ClusterConfig, acdQueryRequest *bean.AcdClientQueryRequest) (*v1alpha1.ApplicationTree, []*argoApplication.PodMetadata, error) {
215+
func (c *ArgoApplicationServiceExtendedImpl) getResourceTreeUsingK8sClient(ctx context.Context, acdQueryRequest *bean.AcdClientQueryRequest) (*v1alpha1.ApplicationTree, []*argoApplication.PodMetadata, error) {
216+
clusterConfig, err := c.clusterService.GetClusterConfigByClusterId(acdQueryRequest.ArgoClusterId)
217+
if err != nil {
218+
c.logger.Errorw("Error in getting cluster config by clusterId", "acdQueryRequest", acdQueryRequest, "err", err)
219+
return nil, nil, err
220+
}
221+
return c.getAcdResourceTreeUsingK8sClient(ctx, clusterConfig, acdQueryRequest)
222+
}
223+
224+
func (c *ArgoApplicationServiceExtendedImpl) getAcdResourceTreeUsingK8sClient(ctx context.Context, clusterConfig *k8s.ClusterConfig, acdQueryRequest *bean.AcdClientQueryRequest) (*v1alpha1.ApplicationTree, []*argoApplication.PodMetadata, error) {
212225
argoCdAppNamespace := acdQueryRequest.GetAppNamespace(c.aCDAuthConfig.ACDConfigMapNamespace)
213226
argoMangedResourceResp, err := c.argoApplicationReadService.GetArgoManagedResources(acdQueryRequest.GetApplicationName(), argoCdAppNamespace, clusterConfig)
214227
if err != nil {
@@ -230,15 +243,6 @@ func (c *ArgoApplicationServiceExtendedImpl) getArgoResourceTreeResponse(ctx con
230243
}
231244
}
232245

233-
func (c *ArgoApplicationServiceExtendedImpl) getResourceTreeUsingCache(ctx context.Context, acdQueryRequest *bean.AcdClientQueryRequest) (*v1alpha1.ApplicationTree, []*argoApplication.PodMetadata, error) {
234-
clusterConfig, err := c.clusterService.GetClusterConfigByClusterId(acdQueryRequest.ArgoClusterId)
235-
if err != nil {
236-
c.logger.Errorw("Error in getting cluster config by clusterId", "acdQueryRequest", acdQueryRequest, "err", err)
237-
return nil, nil, err
238-
}
239-
return c.getArgoResourceTreeResponse(ctx, clusterConfig, acdQueryRequest)
240-
}
241-
242246
func (c *ArgoApplicationServiceExtendedImpl) parseResult(resp *v1alpha1.ApplicationTree, query *application2.ResourcesQuery, ctx context.Context, asc application2.ApplicationServiceClient, err error) []*argoApplication.Result {
243247
var responses = make([]*argoApplication.Result, 0)
244248
qCount := 0

Diff for: pkg/argoApplication/read/ArgoApplicationReadService.go

+39-34
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737

3838
type ArgoApplicationReadService interface {
3939
ValidateArgoResourceRequest(ctx context.Context, appIdentifier *bean.ArgoAppIdentifier, request *k8s.K8sRequestBean) (bool, error)
40-
GetAppDetail(resourceName, resourceNamespace string, clusterId int) (*bean.ArgoApplicationDetailDto, error)
40+
GetAppDetailEA(ctx context.Context, resourceName, resourceNamespace string, clusterId int) (*bean.ArgoApplicationDetailDto, error)
4141
GetArgoManagedResources(resourceName, resourceNamespace string, clusterConfig *k8s.ClusterConfig) (*bean.ArgoManagedResourceResponse, error)
4242
GetArgoAppResourceTree(clusterConfig *k8s.ClusterConfig, targetClusterId int, resp *bean.ArgoManagedResourceResponse) (*gRPC.ResourceTreeResponse, error)
4343
}
@@ -65,7 +65,7 @@ func NewArgoApplicationReadServiceImpl(logger *zap.SugaredLogger,
6565

6666
}
6767

68-
func (impl *ArgoApplicationReadServiceImpl) GetAppDetail(resourceName, resourceNamespace string, clusterId int) (*bean.ArgoApplicationDetailDto, error) {
68+
func (impl *ArgoApplicationReadServiceImpl) GetAppDetailEA(ctx context.Context, resourceName, resourceNamespace string, clusterId int) (*bean.ArgoApplicationDetailDto, error) {
6969
appDetail := &bean.ArgoApplicationDetailDto{
7070
ArgoApplicationListDto: &bean.ArgoApplicationListDto{
7171
Name: resourceName,
@@ -102,20 +102,24 @@ func (impl *ArgoApplicationReadServiceImpl) GetAppDetail(resourceName, resourceN
102102
return nil, err
103103
}
104104
targetClusterId := 0
105-
if resp.DestinationServer == k8sCommonBean.DefaultClusterUrl {
106-
targetClusterId = clusterWithApplicationObject.Id
107-
} else if clusterIdFromMap, ok := clusterServerUrlIdMap[resp.DestinationServer]; ok {
108-
targetClusterId = clusterIdFromMap
105+
if len(resp.DestinationServer) != 0 {
106+
if resp.DestinationServer == k8sCommonBean.DefaultClusterUrl {
107+
targetClusterId = clusterWithApplicationObject.Id
108+
} else if clusterIdFromMap, ok := clusterServerUrlIdMap[resp.DestinationServer]; ok {
109+
targetClusterId = clusterIdFromMap
110+
}
109111
}
110-
appDetail.Manifest = resp.ManifestResponse.Manifest.Object
111-
appDetail.HealthStatus = resp.HealthStatus
112-
appDetail.SyncStatus = resp.SyncStatus
113112
resourceTree, err := impl.GetArgoAppResourceTree(clusterConfig, targetClusterId, resp)
114113
if err != nil {
115114
impl.logger.Errorw("error in getting argo app resource tree", "err", err)
116115
return nil, err
117116
}
118117
appDetail.ResourceTree = resourceTree
118+
if resp.ManifestResponse != nil {
119+
appDetail.Manifest = resp.ManifestResponse.Manifest.Object
120+
}
121+
appDetail.HealthStatus = resp.HealthStatus
122+
appDetail.SyncStatus = resp.SyncStatus
119123
return appDetail, nil
120124
}
121125

@@ -227,7 +231,7 @@ func (impl *ArgoApplicationReadServiceImpl) getResourceTreeForExternalCluster(cl
227231
}
228232

229233
func (impl *ArgoApplicationReadServiceImpl) ValidateArgoResourceRequest(ctx context.Context, appIdentifier *bean.ArgoAppIdentifier, request *k8s.K8sRequestBean) (bool, error) {
230-
app, err := impl.GetAppDetail(appIdentifier.AppName, appIdentifier.Namespace, appIdentifier.ClusterId)
234+
app, err := impl.GetAppDetailEA(ctx, appIdentifier.AppName, appIdentifier.Namespace, appIdentifier.ClusterId)
231235
if err != nil {
232236
impl.logger.Errorw("error in getting app detail", "err", err, "appDetails", appIdentifier)
233237
apiError := clientErrors.ConvertToApiError(err)
@@ -257,39 +261,40 @@ func (impl *ArgoApplicationReadServiceImpl) ValidateArgoResourceRequest(ctx cont
257261
appDetail := &gRPC.AppDetail{
258262
ResourceTreeResponse: app.ResourceTree,
259263
}
260-
return validateContainerNameIfReqd(valid, request, appDetail), nil
264+
if !valid {
265+
valid = validateContainerName(request, appDetail)
266+
}
267+
return valid, nil
261268
}
262269

263-
func validateContainerNameIfReqd(valid bool, request *k8s.K8sRequestBean, app *gRPC.AppDetail) bool {
264-
if !valid {
265-
requestContainerName := request.PodLogsRequest.ContainerName
266-
podName := request.ResourceIdentifier.Name
267-
for _, pod := range app.ResourceTreeResponse.PodMetadata {
268-
if pod.Name == podName {
270+
func validateContainerName(request *k8s.K8sRequestBean, app *gRPC.AppDetail) bool {
271+
requestContainerName := request.PodLogsRequest.ContainerName
272+
podName := request.ResourceIdentifier.Name
273+
for _, pod := range app.ResourceTreeResponse.PodMetadata {
274+
if pod.Name == podName {
269275

270-
// finding the container name in main Containers
271-
for _, container := range pod.Containers {
272-
if container == requestContainerName {
273-
return true
274-
}
276+
// finding the container name in main Containers
277+
for _, container := range pod.Containers {
278+
if container == requestContainerName {
279+
return true
275280
}
281+
}
276282

277-
// finding the container name in init containers
278-
for _, initContainer := range pod.InitContainers {
279-
if initContainer == requestContainerName {
280-
return true
281-
}
283+
// finding the container name in init containers
284+
for _, initContainer := range pod.InitContainers {
285+
if initContainer == requestContainerName {
286+
return true
282287
}
288+
}
283289

284-
// finding the container name in ephemeral containers
285-
for _, ephemeralContainer := range pod.EphemeralContainers {
286-
if ephemeralContainer.Name == requestContainerName {
287-
return true
288-
}
290+
// finding the container name in ephemeral containers
291+
for _, ephemeralContainer := range pod.EphemeralContainers {
292+
if ephemeralContainer.Name == requestContainerName {
293+
return true
289294
}
290-
291295
}
296+
292297
}
293298
}
294-
return valid
299+
return false
295300
}

0 commit comments

Comments
 (0)