Skip to content

Commit

Permalink
feat(k8s): add wait and status color to k8s pool
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Cyvoct <[email protected]>
  • Loading branch information
Sh4d1 committed Mar 16, 2020
1 parent 0b86ee5 commit 151aa9c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/namespaces/k8s/v1beta4/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ func GetCommands() *core.Commands {
cmds := GetGeneratedCommands()

human.RegisterMarshalerFunc(k8s.ClusterStatus(0), human.BindAttributesMarshalFunc(clusterStatusAttributes))
human.RegisterMarshalerFunc(k8s.PoolStatus(0), human.BindAttributesMarshalFunc(poolStatusAttributes))

cmds.MustFind("k8s", "cluster", "list-available-versions").Override(clusterAvailableVersionsListBuilder)
cmds.MustFind("k8s", "cluster", "create").Override(clusterCreateBuilder)
cmds.MustFind("k8s", "cluster", "update").Override(clusterUpdateBuilder)
cmds.MustFind("k8s", "cluster", "upgrade").Override(clusterUpgradeBuilder)
cmds.MustFind("k8s", "cluster", "delete").Override(clusterDeleteBuilder)

cmds.MustFind("k8s", "pool", "create").Override(poolCreateBuilder)
cmds.MustFind("k8s", "pool", "update").Override(poolUpdateBuilder)
cmds.MustFind("k8s", "pool", "upgrade").Override(poolUpgradeBuilder)
cmds.MustFind("k8s", "pool", "delete").Override(poolDeleteBuilder)

return cmds
}
91 changes: 91 additions & 0 deletions internal/namespaces/k8s/v1beta4/custom_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package k8s

import (
"context"
"errors"
"fmt"
"net/http"
"time"

"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-cli/internal/human"
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1beta4"
"github.com/scaleway/scaleway-sdk-go/scw"
)

const (
poolActionTimeout = 10 * time.Minute
)

//
// Marshalers
//

// poolStatusMarshalerFunc marshals a k8s.PoolStatus.
var (
poolStatusAttributes = human.Attributes{
k8s.PoolStatusScaling: color.FgBlue,
k8s.PoolStatusReady: color.FgGreen,
k8s.PoolStatusError: color.FgRed,
k8s.PoolStatusLocked: color.FgRed,
k8s.PoolStatusUpdating: color.FgBlue,
k8s.PoolStatusUpgrading: color.FgBlue,
k8s.PoolStatusWarning: color.FgHiYellow,
}
)

const (
poolActionCreate = iota
poolActionUpdate
poolActionUpgrade
poolActionDelete
)

func poolCreateBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForPoolFunc(poolActionCreate)
return c
}

func poolDeleteBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForPoolFunc(poolActionDelete)
return c
}

func poolUpgradeBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForPoolFunc(poolActionUpgrade)
return c
}

func poolUpdateBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForPoolFunc(poolActionUpdate)
return c
}

func waitForPoolFunc(action int) core.WaitFunc {
return func(ctx context.Context, _, respI interface{}) (interface{}, error) {
pool, err := k8s.NewAPI(core.ExtractClient(ctx)).WaitForPool(&k8s.WaitForPoolRequest{
Region: respI.(*k8s.Pool).Region,
PoolID: respI.(*k8s.Pool).ID,
Timeout: scw.DurationPtr(poolActionTimeout),
})
switch action {
case poolActionCreate:
return pool, err
case poolActionUpdate:
return pool, err
case poolActionUpgrade:
return pool, err
case poolActionDelete:
if err != nil {
// if we get a 404 here, it means the resource was successfully deleted
notFoundError := &scw.ResourceNotFoundError{}
responseError := &scw.ResponseError{}
if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || errors.As(err, &notFoundError) {
return fmt.Sprintf("Pool %s successfully deleted.", respI.(*k8s.Pool).ID), nil
}
}
}
return nil, err
}
}

0 comments on commit 151aa9c

Please sign in to comment.