Skip to content

Commit 5150184

Browse files
authored
k8s: add option to wait for pools in the wait for cluster (#1193)
1 parent 7cf7d2d commit 5150184

File tree

6 files changed

+3622
-9
lines changed

6 files changed

+3622
-9
lines changed

cmd/scw/testdata/test-all-usage-k8s-cluster-wait-usage.golden

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ EXAMPLES:
1010
scw k8s cluster wait 11111111-1111-1111-1111-111111111111
1111

1212
ARGS:
13-
cluster-id ID of the cluster.
14-
[region=fr-par] Region to target. If none is passed will use default region from the config
13+
cluster-id ID of the cluster.
14+
[wait-for-pools] Wait for pools to be ready.
15+
[region=fr-par] Region to target. If none is passed will use default region from the config
1516

1617
FLAGS:
1718
-h, --help help for wait

internal/namespaces/k8s/v1/custom_cluster.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,53 @@ func waitForClusterFunc(action int) core.WaitFunc {
129129
}
130130

131131
func k8sClusterWaitCommand() *core.Command {
132+
type customClusterWaitArgs struct {
133+
k8s.WaitForClusterRequest
134+
WaitForPools bool
135+
}
132136
return &core.Command{
133137
Short: `Wait for a cluster to reach a stable state`,
134138
Long: `Wait for server to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the server.`,
135139
Namespace: "k8s",
136140
Resource: "cluster",
137141
Verb: "wait",
138-
ArgsType: reflect.TypeOf(k8s.WaitForClusterRequest{}),
142+
ArgsType: reflect.TypeOf(customClusterWaitArgs{}),
139143
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
144+
args := argsI.(*customClusterWaitArgs)
145+
140146
api := k8s.NewAPI(core.ExtractClient(ctx))
141-
return api.WaitForCluster(&k8s.WaitForClusterRequest{
142-
Region: argsI.(*k8s.WaitForClusterRequest).Region,
143-
ClusterID: argsI.(*k8s.WaitForClusterRequest).ClusterID,
147+
cluster, err := api.WaitForCluster(&k8s.WaitForClusterRequest{
148+
Region: args.Region,
149+
ClusterID: args.ClusterID,
144150
Timeout: scw.TimeDurationPtr(clusterActionTimeout),
145151
RetryInterval: core.DefaultRetryInterval,
146152
})
153+
if err != nil {
154+
return nil, err
155+
}
156+
157+
if args.WaitForPools {
158+
pools, err := api.ListPools(&k8s.ListPoolsRequest{
159+
Region: cluster.Region,
160+
ClusterID: cluster.ID,
161+
}, scw.WithAllPages())
162+
if err != nil {
163+
return cluster, err
164+
}
165+
for _, pool := range pools.Pools {
166+
_, err := api.WaitForPool(&k8s.WaitForPoolRequest{
167+
Region: pool.Region,
168+
PoolID: pool.ID,
169+
Timeout: scw.TimeDurationPtr(poolActionTimeout),
170+
RetryInterval: core.DefaultRetryInterval,
171+
})
172+
if err != nil {
173+
return cluster, err
174+
}
175+
}
176+
}
177+
178+
return cluster, nil
147179
},
148180
ArgSpecs: core.ArgSpecs{
149181
{
@@ -152,6 +184,10 @@ func k8sClusterWaitCommand() *core.Command {
152184
Required: true,
153185
Positional: true,
154186
},
187+
{
188+
Name: "wait-for-pools",
189+
Short: "Wait for pools to be ready.",
190+
},
155191
core.RegionArgSpec(),
156192
},
157193
Examples: []*core.Example{

internal/namespaces/k8s/v1/custom_cluster_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func Test_GetCluster(t *testing.T) {
1010
t.Run("Simple", core.Test(&core.TestConfig{
1111
Commands: GetCommands(),
12-
BeforeFunc: createCluster("Cluster", kapsuleVersion, 1),
12+
BeforeFunc: createCluster("Cluster", kapsuleVersion, 1, "DEV1-M"),
1313
Cmd: "scw k8s cluster get {{ .Cluster.ID }}",
1414
Check: core.TestCheckCombine(
1515
core.TestCheckGolden(),
@@ -18,3 +18,16 @@ func Test_GetCluster(t *testing.T) {
1818
AfterFunc: deleteCluster("Cluster"),
1919
}))
2020
}
21+
22+
func Test_WaitCluster(t *testing.T) {
23+
t.Run("wait for pools", core.Test(&core.TestConfig{
24+
Commands: GetCommands(),
25+
BeforeFunc: createCluster("Cluster", kapsuleVersion, 1, "GP1-XS"),
26+
Cmd: "scw k8s cluster wait {{ .Cluster.ID }} wait-for-pools=true",
27+
Check: core.TestCheckCombine(
28+
core.TestCheckGolden(),
29+
core.TestCheckExitCode(0),
30+
),
31+
AfterFunc: deleteCluster("Cluster"),
32+
}))
33+
}

internal/namespaces/k8s/v1/helpers_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ const (
1919

2020
// createCluster creates a basic cluster with "poolSize" dev1-m as nodes, the given version and
2121
// register it in the context Meta at metaKey.
22-
func createCluster(metaKey string, version string, poolSize int) core.BeforeFunc {
22+
func createCluster(metaKey string, version string, poolSize int, nodeType string) core.BeforeFunc {
2323
return core.ExecStoreBeforeCmd(
2424
metaKey,
25-
fmt.Sprintf("scw k8s cluster create name=cli-test version=%s cni=cilium pools.0.node-type=DEV1-M pools.0.size=%d pools.0.name=default", version, poolSize))
25+
fmt.Sprintf("scw k8s cluster create name=cli-test version=%s cni=cilium pools.0.node-type=%s pools.0.size=%d pools.0.name=default", version, nodeType, poolSize))
2626
}
2727

2828
// createClusterAndWaitAndKubeconfig creates a basic cluster with 1 dev1-m as node, the given version and

0 commit comments

Comments
 (0)