From f1c09bab30ffa2847ed7474c257c4d8abab9253e Mon Sep 17 00:00:00 2001 From: "taekyu.kang" Date: Mon, 25 Sep 2023 19:25:18 +0900 Subject: [PATCH] feature. add stack resource info to fetching stacks. --- api/swagger/docs.go | 3 +++ api/swagger/swagger.json | 3 +++ api/swagger/swagger.yaml | 2 ++ internal/database/database.go | 3 +++ internal/repository/cluster.go | 4 ++-- internal/route/route.go | 24 ++++++++++++------------ internal/usecase/dashboard.go | 6 +++--- internal/usecase/stack.go | 15 ++++++++++++++- pkg/domain/stack.go | 2 ++ 9 files changed, 44 insertions(+), 18 deletions(-) diff --git a/api/swagger/docs.go b/api/swagger/docs.go index 6d5d58d3..a3b98ab8 100644 --- a/api/swagger/docs.go +++ b/api/swagger/docs.go @@ -5814,6 +5814,9 @@ const docTemplate = `{ "primaryCluster": { "type": "boolean" }, + "resource": { + "$ref": "#/definitions/domain.DashboardStackResponse" + }, "stackTemplate": { "$ref": "#/definitions/domain.SimpleStackTemplateResponse" }, diff --git a/api/swagger/swagger.json b/api/swagger/swagger.json index 5b065ef1..a0718d02 100644 --- a/api/swagger/swagger.json +++ b/api/swagger/swagger.json @@ -5807,6 +5807,9 @@ "primaryCluster": { "type": "boolean" }, + "resource": { + "$ref": "#/definitions/domain.DashboardStackResponse" + }, "stackTemplate": { "$ref": "#/definitions/domain.SimpleStackTemplateResponse" }, diff --git a/api/swagger/swagger.yaml b/api/swagger/swagger.yaml index 564ae5bb..e4e95012 100644 --- a/api/swagger/swagger.yaml +++ b/api/swagger/swagger.yaml @@ -1437,6 +1437,8 @@ definitions: type: string primaryCluster: type: boolean + resource: + $ref: '#/definitions/domain.DashboardStackResponse' stackTemplate: $ref: '#/definitions/domain.SimpleStackTemplateResponse' status: diff --git a/internal/database/database.go b/internal/database/database.go index 7d7ff222..6f483bcc 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -89,6 +89,9 @@ func migrateSchema(db *gorm.DB) error { if err := db.AutoMigrate(&repository.ClusterFavorite{}); err != nil { return err } + if err := db.AutoMigrate(&repository.ClusterHost{}); err != nil { + return err + } // Services if err := db.AutoMigrate(&repository.AppGroup{}); err != nil { diff --git a/internal/repository/cluster.go b/internal/repository/cluster.go index 725bc416..5b079db5 100644 --- a/internal/repository/cluster.go +++ b/internal/repository/cluster.go @@ -62,7 +62,7 @@ type Cluster struct { StackTemplateId uuid.UUID StackTemplate StackTemplate `gorm:"foreignKey:StackTemplateId"` Favorites *[]ClusterFavorite - ByohHosts *[]ClusterByohHost + ByohHosts *[]ClusterHost ClusterType domain.ClusterType `gorm:"default:0"` TksCpNode int TksCpNodeMax int @@ -94,7 +94,7 @@ type ClusterFavorite struct { User User `gorm:"foreignKey:UserId"` } -type ClusterByohHost struct { +type ClusterHost struct { ID uuid.UUID `gorm:"primarykey"` ClusterId domain.ClusterId Cluster Cluster `gorm:"foreignKey:ClusterId"` diff --git a/internal/route/route.go b/internal/route/route.go index 4f1f01ca..b86d9da2 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -157,18 +157,6 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/stack-templates/{stackTemplateId}", authMiddleware.Handle(http.HandlerFunc(stackTemplateHandler.UpdateStackTemplate))).Methods(http.MethodPut) r.Handle(API_PREFIX+API_VERSION+"/stack-templates/{stackTemplateId}", authMiddleware.Handle(http.HandlerFunc(stackTemplateHandler.DeleteStackTemplate))).Methods(http.MethodDelete) - stackHandler := delivery.NewStackHandler(usecase.NewStackUsecase(repoFactory, argoClient)) - r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStacks))).Methods(http.MethodGet) - r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks", authMiddleware.Handle(http.HandlerFunc(stackHandler.CreateStack))).Methods(http.MethodPost) - r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/name/{name}/existence", authMiddleware.Handle(http.HandlerFunc(stackHandler.CheckStackName))).Methods(http.MethodGet) - r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStack))).Methods(http.MethodGet) - r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.UpdateStack))).Methods(http.MethodPut) - r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.DeleteStack))).Methods(http.MethodDelete) - r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/kube-config", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStackKubeConfig))).Methods(http.MethodGet) - r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/status", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStackStatus))).Methods(http.MethodGet) - r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/favorite", authMiddleware.Handle(http.HandlerFunc(stackHandler.SetFavorite))).Methods(http.MethodPost) - r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/favorite", authMiddleware.Handle(http.HandlerFunc(stackHandler.DeleteFavorite))).Methods(http.MethodDelete) - dashboardHandler := delivery.NewDashboardHandler(usecase.NewDashboardUsecase(repoFactory, cache)) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/charts", authMiddleware.Handle(http.HandlerFunc(dashboardHandler.GetCharts))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/charts/{chartType}", authMiddleware.Handle(http.HandlerFunc(dashboardHandler.GetChart))).Methods(http.MethodGet) @@ -184,6 +172,18 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/alerts/{alertId}/actions", authMiddleware.Handle(http.HandlerFunc(alertHandler.CreateAlertAction))).Methods(http.MethodPost) //r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/alerts/{alertId}/actions/status", authMiddleware.Handle(http.HandlerFunc(alertHandler.UpdateAlertActionStatus))).Methods(http.MethodPatch) + stackHandler := delivery.NewStackHandler(usecase.NewStackUsecase(repoFactory, argoClient, usecase.NewDashboardUsecase(repoFactory, cache))) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStacks))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks", authMiddleware.Handle(http.HandlerFunc(stackHandler.CreateStack))).Methods(http.MethodPost) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/name/{name}/existence", authMiddleware.Handle(http.HandlerFunc(stackHandler.CheckStackName))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStack))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.UpdateStack))).Methods(http.MethodPut) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}", authMiddleware.Handle(http.HandlerFunc(stackHandler.DeleteStack))).Methods(http.MethodDelete) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/kube-config", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStackKubeConfig))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/status", authMiddleware.Handle(http.HandlerFunc(stackHandler.GetStackStatus))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/favorite", authMiddleware.Handle(http.HandlerFunc(stackHandler.SetFavorite))).Methods(http.MethodPost) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/favorite", authMiddleware.Handle(http.HandlerFunc(stackHandler.DeleteFavorite))).Methods(http.MethodDelete) + r.HandleFunc(API_PREFIX+API_VERSION+"/alerttest", alertHandler.CreateAlert).Methods(http.MethodPost) // assets r.PathPrefix("/api/").HandlerFunc(http.NotFound) diff --git a/internal/usecase/dashboard.go b/internal/usecase/dashboard.go index 28796978..b32c8ea1 100644 --- a/internal/usecase/dashboard.go +++ b/internal/usecase/dashboard.go @@ -111,13 +111,13 @@ func (u *DashboardUsecase) GetStacks(ctx context.Context, organizationId string) cpu := u.getStackCpu(stackCpu.Data.Result, cluster.ID.String()) if cpu != "" { - cpu = cpu + " %" + cpu = cpu + "%" } if memory != "" { - memory = memory + " %" + memory = memory + "%" } if disk != "" { - disk = disk + " %" + disk = disk + "%" } dashboardStack.Cpu = cpu diff --git a/internal/usecase/stack.go b/internal/usecase/stack.go index b5bf8a8d..f9088194 100644 --- a/internal/usecase/stack.go +++ b/internal/usecase/stack.go @@ -43,9 +43,10 @@ type StackUsecase struct { stackTemplateRepo repository.IStackTemplateRepository appServeAppRepo repository.IAppServeAppRepository argo argowf.ArgoClient + dashbordUsecase IDashboardUsecase } -func NewStackUsecase(r repository.Repository, argoClient argowf.ArgoClient) IStackUsecase { +func NewStackUsecase(r repository.Repository, argoClient argowf.ArgoClient, dashbordUsecase IDashboardUsecase) IStackUsecase { return &StackUsecase{ clusterRepo: r.Cluster, appGroupRepo: r.AppGroup, @@ -54,6 +55,7 @@ func NewStackUsecase(r repository.Repository, argoClient argowf.ArgoClient) ISta stackTemplateRepo: r.StackTemplate, appServeAppRepo: r.AppServeApp, argo: argoClient, + dashbordUsecase: dashbordUsecase, } } @@ -224,6 +226,8 @@ func (u *StackUsecase) Fetch(ctx context.Context, organizationId string, pg *pag return out, err } + stackResources, err := u.dashbordUsecase.GetStacks(ctx, organizationId) + for _, cluster := range clusters { appGroups, err := u.appGroupRepo.Fetch(cluster.ID, nil) if err != nil { @@ -246,6 +250,15 @@ func (u *StackUsecase) Fetch(ctx context.Context, organizationId string, pg *pag } } } + + for _, resource := range stackResources { + if resource.ID == domain.StackId(cluster.ID) { + if err := serializer.Map(resource, &outStack.Resource); err != nil { + log.Error(err) + } + } + } + out = append(out, outStack) } diff --git a/pkg/domain/stack.go b/pkg/domain/stack.go index efd6ced8..a6cefcad 100644 --- a/pkg/domain/stack.go +++ b/pkg/domain/stack.go @@ -93,6 +93,7 @@ type Stack = struct { UpdatedAt time.Time Favorited bool AdminClusterUrl string + Resource DashboardStackResponse } type StackConf struct { @@ -163,6 +164,7 @@ type StackResponse struct { Updator SimpleUserResponse `json:"updator,omitempty"` Favorited bool `json:"favorited"` AdminClusterUrl string `json:"adminClusterUrl,omitempty"` + Resource DashboardStackResponse `json:"resource,omitempty"` CreatedAt time.Time `json:"createdAt"` UpdatedAt time.Time `json:"updatedAt"` }