From 15d46ce4284b2759a80523c4896c8e7d70b4d25d Mon Sep 17 00:00:00 2001 From: Pilou Date: Fri, 28 Sep 2018 00:43:29 +0200 Subject: [PATCH] k8s/oc plugins: fix 'ResourceField' is not JSON serializable (#46145) Fixes #44408 --- lib/ansible/plugins/inventory/k8s.py | 13 +++++++------ lib/ansible/plugins/inventory/openshift.py | 8 +++++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/ansible/plugins/inventory/k8s.py b/lib/ansible/plugins/inventory/k8s.py index 6012a120521..c568387c424 100644 --- a/lib/ansible/plugins/inventory/k8s.py +++ b/lib/ansible/plugins/inventory/k8s.py @@ -223,17 +223,18 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable, K8sAnsibleM for pod in obj.items: pod_name = pod.metadata.name pod_groups = [] - pod_labels = {} if not pod.metadata.labels else pod.metadata.labels - pod_annotations = {} if not pod.metadata.annotations else pod.metadata.annotations + pod_annotations = {} if not pod.metadata.annotations else dict(pod.metadata.annotations) if pod.metadata.labels: - pod_labels = pod.metadata.labels # create a group for each label_value for key, value in pod.metadata.labels: group_name = 'label_{0}_{1}'.format(key, value) if group_name not in pod_groups: pod_groups.append(group_name) self.inventory.add_group(group_name) + pod_labels = dict(pod.metadata.labels) + else: + pod_labels = {} for container in pod.status.containerStatuses: # add each pod_container to the namespace group, and to each label_value group @@ -294,8 +295,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable, K8sAnsibleM for service in obj.items: service_name = service.metadata.name - service_labels = {} if not service.metadata.labels else service.metadata.labels - service_annotations = {} if not service.metadata.annotations else service.metadata.annotations + service_labels = {} if not service.metadata.labels else dict(service.metadata.labels) + service_annotations = {} if not service.metadata.annotations else dict(service.metadata.annotations) self.inventory.add_host(service_name) @@ -344,7 +345,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable, K8sAnsibleM self.inventory.set_variable(service_name, 'load_balancer_ip', service.spec.loadBalancerIP) if service.spec.selector: - self.inventory.set_variable(service_name, 'selector', service.spec.selector) + self.inventory.set_variable(service_name, 'selector', dict(service.spec.selector)) if hasattr(service.status.loadBalancer, 'ingress') and service.status.loadBalancer.ingress: load_balancer = [{'hostname': ingress.hostname, diff --git a/lib/ansible/plugins/inventory/openshift.py b/lib/ansible/plugins/inventory/openshift.py index d9e7602ce20..64d3b7f498c 100644 --- a/lib/ansible/plugins/inventory/openshift.py +++ b/lib/ansible/plugins/inventory/openshift.py @@ -159,8 +159,7 @@ class InventoryModule(K8sInventoryModule): self.inventory.add_child(namespace_group, namespace_routes_group) for route in obj.items: route_name = route.metadata.name - route_labels = {} if not route.metadata.labels else route.metadata.labels - route_annotations = {} if not route.metadata.annotations else route.metadata.annotations + route_annotations = {} if not route.metadata.annotations else dict(route.metadata.annotations) self.inventory.add_host(route_name) @@ -170,6 +169,9 @@ class InventoryModule(K8sInventoryModule): group_name = 'label_{0}_{1}'.format(key, value) self.inventory.add_group(group_name) self.inventory.add_child(group_name, route_name) + route_labels = dict(route.metadata.labels) + else: + route_labels = {} self.inventory.add_child(namespace_routes_group, route_name) @@ -189,4 +191,4 @@ class InventoryModule(K8sInventoryModule): self.inventory.set_variable(route_name, 'path', route.spec.path) if hasattr(route.spec.port, 'targetPort') and route.spec.port.targetPort: - self.inventory.set_variable(route_name, 'port', route.spec.port) + self.inventory.set_variable(route_name, 'port', dict(route.spec.port))