Skip to content

Commit

Permalink
feature. add stack status
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Sep 20, 2023
1 parent f624ab0 commit 8ec4966
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/openinfradev/tks-batch/internal/cluster"
"github.com/openinfradev/tks-batch/internal/database"
"github.com/openinfradev/tks-batch/internal/organization"
"github.com/openinfradev/tks-batch/internal/stack"
)

const INTERVAL_SEC = 1
Expand All @@ -25,6 +26,7 @@ var (
applicationAccessor *application.ApplicationAccessor
cloudAccountAccessor *cloudAccount.CloudAccountAccessor
organizationAccessor *organization.OrganizationAccessor
stackAccessor *stack.StackAccessor
)

func init() {
Expand Down Expand Up @@ -62,6 +64,7 @@ func main() {
applicationAccessor = application.New(db)
cloudAccountAccessor = cloudAccount.New(db)
organizationAccessor = organization.New(db)
stackAccessor = stack.New(db)

// initialize external clients
argowfClient, err = argo.New(viper.GetString("argo-address"), viper.GetInt("argo-port"), false, "")
Expand All @@ -86,6 +89,10 @@ func main() {
if err != nil {
log.Error(err)
}
err = processStackStatus()
if err != nil {
log.Error(err)
}

time.Sleep(time.Second * INTERVAL_SEC)
}
Expand Down
84 changes: 84 additions & 0 deletions cmd/server/stack_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package main

import (
"fmt"

"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/log"
)

func processStackStatus() error {
// get stacks
stacks, err := stackAccessor.GetIncompleteStacks()
if err != nil {
return err
}
if len(stacks) == 0 {
return nil
}
log.Info("stacks : ", stacks)

for i := range stacks {
stack := stacks[i]

stackId := stack.ID
workflowId := stack.WorkflowId
status := stack.Status
statusDesc := stack.StatusDesc

// update status
var newStatus domain.StackStatus
var newMessage string

if workflowId != "" {
workflow, err := argowfClient.GetWorkflow("argo", workflowId)
if err != nil {
log.Error("failed to get argo workflow. err : ", err)
continue
}

log.Info(workflow)
newMessage = fmt.Sprintf("(%s) %s", workflow.Status.Progress, workflow.Status.Message)
log.Debug(fmt.Sprintf("status [%s], newMessage [%s], phase [%s]", status, newMessage, workflow.Status.Phase))

if status == domain.StackStatus_INSTALLING {
switch workflow.Status.Phase {
case "Running":
newStatus = domain.StackStatus_INSTALLING
case "Succeeded":
newStatus = domain.StackStatus_RUNNING
case "Failed":
newStatus = domain.StackStatus_INSTALL_ERROR
case "Error":
newStatus = domain.StackStatus_INSTALL_ERROR
}
} else if status == domain.StackStatus_DELETING {
switch workflow.Status.Phase {
case "Running":
newStatus = domain.StackStatus_DELETING
case "Succeeded":
newStatus = domain.StackStatus_DELETED
case "Failed":
newStatus = domain.StackStatus_DELETE_ERROR
case "Error":
newStatus = domain.StackStatus_DELETE_ERROR
}
}
if newStatus == domain.StackStatus_PENDING {
continue
}
} else {
continue
}

if status != newStatus || statusDesc != newMessage {
log.Debug(fmt.Sprintf("update status!! stackId [%s], newStatus [%s], newMessage [%s]", stackId, newStatus, newMessage))
err := stackAccessor.UpdateStackStatus(stackId, newStatus, newMessage, workflowId)
if err != nil {
log.Error("Failed to update stack status err : ", err)
continue
}
}
}
return nil
}
58 changes: 58 additions & 0 deletions internal/stack/stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package stack

import (
"fmt"

"gorm.io/gorm"

"github.com/openinfradev/tks-api/pkg/domain"
"github.com/openinfradev/tks-api/pkg/log"
)

type Stack struct {
ID string `gorm:"primarykey"`
WorkflowId string
Status domain.StackStatus
StatusDesc string
}

type StackAccessor struct {
db *gorm.DB
}

func New(db *gorm.DB) *StackAccessor {
return &StackAccessor{
db: db,
}
}

// For Unittest
func (x *StackAccessor) GetDb() *gorm.DB {
return x.db
}

func (x *StackAccessor) GetIncompleteStacks() ([]Stack, error) {
var stacks []Stack

res := x.db.
Where("status IN ?", []domain.StackStatus{domain.StackStatus_INSTALLING, domain.StackStatus_DELETING}).
Find(&stacks)

if res.Error != nil {
return nil, res.Error
}

return stacks, nil
}

func (x *StackAccessor) UpdateStackStatus(stackId string, status domain.StackStatus, statusDesc string, workflowId string) error {
log.Info(fmt.Sprintf("UpdateStackStatus. stackId[%s], status[%d], statusDesc[%s], workflowId[%s]", stackId, status, statusDesc, workflowId))
res := x.db.Model(Stack{}).
Where("ID = ?", stackId).
Updates(map[string]interface{}{"Status": status, "StatusDesc": statusDesc, "WorkflowId": workflowId})

if res.Error != nil || res.RowsAffected == 0 {
return fmt.Errorf("nothing updated in stack with id %s", stackId)
}
return nil
}

0 comments on commit 8ec4966

Please sign in to comment.