Repository navigation
Expand file tree
/
Copy pathstorage-migration.py
More file actions
executable file
·502 lines (412 loc) · 17.2 KB
/
Copy pathstorage-migration.py
File metadata and controls
executable file
·502 lines (412 loc) · 17.2 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#!/usr/bin/env python3
"""
storage-migration.py - Automate storage class migration for OpenShift Virtualization VMs
This tool addresses Dave Thomas's problem: migrating thousands of VMs between storage classes.
Modes:
plan - Analyze what will be migrated (dry-run, impact analysis)
execute - Perform the actual migration
Usage:
./storage-migration.py plan --from-sc standard --to-sc standard-fast
./storage-migration.py execute --from-sc standard --to-sc standard-fast [--dry-run]
Example:
# Plan migration from standard to standard-fast
./storage-migration.py plan --from-sc standard --to-sc standard-fast -n default
# Execute migration (dry-run first)
./storage-migration.py execute --from-sc standard --to-sc standard-fast --dry-run
# Actually execute
./storage-migration.py execute --from-sc standard --to-sc standard-fast
"""
import argparse
import json
import subprocess
import sys
import time
from typing import Dict, List, Optional, Any
from datetime import datetime
class Colors:
"""ANSI color codes for terminal output"""
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def run_kubectl(args: List[str], check=True) -> Dict[str, Any]:
"""Run kubectl/oc command and return parsed JSON output"""
for cmd in ['oc', 'kubectl']:
try:
result = subprocess.run(
[cmd] + args + ['-o', 'json'],
capture_output=True,
text=True,
check=check
)
if result.returncode != 0:
return None
return json.loads(result.stdout) if result.stdout else None
except FileNotFoundError:
continue
except subprocess.CalledProcessError:
return None
except json.JSONDecodeError:
return None
return None
def run_kubectl_apply(yaml_content: str, dry_run: bool = False) -> bool:
"""Apply YAML content using kubectl"""
for cmd in ['oc', 'kubectl']:
try:
args = [cmd, 'apply', '-f', '-']
if dry_run:
args.append('--dry-run=client')
result = subprocess.run(
args,
input=yaml_content,
capture_output=True,
text=True,
check=True
)
return True
except FileNotFoundError:
continue
except subprocess.CalledProcessError as e:
print(f"{Colors.FAIL}Error applying YAML: {e.stderr}{Colors.ENDC}")
return False
return False
def run_kubectl_patch(resource_type: str, resource_name: str, namespace: str,
patch: str, patch_type: str = 'json', dry_run: bool = False) -> bool:
"""Patch a Kubernetes resource"""
for cmd in ['oc', 'kubectl']:
try:
args = [cmd, 'patch', resource_type, resource_name, '-n', namespace,
'--type', patch_type, '-p', patch]
if dry_run:
args.append('--dry-run=client')
result = subprocess.run(
args,
capture_output=True,
text=True,
check=True
)
return True
except FileNotFoundError:
continue
except subprocess.CalledProcessError as e:
print(f"{Colors.FAIL}Error patching resource: {e.stderr}{Colors.ENDC}")
return False
return False
def patch_vm_to_use_new_datavolumes(vm_name: str, vm_namespace: str,
old_dv_name: str, new_dv_name: str,
dry_run: bool = False) -> bool:
"""Patch VM to use new DataVolume instead of old one"""
# We need to update two places:
# 1. spec.dataVolumeTemplates[].metadata.name
# 2. spec.template.spec.volumes[].dataVolume.name
# Get current VM to find the indices
vm = run_kubectl(['get', 'vm', vm_name, '-n', vm_namespace], check=False)
if not vm:
return False
# Build JSON patch operations
patches = []
# Find and update dataVolumeTemplates
dv_templates = vm.get('spec', {}).get('dataVolumeTemplates', [])
for idx, template in enumerate(dv_templates):
if template.get('metadata', {}).get('name') == old_dv_name:
patches.append({
"op": "replace",
"path": f"/spec/dataVolumeTemplates/{idx}/metadata/name",
"value": new_dv_name
})
# Also need to update the storageClassName in the template
# But we'll keep it simple for now - just update the name
break
# Find and update volumes in template
volumes = vm.get('spec', {}).get('template', {}).get('spec', {}).get('volumes', [])
for idx, volume in enumerate(volumes):
dv_ref = volume.get('dataVolume', {})
if dv_ref.get('name') == old_dv_name:
patches.append({
"op": "replace",
"path": f"/spec/template/spec/volumes/{idx}/dataVolume/name",
"value": new_dv_name
})
break
if not patches:
print(f" {Colors.WARNING}⚠️ Could not find DataVolume references in VM{Colors.ENDC}")
return False
# Apply patch
patch_json = json.dumps(patches)
return run_kubectl_patch('vm', vm_name, vm_namespace, patch_json, 'json', dry_run)
def get_all_vms(namespace: Optional[str] = None) -> List[Dict]:
"""Get all VMs in namespace or all namespaces"""
cmd = ['get', 'vm']
if namespace:
cmd.extend(['-n', namespace])
else:
cmd.append('--all-namespaces')
result = run_kubectl(cmd, check=False)
return result.get('items', []) if result else []
def get_datavolumes(namespace: str) -> List[Dict]:
"""Get all DataVolumes in namespace"""
result = run_kubectl(['get', 'dv', '-n', namespace], check=False)
return result.get('items', []) if result else []
def get_storage_class(name: str) -> Optional[Dict]:
"""Get storage class details"""
return run_kubectl(['get', 'sc', name], check=False)
def find_vms_using_storage_class(storage_class: str, namespace: Optional[str] = None) -> List[Dict]:
"""Find all VMs using a specific storage class"""
vms_to_migrate = []
all_vms = get_all_vms(namespace)
for vm in all_vms:
vm_name = vm['metadata']['name']
vm_namespace = vm['metadata']['namespace']
# Get DataVolumes for this VM
dvs = get_datavolumes(vm_namespace)
# Find DVs owned by this VM
vm_uid = vm['metadata']['uid']
vm_dvs = []
for dv in dvs:
owner_refs = dv.get('metadata', {}).get('ownerReferences', [])
for ref in owner_refs:
if ref.get('kind') == 'VirtualMachine' and ref.get('uid') == vm_uid:
vm_dvs.append(dv)
break
# Check if any DV uses the target storage class
for dv in vm_dvs:
dv_sc = dv.get('spec', {}).get('storage', {}).get('storageClassName')
if dv_sc == storage_class:
vms_to_migrate.append({
'vm': vm,
'datavolumes': vm_dvs
})
break
return vms_to_migrate
def print_migration_plan(from_sc: str, to_sc: str, namespace: Optional[str] = None):
"""Print migration plan (what will be migrated)"""
print("=" * 80)
print(f" {Colors.BOLD}STORAGE MIGRATION PLAN{Colors.ENDC}")
print("=" * 80)
print()
print(f" From StorageClass: {Colors.WARNING}{from_sc}{Colors.ENDC}")
print(f" To StorageClass: {Colors.OKGREEN}{to_sc}{Colors.ENDC}")
if namespace:
print(f" Namespace: {namespace}")
else:
print(f" Namespace: All namespaces")
print()
# Validate target storage class exists
target_sc = get_storage_class(to_sc)
if not target_sc:
print(f"{Colors.FAIL}❌ Error: Storage class '{to_sc}' not found!{Colors.ENDC}")
sys.exit(1)
print(f"{Colors.OKGREEN}✅ Target storage class '{to_sc}' exists{Colors.ENDC}")
print()
# Find VMs to migrate
print(f"Searching for VMs using storage class '{from_sc}'...")
vms_to_migrate = find_vms_using_storage_class(from_sc, namespace)
if not vms_to_migrate:
print(f"{Colors.WARNING}No VMs found using storage class '{from_sc}'{Colors.ENDC}")
print()
print("=" * 80)
return
print(f"{Colors.OKBLUE}Found {len(vms_to_migrate)} VM(s) to migrate:{Colors.ENDC}")
print()
total_storage = 0
total_dvs = 0
for idx, item in enumerate(vms_to_migrate, 1):
vm = item['vm']
dvs = item['datavolumes']
vm_name = vm['metadata']['name']
vm_namespace = vm['metadata']['namespace']
vm_status = vm.get('status', {}).get('printableStatus', 'Unknown')
print(f"{idx}. {Colors.OKGREEN}VM:{Colors.ENDC} {vm_name} (namespace: {vm_namespace})")
print(f" ├─ Status: {vm_status}")
print(f" ├─ DataVolumes to migrate: {len(dvs)}")
for dv_idx, dv in enumerate(dvs):
dv_name = dv['metadata']['name']
dv_size_str = dv.get('spec', {}).get('storage', {}).get('resources', {}).get('requests', {}).get('storage', '0Gi')
dv_sc = dv.get('spec', {}).get('storage', {}).get('storageClassName', 'N/A')
# Parse size (simplistic - assumes Gi)
try:
size_gi = int(dv_size_str.replace('Gi', '').replace('G', ''))
total_storage += size_gi
except:
pass
total_dvs += 1
is_last = (dv_idx == len(dvs) - 1)
connector = "└─" if is_last else "├─"
print(f" {connector} DataVolume: {dv_name}")
print(f" {' ' if is_last else '│ '} ├─ Size: {dv_size_str}")
print(f" {' ' if is_last else '│ '} └─ Current StorageClass: {dv_sc}")
print()
print("=" * 80)
print(f" {Colors.BOLD}MIGRATION SUMMARY{Colors.ENDC}")
print("=" * 80)
print(f" VMs to migrate: {len(vms_to_migrate)}")
print(f" DataVolumes to clone: {total_dvs}")
print(f" Total storage: ~{total_storage} Gi")
print()
print(f" {Colors.WARNING}⚠️ Migration will:{Colors.ENDC}")
print(f" 1. Create new DataVolumes on '{to_sc}'")
print(f" 2. Clone data from existing DataVolumes")
print(f" 3. Update VMs to use new DataVolumes")
print(f" 4. Mark old DataVolumes as orphaned")
print()
print(f" {Colors.WARNING}⚠️ Recommended steps:{Colors.ENDC}")
print(f" 1. Stop all VMs before migration (prevents data corruption)")
print(f" 2. Use --dry-run first to test")
print(f" 3. Back up critical VMs")
print(f" 4. Clean up orphaned resources after verification")
print()
print("=" * 80)
def execute_migration(from_sc: str, to_sc: str, namespace: Optional[str] = None, dry_run: bool = False):
"""Execute the migration"""
print("=" * 80)
print(f" {Colors.BOLD}EXECUTING STORAGE MIGRATION{Colors.ENDC}")
if dry_run:
print(f" {Colors.WARNING}(DRY RUN MODE - No actual changes will be made){Colors.ENDC}")
print("=" * 80)
print()
# Get VMs to migrate
vms_to_migrate = find_vms_using_storage_class(from_sc, namespace)
if not vms_to_migrate:
print(f"{Colors.WARNING}No VMs found to migrate.{Colors.ENDC}")
return
print(f"Migrating {len(vms_to_migrate)} VM(s)...")
print()
for idx, item in enumerate(vms_to_migrate, 1):
vm = item['vm']
dvs = item['datavolumes']
vm_name = vm['metadata']['name']
vm_namespace = vm['metadata']['namespace']
print(f"[{idx}/{len(vms_to_migrate)}] Migrating VM: {Colors.OKGREEN}{vm_name}{Colors.ENDC}")
# Track old → new DV name mapping for this VM
dv_mapping = {}
# Step 1: Create new DataVolumes
for dv in dvs:
dv_name = dv['metadata']['name']
dv_size = dv.get('spec', {}).get('storage', {}).get('resources', {}).get('requests', {}).get('storage', '5Gi')
dv_access_modes = dv.get('spec', {}).get('storage', {}).get('accessModes', ['ReadWriteOnce'])
new_dv_name = f"{dv_name}-migrated-{int(time.time())}"
dv_mapping[dv_name] = new_dv_name # Store mapping
print(f" Creating new DataVolume: {new_dv_name}")
# Create new DataVolume YAML
timestamp = datetime.now().isoformat()
new_dv_yaml = f"""
apiVersion: cdi.kubevirt.io/v1beta1
kind: DataVolume
metadata:
name: {new_dv_name}
namespace: {vm_namespace}
labels:
storage-migration: "true"
source-sc: "{from_sc}"
target-sc: "{to_sc}"
annotations:
migration-timestamp: "{timestamp}"
spec:
source:
pvc:
namespace: {vm_namespace}
name: {dv_name}
storage:
storageClassName: {to_sc}
accessModes:
{chr(10).join(f' - {mode}' for mode in dv_access_modes)}
resources:
requests:
storage: {dv_size}
"""
if dry_run:
print(f" {Colors.OKCYAN}[DRY RUN] Would create:{Colors.ENDC}")
print(new_dv_yaml)
else:
success = run_kubectl_apply(new_dv_yaml, dry_run=False)
if success:
print(f" {Colors.OKGREEN}✅ DataVolume created{Colors.ENDC}")
else:
print(f" {Colors.FAIL}❌ Failed to create DataVolume{Colors.ENDC}")
continue
# Small delay to ensure unique timestamps
time.sleep(0.1)
# Step 2: Update VM spec to use new DataVolumes
print(f" Updating VM spec to use new DataVolumes...")
for old_dv_name, new_dv_name in dv_mapping.items():
if dry_run:
print(f" {Colors.OKCYAN}[DRY RUN] Would patch VM: {old_dv_name} → {new_dv_name}{Colors.ENDC}")
else:
success = patch_vm_to_use_new_datavolumes(
vm_name, vm_namespace, old_dv_name, new_dv_name, dry_run=False
)
if success:
print(f" {Colors.OKGREEN}✅ VM patched: {old_dv_name} → {new_dv_name}{Colors.ENDC}")
else:
print(f" {Colors.FAIL}❌ Failed to patch VM{Colors.ENDC}")
print()
print("=" * 80)
if dry_run:
print(f"{Colors.OKGREEN}DRY RUN COMPLETE{Colors.ENDC}")
print(f"Run without --dry-run to execute actual migration")
else:
print(f"{Colors.OKGREEN}MIGRATION INITIATED{Colors.ENDC}")
print()
print(f"Monitor progress with:")
print(f" kubectl get dv -n {namespace or 'default'} -w")
print()
print(f"After migration completes:")
print(f" 1. Verify VMs start successfully")
print(f" 2. Find orphaned resources: ./vm-tree.py --find-orphans")
print(f" 3. Clean up old DataVolumes")
print("=" * 80)
def main():
parser = argparse.ArgumentParser(
description='Automate storage class migration for VMs',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Plan migration (shows what will be migrated)
%(prog)s plan --from-sc standard --to-sc standard-fast
# Execute migration with dry-run
%(prog)s execute --from-sc standard --to-sc standard-fast --dry-run
# Execute actual migration
%(prog)s execute --from-sc standard --to-sc standard-fast
# Plan migration for specific namespace
%(prog)s plan --from-sc standard --to-sc standard-fast -n production
"""
)
parser.add_argument('mode', choices=['plan', 'execute'], help='Operation mode')
parser.add_argument('--from-sc', '--from-storage-class', required=True, dest='from_sc',
help='Source storage class to migrate from')
parser.add_argument('--to-sc', '--to-storage-class', required=True, dest='to_sc',
help='Target storage class to migrate to')
parser.add_argument('-n', '--namespace', help='Kubernetes namespace (default: searches all)')
parser.add_argument('--dry-run', action='store_true',
help='Dry run mode (only for execute mode)')
parser.add_argument('--no-color', action='store_true', help='Disable colored output')
args = parser.parse_args()
# Disable colors if requested
if args.no_color:
for attr in dir(Colors):
if not attr.startswith('_'):
setattr(Colors, attr, '')
# Check if kubectl/oc is available
cmd_available = False
for cmd in ['oc', 'kubectl']:
try:
subprocess.run([cmd, 'version'], capture_output=True, check=True)
cmd_available = True
break
except (subprocess.CalledProcessError, FileNotFoundError):
continue
if not cmd_available:
print(f"{Colors.FAIL}Error: Neither 'oc' nor 'kubectl' command found.{Colors.ENDC}")
sys.exit(1)
# Execute mode
if args.mode == 'plan':
print_migration_plan(args.from_sc, args.to_sc, args.namespace)
elif args.mode == 'execute':
execute_migration(args.from_sc, args.to_sc, args.namespace, args.dry_run)
if __name__ == '__main__':
main()