Skip to content

Commit 6d97706

Browse files
authored
feat: refactoring argo application service and common-lib constants (#5944)
* refactoring argo application service and common-lib constants * hash update common-lib
1 parent 00918bb commit 6d97706

File tree

39 files changed

+2610
-581
lines changed

39 files changed

+2610
-581
lines changed

Wire.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ func InitializeApp() (*App, error) {
202202
terminal.TerminalWireSet,
203203
build.BuildWireSet,
204204
deployment2.DeploymentWireSet,
205-
argoApplication.ArgoApplicationWireSet,
205+
argoApplication.ArgoApplicationWireSetFull,
206206
fluxApplication.FluxApplicationWireSet,
207207
eventProcessor.EventProcessorWireSet,
208208
workflow3.WorkflowWireSet,

api/appStore/AppStoreRouter.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ func (router AppStoreRouterImpl) Init(configRouter *mux.Router) {
8383
HandlerFunc(router.deployRestHandler.CheckAppExists).Methods("POST")
8484
configRouter.Path("/group/install").
8585
HandlerFunc(router.deployRestHandler.DeployBulk).Methods("POST")
86-
configRouter.Path("/installed-app/detail").Queries("installed-app-id", "{installed-app-id}").Queries("env-id", "{env-id}").
87-
HandlerFunc(router.deployRestHandler.FetchAppDetailsForInstalledApp).
88-
Methods("GET")
8986
configRouter.Path("/installed-app/delete/{installedAppId}/non-cascade").
9087
HandlerFunc(router.deployRestHandler.DeleteArgoInstalledAppWithNonCascade).
9188
Methods("DELETE")

api/appStore/InstalledAppRestHandler.go

Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ import (
4949
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
5050
"github.com/devtron-labs/devtron/pkg/auth/user"
5151
"github.com/devtron-labs/devtron/pkg/cluster"
52-
bean3 "github.com/devtron-labs/devtron/pkg/deployment/common/bean"
5352
"github.com/devtron-labs/devtron/util"
5453
"github.com/devtron-labs/devtron/util/argo"
5554
"github.com/devtron-labs/devtron/util/rbac"
@@ -65,7 +64,6 @@ type InstalledAppRestHandler interface {
6564
DeployBulk(w http.ResponseWriter, r *http.Request)
6665
CheckAppExists(w http.ResponseWriter, r *http.Request)
6766
DefaultComponentInstallation(w http.ResponseWriter, r *http.Request)
68-
FetchAppDetailsForInstalledApp(w http.ResponseWriter, r *http.Request)
6967
DeleteArgoInstalledAppWithNonCascade(w http.ResponseWriter, r *http.Request)
7068
FetchAppDetailsForInstalledAppV2(w http.ResponseWriter, r *http.Request)
7169
FetchResourceTree(w http.ResponseWriter, r *http.Request)
@@ -692,90 +690,6 @@ func (handler *InstalledAppRestHandlerImpl) checkNotesAuth(token string, appName
692690
return ok
693691
}
694692

695-
func (handler *InstalledAppRestHandlerImpl) FetchAppDetailsForInstalledApp(w http.ResponseWriter, r *http.Request) {
696-
userId, err := handler.userAuthService.GetLoggedInUser(r)
697-
if userId == 0 || err != nil {
698-
common.WriteJsonResp(w, err, nil, http.StatusUnauthorized)
699-
return
700-
}
701-
702-
vars := mux.Vars(r)
703-
installedAppId, err := strconv.Atoi(vars["installed-app-id"])
704-
if err != nil {
705-
handler.Logger.Errorw("request err, FetchAppDetailsForInstalledApp", "err", err, "installedAppId", installedAppId)
706-
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
707-
return
708-
}
709-
token := r.Header.Get("token")
710-
envId, err := strconv.Atoi(vars["env-id"])
711-
if err != nil {
712-
handler.Logger.Errorw("request err, FetchAppDetailsForInstalledApp", "err", err, "installedAppId", installedAppId, "envId", envId)
713-
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
714-
return
715-
}
716-
handler.Logger.Infow("request payload, FetchAppDetailsForInstalledApp, app store", "installedAppId", installedAppId, "envId", envId)
717-
718-
installedApp, err := handler.installedAppService.GetInstalledAppById(installedAppId)
719-
if err == pg.ErrNoRows {
720-
common.WriteJsonResp(w, err, "App not found in database", http.StatusBadRequest)
721-
return
722-
}
723-
if util3.IsExternalChartStoreApp(installedApp.App.DisplayName) {
724-
//this is external app case where app_name is a unique identifier, and we want to fetch resource based on display_name
725-
handler.installedAppService.ChangeAppNameToDisplayNameForInstalledApp(installedApp)
726-
}
727-
728-
appDetail, err := handler.installedAppService.FindAppDetailsForAppstoreApplication(installedAppId, envId)
729-
if err != nil {
730-
handler.Logger.Errorw("service err, FetchAppDetailsForInstalledApp, app store", "err", err, "installedAppId", installedAppId, "envId", envId)
731-
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
732-
return
733-
}
734-
735-
//rbac block starts from here
736-
object, object2 := handler.enforcerUtil.GetHelmObjectByAppNameAndEnvId(appDetail.AppName, appDetail.EnvironmentId)
737-
738-
var ok bool
739-
740-
if object2 == "" {
741-
ok = handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionGet, object)
742-
} else {
743-
ok = handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionGet, object) || handler.enforcer.Enforce(token, casbin.ResourceHelmApp, casbin.ActionGet, object2)
744-
}
745-
746-
if !ok {
747-
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
748-
return
749-
}
750-
//rback block ends here
751-
resourceTreeAndNotesContainer := bean2.AppDetailsContainer{}
752-
resourceTreeAndNotesContainer.ResourceTree = map[string]interface{}{}
753-
754-
if len(installedApp.App.AppName) > 0 && len(installedApp.Environment.Name) > 0 {
755-
err = handler.fetchResourceTree(w, r, &resourceTreeAndNotesContainer, *installedApp, appDetail.DeploymentConfig, "", "")
756-
if appDetail.DeploymentAppType == util2.PIPELINE_DEPLOYMENT_TYPE_ACD {
757-
apiError, ok := err.(*util2.ApiError)
758-
if ok && apiError != nil {
759-
if apiError.Code == constants.AppDetailResourceTreeNotFound && installedApp.DeploymentAppDeleteRequest == true {
760-
// TODO refactoring: should be performed through nats
761-
err = handler.appStoreDeploymentService.MarkGitOpsInstalledAppsDeletedIfArgoAppIsDeleted(installedAppId, envId)
762-
appDeleteErr, appDeleteErrOk := err.(*util2.ApiError)
763-
if appDeleteErrOk && appDeleteErr != nil {
764-
handler.Logger.Errorw(appDeleteErr.InternalMessage)
765-
return
766-
}
767-
}
768-
}
769-
} else if err != nil {
770-
common.WriteJsonResp(w, fmt.Errorf("error in fetching resource tree"), nil, http.StatusInternalServerError)
771-
return
772-
}
773-
}
774-
appDetail.ResourceTree = resourceTreeAndNotesContainer.ResourceTree
775-
appDetail.Notes = resourceTreeAndNotesContainer.Notes
776-
common.WriteJsonResp(w, nil, appDetail, http.StatusOK)
777-
}
778-
779693
func (handler *InstalledAppRestHandlerImpl) FetchAppDetailsForInstalledAppV2(w http.ResponseWriter, r *http.Request) {
780694
userId, err := handler.userAuthService.GetLoggedInUser(r)
781695
if userId == 0 || err != nil {
@@ -820,6 +734,8 @@ func (handler *InstalledAppRestHandlerImpl) FetchAppDetailsForInstalledAppV2(w h
820734
}
821735

822736
func (handler *InstalledAppRestHandlerImpl) FetchResourceTree(w http.ResponseWriter, r *http.Request) {
737+
token := r.Header.Get("token")
738+
ctx := context.WithValue(r.Context(), "token", token)
823739
userId, err := handler.userAuthService.GetLoggedInUser(r)
824740
if userId == 0 || err != nil {
825741
common.WriteJsonResp(w, err, nil, http.StatusUnauthorized)
@@ -853,7 +769,6 @@ func (handler *InstalledAppRestHandlerImpl) FetchResourceTree(w http.ResponseWri
853769
common.WriteJsonResp(w, nil, nil, http.StatusOK)
854770
return
855771
}
856-
token := r.Header.Get("token")
857772
object, object2 := handler.enforcerUtil.GetHelmObjectByAppNameAndEnvId(installedApp.App.AppName, installedApp.EnvironmentId)
858773
var ok bool
859774
if object2 == "" {
@@ -876,7 +791,8 @@ func (handler *InstalledAppRestHandlerImpl) FetchResourceTree(w http.ResponseWri
876791
resourceTreeAndNotesContainer.ResourceTree = map[string]interface{}{}
877792

878793
if len(installedApp.App.AppName) > 0 && len(installedApp.Environment.Name) > 0 {
879-
err = handler.fetchResourceTree(w, r, &resourceTreeAndNotesContainer, *installedApp, appDetail.DeploymentConfig, appDetail.HelmReleaseInstallStatus, appDetail.Status)
794+
cn, _ := w.(http.CloseNotifier)
795+
err = handler.installedAppResourceService.FetchResourceTree(ctx, cn, &resourceTreeAndNotesContainer, *installedApp, appDetail.DeploymentConfig, appDetail.HelmReleaseInstallStatus, appDetail.Status)
880796
if appDetail.DeploymentAppType == util2.PIPELINE_DEPLOYMENT_TYPE_ACD {
881797
//resource tree has been fetched now prepare to sync application deployment status with this resource tree call
882798
handler.syncDeploymentStatusWithResourceTreeCall(appDetail)
@@ -968,13 +884,6 @@ func (handler *InstalledAppRestHandlerImpl) FetchResourceTreeForACDApp(w http.Re
968884
common.WriteJsonResp(w, err, appDetail, http.StatusOK)
969885
}
970886

971-
func (handler *InstalledAppRestHandlerImpl) fetchResourceTree(w http.ResponseWriter, r *http.Request, resourceTreeAndNotesContainer *bean2.AppDetailsContainer, installedApp repository.InstalledApps, deploymentConfig *bean3.DeploymentConfig, helmReleaseInstallStatus string, status string) error {
972-
ctx := r.Context()
973-
cn, _ := w.(http.CloseNotifier)
974-
err := handler.installedAppResourceService.FetchResourceTree(ctx, cn, resourceTreeAndNotesContainer, installedApp, deploymentConfig, helmReleaseInstallStatus, status)
975-
return err
976-
}
977-
978887
func (handler *InstalledAppRestHandlerImpl) fetchResourceTreeWithHibernateForACD(w http.ResponseWriter, r *http.Request, appDetail *bean2.AppDetailContainer) {
979888
ctx := r.Context()
980889
cn, _ := w.(http.CloseNotifier)

api/argoApplication/wire_argoApplication.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,21 @@ import (
2222
"github.com/google/wire"
2323
)
2424

25-
var ArgoApplicationWireSet = wire.NewSet(
25+
var ArgoApplicationWireSetFull = wire.NewSet(
26+
read.NewArgoApplicationReadServiceImpl,
27+
wire.Bind(new(read.ArgoApplicationReadService), new(*read.ArgoApplicationReadServiceImpl)),
28+
29+
argoApplication.NewArgoApplicationServiceExtendedServiceImpl,
30+
wire.Bind(new(argoApplication.ArgoApplicationService), new(*argoApplication.ArgoApplicationServiceExtendedImpl)),
31+
32+
NewArgoApplicationRestHandlerImpl,
33+
wire.Bind(new(ArgoApplicationRestHandler), new(*ArgoApplicationRestHandlerImpl)),
34+
35+
NewArgoApplicationRouterImpl,
36+
wire.Bind(new(ArgoApplicationRouter), new(*ArgoApplicationRouterImpl)),
37+
)
38+
39+
var ArgoApplicationWireSetEA = wire.NewSet(
2640
read.NewArgoApplicationReadServiceImpl,
2741
wire.Bind(new(read.ArgoApplicationReadService), new(*read.ArgoApplicationReadServiceImpl)),
2842

api/helm-app/service/HelmAppService.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"github.com/devtron-labs/common-lib/utils/k8s"
24+
"github.com/devtron-labs/common-lib/utils/k8s/commonBean"
2425
"github.com/devtron-labs/devtron/api/helm-app/bean"
2526
"github.com/devtron-labs/devtron/api/helm-app/gRPC"
2627
"github.com/devtron-labs/devtron/api/helm-app/models"
@@ -234,15 +235,15 @@ func (impl *HelmAppServiceImpl) GetClusterConf(clusterId int) (*gRPC.ClusterConf
234235
}
235236
config := &gRPC.ClusterConfig{
236237
ApiServerUrl: cluster.ServerUrl,
237-
Token: cluster.Config[k8s.BearerToken],
238+
Token: cluster.Config[commonBean.BearerToken],
238239
ClusterId: int32(cluster.Id),
239240
ClusterName: cluster.ClusterName,
240241
InsecureSkipTLSVerify: cluster.InsecureSkipTLSVerify,
241242
}
242243
if cluster.InsecureSkipTLSVerify == false {
243-
config.KeyData = cluster.Config[k8s.TlsKey]
244-
config.CertData = cluster.Config[k8s.CertData]
245-
config.CaData = cluster.Config[k8s.CertificateAuthorityData]
244+
config.KeyData = cluster.Config[commonBean.TlsKey]
245+
config.CertData = cluster.Config[commonBean.CertData]
246+
config.CaData = cluster.Config[commonBean.CertificateAuthorityData]
246247
}
247248
return config, nil
248249
}
@@ -1095,15 +1096,15 @@ func (impl *HelmAppServiceImpl) listApplications(ctx context.Context, clusterIds
10951096
for _, clusterDetail := range clusters {
10961097
config := &gRPC.ClusterConfig{
10971098
ApiServerUrl: clusterDetail.ServerUrl,
1098-
Token: clusterDetail.Config[k8s.BearerToken],
1099+
Token: clusterDetail.Config[commonBean.BearerToken],
10991100
ClusterId: int32(clusterDetail.Id),
11001101
ClusterName: clusterDetail.ClusterName,
11011102
InsecureSkipTLSVerify: clusterDetail.InsecureSkipTLSVerify,
11021103
}
11031104
if clusterDetail.InsecureSkipTLSVerify == false {
1104-
config.KeyData = clusterDetail.Config[k8s.TlsKey]
1105-
config.CertData = clusterDetail.Config[k8s.CertData]
1106-
config.CaData = clusterDetail.Config[k8s.CertificateAuthorityData]
1105+
config.KeyData = clusterDetail.Config[commonBean.TlsKey]
1106+
config.CertData = clusterDetail.Config[commonBean.CertData]
1107+
config.CaData = clusterDetail.Config[commonBean.CertificateAuthorityData]
11071108
}
11081109
req.Clusters = append(req.Clusters, config)
11091110
}

api/restHandler/app/appList/AppListingRestHandler.go

Lines changed: 5 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/service/FullMode"
4646
"github.com/devtron-labs/devtron/pkg/appStore/installedApp/service/FullMode/resource"
4747
util4 "github.com/devtron-labs/devtron/pkg/appStore/util"
48+
argoApplication2 "github.com/devtron-labs/devtron/pkg/argoApplication"
4849
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
4950
"github.com/devtron-labs/devtron/pkg/auth/user"
5051
"github.com/devtron-labs/devtron/pkg/cluster"
@@ -72,7 +73,6 @@ import (
7273
)
7374

7475
type AppListingRestHandler interface {
75-
FetchAppDetails(w http.ResponseWriter, r *http.Request)
7676
FetchJobs(w http.ResponseWriter, r *http.Request)
7777
FetchJobOverviewCiPipelines(w http.ResponseWriter, r *http.Request)
7878
FetchAppDetailsV2(w http.ResponseWriter, r *http.Request)
@@ -113,6 +113,7 @@ type AppListingRestHandlerImpl struct {
113113
k8sApplicationService application3.K8sApplicationService
114114
deploymentTemplateService generateManifest.DeploymentTemplateService
115115
deploymentConfigService common2.DeploymentConfigService
116+
argoApplicationService argoApplication2.ArgoApplicationService
116117
}
117118

118119
type AppStatus struct {
@@ -146,6 +147,7 @@ func NewAppListingRestHandlerImpl(application application.ServiceClient,
146147
k8sApplicationService application3.K8sApplicationService,
147148
deploymentTemplateService generateManifest.DeploymentTemplateService,
148149
deploymentConfigService common2.DeploymentConfigService,
150+
argoApplicationService argoApplication2.ArgoApplicationService,
149151
) *AppListingRestHandlerImpl {
150152
appListingHandler := &AppListingRestHandlerImpl{
151153
application: application,
@@ -170,6 +172,7 @@ func NewAppListingRestHandlerImpl(application application.ServiceClient,
170172
k8sApplicationService: k8sApplicationService,
171173
deploymentTemplateService: deploymentTemplateService,
172174
deploymentConfigService: deploymentConfigService,
175+
argoApplicationService: argoApplicationService,
173176
}
174177
return appListingHandler
175178
}
@@ -498,86 +501,6 @@ func (handler AppListingRestHandlerImpl) FetchOverviewAppsByEnvironment(w http.R
498501

499502
}
500503

501-
func (handler AppListingRestHandlerImpl) FetchAppDetails(w http.ResponseWriter, r *http.Request) {
502-
vars := mux.Vars(r)
503-
token := r.Header.Get("token")
504-
appId, err := strconv.Atoi(vars["app-id"])
505-
if err != nil {
506-
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
507-
return
508-
}
509-
510-
envId, err := strconv.Atoi(vars["env-id"])
511-
if err != nil {
512-
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
513-
return
514-
}
515-
pipelines, err := handler.pipelineRepository.FindActiveByAppIdAndEnvironmentId(appId, envId)
516-
if err == pg.ErrNoRows {
517-
common.WriteJsonResp(w, err, "pipeline Not found in database", http.StatusNotFound)
518-
return
519-
}
520-
if err != nil {
521-
handler.logger.Errorw("error in fetching pipelines from db", "appId", appId, "envId", envId)
522-
common.WriteJsonResp(w, err, "error in fetching pipeline from database", http.StatusInternalServerError)
523-
return
524-
}
525-
if len(pipelines) == 0 {
526-
common.WriteJsonResp(w, fmt.Errorf("app deleted"), nil, http.StatusNotFound)
527-
return
528-
}
529-
if len(pipelines) != 1 {
530-
common.WriteJsonResp(w, err, "multiple pipelines found for an envId", http.StatusBadRequest)
531-
return
532-
}
533-
cdPipeline := pipelines[0]
534-
appDetail, err := handler.appListingService.FetchAppDetails(r.Context(), appId, envId)
535-
if err != nil {
536-
handler.logger.Errorw("service err, FetchAppDetails", "err", err, "appId", appId, "envId", envId)
537-
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
538-
return
539-
}
540-
541-
object := handler.enforcerUtil.GetAppRBACNameByAppId(appId)
542-
if ok := handler.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, object); !ok {
543-
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
544-
return
545-
}
546-
acdToken, err := handler.argoUserService.GetLatestDevtronArgoCdUserToken()
547-
if err != nil {
548-
common.WriteJsonResp(w, fmt.Errorf("error in getting acd token"), nil, http.StatusInternalServerError)
549-
return
550-
}
551-
envDeploymentConfig, err := handler.deploymentConfigService.GetConfigForDevtronApps(appId, envId)
552-
if err != nil {
553-
handler.logger.Errorw("error in fetching deployment config", "appId", appId, "envId", envId, "err", err)
554-
common.WriteJsonResp(w, fmt.Errorf("error in getting deployment config for env"), nil, http.StatusInternalServerError)
555-
return
556-
}
557-
resourceTree, err := handler.fetchResourceTree(w, r, appId, envId, acdToken, cdPipeline, envDeploymentConfig)
558-
if appDetail.DeploymentAppType == util.PIPELINE_DEPLOYMENT_TYPE_ACD {
559-
apiError, ok := err.(*util.ApiError)
560-
if ok && apiError != nil {
561-
if apiError.Code == constants.AppDetailResourceTreeNotFound && appDetail.DeploymentAppDeleteRequest == true {
562-
acdAppFound, _ := handler.pipeline.MarkGitOpsDevtronAppsDeletedWhereArgoAppIsDeleted(acdToken, cdPipeline)
563-
if acdAppFound {
564-
common.WriteJsonResp(w, fmt.Errorf("unable to fetch resource tree"), nil, http.StatusInternalServerError)
565-
return
566-
} else {
567-
common.WriteJsonResp(w, fmt.Errorf("app deleted"), nil, http.StatusNotFound)
568-
return
569-
}
570-
}
571-
}
572-
}
573-
if err != nil {
574-
common.WriteJsonResp(w, fmt.Errorf("unable to fetch resource tree"), nil, http.StatusInternalServerError)
575-
return
576-
}
577-
appDetail.ResourceTree = resourceTree
578-
common.WriteJsonResp(w, err, appDetail, http.StatusOK)
579-
}
580-
581504
func (handler AppListingRestHandlerImpl) FetchAppDetailsV2(w http.ResponseWriter, r *http.Request) {
582505
vars := mux.Vars(r)
583506
token := r.Header.Get("token")
@@ -1040,7 +963,7 @@ func (handler AppListingRestHandlerImpl) fetchResourceTree(w http.ResponseWriter
1040963
defer cancel()
1041964
ctx = context.WithValue(ctx, "token", acdToken)
1042965
start := time.Now()
1043-
resp, err := handler.application.ResourceTree(ctx, query)
966+
resp, err := handler.argoApplicationService.ResourceTree(ctx, query)
1044967
elapsed := time.Since(start)
1045968
handler.logger.Debugw("FetchAppDetailsV2, time elapsed in fetching application for environment ", "elapsed", elapsed, "appId", appId, "envId", envId)
1046969
if err != nil {

api/router/app/AppRouter.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,6 @@ func (router AppRouterImpl) InitAppRouter(AppRouter *mux.Router) {
111111
HandlerFunc(router.appListingRestHandler.GetHostUrlsByBatch).
112112
Methods("GET")
113113

114-
//This API used for fetch app details, not deployment details
115-
AppRouter.Path("/detail").
116-
Queries("app-id", "{app-id}").
117-
Queries("env-id", "{env-id}").
118-
HandlerFunc(router.appListingRestHandler.FetchAppDetails).
119-
Methods("GET")
120-
121114
AppRouter.Path("/detail/v2").Queries("app-id", "{app-id}").
122115
Queries("env-id", "{env-id}").
123116
HandlerFunc(router.appListingRestHandler.FetchAppDetailsV2).

0 commit comments

Comments
 (0)