Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(k8s): add wait and status color to k8s pool #773

Merged
merged 2 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ARGS:

FLAGS:
-h, --help help for create
-w, --wait wait until the pool is ready

GLOBAL FLAGS:
-D, --debug Enable debug mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ARGS:

FLAGS:
-h, --help help for delete
-w, --wait wait until the pool is ready

GLOBAL FLAGS:
-D, --debug Enable debug mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ARGS:

FLAGS:
-h, --help help for update
-w, --wait wait until the pool is ready

GLOBAL FLAGS:
-D, --debug Enable debug mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ARGS:

FLAGS:
-h, --help help for upgrade
-w, --wait wait until the pool is ready

GLOBAL FLAGS:
-D, --debug Enable debug mode
Expand Down
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
}
}