Skip to content

Commit

Permalink
Merge pull request #404 from openinfradev/stack_template_fix
Browse files Browse the repository at this point in the history
feature. delete policies before stack deletion
  • Loading branch information
ktkfree authored Apr 19, 2024
2 parents 608f952 + 1811366 commit fc2bc3a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 46 deletions.
16 changes: 1 addition & 15 deletions api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down
16 changes: 1 addition & 15 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down
11 changes: 1 addition & 10 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 코드
Expand Down
23 changes: 20 additions & 3 deletions internal/delivery/http/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions internal/usecase/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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{
Expand Down
1 change: 1 addition & 0 deletions pkg/httpErrors/errorCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": "지정한 앨럿이 존재하지 않습니다.",
Expand Down

0 comments on commit fc2bc3a

Please sign in to comment.