Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenShift and k8s inventory plugins: fix "resource field is not json serializable" #46145

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions lib/ansible/plugins/inventory/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,18 @@ def get_pods_for_namespace(self, client, name, namespace):
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
Expand Down Expand Up @@ -294,8 +295,8 @@ def get_services_for_namespace(self, client, name, namespace):

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service.metadata.labels and service.metadata.annotations should have a .to_dict() method defined, I think that will have a better guarantee of correct behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service.metadata.labels and service.metadata.annotations are ResourceField instances, ResourceField class doesn't have a to_dict method - but ResourceInstance has, should service.metadata.labels and service.metadata.annotations be ResourceInstance instances ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, I'm just wrong, my bad

service_annotations = {} if not service.metadata.annotations else dict(service.metadata.annotations)

self.inventory.add_host(service_name)

Expand Down Expand Up @@ -344,7 +345,7 @@ def get_services_for_namespace(self, client, name, namespace):
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,
Expand Down
8 changes: 5 additions & 3 deletions lib/ansible/plugins/inventory/openshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ def get_routes_for_namespace(self, client, name, namespace):
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)

Expand All @@ -170,6 +169,9 @@ def get_routes_for_namespace(self, client, name, namespace):
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)

Expand All @@ -189,4 +191,4 @@ def get_routes_for_namespace(self, client, name, namespace):
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))