Skip to content

Commit

Permalink
[k8s-configuration] add support for provisionedClusters (#146)
Browse files Browse the repository at this point in the history
* [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 <[email protected]>
  • Loading branch information
bavneetsingh16 and Bavneet Singh authored Aug 2, 2022
1 parent 06eb7f8 commit 234a32c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
9 changes: 7 additions & 2 deletions src/k8s-configuration/azext_k8s_configuration/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
3 changes: 3 additions & 0 deletions src/k8s-configuration/azext_k8s_configuration/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -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(
Expand All @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 15 additions & 1 deletion src/k8s-configuration/azext_k8s_configuration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 234a32c

Please sign in to comment.