KubeZero/admin/migrate_argo_values.py

98 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import sys
import argparse
import io
import yaml
def migrate(values):
"""Actual changes here"""
2023-11-21 20:01:07 +00:00
# Cleanup
values.pop("Domain", None)
values.pop("clusterName", None)
if "addons" in values:
if not values["addons"]:
values.pop("addons")
# migrate eck operator to new operator module
try:
if values["logging"]["eck-operator"]["enabled"]:
if "operators" not in values:
2023-11-22 13:19:13 +00:00
values["operators"] = { "enabled": True }
values["operators"]["eck-operator"] = { "enabled": True }
values["logging"].pop("eck-operator", None)
2023-11-21 20:01:07 +00:00
except KeyError:
pass
return values
def deleteKey(values, key):
"""Delete key from dictionary if exists"""
try:
values.pop(key)
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
"""
2022-09-15 11:05:38 +00:00
def increase_indent(self, flow=False, indentless=False):
return super(MyDumper, self).increase_indent(flow, False)
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)
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)
return d
2022-09-15 11:05:38 +00:00
yaml.add_representer(str, str_presenter)
2022-09-15 11:05:38 +00:00
# to use with safe_dump:
yaml.representer.SafeRepresenter.add_representer(str, str_presenter)
2022-09-15 11:05:38 +00:00
# Read values
values = yaml.safe_load(sys.stdin)
2022-09-15 11:05:38 +00:00
# Output new values
buffer = io.StringIO()
2022-09-15 11:05:38 +00:00
yaml.dump(
rec_sort(migrate(values)),
2022-09-15 11:05:38 +00:00
sys.stdout,
default_flow_style=False,
indent=2,
sort_keys=False,
Dumper=MyDumper,
)