Skip to content

Commit

Permalink
fix k8s apply
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Jun 11, 2021
1 parent 98cb88a commit 3410a95
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
bugfixes:
- Fix apply for k8s module when an array attribute from definition contains empty dict (https://github.com/ansible-collections/community.kubernetes/pull/129).
59 changes: 59 additions & 0 deletions molecule/default/tasks/apply.yml
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,65 @@
that:
- k8s_secret is not changed

- name: Create network policy (egress array with empty dict)
k8s:
namespace: "{{ apply_namespace }}"
definition:
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: apply-netpolicy
labels:
app: apply-netpolicy
annotations:
{}
spec:
podSelector:
matchLabels:
app: apply-netpolicy
policyTypes:
- Ingress
- Egress
ingress:
- ports:
- port: 9093
protocol: TCP
egress:
- {}

- name: Apply network policy
k8s:
namespace: "{{ apply_namespace }}"
definition:
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: apply-netpolicy
labels:
app: apply-netpolicy
annotations:
{}
spec:
podSelector:
matchLabels:
app: apply-netpolicy
policyTypes:
- Ingress
- Egress
ingress:
- ports:
- port: 9093
protocol: TCP
egress:
- {}
apply: true
register: k8s_networkpolicy

- name: Check that nothing changed
assert:
that:
- k8s_networkpolicy is not changed

always:
- name: Remove namespace
k8s:
Expand Down
33 changes: 2 additions & 31 deletions plugins/module_utils/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from collections import OrderedDict
import json
import sys

from ansible.module_utils.common.dict_transformations import dict_merge
from ansible.module_utils.six import PY3
Expand Down Expand Up @@ -79,34 +78,6 @@
)


if sys.version_info.major >= 3:
json_loads_byteified = json.loads
else:
# https://stackoverflow.com/a/33571117
def json_loads_byteified(json_text):
return _byteify(
json.loads(json_text, object_hook=_byteify),
ignore_dicts=True
)

def _byteify(data, ignore_dicts=False):
# if this is a unicode string, return its string representation
if isinstance(data, unicode): # noqa: F821
return data.encode('utf-8')
# if this is a list of values, return list of byteified values
if isinstance(data, list):
return [_byteify(item, ignore_dicts=True) for item in data]
# if this is a dictionary, return dictionary of byteified keys and values
# but only if we haven't already byteified it
if isinstance(data, dict) and not ignore_dicts:
return {
_byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True)
for key, value in data.items()
}
# if it's anything else, return it in its original form
return data


def annotate(desired):
return dict(
metadata=dict(
Expand All @@ -123,7 +94,7 @@ def apply_patch(actual, desired):
if last_applied:
# ensure that last_applied doesn't come back as a dict of unicode key/value pairs
# json.loads can be used if we stop supporting python 2
last_applied = json_loads_byteified(last_applied)
last_applied = json.loads(last_applied)
patch = merge(dict_merge(last_applied, annotate(last_applied)),
dict_merge(desired, annotate(desired)), actual)
if patch:
Expand Down Expand Up @@ -284,7 +255,7 @@ def get_delta(last_applied, actual, desired, position=None):
elif isinstance(desired_value, list):
p = list_merge(last_applied.get(k, []), actual_value, desired_value, this_position)
if p:
patch[k] = [item for item in p if item]
patch[k] = [item for item in p if item is not None]
elif actual_value != desired_value:
patch[k] = desired_value
return patch

0 comments on commit 3410a95

Please sign in to comment.