Skip to content

Commit 4814712

Browse files
authored
Improve handling of dict patches in lists (#362)
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
1 parent f947ca9 commit 4814712

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

openshift/dynamic/apply.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ def apply(resource, definition):
129129
# from last_applied to desired. To find it, we compute deletions, which are the deletions from
130130
# last_applied to desired, and delta, which is the difference from actual to desired without
131131
# deletions, and then apply delta to deletions as a patch, which should be strictly additive.
132-
def merge(last_applied, desired, actual):
132+
def merge(last_applied, desired, actual, position=None):
133133
deletions = get_deletions(last_applied, desired)
134-
delta = get_delta(last_applied, actual, desired, desired['kind'])
134+
delta = get_delta(last_applied, actual, desired, position or desired['kind'])
135135
return dict_merge(deletions, delta)
136136

137137

@@ -176,9 +176,8 @@ def list_merge(last_applied, actual, desired, position):
176176
if key not in actual_dict or key not in last_applied_dict:
177177
result.append(desired_dict[key])
178178
else:
179-
deletions = set(last_applied_dict[key].keys()) - set(desired_dict[key].keys())
180-
result.append(dict_merge({k: v for k, v in actual_dict[key].items() if k not in deletions},
181-
desired_dict[key]))
179+
patch = merge(last_applied_dict[key], desired_dict[key], actual_dict[key], position)
180+
result.append(dict_merge(actual_dict[key], patch))
182181
return result
183182
else:
184183
return desired

test/unit/test_apply.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,29 @@
141141
metadata=dict(name="foo"),
142142
spec=dict(ports=[dict(port=8443, name="https")])
143143
),
144-
expected = dict(spec=dict(ports=[dict(port=8443, name="https", protocol='TCP')]))
144+
expected = dict(spec=dict(ports=[dict(madeup=None, port=8443, name="https", protocol='TCP')]))
145+
),
146+
dict(
147+
last_applied = dict(
148+
kind="Pod",
149+
metadata=dict(name="foo"),
150+
spec=dict(containers=[dict(name="busybox", image="busybox",
151+
resources=dict(requests=dict(cpu="100m", memory="100Mi"), limits=dict(cpu="100m", memory="100Mi")))])
152+
),
153+
actual = dict(
154+
kind="Pod",
155+
metadata=dict(name="foo"),
156+
spec=dict(containers=[dict(name="busybox", image="busybox",
157+
resources=dict(requests=dict(cpu="100m", memory="100Mi"), limits=dict(cpu="100m", memory="100Mi")))])
158+
),
159+
desired = dict(
160+
kind="Pod",
161+
metadata=dict(name="foo"),
162+
spec=dict(containers=[dict(name="busybox", image="busybox",
163+
resources=dict(requests=dict(cpu="50m", memory="50Mi"), limits=dict(memory="50Mi")))])
164+
),
165+
expected=dict(spec=dict(containers=[dict(name="busybox", image="busybox",
166+
resources=dict(requests=dict(cpu="50m", memory="50Mi"), limits=dict(cpu=None, memory="50Mi")))]))
145167
),
146168

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

0 commit comments

Comments
 (0)