diff --git a/cmd/scw/testdata/test-all-usage-k8s-pool-create-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-k8s-pool-create-usage.stderr.golden index ee4680a059..64f3c8e1d1 100644 --- a/cmd/scw/testdata/test-all-usage-k8s-pool-create-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-k8s-pool-create-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-k8s-pool-delete-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-k8s-pool-delete-usage.stderr.golden index cf651adeb7..b7d1138f3a 100644 --- a/cmd/scw/testdata/test-all-usage-k8s-pool-delete-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-k8s-pool-delete-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-k8s-pool-update-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-k8s-pool-update-usage.stderr.golden index 6bad15f32e..95c6aef983 100644 --- a/cmd/scw/testdata/test-all-usage-k8s-pool-update-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-k8s-pool-update-usage.stderr.golden @@ -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 diff --git a/cmd/scw/testdata/test-all-usage-k8s-pool-upgrade-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-k8s-pool-upgrade-usage.stderr.golden index 5f21c22912..48b7e67199 100644 --- a/cmd/scw/testdata/test-all-usage-k8s-pool-upgrade-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-k8s-pool-upgrade-usage.stderr.golden @@ -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 diff --git a/internal/namespaces/k8s/v1beta4/custom.go b/internal/namespaces/k8s/v1beta4/custom.go index a8d7f5c826..2905105e02 100644 --- a/internal/namespaces/k8s/v1beta4/custom.go +++ b/internal/namespaces/k8s/v1beta4/custom.go @@ -16,6 +16,7 @@ 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) @@ -23,5 +24,10 @@ func GetCommands() *core.Commands { 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 } diff --git a/internal/namespaces/k8s/v1beta4/custom_pool.go b/internal/namespaces/k8s/v1beta4/custom_pool.go new file mode 100644 index 0000000000..ac221ca1f0 --- /dev/null +++ b/internal/namespaces/k8s/v1beta4/custom_pool.go @@ -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, ¬FoundError) { + return fmt.Sprintf("Pool %s successfully deleted.", respI.(*k8s.Pool).ID), nil + } + } + } + return nil, err + } +}