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

Conversation

pilou-
Copy link
Contributor

@pilou- pilou- commented Sep 26, 2018

SUMMARY

Note that this fix requires #45826 (first three commits come from #45826, last commit contains the fix) #45826 should be merged first.

Fix TypeError: Object of type 'ResourceField' is not JSON serializable

Test command: ANSIBLE_INVENTORY_ENABLED=openshift ansible-inventory -i test.yml --list -vvv

$ cat test.yml
plugin: openshift
connections: []

Exception was:

  • Python 3.6:

    ansible-inventory 2.8.0.dev0 (devel 05e3b5d632) last updated 2018/09/26 01:43:44 (GMT +200)
     config file = None
     configured module search path = ['$HOME/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = $HOME/ansible/lib/ansible
      executable location = $HOME/ansible/bin/ansible-inventory
      python version = 3.6.6+ (default, Sep  1 2018, 01:05:25) [GCC 8.2.0]
    No config file found; using defaults
    Parsed ansible/test.yml inventory source with openshift plugin
    ERROR! Unexpected Exception, this is probably a bug: Object of type 'ResourceField' is not JSON serializable
    the full traceback was:
    
    Traceback (most recent call last):
      File "ansible/bin/ansible-inventory", line 118, in <module>
        exit_code = cli.run()
      File "ansible/lib/ansible/cli/inventory.py", line 181, in run
        results = self.dump(results)
      File "ansible/lib/ansible/cli/inventory.py", line 199, in dump
        results = json.dumps(stuff, cls=AnsibleJSONEncoder, sort_keys=True, indent=4)
      File "/usr/lib/python3.6/json/__init__.py", line 238, in dumps
        **kw).encode(obj)
      File "/usr/lib/python3.6/json/encoder.py", line 201, in encode
        chunks = list(chunks)
      File "/usr/lib/python3.6/json/encoder.py", line 430, in _iterencode
        yield from _iterencode_dict(o, _current_indent_level)
      File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
        yield from chunks
      File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
        yield from chunks
      File "/usr/lib/python3.6/json/encoder.py", line 404, in _iterencode_dict
        yield from chunks
      File "/usr/lib/python3.6/json/encoder.py", line 437, in _iterencode
        o = _default(o)
      File "ansible/lib/ansible/parsing/ajson.py", line 66, in default
        value = super(AnsibleJSONEncoder, self).default(o)
      File "/usr/lib/python3.6/json/encoder.py", line 180, in default
        o.__class__.__name__)
    TypeError: Object of type 'ResourceField' is not JSON serializable
    
  • Python 2.7

    ansible-inventory 2.8.0.dev0 (devel 05e3b5d632) last updated 2018/09/26 01:43:44 (GMT +200)
      config file = None
      configured module search path = [u'$HOME/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
      ansible python module location = $HOME/ansible/lib/ansible
      executable location = $HOME/ansible/bin/ansible-inventory
      python version = 2.7.14 (default, Sep 17 2017, 18:50:44) [GCC 7.2.0]
    No config file found; using defaults
    Parsed $HOME/ansible/test.yml inventory source with openshift plugin
    ERROR! Unexpected Exception, this is probably a bug: {u'openshift.io/build.name': u'ansiblemaint-apb-1',
     u'openshift.io/scc': u'privileged'} is not JSON serializable
    the full traceback was:
    
    Traceback (most recent call last):
      File "ansible/bin/ansible-inventory", line 118, in <module>
        exit_code = cli.run()
      File "ansible/lib/ansible/cli/inventory.py", line 181, in run
        results = self.dump(results)
      File "ansible/lib/ansible/cli/inventory.py", line 199, in dump
        results = json.dumps(stuff, cls=AnsibleJSONEncoder, sort_keys=True, indent=4)
      File "/usr/lib/python2.7/json/__init__.py", line 251, in dumps
        sort_keys=sort_keys, **kw).encode(obj)
      File "/usr/lib/python2.7/json/encoder.py", line 209, in encode
        chunks = list(chunks)
      File "/usr/lib/python2.7/json/encoder.py", line 434, in _iterencode
        for chunk in _iterencode_dict(o, _current_indent_level):
      File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
        for chunk in chunks:
      File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
        for chunk in chunks:
      File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
        for chunk in chunks:
      File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
        for chunk in chunks:
      File "/usr/lib/python2.7/json/encoder.py", line 442, in _iterencode
        o = _default(o)
      File "ansible/lib/ansible/parsing/ajson.py", line 66, in default
        value = super(AnsibleJSONEncoder, self).default(o)
      File "/usr/lib/python2.7/json/encoder.py", line 184, in default
        raise TypeError(repr(o) + " is not JSON serializable")
    TypeError: {u'openshift.io/build.name': u'ansiblemaint-apb-1',
     u'openshift.io/scc': u'privileged'} is not JSON serializable
    

Fixes #44408

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

openshift and k8s inventory plugins

ANSIBLE VERSION
2.8

@ansibot
Copy link
Contributor

ansibot commented Sep 26, 2018

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. python3 support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback. labels Sep 26, 2018

for service in obj.items:
service_name = service.metadata.name
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

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label Sep 26, 2018
@fabianvf
Copy link
Contributor

@pilou- #45826 should go in shortly, would you mind rebasing and then I'll do a more thorough review? Had one comment so far but on cursory inspection lgtm. Thanks for the contribution!

@pilou- pilou- force-pushed the oc_ResourceField_is_not_JSON_serializable branch from 1544523 to 7f8a1e6 Compare September 27, 2018 00:16
@pilou-
Copy link
Contributor Author

pilou- commented Sep 27, 2018

Rebased

@ansibot ansibot added the inventory Inventory category label Sep 27, 2018
@fabianvf
Copy link
Contributor

shipit

@fabianvf
Copy link
Contributor

bot_status

@ansibot
Copy link
Contributor

ansibot commented Sep 27, 2018

Components

lib/ansible/plugins/inventory/k8s.py
support: community
maintainers: chouseknecht fabianvf flaper87 maxamillion willthames

lib/ansible/plugins/inventory/openshift.py
support: community
maintainers: chouseknecht fabianvf flaper87 maxamillion willthames

Metadata

waiting_on: maintainer
changes_requested_by: null
needs_info: False
needs_revision: False
needs_rebase: False
merge_commits: []
too many files or commits: False
mergeable_state: clean
shippable_status: success
maintainer_shipits (module maintainers): 1
community_shipits (namespace maintainers): 0
ansible_shipits (core team members): 0
shipit_actors (maintainers or core team members): fabianvf
shipit_actors_other: []
automerge: automerge shipit test failed

click here for bot help

@maxamillion
Copy link
Contributor

rebuild_merge

@ansibot ansibot merged commit 15d46ce into ansible:devel Sep 27, 2018
AlanCoding added a commit to AlanCoding/ansible that referenced this pull request Nov 30, 2018
abadger pushed a commit that referenced this pull request Dec 4, 2018
@dagwieers dagwieers added the k8s label Feb 8, 2019
@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. inventory Inventory category k8s python3 support:community This issue/PR relates to code supported by the Ansible community. traceback This issue/PR includes a traceback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

openshift inventory plugin, 'ascii' codec can't decode byte
5 participants