Skip to content

Commit f5a3ace

Browse files
authored
[connectedk8s] Add check for AKS cluster (Azure#2464)
1 parent b17f994 commit f5a3ace

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/connectedk8s/azext_connectedk8s/_constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,7 @@
4141
Get_Kubernetes_Namespace_Fault_Type = 'kubernetes-get-namespace-error'
4242
Update_Agent_Success = 'Agents for Connected Cluster {} have been updated successfully'
4343
Update_Agent_Failure = 'Error while updating agents. Please run \"kubectl get pods -n azure-arc\" to check the pods in case of timeout error. Error: {}'
44+
Cluster_Info_Not_Found_Type = 'Error while finding current cluster server details'
45+
Kubeconfig_Failed_To_Load_Fault_Type = "failed-to-load-kubeconfig-file"
4446
Proxy_Cert_Path_Does_Not_Exist_Fault_Type = 'proxy-cert-path-does-not-exist-error'
4547
Proxy_Cert_Path_Does_Not_Exist_Error = 'Proxy cert path {} does not exist. Please check the path provided'

src/connectedk8s/azext_connectedk8s/custom.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ def create_connectedk8s(cmd, client, resource_group_name, cluster_name, https_pr
115115
}
116116
telemetry.add_extension_event('connectedk8s', kubernetes_properties)
117117

118+
# Checking if it is an AKS cluster
119+
is_aks_cluster = check_aks_cluster(kube_config, kube_context)
120+
if is_aks_cluster:
121+
logger.warning("The cluster you are trying to connect to Azure Arc is an Azure Kubernetes Service (AKS) cluster. While Arc onboarding an AKS cluster is possible, it's not necessary. Learn more at {}.".format(" https://go.microsoft.com/fwlink/?linkid=2144200"))
122+
118123
# Checking helm installation
119124
check_helm_install(kube_config, kube_context)
120125

@@ -172,7 +177,7 @@ def create_connectedk8s(cmd, client, resource_group_name, cluster_name, https_pr
172177
summary='Connected cluster resource already exists')
173178
raise CLIError("The connected cluster resource {} already exists ".format(cluster_name) +
174179
"in the resource group {} ".format(resource_group_name) +
175-
"and corresponds to a different Kubernetes cluster. To onboard this Kubernetes cluster" +
180+
"and corresponds to a different Kubernetes cluster. To onboard this Kubernetes cluster " +
176181
"to Azure, specify different resource name or resource group name.")
177182

178183
# Resource group Creation
@@ -429,6 +434,54 @@ def generate_request_payload(configuration, location, public_key, tags):
429434
return cc
430435

431436

437+
def get_kubeconfig_node_dict(kube_config=None):
438+
if kube_config is None:
439+
kube_config = os.getenv('KUBECONFIG') if os.getenv('KUBECONFIG') else os.path.join(os.path.expanduser('~'), '.kube', 'config')
440+
try:
441+
kubeconfig_data = config.kube_config._get_kube_config_loader_for_yaml_file(kube_config)._config
442+
except Exception as ex:
443+
telemetry.set_user_fault()
444+
telemetry.set_exception(exception=ex, fault_type=consts.Load_Kubeconfig_Fault_Type,
445+
summary='Error while fetching details from kubeconfig')
446+
raise CLIError("Error while fetching details from kubeconfig." + str(ex))
447+
return kubeconfig_data
448+
449+
450+
def check_aks_cluster(kube_config, kube_context):
451+
config_data = get_kubeconfig_node_dict(kube_config=kube_config)
452+
try:
453+
all_contexts, current_context = config.list_kube_config_contexts(config_file=kube_config)
454+
except Exception as e: # pylint: disable=broad-except
455+
logger.warning("Exception while trying to list kube contexts: %s\n", e)
456+
457+
if kube_context is None:
458+
# Get name of the cluster from current context as kube_context is none.
459+
cluster_name = current_context.get('context').get('cluster')
460+
if cluster_name is None:
461+
logger.warning("Cluster not found in currentcontext: " + str(current_context))
462+
else:
463+
cluster_found = False
464+
for context in all_contexts:
465+
if context.get('name') == kube_context:
466+
cluster_found = True
467+
cluster_name = context.get('context').get('cluster')
468+
break
469+
if not cluster_found or cluster_name is None:
470+
logger.warning("Cluster not found in kubecontext: " + str(kube_context))
471+
472+
clusters = config_data.safe_get('clusters')
473+
server_address = ""
474+
for cluster in clusters:
475+
if cluster.safe_get('name') == cluster_name:
476+
server_address = cluster.safe_get('cluster').get('server')
477+
break
478+
479+
if server_address.find(".azmk8s.io:") == -1:
480+
return False
481+
else:
482+
return True
483+
484+
432485
def get_connectedk8s(cmd, client, resource_group_name, cluster_name):
433486
return client.get(resource_group_name, cluster_name)
434487

0 commit comments

Comments
 (0)