Skip to content

Commit be4dd67

Browse files
authored
feat(k8s): add cluster migrate-to-private-network command (#3145)
1 parent e44ab83 commit be4dd67

File tree

4 files changed

+102
-6
lines changed

4 files changed

+102
-6
lines changed

cmd/scw/testdata/test-all-usage-k8s-cluster-migrate-to-private-network-usage.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
22
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3-
Migrate a cluster that was created before the release of Private Network clusters to a new one with a Private Network.
3+
Migrate a cluster that was created before the release of Private Network clusters to a new one with a Private Network. If none is provided, a private network will be created
44

55
USAGE:
66
scw k8s cluster migrate-to-private-network <cluster-id ...> [arg=value ...]
@@ -10,9 +10,9 @@ EXAMPLES:
1010
scw k8s cluster migrate-to-private-network 11111111-1111-1111-111111111111 private-network-id=11111111-1111-1111-111111111111
1111

1212
ARGS:
13-
cluster-id ID of the cluster to migrate
14-
private-network-id ID of the Private Network to link to the cluster
15-
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
13+
cluster-id ID of the cluster to migrate
14+
[private-network-id] ID of the Private Network to link to the cluster. If none is provided, a private network will be created
15+
[region=fr-par] Region to target. If none is passed will use default region from the config (fr-par | nl-ams | pl-waw)
1616

1717
FLAGS:
1818
-h, --help help for migrate-to-private-network

docs/commands/k8s.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ scw k8s cluster list-available-versions 11111111-1111-1111-111111111111
272272

273273
### Migrate an existing cluster to a Private Network cluster
274274

275-
Migrate a cluster that was created before the release of Private Network clusters to a new one with a Private Network.
275+
Migrate a cluster that was created before the release of Private Network clusters to a new one with a Private Network. If none is provided, a private network will be created
276276

277277
**Usage:**
278278

@@ -286,7 +286,7 @@ scw k8s cluster migrate-to-private-network <cluster-id ...> [arg=value ...]
286286
| Name | | Description |
287287
|------|---|-------------|
288288
| cluster-id | Required | ID of the cluster to migrate |
289-
| private-network-id | Required | ID of the Private Network to link to the cluster |
289+
| private-network-id | | ID of the Private Network to link to the cluster. If none is provided, a private network will be created |
290290
| region | Default: `fr-par`<br />One of: `fr-par`, `nl-ams`, `pl-waw` | Region to target. If none is passed will use default region from the config |
291291

292292

internal/namespaces/k8s/v1/custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func GetCommands() *core.Commands {
3636
cmds.MustFind("k8s", "cluster", "update").Override(clusterUpdateBuilder)
3737
cmds.MustFind("k8s", "cluster", "upgrade").Override(clusterUpgradeBuilder)
3838
cmds.MustFind("k8s", "cluster", "delete").Override(clusterDeleteBuilder)
39+
cmds.MustFind("k8s", "cluster", "migrate-to-private-network").Override(clusterMigrateToPrivateNetworkBuilder)
3940

4041
cmds.MustFind("k8s", "pool", "create").Override(poolCreateBuilder)
4142
cmds.MustFind("k8s", "pool", "update").Override(poolUpdateBuilder)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package k8s
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/scaleway/scaleway-cli/v2/internal/core"
8+
"github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
9+
"github.com/scaleway/scaleway-sdk-go/api/vpc/v2"
10+
"github.com/scaleway/scaleway-sdk-go/scw"
11+
)
12+
13+
func clusterMigrateToPrivateNetworkBuilder(c *core.Command) *core.Command {
14+
c.ArgSpecs.GetByName("private-network-id").Required = false
15+
16+
infoPNID := " If none is provided, a private network will be created"
17+
c.Long += infoPNID
18+
c.ArgSpecs.GetByName("private-network-id").Short += "." + infoPNID
19+
20+
c.Run = func(ctx context.Context, args interface{}) (i interface{}, e error) {
21+
request := args.(*k8s.MigrateToPrivateNetworkClusterRequest)
22+
client := core.ExtractClient(ctx)
23+
k8sAPI := k8s.NewAPI(client)
24+
vpcAPI := vpc.NewAPI(client)
25+
26+
pnCreated := false
27+
var pn *vpc.PrivateNetwork
28+
var err error
29+
30+
if request.PrivateNetworkID == "" {
31+
pn, err = vpcAPI.CreatePrivateNetwork(&vpc.CreatePrivateNetworkRequest{
32+
Region: request.Region,
33+
Tags: []string{"created-along-with-k8s-cluster", "created-by-cli"},
34+
}, scw.WithContext(ctx))
35+
if err != nil {
36+
return nil, err
37+
}
38+
request.PrivateNetworkID = pn.ID
39+
pnCreated = true
40+
} else {
41+
pn, err = vpcAPI.GetPrivateNetwork(&vpc.GetPrivateNetworkRequest{
42+
Region: request.Region,
43+
PrivateNetworkID: request.PrivateNetworkID,
44+
}, scw.WithContext(ctx))
45+
if err != nil {
46+
return nil, err
47+
}
48+
}
49+
50+
cluster, err := k8sAPI.MigrateToPrivateNetworkCluster(request, scw.WithContext(ctx))
51+
if err != nil {
52+
if pnCreated {
53+
errPN := vpcAPI.DeletePrivateNetwork(&vpc.DeletePrivateNetworkRequest{
54+
Region: request.Region,
55+
PrivateNetworkID: request.PrivateNetworkID,
56+
}, scw.WithContext(ctx))
57+
58+
if err != nil {
59+
return nil, errors.Join(err, errPN)
60+
}
61+
}
62+
return nil, err
63+
}
64+
65+
return struct {
66+
*k8s.Cluster
67+
*vpc.PrivateNetwork `json:"PrivateNetwork"`
68+
}{
69+
cluster,
70+
pn,
71+
}, nil
72+
}
73+
74+
c.View = &core.View{
75+
Sections: []*core.ViewSection{
76+
{
77+
FieldName: "AutoscalerConfig",
78+
Title: "Autoscaler configuration",
79+
},
80+
{
81+
FieldName: "AutoUpgrade",
82+
Title: "Auto-upgrade settings",
83+
},
84+
{
85+
FieldName: "OpenIDConnectConfig",
86+
Title: "Open ID Connect configuration",
87+
},
88+
{
89+
FieldName: "PrivateNetwork",
90+
Title: "Private Network",
91+
},
92+
},
93+
}
94+
return c
95+
}

0 commit comments

Comments
 (0)