Skip to content

Commit 30b9fec

Browse files
authored
feat(k8s): add wait and status color to k8s pool (#773)
1 parent 93c7490 commit 30b9fec

6 files changed

+101
-0
lines changed

cmd/scw/testdata/test-all-usage-k8s-pool-create-usage.stderr.golden

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ARGS:
1919

2020
FLAGS:
2121
-h, --help help for create
22+
-w, --wait wait until the pool is ready
2223

2324
GLOBAL FLAGS:
2425
-D, --debug Enable debug mode

cmd/scw/testdata/test-all-usage-k8s-pool-delete-usage.stderr.golden

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ARGS:
99

1010
FLAGS:
1111
-h, --help help for delete
12+
-w, --wait wait until the pool is ready
1213

1314
GLOBAL FLAGS:
1415
-D, --debug Enable debug mode

cmd/scw/testdata/test-all-usage-k8s-pool-update-usage.stderr.golden

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ARGS:
1515

1616
FLAGS:
1717
-h, --help help for update
18+
-w, --wait wait until the pool is ready
1819

1920
GLOBAL FLAGS:
2021
-D, --debug Enable debug mode

cmd/scw/testdata/test-all-usage-k8s-pool-upgrade-usage.stderr.golden

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ARGS:
1010

1111
FLAGS:
1212
-h, --help help for upgrade
13+
-w, --wait wait until the pool is ready
1314

1415
GLOBAL FLAGS:
1516
-D, --debug Enable debug mode

internal/namespaces/k8s/v1beta4/custom.go

+6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ func GetCommands() *core.Commands {
1616
cmds := GetGeneratedCommands()
1717

1818
human.RegisterMarshalerFunc(k8s.ClusterStatus(0), human.BindAttributesMarshalFunc(clusterStatusAttributes))
19+
human.RegisterMarshalerFunc(k8s.PoolStatus(0), human.BindAttributesMarshalFunc(poolStatusAttributes))
1920

2021
cmds.MustFind("k8s", "cluster", "list-available-versions").Override(clusterAvailableVersionsListBuilder)
2122
cmds.MustFind("k8s", "cluster", "create").Override(clusterCreateBuilder)
2223
cmds.MustFind("k8s", "cluster", "update").Override(clusterUpdateBuilder)
2324
cmds.MustFind("k8s", "cluster", "upgrade").Override(clusterUpgradeBuilder)
2425
cmds.MustFind("k8s", "cluster", "delete").Override(clusterDeleteBuilder)
2526

27+
cmds.MustFind("k8s", "pool", "create").Override(poolCreateBuilder)
28+
cmds.MustFind("k8s", "pool", "update").Override(poolUpdateBuilder)
29+
cmds.MustFind("k8s", "pool", "upgrade").Override(poolUpgradeBuilder)
30+
cmds.MustFind("k8s", "pool", "delete").Override(poolDeleteBuilder)
31+
2632
return cmds
2733
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package k8s
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"net/http"
8+
"time"
9+
10+
"github.com/fatih/color"
11+
"github.com/scaleway/scaleway-cli/internal/core"
12+
"github.com/scaleway/scaleway-cli/internal/human"
13+
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1beta4"
14+
"github.com/scaleway/scaleway-sdk-go/scw"
15+
)
16+
17+
const (
18+
poolActionTimeout = 10 * time.Minute
19+
)
20+
21+
//
22+
// Marshalers
23+
//
24+
25+
// poolStatusMarshalerFunc marshals a k8s.PoolStatus.
26+
var (
27+
poolStatusAttributes = human.Attributes{
28+
k8s.PoolStatusScaling: color.FgBlue,
29+
k8s.PoolStatusReady: color.FgGreen,
30+
k8s.PoolStatusError: color.FgRed,
31+
k8s.PoolStatusLocked: color.FgRed,
32+
k8s.PoolStatusUpdating: color.FgBlue,
33+
k8s.PoolStatusUpgrading: color.FgBlue,
34+
k8s.PoolStatusWarning: color.FgHiYellow,
35+
}
36+
)
37+
38+
const (
39+
poolActionCreate = iota
40+
poolActionUpdate
41+
poolActionUpgrade
42+
poolActionDelete
43+
)
44+
45+
func poolCreateBuilder(c *core.Command) *core.Command {
46+
c.WaitFunc = waitForPoolFunc(poolActionCreate)
47+
return c
48+
}
49+
50+
func poolDeleteBuilder(c *core.Command) *core.Command {
51+
c.WaitFunc = waitForPoolFunc(poolActionDelete)
52+
return c
53+
}
54+
55+
func poolUpgradeBuilder(c *core.Command) *core.Command {
56+
c.WaitFunc = waitForPoolFunc(poolActionUpgrade)
57+
return c
58+
}
59+
60+
func poolUpdateBuilder(c *core.Command) *core.Command {
61+
c.WaitFunc = waitForPoolFunc(poolActionUpdate)
62+
return c
63+
}
64+
65+
func waitForPoolFunc(action int) core.WaitFunc {
66+
return func(ctx context.Context, _, respI interface{}) (interface{}, error) {
67+
pool, err := k8s.NewAPI(core.ExtractClient(ctx)).WaitForPool(&k8s.WaitForPoolRequest{
68+
Region: respI.(*k8s.Pool).Region,
69+
PoolID: respI.(*k8s.Pool).ID,
70+
Timeout: scw.DurationPtr(poolActionTimeout),
71+
})
72+
switch action {
73+
case poolActionCreate:
74+
return pool, err
75+
case poolActionUpdate:
76+
return pool, err
77+
case poolActionUpgrade:
78+
return pool, err
79+
case poolActionDelete:
80+
if err != nil {
81+
// if we get a 404 here, it means the resource was successfully deleted
82+
notFoundError := &scw.ResourceNotFoundError{}
83+
responseError := &scw.ResponseError{}
84+
if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound || errors.As(err, &notFoundError) {
85+
return fmt.Sprintf("Pool %s successfully deleted.", respI.(*k8s.Pool).ID), nil
86+
}
87+
}
88+
}
89+
return nil, err
90+
}
91+
}

0 commit comments

Comments
 (0)