Skip to content

Commit

Permalink
Improve handling of dict patches in lists (#364)
Browse files Browse the repository at this point in the history
List items should be properly recursively patched, otherwise
changes to nested fields can be ignored.

This does change one of the tests to allow the deeper patching
to work correctly

Co-authored-by: Will Thames <[email protected]>
  • Loading branch information
openshift-cherrypick-robot and willthames authored May 15, 2020
1 parent d257672 commit c0059f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
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

0 comments on commit c0059f4

Please sign in to comment.