|
#!/usr/bin/env python |
|
|
|
import os |
|
import sys |
|
import yaml |
|
|
|
if len(sys.argv) != 2: |
|
print("Usage: federer.py file") |
|
sys.exit(1) |
|
elif not os.path.exists(sys.argv[1]): |
|
print("Input file not found") |
|
sys.exit(1) |
|
else: |
|
inputfile = sys.argv[1] |
|
|
|
results = [] |
|
data = "" |
|
|
|
delimiter = False |
|
inputlines = open(inputfile).readlines() |
|
for index, line in enumerate(inputlines): |
|
if (line.startswith('---') and index != 0) or index == len(inputlines) - 1: |
|
delimiter = True |
|
results.append(data) |
|
data = "" |
|
else: |
|
data += line |
|
|
|
if not delimiter: |
|
data = open(inputfile).read() |
|
results = [data] |
|
|
|
federatedresources = [] |
|
federatedplacements = [] |
|
federatedtypes = [] |
|
customresources = [] |
|
for r in results: |
|
try: |
|
data = yaml.load(r) |
|
except Exception as e: |
|
print("Failed to parse input file. Got %s" % e) |
|
name, kind = data['metadata']['name'].replace(':', '.'), data['kind'] |
|
print("Processing %s %s" % (kind, name)) |
|
placement = {'kind': 'Federated%sPlacement' % kind, 'spec': {'clusterNames': ['cluster1', 'cluster2']}, |
|
'metadata': {'name': name}} |
|
if 'namespace' in data['metadata']: |
|
placement['metadata']['namespace'] = data['metadata']['namespace'] |
|
if data['kind'] == 'Namespace': |
|
continue |
|
if data['kind'] not in federatedtypes and data['kind'] != 'Namespace': |
|
federatedtypes.append(data['kind']) |
|
if data['kind'] == 'CustomResourceDefinition': |
|
customname = name.split('.')[0] |
|
customresources.append(customname) |
|
newdata = {'metadata': {'name': name}} |
|
if 'namespace' in data['metadata']: |
|
newdata['metadata']['namespace'] = data['metadata']['namespace'] |
|
newdata['kind'] = "Federated%s" % data['kind'] |
|
newdata['apiVersion'] = "primitives.federation.k8s.io/v1alpha1" |
|
spec = {k: data[k] for k in data if k not in ['kind', 'apiVersion', 'metadata']} |
|
newdata['spec'] = {'template': spec} |
|
if data['kind'] == 'Deployment': |
|
labels = data['spec']['template']['metadata']['labels'] |
|
newdata['metadata']['labels'] = labels |
|
newdata['spec']['template']['spec']['selector'] = {'matchLabels': labels} |
|
placement['apiVersion'] = "primitives.federation.k8s.io/v1alpha1" |
|
federatedresources.append([newdata, placement]) |
|
|
|
|
|
Dumper = yaml.dumper.SafeDumper |
|
Dumper.ignore_aliases = lambda self, data: True |
|
with open("federated_%s" % inputfile, 'w') as f: |
|
for entry in federatedresources: |
|
resource, placement = entry |
|
if resource is not None: |
|
output = yaml.dump(resource, default_flow_style=False, indent=2, allow_unicode=True, |
|
encoding=None, Dumper=Dumper)[:-1] |
|
f.write(output) |
|
f.write('\n---\n') |
|
output = yaml.dump(placement, default_flow_style=False, indent=2, allow_unicode=True, |
|
encoding=None, Dumper=Dumper)[:-1] |
|
f.write(output) |
|
f.write('\n---\n') |
|
print("federated_%s created" % inputfile) |
|
|
|
with open("federated_%s.sh" % inputfile, 'w') as f: |
|
print("federated_%s.sh created" % inputfile) |
|
for _type in sorted(federatedtypes): |
|
f.write("kubefed2 enable %s\n" % (_type)) |
|
f.write("kubectl create -f federated_%s\n" % inputfile) |
|
for _type in sorted(customresources): |
|
f.write("kubefed2 enable %s\n" % (_type)) |
|
|
|
sys.exit(0) |