Skip to content

feat(k8s): add wait flag to cluster actions #752

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

Merged
merged 5 commits into from
Mar 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -37,6 +37,7 @@ ARGS:

FLAGS:
-h, --help help for create
-w, --wait wait until the cluster 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 cluster is ready

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

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

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

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

GLOBAL FLAGS:
-D, --debug Enable debug mode
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.5 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
Expand Down
4 changes: 4 additions & 0 deletions internal/namespaces/k8s/v1beta4/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func GetCommands() *core.Commands {
human.RegisterMarshalerFunc(k8s.ClusterStatus(0), human.BindAttributesMarshalFunc(clusterStatusAttributes))

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)

return cmds
}
47 changes: 47 additions & 0 deletions internal/namespaces/k8s/v1beta4/custom_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ package k8s

import (
"context"
"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"
"golang.org/x/xerrors"
)

const (
clusterActionTimeout = 10 * time.Minute
)

//
Expand Down Expand Up @@ -40,3 +49,41 @@ func clusterAvailableVersionsListBuilder(c *core.Command) *core.Command {

return c
}

func clusterCreateBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForClusterFunc()
return c
}

func clusterDeleteBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForClusterFunc()
return c
}

func clusterUpgradeBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForClusterFunc()
return c
}

func clusterUpdateBuilder(c *core.Command) *core.Command {
c.WaitFunc = waitForClusterFunc()
return c
}

func waitForClusterFunc() core.WaitFunc {
return func(ctx context.Context, _, respI interface{}) (interface{}, error) {
cluster, err := k8s.NewAPI(core.ExtractClient(ctx)).WaitForCluster(&k8s.WaitForClusterRequest{
Region: respI.(*k8s.Cluster).Region,
ClusterID: respI.(*k8s.Cluster).ID,
Timeout: scw.DurationPtr(clusterActionTimeout),
})
if err != nil {
notFoundError := &scw.ResourceNotFoundError{}
responseError := &scw.ResponseError{}
if xerrors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || xerrors.As(err, &notFoundError) {
return fmt.Sprintf("Cluster %s successfully deleted.", respI.(*k8s.Cluster).ID), nil
}
}
return cluster, nil
}
}