-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |