Last active
September 15, 2018 11:31
-
-
Save leoh0/03ca401d690579fdf65c222fa5bfccef to your computer and use it in GitHub Desktop.
python csi-pv.py $PV # for migrating CSI RBD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| import argparse | |
| import os | |
| import subprocess | |
| import sys | |
| import time | |
| import uuid | |
| from random import randrange | |
| import yaml | |
| CSIKEY = 'csi-rbd-secret' | |
| CSIKEY_NS = 'kube-system' | |
| def main(): | |
| parser = argparse.ArgumentParser(description='rbd pv to csi rbd pv') | |
| parser.add_argument('pv', type=str, | |
| help='target pv name') | |
| args = parser.parse_args() | |
| kwargs = {} | |
| kwargs.setdefault('stdout', subprocess.PIPE) | |
| kwargs.setdefault('stderr', subprocess.STDOUT) | |
| kwargs.setdefault('universal_newlines', True) | |
| kwargs['env'] = os.environ.copy() | |
| command = ['kubectl', 'get', 'pv', | |
| args.pv, "--export=true", "-o", "yaml"] | |
| pipe = subprocess.Popen(command, **kwargs) | |
| output, _ = pipe.communicate() | |
| try: | |
| data = yaml.load(output) | |
| except yaml.YAMLError as exc: | |
| print(exc) | |
| if 'metadata' not in data: | |
| print("There is no pv") | |
| return | |
| # 1 | |
| data['metadata']['annotations']['pv.kubernetes.io/provisioned-by'] = \ | |
| 'csi-rbdplugin' | |
| data['metadata']['annotations'].pop('rbdProvisionerIdentity', None) | |
| data['metadata'].pop('creationTimestamp', None) | |
| # 2 | |
| data['metadata']['finalizers'].append('external-attacher/csi-rbdplugin') | |
| # 3 | |
| newname = "pvc-" + "".join(data['metadata']['name'].split('-')[1:4]) | |
| data['metadata']['name'] = newname | |
| data['metadata'].pop('resourceVersion', None) | |
| data['metadata'].pop('selfLink', None) | |
| data['metadata'].pop('uid', None) | |
| data['spec'].pop('claimRef', None) | |
| # 4 | |
| csi = {} | |
| csi['driver'] = 'csi-rbdplugin' | |
| csi['nodePublishSecretRef'] = {} | |
| csi['nodePublishSecretRef']['name'] = CSIKEY | |
| csi['nodePublishSecretRef']['namespace'] = CSIKEY_NS | |
| va = {} | |
| va['csiNodePublishSecretName'] = CSIKEY | |
| va['csiNodePublishSecretNamespace'] = CSIKEY_NS | |
| va['csiProvisionerSecretName'] = CSIKEY | |
| va['csiProvisionerSecretNamespace'] = CSIKEY_NS | |
| if data['spec']['rbd'].get('imageFeatures', None): | |
| va['imageFeatures'] = data['spec']['rbd']['imageFeatures'] | |
| if data['spec']['rbd'].get('imageFormat', None): | |
| va['imageFormat'] = data['spec']['rbd']['imageFormat'] | |
| if data['spec']['rbd'].get('fsType', None): | |
| csi['fsType'] = data['spec']['rbd']['fsType'] | |
| va['fsType'] = data['spec']['rbd']['fsType'] | |
| va['monitors'] = ",".join(data['spec']['rbd']['monitors']) | |
| va['pool'] = data['spec']['rbd']['pool'] | |
| # 6 | |
| va['storage.kubernetes.io/csiProvisionerIdentity'] = \ | |
| str(int(time.time())) + "%03d" % randrange(1000) + \ | |
| '-' + str(randrange(10000)) + \ | |
| '-csi-rbdplugin' | |
| csi['volumeAttributes'] = va | |
| data['spec']['csi'] = csi | |
| data['spec'].pop('rbd', None) | |
| # 7 | |
| csi['volumeHandle'] = \ | |
| 'csi-rbd-' + str(uuid.uuid1()) | |
| # 5 | |
| # This is relevant with storageclass | |
| data['spec']['storageClassName'] = 'csi-rbd' | |
| # For safety | |
| data['spec']['persistentVolumeReclaimPolicy'] = 'Retain' | |
| data.pop('status', None) | |
| with open('csi-pv-' + newname + '.yaml', 'wt') as f: | |
| yamlfile = yaml.dump(data, | |
| default_flow_style=False, | |
| allow_unicode=True, | |
| encoding=None) | |
| f.write(yamlfile) | |
| print('check csi-pv-%s.yaml' % newname) | |
| if __name__ == '__main__': | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment