Skip to content

Commit 7ac5443

Browse files
authored
feat: add sbs-migration tool (#68)
1 parent 8734e7a commit 7ac5443

File tree

9 files changed

+625
-4
lines changed

9 files changed

+625
-4
lines changed

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.22-alpine as builder
1+
FROM golang:1.22-alpine AS builder
22

33
RUN apk update && apk add --no-cache git ca-certificates && update-ca-certificates
44

@@ -15,9 +15,11 @@ ARG TAG
1515
ARG COMMIT_SHA
1616
ARG BUILD_DATE
1717
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -ldflags "-w -s -X github.com/scaleway/scaleway-csi/driver.driverVersion=${TAG} -X github.com/scaleway/scaleway-csi/driver.buildDate=${BUILD_DATE} -X github.com/scaleway/scaleway-csi/driver.gitCommit=${COMMIT_SHA} " -o scaleway-csi ./cmd/scaleway-csi
18+
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -a -ldflags "-w -s -X github.com/scaleway/scaleway-csi/driver.driverVersion=${TAG} -X github.com/scaleway/scaleway-csi/driver.buildDate=${BUILD_DATE} -X github.com/scaleway/scaleway-csi/driver.gitCommit=${COMMIT_SHA} " -o sbs-migration ./cmd/sbs-migration
1819

1920
FROM alpine:3.15
2021
RUN apk update && apk add --no-cache e2fsprogs e2fsprogs-extra xfsprogs xfsprogs-extra cryptsetup ca-certificates blkid && update-ca-certificates
2122
WORKDIR /
2223
COPY --from=builder /go/src/github.com/scaleway/scaleway-csi/scaleway-csi .
24+
COPY --from=builder /go/src/github.com/scaleway/scaleway-csi/sbs-migration .
2325
ENTRYPOINT ["/scaleway-csi"]

cmd/sbs-migration/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# sbs-migration
2+
3+
The `sbs-migration` tool migrates your Kubernetes `PersistentVolumes` from [Instance](https://console.scaleway.com/instance/volumes)
4+
to the new [Block Storage](https://console.scaleway.com/block-storage/volumes) product. There is no downtime during the migration.
5+
Your Snapshots will also be migrated.
6+
7+
**You must run this tool during the `v0.{1,2}.X` to `v0.3.X` upgrade of the Scaleway CSI.**
8+
9+
> [!CAUTION]
10+
> This tool is intended to be used by customers who manage their own Kubernetes cluster.
11+
>
12+
> **DO NOT USE THIS TOOL IF YOUR CLUSTER IS MANAGED BY SCALEWAY (e.g. Kapsule / Kosmos)**.
13+
14+
## Requirements
15+
16+
- The kubeconfig of a Kubernetes cluster that you manage, with the Scaleway CSI installed (version `v0.1.X` or `v0.2.X`).
17+
- [Go 1.20+](https://go.dev/dl/).
18+
- Kubectl CLI.
19+
20+
## Usage
21+
22+
Please read all the steps before executing any command.
23+
24+
1. Stop the CSI controller: `$ kubectl scale deployment scaleway-csi-controller -n kube-system --replicas=0`.
25+
2. Set the following environment variables:
26+
27+
```bash
28+
export SCW_DEFAULT_ZONE=fr-par-1
29+
export SCW_DEFAULT_PROJECT_ID=11111111-1111-1111-1111-111111111111
30+
export SCW_ACCESS_KEY=SCW123456789ABCDE
31+
export SCW_SECRET_KEY=11111111-1111-1111-1111-111111111111
32+
```
33+
34+
3. Run the `sbs-migration` tool with dry-run enabled: `$ go run cmd/sbs-migration/main.go -kubeconfig=<path to your kubeconfig> -dry-run`.
35+
4. If you are happy with the dry-run result, run the `sbs-migration` with dry-run disabled
36+
to effectively migrate your volumes and snapshots: `$ go run cmd/sbs-migration/main.go -kubeconfig=<path to your kubeconfig>`.
37+
5. Upgrade the CSI to `v0.3.1` or higher.

cmd/sbs-migration/main.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
7+
"github.com/scaleway/scaleway-csi/pkg/driver"
8+
"github.com/scaleway/scaleway-csi/pkg/migration"
9+
"github.com/scaleway/scaleway-csi/pkg/scaleway"
10+
"k8s.io/client-go/dynamic"
11+
"k8s.io/client-go/kubernetes"
12+
"k8s.io/client-go/tools/clientcmd"
13+
"k8s.io/klog/v2"
14+
)
15+
16+
func main() {
17+
var (
18+
ctx = context.Background()
19+
20+
// Flags
21+
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
22+
disableVolumeMigration = flag.Bool("disable-volume-migration", false, "Disables listing volumes and migrating them")
23+
disableSnapshotMigration = flag.Bool("disable-snapshot-migration", false, "Disables listing snapshots and migrating them")
24+
dryRun = flag.Bool("dry-run", false, "Simulates the volume and snapshot migration process")
25+
)
26+
flag.Parse()
27+
28+
// Create Kubernetes client.
29+
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
30+
if err != nil {
31+
klog.Fatal(err)
32+
}
33+
34+
clientset, err := kubernetes.NewForConfig(config)
35+
if err != nil {
36+
klog.Fatal(err)
37+
}
38+
39+
dynClient, err := dynamic.NewForConfig(config)
40+
if err != nil {
41+
klog.Fatal(err)
42+
}
43+
44+
// Create Scaleway client.
45+
scw, err := scaleway.New(driver.UserAgent())
46+
if err != nil {
47+
klog.Fatal(err)
48+
}
49+
50+
// Migrate volumes and snapshots from Instance to Block API.
51+
opts := &migration.Options{
52+
DryRun: *dryRun,
53+
DisableVolumeMigration: *disableVolumeMigration,
54+
DisableSnapshotMigration: *disableSnapshotMigration,
55+
}
56+
57+
if err := migration.New(clientset, dynClient, scw, opts).Do(ctx); err != nil {
58+
klog.Fatal(err)
59+
}
60+
}

go.mod

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/scaleway/scaleway-csi
33
go 1.22.2
44

55
require (
6+
github.com/avast/retry-go/v4 v4.6.0
67
github.com/container-storage-interface/spec v1.9.0
78
github.com/golang/protobuf v1.5.4
89
github.com/google/uuid v1.6.0
@@ -13,27 +14,55 @@ require (
1314
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.26
1415
golang.org/x/crypto v0.22.0
1516
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f
17+
golang.org/x/sync v0.7.0
1618
golang.org/x/sys v0.19.0
1719
google.golang.org/grpc v1.63.2
1820
google.golang.org/protobuf v1.33.0
21+
k8s.io/apimachinery v0.30.0
22+
k8s.io/client-go v0.30.0
1923
k8s.io/klog/v2 v2.120.1
2024
k8s.io/mount-utils v0.30.0
2125
k8s.io/utils v0.0.0-20240310230437-4693a0247e57
2226
oya.to/namedlocker v1.0.0
2327
)
2428

2529
require (
30+
github.com/davecgh/go-spew v1.1.1 // indirect
31+
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
2632
github.com/go-logr/logr v1.4.1 // indirect
33+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
34+
github.com/go-openapi/jsonreference v0.20.2 // indirect
35+
github.com/go-openapi/swag v0.22.3 // indirect
2736
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
37+
github.com/gogo/protobuf v1.3.2 // indirect
38+
github.com/google/gnostic-models v0.6.8 // indirect
2839
github.com/google/go-cmp v0.6.0 // indirect
40+
github.com/google/gofuzz v1.2.0 // indirect
2941
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
42+
github.com/imdario/mergo v0.3.6 // indirect
43+
github.com/josharian/intern v1.0.0 // indirect
44+
github.com/json-iterator/go v1.1.12 // indirect
3045
github.com/kr/fs v0.1.0 // indirect
31-
github.com/kr/text v0.2.0 // indirect
46+
github.com/mailru/easyjson v0.7.7 // indirect
3247
github.com/moby/sys/mountinfo v0.6.2 // indirect
48+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
49+
github.com/modern-go/reflect2 v1.0.2 // indirect
50+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
51+
github.com/spf13/pflag v1.0.5 // indirect
3352
golang.org/x/net v0.24.0 // indirect
53+
golang.org/x/oauth2 v0.17.0 // indirect
54+
golang.org/x/term v0.19.0 // indirect
3455
golang.org/x/text v0.14.0 // indirect
56+
golang.org/x/time v0.3.0 // indirect
3557
golang.org/x/tools v0.20.0 // indirect
58+
google.golang.org/appengine v1.6.8 // indirect
3659
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
60+
gopkg.in/inf.v0 v0.9.1 // indirect
3761
gopkg.in/yaml.v2 v2.4.0 // indirect
3862
gopkg.in/yaml.v3 v3.0.1 // indirect
63+
k8s.io/api v0.30.0 // indirect
64+
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
65+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
66+
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
67+
sigs.k8s.io/yaml v1.3.0 // indirect
3968
)

0 commit comments

Comments
 (0)