-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathmain.go
345 lines (290 loc) · 12.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
v1beta1snapshot "github.com/kubernetes-csi/external-snapshotter/v2/pkg/apis/volumesnapshot/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
)
const (
dobsDriverNewStyle = "dobs.csi.digitalocean.com"
dobsDriverOldStyle = "com.digitalocean.csi.dobs"
)
var (
alphaSnapClassRes = schema.GroupVersionResource{Group: "snapshot.storage.k8s.io", Version: "v1alpha1", Resource: "volumesnapshotclasses"}
alphaSnapContentRes = schema.GroupVersionResource{Group: "snapshot.storage.k8s.io", Version: "v1alpha1", Resource: "volumesnapshotcontents"}
alphaSnapshotRes = schema.GroupVersionResource{Group: "snapshot.storage.k8s.io", Version: "v1alpha1", Resource: "volumesnapshots"}
)
var errNoVolumeSnapshotClassFound = fmt.Errorf("could not find v1alpha1/VolumeStorageClass for driver %q", dobsDriverNewStyle)
type snapshotClassDefinition struct {
dobsDriver string
dobsSnapClassName string
dobsIsDefaultClass bool
}
type params struct {
kubeconfig string
server string
directory string
}
var p params
func errExit(err error) {
fmt.Fprintf(os.Stderr, "Failed to migrate snapshots: %s\n", err)
os.Exit(1)
}
func main() {
var defaultKubeConfigPath string
home, err := os.UserHomeDir()
if err == nil {
defaultKubeConfigPath = filepath.Join(home, ".kube", "config")
}
flag.StringVar(&p.kubeconfig, "kubeconfig", defaultKubeConfigPath, "(optional) absolute path to the kubeconfig file")
flag.StringVar(&p.server, "server", "", "(optional) address and port of the Kubernetes API server")
flag.StringVar(&p.directory, "directory", "", "the top-level directory to write YAML-marshaled objects into, or stdout if omitted")
flag.Parse()
if envVar := os.Getenv("KUBECONFIG"); envVar != "" {
p.kubeconfig = envVar
}
if p.server != "" {
p.kubeconfig = ""
}
if err := run(p); err != nil {
errExit(err)
}
}
func run(p params) error {
config, err := clientcmd.BuildConfigFromFlags(p.server, p.kubeconfig)
if err != nil {
return fmt.Errorf("failed to create config from flags: %s", err)
}
dynClient, err := dynamic.NewForConfig(config)
if err != nil {
return fmt.Errorf("failed to create dynamic client for v1alpha1 snapshots: %s", err)
}
volWriter, err := newVolumeSnapshotWriter(p.directory)
if err != nil {
return fmt.Errorf("failed to create volume snapshot writer: %s", err)
}
err = writeVolumeSnapshotObjects(context.TODO(), dynClient, volWriter)
if err != nil {
return fmt.Errorf("failed to write volume snapshot objects: %s", err)
}
return nil
}
func writeVolumeSnapshotObjects(ctx context.Context, client dynamic.Interface, volWriter *volumeSnapshotWriter) error {
snapClassDef, err := analyzeVolumeSnapshotClass(ctx, client.Resource(alphaSnapClassRes))
if err != nil {
return err
}
fmt.Printf("Found VolumeSnapshotClass for DigitalOcean Block Storage (driver=%s, name=%s, default class? %t)\n", snapClassDef.dobsDriver, snapClassDef.dobsSnapClassName, snapClassDef.dobsIsDefaultClass)
num, err := writeVolumeSnapshotContents(ctx, client.Resource(alphaSnapContentRes), snapClassDef, volWriter)
if err != nil {
return err
}
fmt.Printf("Converted %d VolumeSnapshotContent object(s)\n", num)
num, err = writeVolumeSnapshots(ctx, client.Resource(alphaSnapshotRes), snapClassDef, volWriter)
if err != nil {
return err
}
fmt.Printf("Converted %d VolumeSnapshot object(s)\n", num)
return nil
}
// analyzeVolumeSnapshotClass extracts metadata from the DigitalOcean Block
// Storage (DOBS) VolumeSnapshotClass needed to write other volume snapshot
// objects.
func analyzeVolumeSnapshotClass(ctx context.Context, client dynamic.NamespaceableResourceInterface) (*snapshotClassDefinition, error) {
alphaSnapClasses, err := client.List(ctx, metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("failed to list v1alpha1/VolumeSnapshotClass: %s", err)
}
for _, alphaSnapClass := range alphaSnapClasses.Items {
name, _, err := unstructured.NestedString(alphaSnapClass.Object, "metadata", "name")
if err != nil {
return nil, fmt.Errorf("failed to get v1alpha1/VolumeSnapshotClass.metadata.name: %s", err)
}
snapshotter, found, err := unstructured.NestedString(alphaSnapClass.Object, "snapshotter")
if err != nil {
return nil, fmt.Errorf("failed to get v1alpha1/VolumeSnapshotClass.snapshotter for %s: %s", name, err)
}
if !found {
return nil, fmt.Errorf("v1alpha1 VolumeSnapshotClass.snapshotter does not exist for %s", name)
}
if snapshotter == dobsDriverNewStyle || snapshotter == dobsDriverOldStyle {
dobsDriver := snapshotter
dobsSnapClassName := name
isDefaultClass, _, err := unstructured.NestedString(alphaSnapClass.Object, "metadata", "annotations", "snapshot.storage.kubernetes.io/is-default-class")
if err != nil {
return nil, fmt.Errorf("failed to get v1alpha1/VolumeSnapshotClass.metadataannotation.'snapshot.storage.kubernetes.io/is-default-class' for %s: %s", name, err)
}
dobsIsDefaultClass := isDefaultClass == "true"
return &snapshotClassDefinition{dobsDriver, dobsSnapClassName, dobsIsDefaultClass}, nil
}
}
return nil, errNoVolumeSnapshotClassFound
}
// writeVolumeSnapshotContents converts v1alpha1 VolumeSnapshotContent objects
// into their v1beta1 equivalents and writes them out.
func writeVolumeSnapshotContents(ctx context.Context, client dynamic.NamespaceableResourceInterface, snapClassDef *snapshotClassDefinition, volWriter *volumeSnapshotWriter) (num int, err error) {
alphaSnapContents, err := client.List(ctx, metav1.ListOptions{})
if err != nil {
return 0, fmt.Errorf("failed to list v1alpha1/VolumeSnapshotContents: %s", err)
}
for _, alphaSnapContent := range alphaSnapContents.Items {
name, _, err := unstructured.NestedString(alphaSnapContent.Object, "metadata", "name")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshotContent.metadata.name: %s", err)
}
volSnapClassNameStr, found, err := unstructured.NestedString(alphaSnapContent.Object, "spec", "snapshotClassName")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshotContent.spec.snapshotClassName for %s: %s", name, err)
}
if !found && !snapClassDef.dobsIsDefaultClass {
fmt.Printf("Skipping v1alpha1/VolumeSnapshotContent %q because spec.snapshotClassName is missing and DOBS is not the default driver\n", name)
continue
}
var volSnapClassName *string
if found {
if volSnapClassNameStr != snapClassDef.dobsSnapClassName {
fmt.Printf("Skipping v1alpha1/VolumeSnapshotContent %q because object is managed by non-DOBS driver %q\n", name, volSnapClassNameStr)
continue
}
volSnapClassName = &volSnapClassNameStr
}
volSnapRefName, _, err := unstructured.NestedString(alphaSnapContent.Object, "spec", "volumeSnapshotRef", "name")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshotContent.spec.volumeSnapshotRef.name for %s: %s", name, err)
}
volSnapRefNamespace, _, err := unstructured.NestedString(alphaSnapContent.Object, "spec", "volumeSnapshotRef", "namespace")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshotContent.spec.volumeSnapshotRef.namespace for %s: %s", name, err)
}
deletionPolicy, _, err := unstructured.NestedString(alphaSnapContent.Object, "spec", "deletionPolicy")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshotContent.spec.deletionPolicy for %s: %s", name, err)
}
csiVolSnapSourceSnapHandleStr, found, err := unstructured.NestedString(alphaSnapContent.Object, "spec", "csiVolumeSnapshotSource", "snapshotHandle")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshotContent.spec.csiVolumeSnapshotSource.snapshotHandle for %s: %s", name, err)
}
var csiVolSnapSourceSnapHandle *string
if found {
csiVolSnapSourceSnapHandle = &csiVolSnapSourceSnapHandleStr
}
betaSnapContent := v1beta1snapshot.VolumeSnapshotContent{
TypeMeta: metav1.TypeMeta{
Kind: "VolumeSnapshotContent",
APIVersion: v1beta1snapshot.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: v1beta1snapshot.VolumeSnapshotContentSpec{
VolumeSnapshotRef: corev1.ObjectReference{
Name: volSnapRefName,
Namespace: volSnapRefNamespace,
},
DeletionPolicy: v1beta1snapshot.DeletionPolicy(deletionPolicy),
Driver: snapClassDef.dobsDriver,
VolumeSnapshotClassName: volSnapClassName,
Source: v1beta1snapshot.VolumeSnapshotContentSource{
SnapshotHandle: csiVolSnapSourceSnapHandle,
},
},
}
fmt.Printf("--- Writing VolumeSnapshotContent %q\n", name)
err = volWriter.writeVolumeSnapshotContent(betaSnapContent)
if err != nil {
return 0, fmt.Errorf("failed to write VolumeSnapshotContent %q: %s", name, err)
}
num++
}
return num, nil
}
// writeVolumeSnapshots converts v1alpha1 VolumeSnapshot objects into their
// v1beta1 equivalents and writes them out.
func writeVolumeSnapshots(ctx context.Context, client dynamic.NamespaceableResourceInterface, snapClassDef *snapshotClassDefinition, volWriter *volumeSnapshotWriter) (num int, err error) {
alphaSnapshots, err := client.List(ctx, metav1.ListOptions{})
if err != nil {
return 0, fmt.Errorf("failed to list v1alpha1/VolumeSnapshots: %s", err)
}
for _, alphaSnapshot := range alphaSnapshots.Items {
name, _, err := unstructured.NestedString(alphaSnapshot.Object, "metadata", "name")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshot.metadata.name: %s", err)
}
volSnapClassNameStr, found, err := unstructured.NestedString(alphaSnapshot.Object, "spec", "snapshotClassName")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshot.spec.snapshotClassName for %s: %s", name, err)
}
if !found && !snapClassDef.dobsIsDefaultClass {
fmt.Printf("Skipping v1alpha1/VolumeSnapshot %q because spec.snapshotClassName is missing and DOBS is not the default driver\n", name)
continue
}
var volSnapClassName *string
if found {
if volSnapClassNameStr != snapClassDef.dobsSnapClassName {
fmt.Printf("Skipping v1alpha1/VolumeSnapshot %q because object is managed by non-DOBS driver %q\n", name, volSnapClassNameStr)
continue
}
volSnapClassName = &volSnapClassNameStr
}
namespace, _, err := unstructured.NestedString(alphaSnapshot.Object, "metadata", "namespace")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshot.metadata.namespace for %s: %s", name, err)
}
var persistentVolumeClaimName *string
persistentVolumeClaimNameStr, found, err := unstructured.NestedString(alphaSnapshot.Object, "spec", "source", "name")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshot.spec.source.name for %s: %s", name, err)
}
if found {
persistentVolumeClaimName = &persistentVolumeClaimNameStr
}
var snapshotContentName *string
snapshotContentNameStr, found, err := unstructured.NestedString(alphaSnapshot.Object, "spec", "snapshotContentName")
if err != nil {
return 0, fmt.Errorf("failed get v1alpha1/VolumeSnapshot.spec.snapshotContentName for %s: %s", name, err)
}
if found {
snapshotContentName = &snapshotContentNameStr
}
betaSnapshot := v1beta1snapshot.VolumeSnapshot{
TypeMeta: metav1.TypeMeta{
Kind: "VolumeSnapshot",
APIVersion: v1beta1snapshot.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: v1beta1snapshot.VolumeSnapshotSpec{
Source: v1beta1snapshot.VolumeSnapshotSource{
VolumeSnapshotContentName: snapshotContentName,
},
VolumeSnapshotClassName: volSnapClassName,
},
}
if snapshotContentName == nil {
// The VolumeSnapshot might not be bound to a VolumeSnapshotContent
// yet (either due to delay or pathological reasons). Associate any
// PersistentVolumeClaim name we may have instead. (Note that
// exactly one of VolumeSnapshotContentName and
// PersistentVolumeClaimName may be defined on a VolumeSnapshot at
// any given time.)
betaSnapshot.Spec.Source.PersistentVolumeClaimName = persistentVolumeClaimName
}
fmt.Printf("--- Writing VolumeSnapshot %q\n", name)
err = volWriter.writeVolumeSnapshot(betaSnapshot)
if err != nil {
return 0, fmt.Errorf("failed to write VolumeSnapshot %q: %s", name, err)
}
num++
}
return num, nil
}