k8s/oc plugins: fix 'ResourceField' is not JSON serializable (#46145)

Fixes #44408

(cherry picked from commit 15d46ce428)
This commit is contained in:
AlanCoding 2018-11-29 08:18:53 -05:00 committed by Toshio Kuratomi
parent 6ba1330191
commit aa1b00f7e9

View file

@ -114,17 +114,18 @@ class K8sInventoryHelper(K8sAnsibleMixin):
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
@ -184,8 +185,8 @@ class K8sInventoryHelper(K8sAnsibleMixin):
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)
@ -234,7 +235,7 @@ class K8sInventoryHelper(K8sAnsibleMixin):
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,
@ -283,8 +284,7 @@ class OpenShiftInventoryHelper(K8sInventoryHelper):
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)
@ -294,6 +294,9 @@ class OpenShiftInventoryHelper(K8sInventoryHelper):
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)
@ -313,4 +316,4 @@ class OpenShiftInventoryHelper(K8sInventoryHelper):
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))