From dcb7b33a9ce19e0dd72e06c510c954b49f4bfaea Mon Sep 17 00:00:00 2001 From: Seungkyu Ahn Date: Mon, 22 Apr 2024 15:34:33 +0900 Subject: [PATCH 1/2] change workload dashboard return type --- internal/usecase/dashboard.go | 77 ++++------------------------------- pkg/domain/dashboard.go | 31 +++++++++----- 2 files changed, 29 insertions(+), 79 deletions(-) diff --git a/internal/usecase/dashboard.go b/internal/usecase/dashboard.go index def36d71..f97a0f48 100644 --- a/internal/usecase/dashboard.go +++ b/internal/usecase/dashboard.go @@ -870,11 +870,11 @@ func (u *DashboardUsecase) GetWorkload(ctx context.Context, organizationId strin return nil, err } - dwr := &domain.GetDashboardWorkloadResponse{} + dwr := &domain.GetDashboardWorkloadResponse{Title: "자원별 Pod 배포 현황"} - // Deployment count - var count int - query := fmt.Sprintf("count (kube_deployment_status_replicas_available{taco_cluster=~'%s'} != 0)", clusterIdStr) + // Deployment pod count + count := 0 + query := fmt.Sprintf("sum (kube_deployment_status_replicas_available{taco_cluster=~'%s'} )", clusterIdStr) wm, err := thanosClient.GetWorkload(ctx, query) if err != nil { return nil, err @@ -882,31 +882,8 @@ func (u *DashboardUsecase) GetWorkload(ctx context.Context, organizationId strin if len(wm.Data.Result) > 0 && len(wm.Data.Result[0].Value) > 1 { count, _ = strconv.Atoi(wm.Data.Result[0].Value[1].(string)) } - dwr.DeploymentCount = count - // Deployment pod count - count = 0 - query = fmt.Sprintf("sum (kube_deployment_status_replicas_available{taco_cluster=~'%s'} )", clusterIdStr) - wm, err = thanosClient.GetWorkload(ctx, query) - if err != nil { - return nil, err - } - if len(wm.Data.Result) > 0 && len(wm.Data.Result[0].Value) > 1 { - count, _ = strconv.Atoi(wm.Data.Result[0].Value[1].(string)) - } - dwr.DeploymentPodCount = count - - // StatefulSet count - count = 0 - query = fmt.Sprintf("count (kube_statefulset_status_replicas_available{taco_cluster=~'%s'} != 0)", clusterIdStr) - wm, err = thanosClient.GetWorkload(ctx, query) - if err != nil { - return nil, err - } - if len(wm.Data.Result) > 0 && len(wm.Data.Result[0].Value) > 1 { - count, _ = strconv.Atoi(wm.Data.Result[0].Value[1].(string)) - } - dwr.StatefulSetCount = count + dwr.Data = append(dwr.Data, domain.WorkloadData{Name: "Deployments", Value: count}) // StatefulSet pod count count = 0 @@ -918,19 +895,7 @@ func (u *DashboardUsecase) GetWorkload(ctx context.Context, organizationId strin if len(wm.Data.Result) > 0 && len(wm.Data.Result[0].Value) > 1 { count, _ = strconv.Atoi(wm.Data.Result[0].Value[1].(string)) } - dwr.StatefulSetPodCount = count - - // DaemonSet count - count = 0 - query = fmt.Sprintf("count (kube_daemonset_status_number_available{taco_cluster=~'%s'} != 0)", clusterIdStr) - wm, err = thanosClient.GetWorkload(ctx, query) - if err != nil { - return nil, err - } - if len(wm.Data.Result) > 0 && len(wm.Data.Result[0].Value) > 1 { - count, _ = strconv.Atoi(wm.Data.Result[0].Value[1].(string)) - } - dwr.DaemonSetCount = count + dwr.Data = append(dwr.Data, domain.WorkloadData{Name: "StatefulSets", Value: count}) // DaemonSet pod count count = 0 @@ -942,19 +907,7 @@ func (u *DashboardUsecase) GetWorkload(ctx context.Context, organizationId strin if len(wm.Data.Result) > 0 && len(wm.Data.Result[0].Value) > 1 { count, _ = strconv.Atoi(wm.Data.Result[0].Value[1].(string)) } - dwr.DaemonSetPodCount = count - - // CronJob count - count = 0 - query = fmt.Sprintf("count (kube_cronjob_status_active{taco_cluster=~'%s'} != 0)", clusterIdStr) - wm, err = thanosClient.GetWorkload(ctx, query) - if err != nil { - return nil, err - } - if len(wm.Data.Result) > 0 && len(wm.Data.Result[0].Value) > 1 { - count, _ = strconv.Atoi(wm.Data.Result[0].Value[1].(string)) - } - dwr.CronJobCount = count + dwr.Data = append(dwr.Data, domain.WorkloadData{Name: "DaemonSets", Value: count}) // CronJob pod count count = 0 @@ -966,19 +919,7 @@ func (u *DashboardUsecase) GetWorkload(ctx context.Context, organizationId strin if len(wm.Data.Result) > 0 && len(wm.Data.Result[0].Value) > 1 { count, _ = strconv.Atoi(wm.Data.Result[0].Value[1].(string)) } - dwr.CronJobPodCount = count - - // Job count - count = 0 - query = fmt.Sprintf("count (kube_job_status_active{taco_cluster=~'%s'} != 0)", clusterIdStr) - wm, err = thanosClient.GetWorkload(ctx, query) - if err != nil { - return nil, err - } - if len(wm.Data.Result) > 0 && len(wm.Data.Result[0].Value) > 1 { - count, _ = strconv.Atoi(wm.Data.Result[0].Value[1].(string)) - } - dwr.JobCount = count + dwr.Data = append(dwr.Data, domain.WorkloadData{Name: "CronJobs", Value: count}) // Job pod count count = 0 @@ -990,7 +931,7 @@ func (u *DashboardUsecase) GetWorkload(ctx context.Context, organizationId strin if len(wm.Data.Result) > 0 && len(wm.Data.Result[0].Value) > 1 { count, _ = strconv.Atoi(wm.Data.Result[0].Value[1].(string)) } - dwr.JobPodCount = count + dwr.Data = append(dwr.Data, domain.WorkloadData{Name: "Jobs", Value: count}) return dwr, nil } diff --git a/pkg/domain/dashboard.go b/pkg/domain/dashboard.go index 0f1c9a37..8aa6bb45 100644 --- a/pkg/domain/dashboard.go +++ b/pkg/domain/dashboard.go @@ -223,18 +223,27 @@ type GetDashboardPolicyStatisticsResponse struct { PolicyStatisticsResponse } -type GetDashboardWorkloadResponse struct { - DeploymentCount int `json:"deploymentCount"` - DeploymentPodCount int `json:"deploymentPodCount"` - StatefulSetCount int `json:"statefulSetCount"` - StatefulSetPodCount int `json:"statefulSetPodCount"` - DaemonSetCount int `json:"daemonSetCount"` - DaemonSetPodCount int `json:"daemonSetPodCount"` - CronJobCount int `json:"cronJobCount"` - CronJobPodCount int `json:"cronJobPodCount"` - JobCount int `json:"jobCount"` - JobPodCount int `json:"jobPodCount"` +type WorkloadData struct { + Name string `json:"name"` + Value int `json:"value"` } +type GetDashboardWorkloadResponse struct { + Title string `json:"title"` + Data []WorkloadData `json:"data"` +} + +//type GetDashboardWorkloadResponse struct { +// DeploymentCount int `json:"deploymentCount"` +// DeploymentPodCount int `json:"deploymentPodCount"` +// StatefulSetCount int `json:"statefulSetCount"` +// StatefulSetPodCount int `json:"statefulSetPodCount"` +// DaemonSetCount int `json:"daemonSetCount"` +// DaemonSetPodCount int `json:"daemonSetPodCount"` +// CronJobCount int `json:"cronJobCount"` +// CronJobPodCount int `json:"cronJobPodCount"` +// JobCount int `json:"jobCount"` +// JobPodCount int `json:"jobPodCount"` +//} type GetDashboardPolicyViolationTop5Response struct { GetDashboardPolicyViolationResponse From c5b63a1353661891e215b6f6863eaf4748100fca Mon Sep 17 00:00:00 2001 From: Seungkyu Ahn Date: Mon, 22 Apr 2024 15:35:37 +0900 Subject: [PATCH 2/2] change workload dashboard return type\n swagger generate --- api/swagger/docs.go | 46 ++++++++++++++++------------------------ api/swagger/swagger.json | 46 ++++++++++++++++------------------------ api/swagger/swagger.yaml | 33 ++++++++++++---------------- pkg/domain/dashboard.go | 13 ------------ 4 files changed, 49 insertions(+), 89 deletions(-) diff --git a/api/swagger/docs.go b/api/swagger/docs.go index f16d23a9..6558515d 100644 --- a/api/swagger/docs.go +++ b/api/swagger/docs.go @@ -12775,35 +12775,14 @@ const docTemplate = `{ "github_com_openinfradev_tks-api_pkg_domain.GetDashboardWorkloadResponse": { "type": "object", "properties": { - "cronJobCount": { - "type": "integer" - }, - "cronJobPodCount": { - "type": "integer" - }, - "daemonSetCount": { - "type": "integer" - }, - "daemonSetPodCount": { - "type": "integer" - }, - "deploymentCount": { - "type": "integer" - }, - "deploymentPodCount": { - "type": "integer" - }, - "jobCount": { - "type": "integer" - }, - "jobPodCount": { - "type": "integer" - }, - "statefulSetCount": { - "type": "integer" + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.WorkloadData" + } }, - "statefulSetPodCount": { - "type": "integer" + "title": { + "type": "string" } } }, @@ -16121,6 +16100,17 @@ const docTemplate = `{ } } }, + "github_com_openinfradev_tks-api_pkg_domain.WorkloadData": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "value": { + "type": "integer" + } + } + }, "github_com_openinfradev_tks-api_pkg_domain_admin.AddPermittedPolicyTemplatesForOrganizationRequest": { "type": "object", "properties": { diff --git a/api/swagger/swagger.json b/api/swagger/swagger.json index b35d6ae3..4cc6f13d 100644 --- a/api/swagger/swagger.json +++ b/api/swagger/swagger.json @@ -12769,35 +12769,14 @@ "github_com_openinfradev_tks-api_pkg_domain.GetDashboardWorkloadResponse": { "type": "object", "properties": { - "cronJobCount": { - "type": "integer" - }, - "cronJobPodCount": { - "type": "integer" - }, - "daemonSetCount": { - "type": "integer" - }, - "daemonSetPodCount": { - "type": "integer" - }, - "deploymentCount": { - "type": "integer" - }, - "deploymentPodCount": { - "type": "integer" - }, - "jobCount": { - "type": "integer" - }, - "jobPodCount": { - "type": "integer" - }, - "statefulSetCount": { - "type": "integer" + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.WorkloadData" + } }, - "statefulSetPodCount": { - "type": "integer" + "title": { + "type": "string" } } }, @@ -16115,6 +16094,17 @@ } } }, + "github_com_openinfradev_tks-api_pkg_domain.WorkloadData": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "value": { + "type": "integer" + } + } + }, "github_com_openinfradev_tks-api_pkg_domain_admin.AddPermittedPolicyTemplatesForOrganizationRequest": { "type": "object", "properties": { diff --git a/api/swagger/swagger.yaml b/api/swagger/swagger.yaml index e5f6fa25..5c42d056 100644 --- a/api/swagger/swagger.yaml +++ b/api/swagger/swagger.yaml @@ -1794,26 +1794,12 @@ definitions: type: object github_com_openinfradev_tks-api_pkg_domain.GetDashboardWorkloadResponse: properties: - cronJobCount: - type: integer - cronJobPodCount: - type: integer - daemonSetCount: - type: integer - daemonSetPodCount: - type: integer - deploymentCount: - type: integer - deploymentPodCount: - type: integer - jobCount: - type: integer - jobPodCount: - type: integer - statefulSetCount: - type: integer - statefulSetPodCount: - type: integer + data: + items: + $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.WorkloadData' + type: array + title: + type: string type: object github_com_openinfradev_tks-api_pkg_domain.GetMandatoryPoliciesResponse: properties: @@ -4021,6 +4007,13 @@ definitions: widgetKey: type: string type: object + github_com_openinfradev_tks-api_pkg_domain.WorkloadData: + properties: + name: + type: string + value: + type: integer + type: object github_com_openinfradev_tks-api_pkg_domain_admin.AddPermittedPolicyTemplatesForOrganizationRequest: properties: policyTemplateIds: diff --git a/pkg/domain/dashboard.go b/pkg/domain/dashboard.go index 8aa6bb45..e6cf09c9 100644 --- a/pkg/domain/dashboard.go +++ b/pkg/domain/dashboard.go @@ -232,19 +232,6 @@ type GetDashboardWorkloadResponse struct { Data []WorkloadData `json:"data"` } -//type GetDashboardWorkloadResponse struct { -// DeploymentCount int `json:"deploymentCount"` -// DeploymentPodCount int `json:"deploymentPodCount"` -// StatefulSetCount int `json:"statefulSetCount"` -// StatefulSetPodCount int `json:"statefulSetPodCount"` -// DaemonSetCount int `json:"daemonSetCount"` -// DaemonSetPodCount int `json:"daemonSetPodCount"` -// CronJobCount int `json:"cronJobCount"` -// CronJobPodCount int `json:"cronJobPodCount"` -// JobCount int `json:"jobCount"` -// JobPodCount int `json:"jobPodCount"` -//} - type GetDashboardPolicyViolationTop5Response struct { GetDashboardPolicyViolationResponse }