Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of dict patches in lists #362

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions openshift/dynamic/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ def apply(resource, definition):
# from last_applied to desired. To find it, we compute deletions, which are the deletions from
# last_applied to desired, and delta, which is the difference from actual to desired without
# deletions, and then apply delta to deletions as a patch, which should be strictly additive.
def merge(last_applied, desired, actual):
def merge(last_applied, desired, actual, position=None):
deletions = get_deletions(last_applied, desired)
delta = get_delta(last_applied, actual, desired, desired['kind'])
delta = get_delta(last_applied, actual, desired, position or desired['kind'])
return dict_merge(deletions, delta)


Expand Down Expand Up @@ -176,9 +176,8 @@ def list_merge(last_applied, actual, desired, position):
if key not in actual_dict or key not in last_applied_dict:
result.append(desired_dict[key])
else:
deletions = set(last_applied_dict[key].keys()) - set(desired_dict[key].keys())
result.append(dict_merge({k: v for k, v in actual_dict[key].items() if k not in deletions},
desired_dict[key]))
patch = merge(last_applied_dict[key], desired_dict[key], actual_dict[key], position)
result.append(dict_merge(actual_dict[key], patch))
return result
else:
return desired
Expand Down
24 changes: 23 additions & 1 deletion test/unit/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,29 @@
metadata=dict(name="foo"),
spec=dict(ports=[dict(port=8443, name="https")])
),
expected = dict(spec=dict(ports=[dict(port=8443, name="https", protocol='TCP')]))
expected = dict(spec=dict(ports=[dict(madeup=None, port=8443, name="https", protocol='TCP')]))
),
dict(
last_applied = dict(
kind="Pod",
metadata=dict(name="foo"),
spec=dict(containers=[dict(name="busybox", image="busybox",
resources=dict(requests=dict(cpu="100m", memory="100Mi"), limits=dict(cpu="100m", memory="100Mi")))])
),
actual = dict(
kind="Pod",
metadata=dict(name="foo"),
spec=dict(containers=[dict(name="busybox", image="busybox",
resources=dict(requests=dict(cpu="100m", memory="100Mi"), limits=dict(cpu="100m", memory="100Mi")))])
),
desired = dict(
kind="Pod",
metadata=dict(name="foo"),
spec=dict(containers=[dict(name="busybox", image="busybox",
resources=dict(requests=dict(cpu="50m", memory="50Mi"), limits=dict(memory="50Mi")))])
),
expected=dict(spec=dict(containers=[dict(name="busybox", image="busybox",
resources=dict(requests=dict(cpu="50m", memory="50Mi"), limits=dict(cpu=None, memory="50Mi")))]))
),

# This next one is based on a real world case where definition was mostly
Expand Down