Skip to content

Commit 5c049ec

Browse files
authored
Fix UnboundLocalError generating a diff (#390)
This happens when two resources of different kind are compared or if 'kind' is missing in the dict. Traceback (most recent call last): File "/tmp/ansible_k8s_payload_trnxlugx/ansible_k8s_payload.zip/ansible_collections/community/kubernetes/plugins/modules/k8s.py", line 320, in <module> File "/tmp/ansible_k8s_payload_trnxlugx/ansible_k8s_payload.zip/ansible_collections/community/kubernetes/plugins/modules/k8s.py", line 316, in main File "/tmp/ansible_k8s_payload_trnxlugx/ansible_k8s_payload.zip/ansible_collections/community/kubernetes/plugins/module_utils/common.py", line 548, in execute_module File "/tmp/ansible_k8s_payload_trnxlugx/ansible_k8s_payload.zip/ansible_collections/community/kubernetes/plugins/module_utils/common.py", line 760, in perform_action File "/tmp/ansible_k8s_payload_trnxlugx/ansible_k8s_payload.zip/ansible_collections/community/kubernetes/plugins/module_utils/common.py", line 347, in diff_objects File "/home/linosteiner/.local/lib/python3.9/site-packages/openshift/dynamic/apply.py", line 232, in recursive_diff result = recursive_list_diff(dict1[k], dict2[k], this_position) UnboundLocalError: local variable 'this_position' referenced before assignment
1 parent 32737c0 commit 5c049ec

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

openshift/dynamic/apply.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ def recursive_diff(dict1, dict2, position=None):
223223
left = dict((k, v) for (k, v) in dict1.items() if k not in dict2)
224224
right = dict((k, v) for (k, v) in dict2.items() if k not in dict1)
225225
for k in (set(dict1.keys()) & set(dict2.keys())):
226-
if position:
227-
this_position = "%s.%s" % (position, k)
226+
this_position = "%s.%s" % (position, k) if position is not None else None
228227
if isinstance(dict1[k], dict) and isinstance(dict2[k], dict):
229228
result = recursive_diff(dict1[k], dict2[k], this_position)
230229
if result:

test/unit/test_diff.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,51 @@
6161
expected=(dict(spec=dict(containers=[dict(name="busybox", env=[dict(name="another", value="next"), dict(name="hello", value="world")])])),
6262
dict(spec=dict(containers=[dict(name="busybox", env=[dict(name="hello", value="everyone")])])))
6363
),
64+
65+
dict(
66+
before = dict(
67+
kind="Pod",
68+
metadata=dict(name="foo"),
69+
spec=dict(containers=[dict(name="busybox", image="busybox")])
70+
),
71+
after = dict(
72+
kind="Service",
73+
metadata=dict(name="foo"),
74+
spec=dict(ports=[dict(port=8081, name="http")])
75+
),
76+
expected=(dict(kind='Pod', spec=dict(containers=[dict(image='busybox', name='busybox')])),
77+
dict(kind='Service', spec=dict(ports=[dict(name='http', port=8081)])))
78+
),
79+
80+
dict(
81+
before = dict(
82+
kind="Pod",
83+
metadata=dict(name="foo"),
84+
spec=dict(containers=[dict(name="busybox", image="busybox")])
85+
),
86+
after = dict(
87+
# kind="...",
88+
metadata=dict(name="foo"),
89+
spec=dict(ports=[dict(port=8081, name="http")])
90+
),
91+
expected=(dict(kind='Pod', spec=dict(containers=[dict(image='busybox', name='busybox')])),
92+
dict(spec=dict(ports=[dict(name='http', port=8081)])))
93+
),
94+
95+
dict(
96+
before = dict(
97+
# kind="...",
98+
metadata=dict(name="foo"),
99+
spec=dict(containers=[dict(name="busybox", image="busybox")])
100+
),
101+
after = dict(
102+
kind="Service",
103+
metadata=dict(name="foo"),
104+
spec=dict(ports=[dict(port=8081, name="http")])
105+
),
106+
expected=(dict(spec=dict(containers=[dict(image='busybox', name='busybox')])),
107+
dict(kind='Service', spec=dict(ports=[dict(name='http', port=8081)])))
108+
),
64109
]
65110

66111

0 commit comments

Comments
 (0)