mirror of
https://github.com/ansible-collections/kubernetes.core.git
synced 2026-05-07 13:32:37 +00:00
k8s apply - fix list merge for array attribute with empty dict (#131)
Fix k8s apply
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
---
|
||||||
|
bugfixes:
|
||||||
|
- Fix apply for k8s module when an array attribute from definition contains empty dict (https://github.com/ansible-collections/kubernetes.core/issues/113).
|
||||||
@@ -836,6 +836,66 @@
|
|||||||
that:
|
that:
|
||||||
- k8s_secret is not changed
|
- k8s_secret is not changed
|
||||||
|
|
||||||
|
- name: Create network policy (egress array with empty dict)
|
||||||
|
k8s:
|
||||||
|
namespace: "{{ apply_namespace }}"
|
||||||
|
apply: true
|
||||||
|
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:
|
always:
|
||||||
- name: Remove namespace
|
- name: Remove namespace
|
||||||
k8s:
|
k8s:
|
||||||
|
|||||||
@@ -18,10 +18,8 @@ __metaclass__ = type
|
|||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
import json
|
import json
|
||||||
import sys
|
|
||||||
|
|
||||||
from ansible.module_utils.common.dict_transformations import dict_merge
|
from ansible.module_utils.common.dict_transformations import dict_merge
|
||||||
from ansible.module_utils.six import PY3
|
|
||||||
from ansible_collections.kubernetes.core.plugins.module_utils.exceptions import ApplyException
|
from ansible_collections.kubernetes.core.plugins.module_utils.exceptions import ApplyException
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -29,8 +27,6 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if PY3:
|
|
||||||
unicode = str
|
|
||||||
|
|
||||||
LAST_APPLIED_CONFIG_ANNOTATION = 'kubectl.kubernetes.io/last-applied-configuration'
|
LAST_APPLIED_CONFIG_ANNOTATION = 'kubectl.kubernetes.io/last-applied-configuration'
|
||||||
|
|
||||||
@@ -79,34 +75,6 @@ STRATEGIC_MERGE_PATCH_KEYS.update(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
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):
|
def annotate(desired):
|
||||||
return dict(
|
return dict(
|
||||||
metadata=dict(
|
metadata=dict(
|
||||||
@@ -123,7 +91,7 @@ def apply_patch(actual, desired):
|
|||||||
if last_applied:
|
if last_applied:
|
||||||
# ensure that last_applied doesn't come back as a dict of unicode key/value pairs
|
# 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
|
# 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)),
|
patch = merge(dict_merge(last_applied, annotate(last_applied)),
|
||||||
dict_merge(desired, annotate(desired)), actual)
|
dict_merge(desired, annotate(desired)), actual)
|
||||||
if patch:
|
if patch:
|
||||||
@@ -284,7 +252,7 @@ def get_delta(last_applied, actual, desired, position=None):
|
|||||||
elif isinstance(desired_value, list):
|
elif isinstance(desired_value, list):
|
||||||
p = list_merge(last_applied.get(k, []), actual_value, desired_value, this_position)
|
p = list_merge(last_applied.get(k, []), actual_value, desired_value, this_position)
|
||||||
if p:
|
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:
|
elif actual_value != desired_value:
|
||||||
patch[k] = desired_value
|
patch[k] = desired_value
|
||||||
return patch
|
return patch
|
||||||
|
|||||||
Reference in New Issue
Block a user