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

aws-cli fails to parse kubeconfig #7966

Closed
mvgmb opened this issue Jun 9, 2023 · 4 comments
Closed

aws-cli fails to parse kubeconfig #7966

mvgmb opened this issue Jun 9, 2023 · 4 comments
Labels
bug This issue is a bug. dependencies This issue is a problem in a dependency.

Comments

@mvgmb
Copy link

mvgmb commented Jun 9, 2023

Describe the bug

When running aws-cli with the ruamel-yaml lib (0.17.29+), it fails to parse the kubeconfig file when executing eks update-kubeconfig more than once.

Expected Behavior

Expected to update the kubeconfig with the new cluster configuration.

Current Behavior

$ rm /root/.kube/config
$ aws eks update-kubeconfig --name cluster-a
Updated context cluster-a in /root/.kube/config
$ aws --debug eks update-kubeconfig --name cluster-b
2023-06-09 23:31:26,938 - MainThread - awscli.customizations.eks.update_kubeconfig - WARNING - Passing /root/.kube/config:preferences is wrong type: <class 'dict'> (Should be <class 'collections.OrderedDict'>)
2023-06-09 23:31:26,941 - MainThread - awscli.clidriver - DEBUG - Exception caught in main()
Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/awscli/clidriver.py", line 460, in main
    return command_table[parsed_args.command](remaining, parsed_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/awscli/clidriver.py", line 595, in __call__
    return command_table[parsed_args.operation](remaining, parsed_globals)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/awscli/customizations/commands.py", line 205, in __call__
    rc = self._run_main(parsed_args, parsed_globals)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/awscli/customizations/eks/update_kubeconfig.py", line 134, in _run_main
    config = config_selector.choose_kubeconfig(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/awscli/customizations/eks/update_kubeconfig.py", line 232, in choose_kubeconfig
    return self._loader.load_kubeconfig(self._paths[0])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/awscli/customizations/eks/kubeconfig.py", line 166, in load_kubeconfig
    self._validator.validate_config(loaded_config)
  File "/usr/lib/python3.11/site-packages/awscli/customizations/eks/kubeconfig.py", line 90, in validate_config
    self._validate_config_types(config)
  File "/usr/lib/python3.11/site-packages/awscli/customizations/eks/kubeconfig.py", line 107, in _validate_config_types
    raise KubeconfigCorruptedError(
awscli.customizations.eks.kubeconfig.KubeconfigCorruptedError: preferences is wrong type: <class 'dict'> (Should be <class 'collections.OrderedDict'>)

preferences is wrong type: <class 'dict'> (Should be <class 'collections.OrderedDict'>)

Reproduction Steps

$ docker run -v $(pwd):/root -it alpine:3.18 sh
# apk add aws-cli
# aws eks update-kubeconfig --name cluster-a
Updated context cluster-a in /root/.kube/config
# aws eks update-kubeconfig --name cluster-b
preferences is wrong type: <class 'dict'> (Should be <class 'collections.OrderedDict'>)

Possible Solution

The ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG value used to be a string, but it was changed to a Tag struct: https://sourceforge.net/p/ruamel-yaml/tickets/467/

This broke the YAML parser the aws-cli uses to read the kubeconfig file:

def _ordered_representer(dumper, data):
return dumper.represent_mapping(
ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
data.items())
def ordered_yaml_load(stream):
""" Load an OrderedDict object from a yaml stream."""
yaml = ruamel.yaml.YAML(typ="safe", pure=True)
yaml.Constructor.add_constructor(
ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
_ordered_constructor)
return yaml.load(stream)

This problem can be solved by casting the DEFAULT_MAPPING_TAG to a string, as suggested by this ticket:

- yaml.Constructor.add_constructor(ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, _ordered_constructor)
+ yaml.Constructor.add_constructor(str(ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG), _ordered_constructor) 

Additional Information/Context

No response

CLI version used

aws-cli/2.11.25 Python/3.11.4 Linux/5.4.231-137.341.amzn2.x86_64 source/x86_64.alpine.3 prompt/off

Environment details (OS name and version, etc.)

alpine:3.18

@mvgmb mvgmb added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 9, 2023
@indrora
Copy link

indrora commented Jun 12, 2023

from https://sourceforge.net/p/ruamel-yaml/tickets/467/#de31

it appears that this is fixed in a later version of ruamel

@indrora indrora added dependencies This issue is a problem in a dependency. and removed needs-triage This issue or PR still needs to be triaged. labels Jun 12, 2023
@indrora
Copy link

indrora commented Jun 13, 2023

Currently, we depend on the specific version of ruamel that is specified in the pyproject.toml -- use that version instead of the current one.

@indrora indrora closed this as completed Jun 13, 2023
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

@AvdN
Copy link

AvdN commented Oct 18, 2023

This was not properly solved, it should be in the next release. ( >0.17.35)

If you want you can work around this until then using:

dmt = yaml.Resolver.DEFAULT_MAPPING_TAG
if ruamel.yaml.version_info < (0, 17, 37):
    dmt = str(dmt)
yaml.Constructor.add_constructor(dmt, _ordered_constructor)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. dependencies This issue is a problem in a dependency.
Projects
None yet
Development

No branches or pull requests

3 participants