From 6424780fdc631b07dea202aba26ef43f34fd6329 Mon Sep 17 00:00:00 2001 From: "taekyu.kang" Date: Fri, 19 Apr 2024 14:07:00 +0900 Subject: [PATCH 1/2] feature. delete policies before stack deletion --- api/swagger/docs.go | 16 +--------------- api/swagger/swagger.json | 16 +--------------- api/swagger/swagger.yaml | 11 +---------- internal/delivery/http/stack.go | 23 ++++++++++++++++++++--- internal/usecase/stack.go | 5 +++-- pkg/httpErrors/errorCode.go | 1 + 6 files changed, 27 insertions(+), 45 deletions(-) diff --git a/api/swagger/docs.go b/api/swagger/docs.go index eb24ce6f..5557423c 100644 --- a/api/swagger/docs.go +++ b/api/swagger/docs.go @@ -16308,20 +16308,6 @@ const docTemplate = `{ } } }, - "github_com_openinfradev_tks-api_pkg_domain_admin.PermittedOrganization": { - "type": "object", - "properties": { - "organizationId": { - "type": "string" - }, - "organizationName": { - "type": "string" - }, - "permitted": { - "type": "boolean" - } - } - }, "github_com_openinfradev_tks-api_pkg_domain_admin.PolicyTemplateResponse": { "type": "object", "properties": { @@ -16365,7 +16351,7 @@ const docTemplate = `{ "permittedOrganizations": { "type": "array", "items": { - "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain_admin.PermittedOrganization" + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleOrganizationResponse" } }, "rego": { diff --git a/api/swagger/swagger.json b/api/swagger/swagger.json index 7179dec1..807132e8 100644 --- a/api/swagger/swagger.json +++ b/api/swagger/swagger.json @@ -16302,20 +16302,6 @@ } } }, - "github_com_openinfradev_tks-api_pkg_domain_admin.PermittedOrganization": { - "type": "object", - "properties": { - "organizationId": { - "type": "string" - }, - "organizationName": { - "type": "string" - }, - "permitted": { - "type": "boolean" - } - } - }, "github_com_openinfradev_tks-api_pkg_domain_admin.PolicyTemplateResponse": { "type": "object", "properties": { @@ -16359,7 +16345,7 @@ "permittedOrganizations": { "type": "array", "items": { - "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain_admin.PermittedOrganization" + "$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleOrganizationResponse" } }, "rego": { diff --git a/api/swagger/swagger.yaml b/api/swagger/swagger.yaml index f89af619..fb81bda2 100644 --- a/api/swagger/swagger.yaml +++ b/api/swagger/swagger.yaml @@ -4183,15 +4183,6 @@ definitions: type: string type: array type: object - github_com_openinfradev_tks-api_pkg_domain_admin.PermittedOrganization: - properties: - organizationId: - type: string - organizationName: - type: string - permitted: - type: boolean - type: object github_com_openinfradev_tks-api_pkg_domain_admin.PolicyTemplateResponse: properties: createdAt: @@ -4222,7 +4213,7 @@ definitions: type: array permittedOrganizations: items: - $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain_admin.PermittedOrganization' + $ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.SimpleOrganizationResponse' type: array rego: example: rego 코드 diff --git a/internal/delivery/http/stack.go b/internal/delivery/http/stack.go index db949d64..1692669f 100644 --- a/internal/delivery/http/stack.go +++ b/internal/delivery/http/stack.go @@ -16,12 +16,14 @@ import ( ) type StackHandler struct { - usecase usecase.IStackUsecase + usecase usecase.IStackUsecase + usecasePolicy usecase.IPolicyUsecase } func NewStackHandler(h usecase.Usecase) *StackHandler { return &StackHandler{ - usecase: h.Stack, + usecase: h.Stack, + usecasePolicy: h.Policy, } } @@ -310,7 +312,22 @@ func (h *StackHandler) DeleteStack(w http.ResponseWriter, r *http.Request) { dto.ID = domain.StackId(strId) dto.OrganizationId = organizationId - err := h.usecase.Delete(r.Context(), dto) + // Delete Policies + policyIds, err := h.usecasePolicy.GetPolicyIDsByClusterID(r.Context(), domain.ClusterId(dto.ID)) + if err != nil { + ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "S_FAILED_DELETE_POLICIES", "")) + return + } + + if policyIds != nil && len(*policyIds) > 0 { + err = h.usecasePolicy.DeletePoliciesForClusterID(r.Context(), organizationId, domain.ClusterId(dto.ID), *policyIds) + if err != nil { + ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "S_FAILED_DELETE_POLICIES", "")) + return + } + } + + err = h.usecase.Delete(r.Context(), dto) if err != nil { ErrorJSON(w, r, err) return diff --git a/internal/usecase/stack.go b/internal/usecase/stack.go index 0410bd3d..13bb6635 100644 --- a/internal/usecase/stack.go +++ b/internal/usecase/stack.go @@ -248,13 +248,13 @@ func (u *StackUsecase) Get(ctx context.Context, stackId domain.StackId) (out mod return out, err } - stackResources, _ := u.dashbordUsecase.GetStacks(ctx, cluster.OrganizationId) out = reflectClusterToStack(ctx, cluster, appGroups) if organization.PrimaryClusterId == cluster.ID.String() { out.PrimaryCluster = true } + stackResources, _ := u.dashbordUsecase.GetStacks(ctx, cluster.OrganizationId) for _, resource := range stackResources { if resource.ID == domain.StackId(cluster.ID) { if err := serializer.Map(ctx, resource, &out.Resource); err != nil { @@ -433,6 +433,7 @@ func (u *StackUsecase) Delete(ctx context.Context, dto model.Stack) (err error) } } + // Check AppServing appsCnt, err := u.appServeAppRepo.GetNumOfAppsOnStack(ctx, dto.OrganizationId, dto.ID.String()) if err != nil { return errors.Wrap(err, "Failed to get numOfAppsOnStack") @@ -441,7 +442,7 @@ func (u *StackUsecase) Delete(ctx context.Context, dto model.Stack) (err error) return httpErrors.NewBadRequestError(fmt.Errorf("existed appServeApps in %s", dto.OrganizationId), "S_FAILED_DELETE_EXISTED_ASA", "") } - // [TODO] BYOH 삭제는 어떻게 처리하는게 좋은가? + // Policy 삭제 workflow := "tks-stack-delete" workflowId, err := u.argo.SumbitWorkflowFromWftpl(ctx, workflow, argowf.SubmitOptions{ diff --git a/pkg/httpErrors/errorCode.go b/pkg/httpErrors/errorCode.go index 175518be..ff17fbef 100644 --- a/pkg/httpErrors/errorCode.go +++ b/pkg/httpErrors/errorCode.go @@ -79,6 +79,7 @@ var errorMap = map[ErrorCode]string{ "S_INVALID_CLUSTER_URL": "BYOH 타입의 클러스터 생성은 반드시 userClusterEndpoint 값이 필요합니다.", "S_INVALID_CLUSTER_ID": "BYOH 타입의 클러스터 생성은 반드시 clusterId 값이 필요합니다.", "S_INVALID_CLOUD_SERVICE": "클라우드 서비스 타입이 잘못되었습니다.", + "S_FAILED_DELETE_POLICIES": "스택의 폴리시들을 삭제하는 실패하였습니다", // Alert "AL_NOT_FOUND_ALERT": "지정한 앨럿이 존재하지 않습니다.", From 1811366878240fd92abcca8526ce94603453de71 Mon Sep 17 00:00:00 2001 From: "taekyu.kang" Date: Fri, 19 Apr 2024 14:59:30 +0900 Subject: [PATCH 2/2] tirivial. add policy_ids to createCluster workflow --- internal/usecase/cluster.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/usecase/cluster.go b/internal/usecase/cluster.go index 2300b8dc..ce7574e8 100644 --- a/internal/usecase/cluster.go +++ b/internal/usecase/cluster.go @@ -198,7 +198,7 @@ func (u *ClusterUsecase) Create(ctx context.Context, dto model.Cluster) (cluster "cloud_account_id=" + tksCloudAccountId, "base_repo_branch=" + viper.GetString("revision"), "keycloak_url=" + viper.GetString("keycloak-address"), - //"manifest_repo_url=" + viper.GetString("git-base-url") + "/" + viper.GetString("git-account") + "/" + clusterId + "-manifests", + "policy_ids=" + strings.Join(dto.PolicyIds, ","), }, }) if err != nil {