azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

`az aks get-credentials` throws exception if ~/.kube/config does not contain the expected sections

Open FooBarWidget opened this issue 7 years ago • 15 comments

Describe the bug az aks get-credentials expects ~/.kube/config to contain a clusters, users and contexts section. If even one of them is missing, then the command will bail out with a Python KeyError exception.

To Reproduce Create ~/.kube/config with the following contents:

apiVersion: v1
clusters: []
kind: Config

Then run:

az aks get-credentials --resource-group SOMETHING --name SOMETHING --overwrite-existing

Actual behavior

The command failed with an unexpected error. Here is the traceback:

'users'
Traceback (most recent call last):
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/knack/cli.py", line 206, in invoke
    cmd_result = self.invocation.execute(args)
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/azure/cli/core/commands/__init__.py", line 603, in execute
    raise ex
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/azure/cli/core/commands/__init__.py", line 661, in _run_jobs_serially
    results.append(self._run_job(expanded_arg, cmd_copy))
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/azure/cli/core/commands/__init__.py", line 654, in _run_job
    six.reraise(*sys.exc_info())
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/six.py", line 693, in reraise
    raise value
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/azure/cli/core/commands/__init__.py", line 631, in _run_job
    result = cmd_copy(params)
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/azure/cli/core/commands/__init__.py", line 306, in __call__
    return self.handler(*args, **kwargs)
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/azure/cli/core/__init__.py", line 485, in default_command_handler
    return op(**command_args)
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/azure/cli/command_modules/acs/custom.py", line 1887, in aks_get_credentials
    _print_or_merge_credentials(path, kubeconfig, overwrite_existing, context_name)
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/azure/cli/command_modules/acs/custom.py", line 2775, in _print_or_merge_credentials
    merge_kubernetes_configurations(path, temp_path, overwrite_existing, context_name)
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/azure/cli/command_modules/acs/custom.py", line 1178, in merge_kubernetes_configurations
    _handle_merge(existing, addition, 'users', replace)
  File "/usr/local/Cellar/azure-cli/2.0.74/libexec/lib/python3.7/site-packages/azure/cli/command_modules/acs/custom.py", line 1112, in _handle_merge
    if existing[key] is None:
KeyError: 'users'

To open an issue, please run: 'az feedback'

Expected behavior I expect the command to succeed, like this:

Merged "SOMETHING" as current context in ~/.kube/config

Environment summary I was able to reproduce this problem on two different environments.

Environment 1:

  • Install method: Homebrew
  • CLI version: 2.0.74
  • OS: macOS Mojave
  • Shell: bash

Environment 2:

  • Install method: APT (via packages.microsoft.com)
  • CLI version: 2.0.74
  • OS: Ubuntu 16.04 (running inside a Docker container)

Additional context The problematic line is here: https://github.com/Azure/azure-cli/blob/0d4a040f56d481afa6b3d8cd1319b51bde9dd69a/src/azure-cli/azure/cli/command_modules/acs/custom.py#L1112

The code there attempts to check whether the config section exists, but does so in a wrong way. In Python, foo[key] raises an exception if the key doesn't exist, so foo[key] is None makes no sense here. You want foo.get(key) is None which does not raise but returns None if the key does not exist.

FooBarWidget avatar Oct 11 '19 08:10 FooBarWidget

Thanks for the feedback. I have routed to the appropriate team for follow up.

maggiepint avatar Oct 12 '19 05:10 maggiepint

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Azure/aks-pm

ghost avatar Dec 04 '19 07:12 ghost

We're looking into this issue - @palma21 fyi

jluk avatar Dec 05 '19 00:12 jluk

Any update on this issue?

deepakk11 avatar Jul 28 '20 12:07 deepakk11

@maggiepint Any update on the issue?

yankeexe avatar Jul 31 '20 03:07 yankeexe

Also need update one this.

maperr avatar Aug 25 '20 19:08 maperr

Is there anything we can do to mitigate?

chriskuech avatar Dec 14 '20 21:12 chriskuech

Its gonna be 2 years now, no updates?

fgbaezp avatar Aug 10 '21 18:08 fgbaezp

ask service team should take a look

yonzhan avatar Aug 10 '21 22:08 yonzhan

@phealy Could you please let us know if you have any update on this issue ?

navba-MSFT avatar Feb 02 '22 11:02 navba-MSFT

@FooBarWidget Apologies for the late reply. We will get back to you once we have an update.

@phealy Please let us know if you had a chance to look into this issue and provide the details on this github thread

navba-MSFT avatar Feb 04 '22 05:02 navba-MSFT

I don't know how to get this prioritized, it looks like people have already been complaining for ages...

amul047 avatar Mar 26 '22 05:03 amul047

@amul047 Until a solution is provided, Could you please follow the workaround mentioned here and here and check if that helps ?

In the meantime, I am tagging the AKS Service PM here to check if they can prioritize this.

@Azure/aks-pm and @phealy Could you please look into this on priority and provide an update on this ? Awaiting your reply.

navba-MSFT avatar Mar 26 '22 05:03 navba-MSFT

@amul047 Could you please let me know if you have tried to workaround mentioned here and here ? Awaiting your reply.

navba-MSFT avatar May 05 '22 08:05 navba-MSFT

Hi, we're sending this friendly reminder because we haven't heard back from you in a while. We need more information about this issue to help address it. Please be sure to give us your input within the next 7 days. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!

ghost avatar May 12 '22 14:05 ghost