Skip to content

Commit

Permalink
Merge pull request #36 from openinfradev/stack_creation
Browse files Browse the repository at this point in the history
feature. fix stack creation
  • Loading branch information
ktkfree authored Apr 19, 2023
2 parents ecd545f + 738878d commit 987255d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
8 changes: 6 additions & 2 deletions internal/delivery/http/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ func (h *StackHandler) CreateStack(w http.ResponseWriter, r *http.Request) {
}
dto.OrganizationId = organizationId

err = h.usecase.Create(r.Context(), dto)
stackId, err := h.usecase.Create(r.Context(), dto)
if err != nil {
ErrorJSON(w, err)
return
}

ResponseJSON(w, http.StatusOK, nil)
out := domain.CreateStackResponse{
ID: stackId.String(),
}

ResponseJSON(w, http.StatusOK, out)
}

// GetStack godoc
Expand Down
33 changes: 19 additions & 14 deletions internal/usecase/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type IStackUsecase interface {
Get(stackId domain.StackId) (domain.Stack, error)
GetByName(organizationId string, name string) (domain.Stack, error)
Fetch(organizationId string) ([]domain.Stack, error)
Create(ctx context.Context, dto domain.Stack) (err error)
Create(ctx context.Context, dto domain.Stack) (stackId domain.StackId, err error)
Delete(ctx context.Context, dto domain.Stack) error
}

Expand All @@ -41,18 +41,18 @@ func NewStackUsecase(r repository.Repository, argoClient argowf.ArgoClient) ISta
}
}

func (u *StackUsecase) Create(ctx context.Context, dto domain.Stack) (err error) {
func (u *StackUsecase) Create(ctx context.Context, dto domain.Stack) (stackId domain.StackId, err error) {
user, ok := request.UserFrom(ctx)
if !ok {
return httpErrors.NewBadRequestError(fmt.Errorf("Invalid token"))
return "", httpErrors.NewBadRequestError(fmt.Errorf("Invalid token"))
}

// 요청한 사ㅇㅏ가 허가 받지 않은 사용자라면 block

// [TODO] check primary cluster
clusters, err := u.clusterRepo.FetchByOrganizationId(dto.OrganizationId)
if err != nil {
return httpErrors.NewBadRequestError(errors.Wrap(err, "Failed to get clusters"))
return "", httpErrors.NewBadRequestError(errors.Wrap(err, "Failed to get clusters"))
}
isPrimary := false
if len(clusters) == 0 {
Expand All @@ -62,7 +62,7 @@ func (u *StackUsecase) Create(ctx context.Context, dto domain.Stack) (err error)

stackTemplate, err := u.stackTemplateRepo.Get(dto.StackTemplateId)
if err != nil {
return httpErrors.NewInternalServerError(errors.Wrap(err, "Invalid stackTemplateId"))
return "", httpErrors.NewInternalServerError(errors.Wrap(err, "Invalid stackTemplateId"))
}

workflow := ""
Expand All @@ -72,7 +72,7 @@ func (u *StackUsecase) Create(ctx context.Context, dto domain.Stack) (err error)
workflow = "tks-stack-create-aws-msa"
} else {
log.Error("Invalid template : ", stackTemplate.Template)
return httpErrors.NewInternalServerError(fmt.Errorf("Invalid stackTemplate. %s", stackTemplate.Template))
return "", httpErrors.NewInternalServerError(fmt.Errorf("Invalid stackTemplate. %s", stackTemplate.Template))
}

workflowId, err := u.argo.SumbitWorkflowFromWftpl(workflow, argowf.SubmitOptions{
Expand All @@ -93,30 +93,35 @@ func (u *StackUsecase) Create(ctx context.Context, dto domain.Stack) (err error)
})
if err != nil {
log.Error(err)
return errors.Wrap(err, "Failed to call workflow. workflow")
return "", errors.Wrap(err, "Failed to call workflow. workflow")
}
log.Debug("Submitted workflow: ", workflowId)

// wait & get clusterId ( max 1min )
dto.ID = domain.StackId("")
for i := 0; i < 60; i++ {
time.Sleep(time.Second * 2)
time.Sleep(time.Second * 5)
workflow, err := u.argo.GetWorkflow("argo", workflowId)
if err != nil {
return err
return "", err
}

log.Debug("workflow ", workflow)
if workflow.Status.Phase != "" && workflow.Status.Phase != "Running" {
return fmt.Errorf("Invalid workflow status")
return "", fmt.Errorf("Invalid workflow status [%s]", workflow.Status.Phase)
}

if workflow.Status.Progress == "1/2" { // start creating cluster
time.Sleep(time.Second * 5) // Buffer
cluster, err := u.clusterRepo.GetByName(dto.OrganizationId, dto.Name)
if err != nil {
continue
}
if cluster.Name == dto.Name {
dto.ID = domain.StackId(cluster.ID)
break
}
}

// [TODO] need clusterId?
return nil
return dto.ID, nil
}

func (u *StackUsecase) Get(stackId domain.StackId) (out domain.Stack, err error) {
Expand Down

0 comments on commit 987255d

Please sign in to comment.