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

Created on 11 Oct 2019  路  7Comments  路  Source: Azure/azure-cli

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:

~yaml
apiVersion: v1
clusters: []
kind: Config
~

Then run:

~bash
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.

AKS Service Attention customer-reported

Most helpful comment

Also need update one this.

All 7 comments

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

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

We're looking into this issue - @palma21 fyi

Any update on this issue?

@maggiepint Any update on the issue?

Also need update one this.

Is there anything we can do to mitigate?

Was this page helpful?
0 / 5 - 0 ratings