From 234a32c73f34379c9196685ffc41c53c29e332a9 Mon Sep 17 00:00:00 2001 From: Bsingh16 <33008256+bavneetsingh16@users.noreply.github.com> Date: Tue, 2 Aug 2022 12:27:54 -0700 Subject: [PATCH] [k8s-configuration] add support for provisionedClusters (#146) * [k8s-configuration] add support for provisionedClusters * resolve cli errors * remove sourceControlConfiguration support for provisionedClusters * code cleanup * updates hybridcontainerservice api version * change description for cluster_type Co-authored-by: Bavneet Singh --- .../azext_k8s_configuration/_params.py | 9 ++- .../azext_k8s_configuration/consts.py | 3 + .../providers/FluxConfigurationProvider.py | 57 +++++++++++-------- .../azext_k8s_configuration/utils.py | 16 +++++- 4 files changed, 59 insertions(+), 26 deletions(-) diff --git a/src/k8s-configuration/azext_k8s_configuration/_params.py b/src/k8s-configuration/azext_k8s_configuration/_params.py index d763f1c3ee2..7ef8abe1ad0 100644 --- a/src/k8s-configuration/azext_k8s_configuration/_params.py +++ b/src/k8s-configuration/azext_k8s_configuration/_params.py @@ -37,11 +37,16 @@ def load_arguments(self, _): c.argument( "cluster_type", options_list=["--cluster-type", "-t"], - arg_type=get_enum_type(["connectedClusters", "managedClusters"]), - help="Specify Arc connected clusters or AKS managed clusters.", + arg_type=get_enum_type(["connectedClusters", "managedClusters", "provisionedClusters"]), + help="Specify Arc connected clusters or AKS managed clusters or provisioned clusters.", ) with self.argument_context("k8s-configuration flux") as c: + c.argument( + "cluster_resource_provider", + options_list=['--cluster-resource-provider', '--cluster-rp'], + help='Cluster Resource Provider name for this clusterType (Required for provisionedClusters)' + ) c.argument( "name", options_list=["--name", "-n"], diff --git a/src/k8s-configuration/azext_k8s_configuration/consts.py b/src/k8s-configuration/azext_k8s_configuration/consts.py index a4210dd61e9..cf022906c5b 100644 --- a/src/k8s-configuration/azext_k8s_configuration/consts.py +++ b/src/k8s-configuration/azext_k8s_configuration/consts.py @@ -233,14 +233,17 @@ CONNECTED_CLUSTER_TYPE = "connectedclusters" MANAGED_CLUSTER_TYPE = "managedclusters" APPLIANCE_TYPE = "appliances" +PROVISIONED_CLUSTER_TYPE = "provisionedclusters" MANAGED_CLUSTER_RP = "Microsoft.ContainerService" CONNECTED_CLUSTER_RP = "Microsoft.Kubernetes" APPLIANCE_RP = "Microsoft.ResourceConnector" +HYBRIDCONTAINERSERVICE_RP = "microsoft.hybridcontainerservice" CONNECTED_CLUSTER_API_VERSION = "2021-10-01" MANAGED_CLUSTER_API_VERSION = "2021-10-01" APPLIANCE_API_VERSION = "2021-10-31-preview" +HYBRIDCONTAINERSERVICE_API_VERSION = "2022-05-01-preview" KUBERNETES_MAX_NAME_SIZE = 63 diff --git a/src/k8s-configuration/azext_k8s_configuration/providers/FluxConfigurationProvider.py b/src/k8s-configuration/azext_k8s_configuration/providers/FluxConfigurationProvider.py index e0a99697efa..8324cbc350f 100644 --- a/src/k8s-configuration/azext_k8s_configuration/providers/FluxConfigurationProvider.py +++ b/src/k8s-configuration/azext_k8s_configuration/providers/FluxConfigurationProvider.py @@ -63,11 +63,11 @@ logger = get_logger(__name__) -def show_config(cmd, client, resource_group_name, cluster_type, cluster_name, name): +def show_config(cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=None): """Get an existing Kubernetes Source Control Configuration.""" # Get Resource Provider to call - cluster_rp, _ = get_cluster_rp_api_version(cluster_type) + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) try: config = client.get( @@ -108,9 +108,9 @@ def show_config(cmd, client, resource_group_name, cluster_type, cluster_name, na raise ex -def list_configs(cmd, client, resource_group_name, cluster_type, cluster_name): +def list_configs(cmd, client, resource_group_name, cluster_type, cluster_name, cluster_resource_provider=None): # Get Resource Provider to call - cluster_rp, _ = get_cluster_rp_api_version(cluster_type) + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) return client.list(resource_group_name, cluster_rp, cluster_type, cluster_name) @@ -150,10 +150,11 @@ def create_config( suspend=False, kustomization=None, no_wait=False, + cluster_resource_provider=None, ): # Get Resource Provider to call - cluster_rp, _ = get_cluster_rp_api_version(cluster_type) + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) factory = source_kind_generator_factory( @@ -263,14 +264,15 @@ def update_config( kustomization=None, no_wait=False, yes=False, + cluster_resource_provider=None, ): # Get Resource Provider to call - cluster_rp, _ = get_cluster_rp_api_version(cluster_type) + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) config = show_config( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=cluster_rp ) if not kind: kind = convert_to_cli_source_kind(config.source_kind) @@ -364,10 +366,11 @@ def create_kustomization( prune=None, force=None, no_wait=False, + cluster_resource_provider=None, ): # Get Resource Provider to call - cluster_rp, _ = get_cluster_rp_api_version(cluster_type) + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) # Pre-Validation @@ -376,7 +379,7 @@ def create_kustomization( validate_duration("--retry-interval", retry_interval) current_config = show_config( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=cluster_rp ) if kustomization_name in current_config.kustomizations: raise ValidationError( @@ -424,10 +427,11 @@ def update_kustomization( prune=None, force=None, no_wait=False, + cluster_resource_provider=None, ): # Get Resource Provider to call - cluster_rp, _ = get_cluster_rp_api_version(cluster_type) + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) # Pre-Validation @@ -436,7 +440,7 @@ def update_kustomization( validate_duration("--retry-interval", retry_interval) current_config = show_config( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=cluster_rp ) if kustomization_name not in current_config.kustomizations: raise ValidationError( @@ -478,17 +482,18 @@ def delete_kustomization( kustomization_name, no_wait=False, yes=False, + cluster_resource_provider=None, ): # Get Resource Provider to call - cluster_rp, _ = get_cluster_rp_api_version(cluster_type) + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) # Confirmation message for deletes user_confirmation_factory(cmd, yes) current_config = show_config( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=cluster_rp ) if kustomization_name not in current_config.kustomizations: raise ValidationError( @@ -519,13 +524,14 @@ def delete_kustomization( def list_kustomization( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=None ): # Get Resource Provider to call + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) current_config = show_config( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=cluster_rp ) return current_config.kustomizations @@ -538,12 +544,13 @@ def show_kustomization( cluster_name, name, kustomization_name, + cluster_resource_provider=None, ): - + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) current_config = show_config( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=cluster_rp ) if kustomization_name not in current_config.kustomizations: raise ValidationError( @@ -554,11 +561,12 @@ def show_kustomization( def list_deployed_object( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=None ): + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) current_config = show_config( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_rp ) return current_config.statuses @@ -573,10 +581,12 @@ def show_deployed_object( object_name, object_namespace, object_kind, + cluster_resource_provider=None, ): + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) current_config = show_config( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=cluster_rp ) for status in current_config.statuses: @@ -604,19 +614,20 @@ def delete_config( force=False, no_wait=False, yes=False, + cluster_resource_provider=None, ): # Confirmation message for deletes user_confirmation_factory(cmd, yes) # Get Resource Provider to call - cluster_rp, _ = get_cluster_rp_api_version(cluster_type) + cluster_rp, _ = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_resource_provider) validate_cc_registration(cmd) config = None try: config = show_config( - cmd, client, resource_group_name, cluster_type, cluster_name, name + cmd, client, resource_group_name, cluster_type, cluster_name, name, cluster_resource_provider=cluster_rp ) except HttpResponseError: logger.warning( @@ -738,7 +749,7 @@ def __add_identity( if cluster_type.lower() == consts.MANAGED_CLUSTER_TYPE: return extension_instance - cluster_rp, parent_api_version = get_cluster_rp_api_version(cluster_type) + cluster_rp, parent_api_version = get_cluster_rp_api_version(cluster_type=cluster_type, cluster_rp=cluster_rp) cluster_resource_id = ( "/subscriptions/{0}/resourceGroups/{1}/providers/{2}/{3}/{4}".format( diff --git a/src/k8s-configuration/azext_k8s_configuration/utils.py b/src/k8s-configuration/azext_k8s_configuration/utils.py index 7999145934e..6d85a433739 100644 --- a/src/k8s-configuration/azext_k8s_configuration/utils.py +++ b/src/k8s-configuration/azext_k8s_configuration/utils.py @@ -11,11 +11,25 @@ from azure.cli.core.azclierror import ( MutuallyExclusiveArgumentError, InvalidArgumentValueError, + RequiredArgumentMissingError, ) from . import consts -def get_cluster_rp_api_version(cluster_type) -> Tuple[str, str]: +def get_cluster_rp_api_version(cluster_type, cluster_rp=None) -> Tuple[str, str]: + if cluster_type.lower() == consts.PROVISIONED_CLUSTER_TYPE: + if cluster_rp is None or cluster_rp.strip() == "": + raise RequiredArgumentMissingError( + "Error! Cluster Resource Provider value is required for Cluster Type '{}'".format(cluster_type) + ) + if cluster_rp.lower() == consts.HYBRIDCONTAINERSERVICE_RP: + return ( + consts.HYBRIDCONTAINERSERVICE_RP, + consts.HYBRIDCONTAINERSERVICE_API_VERSION, + ) + raise InvalidArgumentValueError( + "Error! Cluster type '{}' and Cluster Resource Provider '{}' combination is not supported".format(cluster_type, cluster_rp) + ) if cluster_type.lower() == consts.CONNECTED_CLUSTER_TYPE: return consts.CONNECTED_CLUSTER_RP, consts.CONNECTED_CLUSTER_API_VERSION if cluster_type.lower() == consts.APPLIANCE_TYPE: