Skip to content

Commit

Permalink
Move connectedk8s CLI to track2 python SDK models (#3711)
Browse files Browse the repository at this point in the history
  • Loading branch information
akashkeshari authored Aug 2, 2021
1 parent 4dd9bb5 commit 08e829c
Show file tree
Hide file tree
Showing 106 changed files with 6,388 additions and 5,889 deletions.
8 changes: 8 additions & 0 deletions src/connectedk8s/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
Release History
===============

1.1.6
++++++
* Moved to track2 SDK
* `az connectedk8s connect`: Added onboarding timeout parameter
* `az connectedk8s upgrade`: Added upgrade timeout parameter
* Release namespace detection bug fix in multiple commands


1.1.5
++++++
* Add custom-locations oid parameter for spn scenario
Expand Down
4 changes: 4 additions & 0 deletions src/connectedk8s/azext_connectedk8s/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
text: az connectedk8s connect -g resourceGroupName -n connectedClusterName --proxy-https https://proxy-url --proxy-http http://proxy-url --proxy-skip-range excludedIP,excludedCIDR,exampleCIDRfollowed,10.0.0.0/24
- name: Onboard a connected kubernetes cluster by specifying the https proxy, http proxy, no proxy with cert settings.
text: az connectedk8s connect -g resourceGroupName -n connectedClusterName --proxy-cert /path/to/crt --proxy-https https://proxy-url --proxy-http http://proxy-url --proxy-skip-range excludedIP,excludedCIDR,exampleCIDRfollowed,10.0.0.0/24
- name: Onboard a connected kubernetes cluster with custom onboarding timeout.
text: az connectedk8s connect -g resourceGroupName -n connectedClusterName --onboarding-timeout 600
"""

Expand All @@ -49,6 +51,8 @@
text: az connectedk8s upgrade -g resourceGroupName -n connectedClusterName
- name: Upgrade the agents to a specific version
text: az connectedk8s upgrade -g resourceGroupName -n connectedClusterName --agent-version 0.2.62
- name: Upgrade the agents with custom upgrade timeout.
text: az connectedk8s upgrade -g resourceGroupName -n connectedClusterName --upgrade-timeout 600
"""

helps['connectedk8s list'] = """
Expand Down
3 changes: 3 additions & 0 deletions src/connectedk8s/azext_connectedk8s/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def load_arguments(self, _):
c.argument('infrastructure', options_list=['--infrastructure'], help='The infrastructure on which the Kubernetes cluster represented by this connected cluster will be running on.', arg_type=get_enum_type(Infrastructure_Enum_Values))
c.argument('disable_auto_upgrade', options_list=['--disable-auto-upgrade'], action='store_true', help='Flag to disable auto upgrade of arc agents.')
c.argument('cl_oid', options_list=['--custom-locations-oid'], help="OID of 'custom-locations' app")
c.argument('onboarding_timeout', options_list=['--onboarding-timeout'], arg_group='Timeout', help='Time required (in seconds) for the arc-agent pods to be installed on the kubernetes cluster. Override this value if the hardware/network constraints on your cluster requires more time for installing the arc-agent pods.')
c.argument('no_wait', options_list=['--no-wait'], arg_group='Timeout', help="Do not wait for the long-running operation to finish.")

with self.argument_context('connectedk8s update') as c:
c.argument('cluster_name', options_list=['--name', '-n'], id_part='name', help='The name of the connected cluster.')
Expand All @@ -51,6 +53,7 @@ def load_arguments(self, _):
c.argument('kube_config', options_list=['--kube-config'], help='Path to the kube config file.')
c.argument('kube_context', options_list=['--kube-context'], help='Kubconfig context from current machine.')
c.argument('arc_agent_version', options_list=['--agent-version'], help='Version of agent to update the helm charts to.')
c.argument('upgrade_timeout', options_list=['--upgrade-timeout'], help='Time required (in seconds) for the arc-agent pods to be upgraded on the kubernetes cluster. Override this value if the hardware/network constraints on your cluster requires more time for upgrading the arc-agent pods.')

with self.argument_context('connectedk8s enable-features') as c:
c.argument('cluster_name', options_list=['--name', '-n'], id_part='name', help='The name of the connected cluster.')
Expand Down
10 changes: 8 additions & 2 deletions src/connectedk8s/azext_connectedk8s/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from azure.cli.core.commands.client_factory import get_subscription_id
from azure.cli.core.util import send_raw_request
from azure.cli.core import telemetry
from azure.core.exceptions import ResourceNotFoundError
from msrest.exceptions import AuthenticationError, HttpOperationError, TokenExpiredError, ValidationError
from msrestazure.azure_exceptions import CloudError
from kubernetes.client.rest import ApiException
Expand Down Expand Up @@ -197,6 +198,9 @@ def arm_exception_handler(ex, fault_type, summary, return_if_not_found=False):
raise AzureInternalError("Cloud error occured while making ARM request: " + str(ex) + "\nSummary: {}".format(summary))
raise AzureResponseError("Cloud error occured while making ARM request: " + str(ex) + "\nSummary: {}".format(summary))

if isinstance(ex, ResourceNotFoundError) and return_if_not_found:
return

telemetry.set_exception(exception=ex, fault_type=fault_type, summary=summary)
raise ClientRequestError("Error occured while making ARM request: " + str(ex) + "\nSummary: {}".format(summary))

Expand Down Expand Up @@ -286,7 +290,7 @@ def delete_arc_agents(release_namespace, kube_config, kube_context, configuratio

def helm_install_release(chart_path, subscription_id, kubernetes_distro, kubernetes_infra, resource_group_name, cluster_name,
location, onboarding_tenant_id, http_proxy, https_proxy, no_proxy, proxy_cert, private_key_pem,
kube_config, kube_context, no_wait, values_file_provided, values_file, cloud_name, disable_auto_upgrade, enable_custom_locations, custom_locations_oid):
kube_config, kube_context, no_wait, values_file_provided, values_file, cloud_name, disable_auto_upgrade, enable_custom_locations, custom_locations_oid, onboarding_timeout="300"):
cmd_helm_install = ["helm", "upgrade", "--install", "azure-arc", chart_path,
"--set", "global.subscriptionId={}".format(subscription_id),
"--set", "global.kubernetesDistro={}".format(kubernetes_distro),
Expand Down Expand Up @@ -324,7 +328,9 @@ def helm_install_release(chart_path, subscription_id, kubernetes_distro, kuberne
if kube_context:
cmd_helm_install.extend(["--kube-context", kube_context])
if not no_wait:
cmd_helm_install.extend(["--wait"])
# Change --timeout format for helm client to understand
onboarding_timeout = onboarding_timeout + "s"
cmd_helm_install.extend(["--wait", "--timeout", "{}".format(onboarding_timeout)])
response_helm_install = Popen(cmd_helm_install, stdout=PIPE, stderr=PIPE)
_, error_helm_install = response_helm_install.communicate()
if response_helm_install.returncode != 0:
Expand Down
23 changes: 15 additions & 8 deletions src/connectedk8s/azext_connectedk8s/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import azext_connectedk8s._utils as utils
from glob import glob
from .vendored_sdks.models import ConnectedCluster, ConnectedClusterIdentity
from .vendored_sdks.preview_2021_04_01.models import ListClusterUserCredentialsProperties
from threading import Timer, Thread
import sys
import hashlib
Expand All @@ -57,7 +58,7 @@

def create_connectedk8s(cmd, client, resource_group_name, cluster_name, https_proxy="", http_proxy="", no_proxy="", proxy_cert="", location=None,
kube_config=None, kube_context=None, no_wait=False, tags=None, distribution='auto', infrastructure='auto',
disable_auto_upgrade=False, cl_oid=None):
disable_auto_upgrade=False, cl_oid=None, onboarding_timeout="300"):
logger.warning("Ensure that you have the latest helm version installed before proceeding.")
logger.warning("This operation might take a while...\n")

Expand Down Expand Up @@ -258,7 +259,7 @@ def create_connectedk8s(cmd, client, resource_group_name, cluster_name, https_pr
# Install azure-arc agents
utils.helm_install_release(chart_path, subscription_id, kubernetes_distro, kubernetes_infra, resource_group_name, cluster_name,
location, onboarding_tenant_id, http_proxy, https_proxy, no_proxy, proxy_cert, private_key_pem, kube_config,
kube_context, no_wait, values_file_provided, values_file, azure_cloud, disable_auto_upgrade, enable_custom_locations, custom_locations_oid)
kube_context, no_wait, values_file_provided, values_file, azure_cloud, disable_auto_upgrade, enable_custom_locations, custom_locations_oid, onboarding_timeout)

return put_cc_response

Expand Down Expand Up @@ -695,15 +696,15 @@ def get_release_namespace(kube_config, kube_context):

def create_cc_resource(client, resource_group_name, cluster_name, cc, no_wait):
try:
return sdk_no_wait(no_wait, client.create, resource_group_name=resource_group_name,
return sdk_no_wait(no_wait, client.begin_create, resource_group_name=resource_group_name,
cluster_name=cluster_name, connected_cluster=cc)
except CloudError as e:
utils.arm_exception_handler(e, consts.Create_ConnectedCluster_Fault_Type, 'Unable to create connected cluster resource')


def delete_cc_resource(client, resource_group_name, cluster_name, no_wait):
try:
sdk_no_wait(no_wait, client.delete,
sdk_no_wait(no_wait, client.begin_delete,
resource_group_name=resource_group_name,
cluster_name=cluster_name)
except CloudError as e:
Expand Down Expand Up @@ -870,7 +871,7 @@ def update_agents(cmd, client, resource_group_name, cluster_name, https_proxy=""
return str.format(consts.Update_Agent_Success, connected_cluster.name)


def upgrade_agents(cmd, client, resource_group_name, cluster_name, kube_config=None, kube_context=None, arc_agent_version=None):
def upgrade_agents(cmd, client, resource_group_name, cluster_name, kube_config=None, kube_context=None, arc_agent_version=None, upgrade_timeout="300"):
logger.warning("Ensure that you have the latest helm version installed before proceeding.")
logger.warning("This operation might take a while...\n")

Expand Down Expand Up @@ -946,7 +947,7 @@ def upgrade_agents(cmd, client, resource_group_name, cluster_name, kube_config=N
telemetry.set_exception(exception='connectedk8s upgrade called when auto-update is set to true', fault_type=consts.Manual_Upgrade_Called_In_Auto_Update_Enabled,
summary='az connectedk8s upgrade to manually upgrade agents and extensions is only supported when auto-upgrade is set to false.')
raise ClientRequestError("az connectedk8s upgrade to manually upgrade agents and extensions is only supported when auto-upgrade is set to false.",
recommendation="Please run az connectedk8s update -n <connected-cluster-name> -g <resource-group-name> --auto-upgrade 'false' before performing manual upgrade")
recommendation="Please run 'az connectedk8s update -n <connected-cluster-name> -g <resource-group-name> --auto-upgrade false' before performing manual upgrade")

else:
telemetry.set_exception(exception="The azure-arc release namespace couldn't be retrieved", fault_type=consts.Release_Namespace_Not_Found,
Expand Down Expand Up @@ -1020,8 +1021,10 @@ def upgrade_agents(cmd, client, resource_group_name, cluster_name, kube_config=N
summary='Problem loading the helm existing user supplied values')
raise CLIInternalError("Problem loading the helm existing user supplied values: " + str(e))

# Change --timeout format for helm client to understand
upgrade_timeout = upgrade_timeout + "s"
cmd_helm_upgrade = ["helm", "upgrade", "azure-arc", chart_path, "--namespace", release_namespace,
"--output", "json", "--atomic"]
"--output", "json", "--atomic", "--wait", "--timeout", "{}".format(upgrade_timeout)]

proxy_enabled_param_added = False
infra_added = False
Expand Down Expand Up @@ -1818,7 +1821,11 @@ def client_side_proxy(cmd,

# Fetching hybrid connection details from Userrp
try:
response = client.list_cluster_user_credentials(resource_group_name, cluster_name, auth_method, True)
list_prop = ListClusterUserCredentialsProperties(
authentication_method=auth_method,
client_proxy=True
)
response = client.list_cluster_user_credentials(resource_group_name, cluster_name, list_prop)
except Exception as e:
if flag == 1:
clientproxy_process.terminate()
Expand Down
Loading

0 comments on commit 08e829c

Please sign in to comment.