|
| 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