Last active
September 15, 2018 11:33
-
-
Save leoh0/cd17492cc5274ae7e41fdf56967e0177 to your computer and use it in GitHub Desktop.
python csi-pvc.py ${PVCNAME} -n ${NAMESPACE} # for migration csi pvc
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 yaml | |
| def main(): | |
| parser = argparse.ArgumentParser(description='rbd pvc to csi rbd pvc') | |
| parser.add_argument('pvc', type=str, | |
| help='target pvc name') | |
| parser.add_argument('-n', '--namespace', type=str, | |
| default='default', | |
| help='target namespace (default: default)') | |
| 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', 'pvc', '-n', args.namespace, | |
| args.pvc, "--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 pvc") | |
| return | |
| data['metadata']['annotations'].pop( | |
| 'control-plane.alpha.kubernetes.io/leader', None) | |
| data['metadata']['annotations'].pop( | |
| 'kubectl.kubernetes.io/last-applied-configuration', None) | |
| # 1 | |
| data['metadata']['annotations'][ | |
| 'volume.beta.kubernetes.io/storage-provisioner'] = \ | |
| 'csi-rbdplugin' | |
| data['metadata'].pop('creationTimestamp', None) | |
| data['metadata'].pop('resourceVersion', None) | |
| data['metadata'].pop('selfLink', None) | |
| data['metadata'].pop('uid', None) | |
| # 2 | |
| # This is relevant with storageclass | |
| data['spec']['storageClassName'] = 'csi-rbd' | |
| # 3 | |
| newname = "pvc-" + "".join(data['spec']['volumeName'].split('-')[1:4]) | |
| data['spec']['volumeName'] = newname | |
| data.pop('status', None) | |
| with open('csi-pvc-' + newname + '.yaml', 'wt') as f: | |
| yamlfile = yaml.dump(data, | |
| default_flow_style=False, | |
| allow_unicode=True, | |
| encoding=None) | |
| f.write(yamlfile) | |
| print('check csi-pvc-%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