From 47b56308f54ff62ec8cfb38459c97a248b7d271a Mon Sep 17 00:00:00 2001 From: sangkenlee Date: Mon, 8 Apr 2024 15:58:51 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A0=95=EC=B1=85=20=ED=85=9C=ED=94=8C?= =?UTF-8?q?=EB=A6=BF=20=EB=B2=84=EC=A0=84=EC=97=85=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/delivery/api/endpoint.go | 8 +- .../delivery/api/generated_endpoints.go.go | 42 ++++----- internal/delivery/http/policy.go | 89 ++++++++++--------- internal/model/permission.go | 8 +- internal/policy-template/paramdef-util.go | 4 +- internal/repository/policy.go | 2 +- internal/route/route.go | 6 +- internal/usecase/policy.go | 34 ++++--- pkg/domain/policy.go | 10 +-- pkg/httpErrors/errorCode.go | 1 + 10 files changed, 109 insertions(+), 95 deletions(-) diff --git a/internal/delivery/api/endpoint.go b/internal/delivery/api/endpoint.go index 83477d83..463b406d 100644 --- a/internal/delivery/api/endpoint.go +++ b/internal/delivery/api/endpoint.go @@ -237,10 +237,10 @@ const ( Admin_AddPermittedPolicyTemplatesForOrganization Admin_DeletePermittedPolicyTemplatesForOrganization - // ClusterPolicyStatus - ListClusterPolicyStatus - GetClusterPolicyTemplateStatus - UpdateClusterPolicyTemplateStatus + // StackPolicyStatus + ListStackPolicyStatus + GetStackPolicyTemplateStatus + UpdateStackPolicyTemplateStatus // Policy GetMandatoryPolicies diff --git a/internal/delivery/api/generated_endpoints.go.go b/internal/delivery/api/generated_endpoints.go.go index 42cf4171..74b792b2 100644 --- a/internal/delivery/api/generated_endpoints.go.go +++ b/internal/delivery/api/generated_endpoints.go.go @@ -735,17 +735,17 @@ var ApiMap = map[Endpoint]EndpointInfo{ Name: "Admin_DeletePermittedPolicyTemplatesForOrganization", Group: "PolicyTemplate", }, - ListClusterPolicyStatus: { - Name: "ListClusterPolicyStatus", - Group: "ClusterPolicyStatus", + ListStackPolicyStatus: { + Name: "ListStackPolicyStatus", + Group: "StackPolicyStatus", }, - GetClusterPolicyTemplateStatus: { - Name: "GetClusterPolicyTemplateStatus", - Group: "ClusterPolicyStatus", + GetStackPolicyTemplateStatus: { + Name: "GetStackPolicyTemplateStatus", + Group: "StackPolicyStatus", }, - UpdateClusterPolicyTemplateStatus: { - Name: "UpdateClusterPolicyTemplateStatus", - Group: "ClusterPolicyStatus", + UpdateStackPolicyTemplateStatus: { + Name: "UpdateStackPolicyTemplateStatus", + Group: "StackPolicyStatus", }, GetMandatoryPolicies: { Name: "GetMandatoryPolicies", @@ -1244,12 +1244,12 @@ func (e Endpoint) String() string { return "Admin_AddPermittedPolicyTemplatesForOrganization" case Admin_DeletePermittedPolicyTemplatesForOrganization: return "Admin_DeletePermittedPolicyTemplatesForOrganization" - case ListClusterPolicyStatus: - return "ListClusterPolicyStatus" - case GetClusterPolicyTemplateStatus: - return "GetClusterPolicyTemplateStatus" - case UpdateClusterPolicyTemplateStatus: - return "UpdateClusterPolicyTemplateStatus" + case ListStackPolicyStatus: + return "ListStackPolicyStatus" + case GetStackPolicyTemplateStatus: + return "GetStackPolicyTemplateStatus" + case UpdateStackPolicyTemplateStatus: + return "UpdateStackPolicyTemplateStatus" case GetMandatoryPolicies: return "GetMandatoryPolicies" case SetMandatoryPolicies: @@ -1686,12 +1686,12 @@ func GetEndpoint(name string) Endpoint { return Admin_AddPermittedPolicyTemplatesForOrganization case "Admin_DeletePermittedPolicyTemplatesForOrganization": return Admin_DeletePermittedPolicyTemplatesForOrganization - case "ListClusterPolicyStatus": - return ListClusterPolicyStatus - case "GetClusterPolicyTemplateStatus": - return GetClusterPolicyTemplateStatus - case "UpdateClusterPolicyTemplateStatus": - return UpdateClusterPolicyTemplateStatus + case "ListStackPolicyStatus": + return ListStackPolicyStatus + case "GetStackPolicyTemplateStatus": + return GetStackPolicyTemplateStatus + case "UpdateStackPolicyTemplateStatus": + return UpdateStackPolicyTemplateStatus case "GetMandatoryPolicies": return GetMandatoryPolicies case "SetMandatoryPolicies": diff --git a/internal/delivery/http/policy.go b/internal/delivery/http/policy.go index 2134543e..c26d0ee5 100644 --- a/internal/delivery/http/policy.go +++ b/internal/delivery/http/policy.go @@ -32,9 +32,9 @@ type IPolicyHandler interface { GetMandatoryPolicies(w http.ResponseWriter, r *http.Request) SetMandatoryPolicies(w http.ResponseWriter, r *http.Request) ExistsPolicyName(w http.ResponseWriter, r *http.Request) - ListClusterPolicyStatus(w http.ResponseWriter, r *http.Request) - GetClusterPolicyTemplateStatus(w http.ResponseWriter, r *http.Request) - UpdateClusterPolicyTemplateStatus(w http.ResponseWriter, r *http.Request) + ListStackPolicyStatus(w http.ResponseWriter, r *http.Request) + GetStackPolicyTemplateStatus(w http.ResponseWriter, r *http.Request) + UpdateStackPolicyTemplateStatus(w http.ResponseWriter, r *http.Request) GetPolicyEdit(w http.ResponseWriter, r *http.Request) GetPolicyStatistics(w http.ResponseWriter, r *http.Request) AddPoliciesForStack(w http.ResponseWriter, r *http.Request) @@ -601,29 +601,30 @@ func (h *PolicyHandler) ExistsPolicyName(w http.ResponseWriter, r *http.Request) ResponseJSON(w, r, http.StatusOK, out) } -// ListClusterPolicyStatus godoc +// ListStackPolicyStatus godoc // -// @Tags ClusterPolicyStatus -// @Summary [ListClusterPolicyStatus] 클러스터의 정책과 정책 템플릿, 버전 조회 +// @Tags StackPolicyStatus +// @Summary [ListStackPolicyStatus] 클러스터의 정책과 정책 템플릿, 버전 조회 // @Description 클러스터의 정책과 정책 템플릿, 버전 등을 포함한 상태 목록을 조회한다. // @Accept json // @Produce json -// @Param clusterId path string true "클러스터 식별자(uuid)" -// @Param pageSize query string false "pageSize" -// @Param pageNumber query string false "pageNumber" -// @Param sortColumn query string false "sortColumn" -// @Param sortOrder query string false "sortOrder" -// @Param filters query []string false "filters" -// @Success 200 {object} domain.ListClusterPolicyStatusResponse -// @Router /clusters/{clusterId}/policy-status [get] +// @Param organizationId path string true "조직 식별자(o로 시작)" +// @Param stackId path string true "스택 식별자" +// @Param pageSize query string false "pageSize" +// @Param pageNumber query string false "pageNumber" +// @Param sortColumn query string false "sortColumn" +// @Param sortOrder query string false "sortOrder" +// @Param filters query []string false "filters" +// @Success 200 {object} domain.ListStackPolicyStatusResponse +// @Router /organizations/{organizationId}/stacks/{stackId}/policy-status [get] // @Security JWT -func (h *PolicyHandler) ListClusterPolicyStatus(w http.ResponseWriter, r *http.Request) { +func (h *PolicyHandler) ListStackPolicyStatus(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - clusterId, ok := vars["clusterId"] + stackId, ok := vars["stackId"] if !ok { ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid clusterId"), - "C_INVALID_CLUSTER_ID", "")) + "C_INVALID_STACK_ID", "")) return } @@ -631,39 +632,40 @@ func (h *PolicyHandler) ListClusterPolicyStatus(w http.ResponseWriter, r *http.R pg := pagination.NewPagination(&urlParams) - policyStatuses, err := h.usecase.ListClusterPolicyStatus(r.Context(), clusterId, pg) + policyStatuses, err := h.usecase.ListStackPolicyStatus(r.Context(), stackId, pg) if err != nil { ErrorJSON(w, r, err) return } - out := domain.ListClusterPolicyStatusResponse{ + out := domain.ListStackPolicyStatusResponse{ Polices: policyStatuses, } ResponseJSON(w, r, http.StatusOK, out) } -// GetClusterPolicyTemplateStatus godoc +// GetStackPolicyTemplateStatus godoc // -// @Tags ClusterPolicyStatus -// @Summary [GetClusterPolicyTemplateStatus] 클러스터 템플릿 상태 상세 조회 +// @Tags StackPolicyStatus +// @Summary [GetStackPolicyTemplateStatus] 클러스터 템플릿 상태 상세 조회 // @Description 템플릿의 클러스터 버전 등 상태를 조회한다. // @Accept json // @Produce json -// @Param clusterId path string true "클러스터 식별자(uuid)" -// @Param templateId path string true "정책 템플릿 식별자(uuid)" -// @Success 200 {object} domain.GetClusterPolicyTemplateStatusResponse -// @Router /clusters/{clusterId}/policy-templates/{policyTemplateId} [get] +// @Param organizationId path string true "조직 식별자(o로 시작)" +// @Param stackId path string true "스택 식별자" +// @Param policyTemplateId path string true "정책 템플릿 식별자(uuid)" +// @Success 200 {object} domain.GetStackPolicyTemplateStatusResponse +// @Router /organizations/{organizationId}/stacks/{stackId}/policy-templates/{policyTemplateId} [get] // @Security JWT -func (h *PolicyHandler) GetClusterPolicyTemplateStatus(w http.ResponseWriter, r *http.Request) { +func (h *PolicyHandler) GetStackPolicyTemplateStatus(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - clusterId, ok := vars["clusterId"] + stackId, ok := vars["stackId"] if !ok { ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid clusterId"), - "C_INVALID_CLUSTER_ID", "")) + "C_INVALID_STACK_ID", "")) return } @@ -680,7 +682,7 @@ func (h *PolicyHandler) GetClusterPolicyTemplateStatus(w http.ResponseWriter, r return } - out, err := h.usecase.GetClusterPolicyTemplateStatus(r.Context(), clusterId, id) + out, err := h.usecase.GetStackPolicyTemplateStatus(r.Context(), stackId, id) if err != nil { ErrorJSON(w, r, err) @@ -690,26 +692,27 @@ func (h *PolicyHandler) GetClusterPolicyTemplateStatus(w http.ResponseWriter, r ResponseJSON(w, r, http.StatusOK, out) } -// UpdateClusterPolicyTemplateStatus godoc +// UpdateStackPolicyTemplateStatus godoc // -// @Tags ClusterPolicyStatus -// @Summary [UpdateClusterPolicyTemplateStatus] 템플릿 버전 업데이트 +// @Tags StackPolicyStatus +// @Summary [UpdateStackPolicyTemplateStatus] 템플릿 버전 업데이트 // @Description 해당 템플릿의 버전 업데이트 및 연관된 정책의 새 기본값을 설정한다. // @Accept json // @Produce json -// @Param clusterId path string true "클러스터 식별자(uuid)" -// @Param templateId path string true "정책 템플릿 식별자(uuid)" -// @Param body body domain.UpdateClusterPolicyTemplateStatusRequest true "update cluster policy template status request" -// @Success 200 {object} nil -// @Router /clusters/{clusterId}/policy-templates/{policyTemplateId} [patch] +// @Param organizationId path string true "조직 식별자(o로 시작)" +// @Param stackId path string true "스택 식별자" +// @Param policyTemplateId path string true "정책 템플릿 식별자(uuid)" +// @Param body body domain.UpdateStackPolicyTemplateStatusRequest true "update stack policy template status request" +// @Success 200 {object} nil +// @Router /organizations/{organizationId}/stacks/{stackId}/policy-templates/{policyTemplateId} [patch] // @Security JWT -func (h *PolicyHandler) UpdateClusterPolicyTemplateStatus(w http.ResponseWriter, r *http.Request) { +func (h *PolicyHandler) UpdateStackPolicyTemplateStatus(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) - clusterId, ok := vars["clusterId"] + stackId, ok := vars["stackId"] if !ok { ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid clusterId"), - "C_INVALID_CLUSTER_ID", "")) + "C_INVALID_STACK_ID", "")) return } @@ -719,7 +722,7 @@ func (h *PolicyHandler) UpdateClusterPolicyTemplateStatus(w http.ResponseWriter, return } - input := domain.UpdateClusterPolicyTemplateStatusRequest{} + input := domain.UpdateStackPolicyTemplateStatusRequest{} err := UnmarshalRequestInput(r, &input) @@ -735,7 +738,7 @@ func (h *PolicyHandler) UpdateClusterPolicyTemplateStatus(w http.ResponseWriter, return } - err = h.usecase.UpdateClusterPolicyTemplateStatus(r.Context(), clusterId, id, + err = h.usecase.UpdateStackPolicyTemplateStatus(r.Context(), stackId, id, input.TemplateCurrentVersion, input.TemplateTargetVerson) if err != nil { diff --git a/internal/model/permission.go b/internal/model/permission.go index b81ec8a4..595596d3 100644 --- a/internal/model/permission.go +++ b/internal/model/permission.go @@ -285,9 +285,9 @@ func newPolicy() *Permission { api.Admin_ExistsPolicyTemplateName, api.Admin_ExistsPolicyTemplateKind, - // ClusterPolicyStatus - api.ListClusterPolicyStatus, - api.GetClusterPolicyTemplateStatus, + // StackPolicyStatus + api.ListStackPolicyStatus, + api.GetStackPolicyTemplateStatus, // Policy api.GetMandatoryPolicies, @@ -339,7 +339,7 @@ func newPolicy() *Permission { api.Admin_UpdatePolicyTemplate, // ClusterPolicyStatus - api.UpdateClusterPolicyTemplateStatus, + api.UpdateStackPolicyTemplateStatus, // Policy api.UpdatePolicy, diff --git a/internal/policy-template/paramdef-util.go b/internal/policy-template/paramdef-util.go index ccf4fa27..55603847 100644 --- a/internal/policy-template/paramdef-util.go +++ b/internal/policy-template/paramdef-util.go @@ -23,9 +23,7 @@ func GetNewParamDefs(paramdefs1 []*domain.ParameterDef, paramdefs2 []*domain.Par if paramdef1 == nil { // Not found, it's new parameter result = append(result, paramdef2) - } - - if !CompareParamDef(paramdef2, paramdef1) { + } else if !CompareParamDef(paramdef2, paramdef1) { return nil, fmt.Errorf("not compatible, parameter definition of '%s' is changed", paramdef2.Key) } } diff --git a/internal/repository/policy.go b/internal/repository/policy.go index 844e9afd..ca0e3cd1 100644 --- a/internal/repository/policy.go +++ b/internal/repository/policy.go @@ -124,7 +124,7 @@ func (r *PolicyRepository) FetchByClusterIdAndTemplaeId(ctx context.Context, clu Where("cluster_id = ?", clusterId) res := r.db.WithContext(ctx).Preload(clause.Associations). - Where("template_id = ?").Where("id in (?)", subQueryClusterId).Find(&out) + Where("template_id = ?", templateId).Where("id in (?)", subQueryClusterId).Find(&out) if res.Error != nil { return nil, res.Error diff --git a/internal/route/route.go b/internal/route/route.go index b8906bf6..2b0de38c 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -353,9 +353,9 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/policies", customMiddleware.Handle(internalApi.AddPoliciesForStack, http.HandlerFunc(policyHandler.AddPoliciesForStack))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/policies", customMiddleware.Handle(internalApi.DeletePoliciesForStack, http.HandlerFunc(policyHandler.DeletePoliciesForStack))).Methods(http.MethodPut) - r.Handle(API_PREFIX+API_VERSION+"/clusters/{clusterId}/policy-status", customMiddleware.Handle(internalApi.ListClusterPolicyStatus, http.HandlerFunc(policyHandler.ListClusterPolicyStatus))).Methods(http.MethodGet) - r.Handle(API_PREFIX+API_VERSION+"/clusters/{clusterId}/policy-templates/{policyTemplateId}", customMiddleware.Handle(internalApi.GetClusterPolicyTemplateStatus, http.HandlerFunc(policyHandler.GetClusterPolicyTemplateStatus))).Methods(http.MethodGet) - r.Handle(API_PREFIX+API_VERSION+"/clusters/{clusterId}/policy-templates/{policyTemplateId}", customMiddleware.Handle(internalApi.UpdateClusterPolicyTemplateStatus, http.HandlerFunc(policyHandler.UpdateClusterPolicyTemplateStatus))).Methods(http.MethodPatch) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/policy-status", customMiddleware.Handle(internalApi.ListStackPolicyStatus, http.HandlerFunc(policyHandler.ListStackPolicyStatus))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/policy-templates/{policyTemplateId}", customMiddleware.Handle(internalApi.GetStackPolicyTemplateStatus, http.HandlerFunc(policyHandler.GetStackPolicyTemplateStatus))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/policy-templates/{policyTemplateId}", customMiddleware.Handle(internalApi.UpdateStackPolicyTemplateStatus, http.HandlerFunc(policyHandler.UpdateStackPolicyTemplateStatus))).Methods(http.MethodPatch) // assets r.PathPrefix("/api/").HandlerFunc(http.NotFound) diff --git a/internal/usecase/policy.go b/internal/usecase/policy.go index 08732137..2a7eb41a 100644 --- a/internal/usecase/policy.go +++ b/internal/usecase/policy.go @@ -17,6 +17,7 @@ import ( "github.com/openinfradev/tks-api/pkg/domain" "github.com/openinfradev/tks-api/pkg/httpErrors" "github.com/openinfradev/tks-api/pkg/log" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/utils/strings/slices" ) @@ -34,9 +35,9 @@ type IPolicyUsecase interface { UpdatePolicyTargetClusters(ctx context.Context, organizationId string, policyId uuid.UUID, currentClusterIds []string, targetClusterIds []string) (err error) SetMandatoryPolicies(ctx context.Context, organizationId string, mandatoryPolicyIds []uuid.UUID, nonMandatoryPolicyIds []uuid.UUID) (err error) GetMandatoryPolicies(ctx context.Context, organizationId string) (response *domain.GetMandatoryPoliciesResponse, err error) - ListClusterPolicyStatus(ctx context.Context, clusterId string, pg *pagination.Pagination) (policyStatuses []domain.ClusterPolicyStatusResponse, err error) - GetClusterPolicyTemplateStatus(ctx context.Context, clusterId string, policyTemplateId uuid.UUID) (clusterPolicyTemplateStatusResponse *domain.GetClusterPolicyTemplateStatusResponse, err error) - UpdateClusterPolicyTemplateStatus(ctx context.Context, clusterId string, policyTemplateId uuid.UUID, + ListStackPolicyStatus(ctx context.Context, clusterId string, pg *pagination.Pagination) (policyStatuses []domain.StackPolicyStatusResponse, err error) + GetStackPolicyTemplateStatus(ctx context.Context, clusterId string, policyTemplateId uuid.UUID) (clusterPolicyTemplateStatusResponse *domain.GetStackPolicyTemplateStatusResponse, err error) + UpdateStackPolicyTemplateStatus(ctx context.Context, clusterId string, policyTemplateId uuid.UUID, currentVersion string, targetVerson string) (err error) GetPolicyStatistics(ctx context.Context, organizationId string) (response *domain.PolicyStatisticsResponse, err error) AddPoliciesForClusterID(ctx context.Context, organizationId string, clusterId domain.ClusterId, policyIds []uuid.UUID) (err error) @@ -509,7 +510,7 @@ func (u *PolicyUsecase) GetMandatoryPolicies(ctx context.Context, organizationId return &out, nil } -func (u *PolicyUsecase) ListClusterPolicyStatus(ctx context.Context, clusterId string, pg *pagination.Pagination) (policyStatuses []domain.ClusterPolicyStatusResponse, err error) { +func (u *PolicyUsecase) ListStackPolicyStatus(ctx context.Context, clusterId string, pg *pagination.Pagination) (policyStatuses []domain.StackPolicyStatusResponse, err error) { policies, err := u.repo.FetchByClusterId(ctx, clusterId, pg) if err != nil { @@ -530,7 +531,7 @@ func (u *PolicyUsecase) ListClusterPolicyStatus(ctx context.Context, clusterId s // return nil, err // } - result := make([]domain.ClusterPolicyStatusResponse, len(*policies)) + result := make([]domain.StackPolicyStatusResponse, len(*policies)) for i, policy := range *policies { if err := serializer.Map(ctx, policy, &result[i]); err != nil { @@ -562,7 +563,7 @@ func (u *PolicyUsecase) ListClusterPolicyStatus(ctx context.Context, clusterId s return result, nil } -func (u *PolicyUsecase) UpdateClusterPolicyTemplateStatus(ctx context.Context, clusterId string, policyTemplateId uuid.UUID, +func (u *PolicyUsecase) UpdateStackPolicyTemplateStatus(ctx context.Context, clusterId string, policyTemplateId uuid.UUID, currentVersion string, targetVerson string) (err error) { if currentVersion == targetVerson { // 버전 동일, 할일 없음 @@ -618,7 +619,7 @@ func (u *PolicyUsecase) UpdateClusterPolicyTemplateStatus(ctx context.Context, c return policytemplate.UpdateTksPolicyTemplateCR(ctx, primaryClusterId, tksPolicyTemplate) } -func (u *PolicyUsecase) GetClusterPolicyTemplateStatus(ctx context.Context, clusterId string, policyTemplateId uuid.UUID) (clusterPolicyTemplateStatusResponse *domain.GetClusterPolicyTemplateStatusResponse, err error) { +func (u *PolicyUsecase) GetStackPolicyTemplateStatus(ctx context.Context, clusterId string, policyTemplateId uuid.UUID) (stackPolicyTemplateStatusResponse *domain.GetStackPolicyTemplateStatusResponse, err error) { policies, err := u.repo.FetchByClusterIdAndTemplaeId(ctx, clusterId, policyTemplateId) if err != nil { @@ -630,6 +631,10 @@ func (u *PolicyUsecase) GetClusterPolicyTemplateStatus(ctx context.Context, clus return nil, err } + if latestTemplate == nil { + return nil, httpErrors.NewBadRequestError(err, "P_FAILED_FETCH_TEMPLATE", "") + } + // policies가 빈 목록일 수도 있으므로 policy의 organization 정보는 못 가져올 수도 있음 // 따라서 cluster의 Organization에서 primaryClusterId를 가져옴 cluster, err := u.clusterRepo.Get(ctx, domain.ClusterId(clusterId)) @@ -650,13 +655,20 @@ func (u *PolicyUsecase) GetClusterPolicyTemplateStatus(ctx context.Context, clus // return nil, fmt.Errorf("version not found in CR") // } + var version string + tksPolicyTemplateCR, err := policytemplate.GetTksPolicyTemplateCR(ctx, primaryClusterId, latestTemplate.ResoureName()) if err != nil { - return nil, err + if errors.IsNotFound(err) { + // CR이 배포되지 않았으므로 최신 버전이 현재 버전임(앞으로 설치될 모든 정책은 이 버전으로 설치되므로) + version = latestTemplate.Version + } else { + return nil, err + } + } else { + version = tksPolicyTemplateCR.Spec.Version } - version := tksPolicyTemplateCR.Spec.Version - currentTemplate, err := u.templateRepo.GetPolicyTemplateVersion(ctx, policyTemplateId, version) if err != nil { return nil, err @@ -682,7 +694,7 @@ func (u *PolicyUsecase) GetClusterPolicyTemplateStatus(ctx context.Context, clus affectedPolicies[i].PolicyParameters = parsed } - result := domain.GetClusterPolicyTemplateStatusResponse{ + result := domain.GetStackPolicyTemplateStatusResponse{ TemplateName: currentTemplate.TemplateName, TemplateId: policyTemplateId.String(), TemplateDescription: currentTemplate.Description, diff --git a/pkg/domain/policy.go b/pkg/domain/policy.go index 88b18343..5416f828 100644 --- a/pkg/domain/policy.go +++ b/pkg/domain/policy.go @@ -125,7 +125,7 @@ type SetMandatoryPoliciesRequest struct { Policies []MandatoryPolicyPatchInfo `json:"policies"` } -type ClusterPolicyStatusResponse struct { +type StackPolicyStatusResponse struct { PolicyName string `json:"policyName" example:"org 레이블 요구"` PolicyId string `json:"policyId" example:"0091fe9b-e44b-423d-9562-ac2b73089593"` PolicyDescription string `json:"policyDescription" example:"org 레이블 설정 여부 검사"` @@ -137,11 +137,11 @@ type ClusterPolicyStatusResponse struct { TemplateLatestVerson string `json:"templateLatestVerson" example:"v1.0.3"` } -type ListClusterPolicyStatusResponse struct { - Polices []ClusterPolicyStatusResponse `json:"polices"` +type ListStackPolicyStatusResponse struct { + Polices []StackPolicyStatusResponse `json:"polices"` } -type GetClusterPolicyTemplateStatusResponse struct { +type GetStackPolicyTemplateStatusResponse struct { TemplateName string `json:"templateName" example:"레이블 요구"` TemplateId string `json:"templateId" example:"708d1e5b-4e6f-40e9-87a3-329e2fd051a5"` TemplateDescription string `json:"templateDescription" example:"파라미터로 설정된 레이블 검사"` @@ -182,7 +182,7 @@ type PolicyUpdate struct { UpdatedPolicyParameters []UpdatedPolicyParameters `json:"updatedPolicyParameters"` } -type UpdateClusterPolicyTemplateStatusRequest struct { +type UpdateStackPolicyTemplateStatusRequest struct { TemplateCurrentVersion string `json:"templateCurrentVersion" example:"v1.0.1"` TemplateTargetVerson string `json:"templateTargetVerson" example:"v1.0.3"` // PolicyUpdate []PolicyUpdate `json:"policyUpdate"` diff --git a/pkg/httpErrors/errorCode.go b/pkg/httpErrors/errorCode.go index c48e0404..3b90a6a9 100644 --- a/pkg/httpErrors/errorCode.go +++ b/pkg/httpErrors/errorCode.go @@ -136,6 +136,7 @@ var errorMap = map[ErrorCode]string{ "P_INVALID_MATCH": "유효하지 않은 match 설정입니다. match 설정을 확인하세요.", "P_FAILED_FETCH_POLICY": "정책 ID에 해당하는 정책을 가져오는데 실패했습니다.", "P_FAILED_FETCH_CLUSTER": "정책의 클러스터 정보를 가져오는데 실패했습니다.", + "P_FAILED_FETCH_TEMPLATE": "정책의 클러스터 정보를 가져오는데 실패했습니다.", "P_CALL_TO_APPLY_KUBERNETES": "쿠버네티스 클러스터 호출에 실패했습니다.", "P_FAILED_TO_APPLY_KUBERNETES": "쿠버네티스 클러스터 변경사항 적용에 실패했습니다.", }