2022-04-29 18:22:20 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
import io
|
2022-09-14 17:08:14 +00:00
|
|
|
import yaml
|
2022-04-29 18:22:20 +00:00
|
|
|
|
2022-09-14 17:08:14 +00:00
|
|
|
|
|
|
|
def migrate(values):
|
|
|
|
"""Actual changes here"""
|
|
|
|
|
|
|
|
# migrate ClusterName to clusterName
|
|
|
|
if "ClusterName" in values:
|
|
|
|
values["clusterName"] = values["ClusterName"]
|
|
|
|
values.pop("ClusterName")
|
|
|
|
|
2022-09-20 12:54:33 +00:00
|
|
|
# Remove HighAvailableControlplane
|
|
|
|
try:
|
|
|
|
values["global"]["highAvailable"] = values["HighAvailableControlplane"]
|
|
|
|
values.pop("HighAvailableControlplane")
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
2022-09-14 17:08:14 +00:00
|
|
|
# Create new clusterwide cloudprovider data if possible
|
2022-09-20 12:54:33 +00:00
|
|
|
# IamArn: arn:aws:iam::<ACCOUNT_ID>:role/<REGION>.<CLUSTERNAME>.cert-manager
|
2022-09-14 17:08:14 +00:00
|
|
|
try:
|
2022-09-20 12:54:33 +00:00
|
|
|
if values["cert-manager"]["IamArn"]:
|
|
|
|
account_id = values["cert-manager"]["IamArn"].split(":")[4]
|
|
|
|
region = values["cert-manager"]["IamArn"].split(":")[5].split('.')[0].split('/')[1]
|
2022-09-14 17:08:14 +00:00
|
|
|
if "global" not in values:
|
|
|
|
values["global"] = {}
|
|
|
|
if "aws" not in values["global"]:
|
|
|
|
values["global"]["aws"] = {}
|
|
|
|
|
2022-09-20 12:54:33 +00:00
|
|
|
values["global"]["aws"]["region"] = region
|
|
|
|
values["global"]["aws"]["accountId"] = account_id
|
|
|
|
|
2022-09-14 17:08:14 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return values
|
|
|
|
|
|
|
|
|
2022-09-15 11:05:38 +00:00
|
|
|
class MyDumper(yaml.Dumper):
|
|
|
|
"""
|
|
|
|
Required to add additional indent for arrays to match yq behaviour to reduce noise in diffs
|
|
|
|
"""
|
|
|
|
def increase_indent(self, flow=False, indentless=False):
|
|
|
|
return super(MyDumper, self).increase_indent(flow, False)
|
|
|
|
|
2022-09-14 17:08:14 +00:00
|
|
|
def str_presenter(dumper, data):
|
|
|
|
if len(data.splitlines()) > 1: # check for multiline string
|
|
|
|
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
|
|
|
|
return dumper.represent_scalar("tag:yaml.org,2002:str", data)
|
|
|
|
|
|
|
|
|
2022-09-11 11:54:56 +00:00
|
|
|
def rec_sort(d):
|
|
|
|
if isinstance(d, dict):
|
|
|
|
res = dict()
|
|
|
|
|
|
|
|
# Always have "enabled" first if present
|
|
|
|
if "enabled" in d.keys():
|
|
|
|
res["enabled"] = rec_sort(d["enabled"])
|
|
|
|
d.pop("enabled")
|
|
|
|
|
|
|
|
# next is "name" if present
|
|
|
|
if "name" in d.keys():
|
|
|
|
res["name"] = rec_sort(d["name"])
|
|
|
|
d.pop("name")
|
|
|
|
|
|
|
|
for k in sorted(d.keys()):
|
|
|
|
res[k] = rec_sort(d[k])
|
|
|
|
return res
|
|
|
|
if isinstance(d, list):
|
|
|
|
for idx, elem in enumerate(d):
|
|
|
|
d[idx] = rec_sort(elem)
|
2022-09-14 17:08:14 +00:00
|
|
|
|
2022-09-11 11:54:56 +00:00
|
|
|
return d
|
|
|
|
|
|
|
|
|
2022-09-15 11:05:38 +00:00
|
|
|
yaml.add_representer(str, str_presenter)
|
2022-04-29 18:22:20 +00:00
|
|
|
|
2022-09-15 11:05:38 +00:00
|
|
|
# to use with safe_dump:
|
|
|
|
yaml.representer.SafeRepresenter.add_representer(str, str_presenter)
|
2022-04-29 18:22:20 +00:00
|
|
|
|
2022-09-15 11:05:38 +00:00
|
|
|
# Read values
|
|
|
|
values = yaml.safe_load(sys.stdin)
|
2022-04-29 18:22:20 +00:00
|
|
|
|
2022-09-15 11:05:38 +00:00
|
|
|
# Output new values
|
2022-04-29 18:22:20 +00:00
|
|
|
buffer = io.StringIO()
|
2022-09-15 11:05:38 +00:00
|
|
|
yaml.dump(
|
2022-09-14 17:08:14 +00:00
|
|
|
rec_sort(migrate(values)),
|
2022-09-15 11:05:38 +00:00
|
|
|
sys.stdout,
|
2022-09-14 17:08:14 +00:00
|
|
|
default_flow_style=False,
|
|
|
|
indent=2,
|
|
|
|
sort_keys=False,
|
2022-09-15 11:05:38 +00:00
|
|
|
Dumper=MyDumper
|
2022-09-14 17:08:14 +00:00
|
|
|
)
|