Created
July 31, 2019 13:32
-
-
Save mriedmann/0ddef2ef29f1e2798670f32e53df3439 to your computer and use it in GitHub Desktop.
This file contains 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/python3 | |
from yaml import load, dump | |
import os, sys, re, glob | |
try: | |
from yaml import CLoader as Loader, CDumper as Dumper | |
except ImportError: | |
from yaml import Loader, Dumper | |
_dir_out = sys.argv[1] | |
_filenames_in = glob.glob(sys.argv[2]) | |
def get_short_kindname(kind): | |
return { | |
'Build': 'build', | |
'BuildConfig': 'bc', | |
'DeploymentConfig': 'dc', | |
'Deployments': 'deploy', | |
'Event': 'ev', | |
'ImageStream': 'is', | |
'ImageStreamTag': 'istag', | |
'ImageStreamImage': 'isimage', | |
'Job': 'job', | |
'CronJob': 'cj', | |
'LimitRange': 'limits', | |
'Node': 'node', | |
'Pod': 'po', | |
'ResourceQuota': 'quota', | |
'ReplicationController': 'rc', | |
'ReplicaSet': 'rs', | |
'Secrets': 'secrets', | |
'Service': 'svc', | |
'ServiceAccount': 'sa', | |
'StatefulSets': 'sts', | |
'PersistentVolume': 'pv', | |
'PersistentVolumeClaim': 'pvc', | |
}.get(kind, kind.lower()) | |
def get_subdir(kind): | |
return { | |
'PersistentVolumeClaim': 'files', | |
'DeploymentConfig': 'templates', | |
'Service': 'files', | |
'Route': 'templates', | |
}.get(kind, 'files') | |
def write_object(o, dir_out): | |
sk = get_short_kindname(o['kind']) | |
sd = get_subdir(o['kind']) | |
name = o['metadata']['name'] + '-' + sk + '.yml' | |
filename_out = os.path.join(dir_out,sd,name) | |
print(filename_out) | |
with open(filename_out, 'w') as f: | |
output = dump(o, Dumper=Dumper, default_flow_style=False) | |
f.write(output) | |
def main(filename_in, dir_out): | |
with open(filename_in, 'r') as f: | |
data = load(f, Loader=Loader) | |
if 'items' in data: | |
for o in data['items']: | |
write_object(o, dir_out) | |
for _filename_in in _filenames_in: | |
print("Processing %s => %s" % (_filename_in, _dir_out)) | |
main(_filename_in, _dir_out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment