Skip to content

Commit

Permalink
[AKS] rename admin k8s context so it won't overwite user context (#6529)
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma authored and troydai committed Jun 12, 2018
1 parent 4dcffee commit cba0d16
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/command_modules/azure-cli-acs/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Release History
2.0.35
++++++
* Updated options of `az aks use-dev-spaces` command. Added `--update` support.
* `az aks get-credentials --admin` won't replace the user context in $HOME/.kube/config

2.0.34
++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,16 @@ def merge_kubernetes_configurations(existing_file, addition_file):
existing = load_kubernetes_configuration(existing_file)
addition = load_kubernetes_configuration(addition_file)

# rename the admin context so it doesn't overwrite the user context
for ctx in addition.get('contexts', []):
try:
if ctx['context']['user'].startswith('clusterAdmin'):
admin_name = ctx['name'] + '-admin'
addition['current-context'] = ctx['name'] = admin_name
break
except (KeyError, TypeError):
continue

if addition is None:
raise CLIError('failed to load additional configuration from {}'.format(addition_file))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,107 @@ def test_merge_credentials(self):
self.assertEqual(merged['users'], ['user1', 'user2'])
self.assertEqual(merged['current-context'], obj2['current-context'])

def test_merge_admin_credentials(self):
existing = tempfile.NamedTemporaryFile(delete=False)
existing.close()
addition = tempfile.NamedTemporaryFile(delete=False)
addition.close()
obj1 = {
'apiVersion': 'v1',
'clusters': [
{
'cluster': {
'certificate-authority-data': 'certificateauthoritydata1',
'server': 'https://aztest-aztest-abc123-abcd1234.hcp.eastus.azmk8s.io:443'
},
'name': 'aztest'
}
],
'contexts': [
{
'context': {
'cluster': 'aztest',
'user': 'clusterUser_aztest_aztest'
},
'name': 'aztest'
}
],
'current-context': 'aztest',
'kind': 'Config',
'preferences': {},
'users': [
{
'name': 'clusterUser_aztest_aztest',
'user': {
'client-certificate-data': 'clientcertificatedata1',
'client-key-data': 'clientkeydata1',
'token': 'token1'
}
}
]
}
with open(existing.name, 'w+') as stream:
yaml.dump(obj1, stream)
self.addCleanup(os.remove, existing.name)
obj2 = {
'apiVersion': 'v1',
'clusters': [
{
'cluster': {
'certificate-authority-data': 'certificateauthoritydata2',
'server': 'https://aztest-aztest-abc123-abcd1234.hcp.eastus.azmk8s.io:443'
},
'name': 'aztest'
}
],
'contexts': [
{
'context': {
'cluster': 'aztest',
'user': 'clusterAdmin_aztest_aztest'
},
'name': 'aztest'
}
],
'current-context': 'aztest',
'kind': 'Config',
'preferences': {},
'users': [
{
'name': 'clusterAdmin_aztest_aztest',
'user': {
'client-certificate-data': 'someclientcertificatedata2',
'client-key-data': 'someclientkeydata2',
'token': 'token2'
}
}
]
}
with open(addition.name, 'w+') as stream:
yaml.dump(obj2, stream)
self.addCleanup(os.remove, addition.name)

merge_kubernetes_configurations(existing.name, addition.name)

with open(existing.name, 'r') as stream:
merged = yaml.load(stream)
self.assertEqual(len(merged['clusters']), 2)
self.assertEqual([c['cluster'] for c in merged['clusters']],
[{'certificate-authority-data': 'certificateauthoritydata1',
'server': 'https://aztest-aztest-abc123-abcd1234.hcp.eastus.azmk8s.io:443'},
{'certificate-authority-data': 'certificateauthoritydata2',
'server': 'https://aztest-aztest-abc123-abcd1234.hcp.eastus.azmk8s.io:443'}])
self.assertEqual(len(merged['contexts']), 2)
self.assertEqual(merged['contexts'],
[{'context': {'cluster': 'aztest', 'user': 'clusterUser_aztest_aztest'},
'name': 'aztest'},
{'context': {'cluster': 'aztest', 'user': 'clusterAdmin_aztest_aztest'},
'name': 'aztest-admin'}])
self.assertEqual(len(merged['users']), 2)
self.assertEqual([u['name'] for u in merged['users']],
['clusterUser_aztest_aztest', 'clusterAdmin_aztest_aztest'])
self.assertEqual(merged['current-context'], 'aztest-admin')

def test_merge_credentials_missing(self):
existing = tempfile.NamedTemporaryFile(delete=False)
existing.close()
Expand Down

0 comments on commit cba0d16

Please sign in to comment.