-
Notifications
You must be signed in to change notification settings - Fork 2
/
yamldifftool.py
60 lines (49 loc) · 2.42 KB
/
yamldifftool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# coding: utf-8
import yaml
import argparse
import sys
def traverse_and_set(user_provided, root_diff, path):
k = root_diff
for item in path[:-1]:
if item not in k.keys():
k[item] = {}
k = k.get(item)
k[path[-1]] = user_provided
def filter_defaults(base, user_provided, root_diff, path=[], strict=False):
if type(user_provided) is dict:
for key in user_provided.keys():
if base is None or type(base) is str:
traverse_and_set(user_provided.get(key), root_diff, path + [key])
continue
if not strict and key not in base.keys():
traverse_and_set(user_provided.get(key), root_diff, path + [key])
elif key in base.keys():
filter_defaults(base.get(key), user_provided.get(key), root_diff, path=path + [key], strict=strict)
elif type(user_provided) is list:
if user_provided != base:
traverse_and_set(user_provided, root_diff, path)
else:
if base != user_provided:
traverse_and_set(user_provided, root_diff, path)
if __name__ == "__main__":
root_diff = {}
path = []
parser = argparse.ArgumentParser(description="Tool to identify diffs between values.yaml and default")
parser.add_argument("default_values", metavar="default_values.yaml", type=str,
help="Base values.yaml for the helm chart version you're using")
parser.add_argument("user_customized_values", metavar="customized.yaml", type=str, help="Customized values.yaml")
parser.add_argument("-o", "--output", default=None, type=str,
help="Write to filename. If not specified, output is written to stdout.")
parser.add_argument("-s", "--strict", default=False, action=argparse.BooleanOptionalAction, type=bool,
help="strict mode will drop all options not in default values.yaml.")
args = parser.parse_args()
with open(args.default_values, 'r') as default_values:
default_v = yaml.safe_load(default_values)
with open(args.user_customized_values, 'r') as overwritten_values:
overwritten_v = yaml.safe_load(overwritten_values)
filter_defaults(default_v, overwritten_v, root_diff, path, args.strict)
if args.output:
with open(args.output, 'w') as outfile:
yaml.dump(root_diff, outfile, sort_keys=False)
else:
yaml.dump(root_diff, sys.stdout, sort_keys=False)