Skip to content

Commit

Permalink
Move k8s file merge logic to helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma committed Nov 6, 2017
1 parent 21a1799 commit 11fea79
Showing 1 changed file with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1270,36 +1270,7 @@ def aks_get_credentials(client, resource_group_name, name, admin=False,
access_profile = access_profiles.get('cluster_admin' if admin else 'cluster_user')
encoded_kubeconfig = access_profile.get('kube_config')
kubeconfig = base64.b64decode(encoded_kubeconfig).decode(encoding='UTF-8')

# Special case for printing to stdout
if path == "-":
print(kubeconfig)
return

# ensure that at least an empty ~/.kube/config exists
directory = os.path.dirname(path)
if not os.path.exists(directory):
try:
os.makedirs(directory)
except OSError as ex:
if ex.errno != errno.EEXIST:
raise
if not os.path.exists(path):
with open(path, 'w+t'):
pass

# merge the new kubeconfig into the existing one
fd, temp_path = tempfile.mkstemp()
additional_file = os.fdopen(fd, 'w+t')
try:
additional_file.write(kubeconfig)
additional_file.flush()
merge_kubernetes_configurations(path, temp_path)
except yaml.YAMLError as ex:
logger.warning('Failed to merge credentials to kube config file: %s', ex)
finally:
additional_file.close()
os.remove(temp_path)
_print_or_merge_credentials(path, kubeconfig)


def aks_list(client, resource_group_name=None):
Expand Down Expand Up @@ -1391,6 +1362,41 @@ def _ensure_service_principal(service_principal=None,
return load_acs_service_principal(subscription_id)


def _print_or_merge_credentials(path, kubeconfig):
"""Merge an unencrypted kubeconfig into the file at the specified path, or print it to
stdout if the path is "-".
"""
# Special case for printing to stdout
if path == "-":
print(kubeconfig)
return

# ensure that at least an empty ~/.kube/config exists
directory = os.path.dirname(path)
if not os.path.exists(directory):
try:
os.makedirs(directory)
except OSError as ex:
if ex.errno != errno.EEXIST:
raise
if not os.path.exists(path):
with open(path, 'w+t'):
pass

# merge the new kubeconfig into the existing one
fd, temp_path = tempfile.mkstemp()
additional_file = os.fdopen(fd, 'w+t')
try:
additional_file.write(kubeconfig)
additional_file.flush()
merge_kubernetes_configurations(path, temp_path)
except yaml.YAMLError as ex:
logger.warning('Failed to merge credentials to kube config file: %s', ex)
finally:
additional_file.close()
os.remove(temp_path)


def _remove_nulls(managed_clusters):
"""
Remove some often-empty fields from a list of ManagedClusters, so the JSON representation
Expand Down

0 comments on commit 11fea79

Please sign in to comment.