Skip to content

Commit

Permalink
Update identity with api version and rp in same function
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis committed Dec 8, 2021
1 parent bb3dfa4 commit 6753db5
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 48 deletions.
19 changes: 12 additions & 7 deletions src/k8s-configuration/azext_k8s_configuration/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@
REQUIRED_KUSTOMIZATION_KEYS = {"name"}

VALID_DURATION_REGEX = r"((?P<hours>\d+?)h)?((?P<minutes>\d+?)m)?((?P<seconds>\d+?)s)?"
VALID_URL_REGEX = r"^(((http|https|ssh)://)|(git@))"
VALID_GIT_URL_REGEX = r"^(((http|https|ssh)://)|(git@))"
VALID_BUCKET_URL_REGEX = r"^(((http|https)://))"

VALID_KUBERNETES_DNS_SUBDOMAIN_NAME_REGEX = r"^[a-z0-9]([\.\-a-z0-9]*[a-z0-9])?$"
VALID_KUBERNETES_DNS_NAME_REGEX = r"^[a-z0-9]([\-a-z0-9]*[a-z0-9])?$"
Expand All @@ -231,13 +232,17 @@
BUCKET = "bucket"
GIT_REPOSITORY = "GitRepository"

CONNECTED_CLUSTERS = "connectedclusters"
MANAGED_CLUSTERS = "managedclusters"
APPLIANCES = "appliances"
CONNECTED_CLUSTER_TYPE = "connectedclusters"
MANAGED_CLUSTER_TYPE = "managedclusters"
APPLIANCE_TYPE = "appliances"

MANAGED_RP_NAMESPACE = "Microsoft.ContainerService"
CONNECTED_RP_NAMESPACE = "Microsoft.Kubernetes"
APPLIANCE_RP_NAMESPACE = "Microsoft.ResourceConnector"
MANAGED_CLUSTER_RP = "Microsoft.ContainerService"
CONNECTED_CLUSTER_RP = "Microsoft.Kubernetes"
APPLIANCE_RP = "Microsoft.ResourceConnector"

CONNECTED_CLUSTER_API_VERSION = "2021-10-01"
MANAGED_CLUSTER_API_VERSION = "2021-10-01"
APPLIANCE_API_VERSION = "2021-10-31-preview"

KUBERNETES_MAX_NAME_SIZE = 63

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
k8s_configuration_sourcecontrol_client,
)
from ..utils import (
get_parent_api_version,
get_cluster_rp,
get_cluster_rp_api_version,
get_data_from_key_or_file,
parse_dependencies,
parse_duration,
Expand All @@ -37,6 +36,7 @@
is_dogfood_cluster,
)
from ..validators import (
validate_bucket_url,
validate_cc_registration,
validate_git_url,
validate_known_hosts,
Expand Down Expand Up @@ -67,7 +67,7 @@ def show_config(cmd, client, resource_group_name, cluster_type, cluster_name, na
"""Get an existing Kubernetes Source Control Configuration."""

# Get Resource Provider to call
cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
validate_cc_registration(cmd)
try:
config = client.get(
Expand Down Expand Up @@ -110,7 +110,7 @@ def show_config(cmd, client, resource_group_name, cluster_type, cluster_name, na

def list_configs(cmd, client, resource_group_name, cluster_type, cluster_name):
# Get Resource Provider to call
cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
validate_cc_registration(cmd)

return client.list(resource_group_name, cluster_rp, cluster_type, cluster_name)
Expand Down Expand Up @@ -153,7 +153,7 @@ def create_config(
):

# Get Resource Provider to call
cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
validate_cc_registration(cmd)

factory = source_kind_generator_factory(
Expand Down Expand Up @@ -266,7 +266,7 @@ def update_config(
):

# Get Resource Provider to call
cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
validate_cc_registration(cmd)

config = show_config(
Expand Down Expand Up @@ -367,7 +367,7 @@ def create_kustomization(
):

# Get Resource Provider to call
cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
validate_cc_registration(cmd)

# Pre-Validation
Expand Down Expand Up @@ -434,7 +434,7 @@ def update_kustomization(
):

# Get Resource Provider to call
cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
validate_cc_registration(cmd)

# Pre-Validation
Expand Down Expand Up @@ -495,7 +495,7 @@ def delete_kustomization(
):

# Get Resource Provider to call
cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
validate_cc_registration(cmd)

# Confirmation message for deletes
Expand Down Expand Up @@ -624,7 +624,7 @@ def delete_config(
user_confirmation_factory(cmd, yes)

# Get Resource Provider to call
cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
validate_cc_registration(cmd)

config = None
Expand Down Expand Up @@ -749,15 +749,17 @@ def __add_identity(
subscription_id = get_subscription_id(cmd.cli_ctx)
resources = cf_resources(cmd.cli_ctx, subscription_id)

if cluster_type.lower() == consts.MANAGED_CLUSTER_TYPE:
return extension_instance

cluster_rp, parent_api_version = get_cluster_rp_api_version(cluster_type)

cluster_resource_id = (
"/subscriptions/{0}/resourceGroups/{1}/providers/{2}/{3}/{4}".format(
subscription_id, resource_group_name, cluster_rp, cluster_type, cluster_name
)
)

if cluster_rp == consts.MANAGED_RP_NAMESPACE:
return extension_instance
parent_api_version = get_parent_api_version(cluster_rp)
try:
resource = resources.get_by_id(cluster_resource_id, parent_api_version)
location = str(resource.location.lower())
Expand Down Expand Up @@ -907,7 +909,7 @@ def updater(config):
ssh_known_hosts=self.knownhost_data,
https_user=self.https_user,
local_auth_ref=self.local_auth_ref,
https_ca_file=self.https_ca_data,
https_ca_cert=self.https_ca_data,
)
config.source_kind = SourceKindType.GIT_REPOSITORY
return config
Expand All @@ -931,7 +933,7 @@ def git_repository_updater(config):
ssh_known_hosts=self.knownhost_data,
https_user=self.https_user,
local_auth_ref=self.local_auth_ref,
https_ca_file=self.https_ca_data,
https_ca_cert=self.https_ca_data,
)
if swapped_kind:
self.validate()
Expand Down Expand Up @@ -973,6 +975,7 @@ def __init__(self, **kwargs):

def validate(self):
super().validate_required_params(**self.kwargs)
validate_bucket_url(self.url)
if not ((self.access_key and self.secret_key) or self.local_auth_ref):
raise RequiredArgumentMissingError(
consts.REQUIRED_BUCKET_VALUES_MISSING_ERROR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from ..utils import (
fix_compliance_state,
get_cluster_rp,
get_cluster_rp_api_version,
get_data_from_key_or_file,
to_base64,
)
Expand All @@ -33,7 +33,7 @@ def show_config(cmd, client, resource_group_name, cluster_type, cluster_name, na
validate_cc_registration(cmd)

# Determine ClusterRP
cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
try:
extension = client.get(
resource_group_name, cluster_rp, cluster_type, cluster_name, name
Expand Down Expand Up @@ -68,15 +68,15 @@ def list_configs(cmd, client, resource_group_name, cluster_type, cluster_name):
# Validate that the subscription is registered to Microsoft.KubernetesConfiguration
validate_cc_registration(cmd)

cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
return client.list(resource_group_name, cluster_rp, cluster_type, cluster_name)


def delete_config(cmd, client, resource_group_name, cluster_type, cluster_name, name):
# Validate that the subscription is registered to Microsoft.KubernetesConfiguration
validate_cc_registration(cmd)

cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)
return client.begin_delete(
resource_group_name, cluster_rp, cluster_type, cluster_name, name
)
Expand Down Expand Up @@ -109,7 +109,7 @@ def create_config(

"""Create a new Kubernetes Source Control Configuration."""
# Determine ClusterRP
cluster_rp = get_cluster_rp(cluster_type)
cluster_rp, _ = get_cluster_rp_api_version(cluster_type)

# Determine operatorInstanceName
if operator_instance_name is None:
Expand Down
31 changes: 11 additions & 20 deletions src/k8s-configuration/azext_k8s_configuration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
import json
import re
from datetime import timedelta
from typing import Tuple
from azure.cli.core.azclierror import (
MutuallyExclusiveArgumentError,
InvalidArgumentValueError,
)
from . import consts


def get_cluster_rp(cluster_type):
if cluster_type.lower() == consts.CONNECTED_CLUSTERS:
return consts.CONNECTED_RP_NAMESPACE
if cluster_type.lower() == consts.APPLIANCES:
return consts.APPLIANCE_RP_NAMESPACE
if cluster_type.lower() == "" or cluster_type.lower() == "managedclusters":
return consts.MANAGED_RP_NAMESPACE
def get_cluster_rp_api_version(cluster_type) -> Tuple[str, str]:
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:
return consts.APPLIANCE_RP, consts.APPLIANCE_API_VERSION
if (
cluster_type.lower() == ""
or cluster_type.lower() == consts.MANAGED_CLUSTER_TYPE
):
return consts.MANAGED_CLUSTER_RP, consts.MANAGED_CLUSTER_API_VERSION
raise InvalidArgumentValueError(
"Error! Cluster type '{}' is not supported".format(cluster_type)
)
Expand Down Expand Up @@ -124,19 +128,6 @@ def fix_compliance_state(config):
return config


def get_parent_api_version(cluster_rp):
if cluster_rp == "Microsoft.Kubernetes":
return "2020-01-01-preview"
if cluster_rp == "Microsoft.ResourceConnector":
return "2020-09-15-privatepreview"
if cluster_rp == "Microsoft.ContainerService":
return "2017-07-01"
raise InvalidArgumentValueError(
"Error! Cluster RP '{}' is not supported"
" for extension identity".format(cluster_rp)
)


def is_dogfood_cluster(cmd):
return cmd.cli_ctx.cloud.endpoints.resource_manager == consts.DF_RM_ENDPOINT

Expand Down
9 changes: 8 additions & 1 deletion src/k8s-configuration/azext_k8s_configuration/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ def validate_duration(arg_name: str, duration: str):


def validate_git_url(url: str):
if not re.match(consts.VALID_URL_REGEX, url):
if not re.match(consts.VALID_GIT_URL_REGEX, url):
raise InvalidArgumentValueError(
consts.INVALID_URL_ERROR, consts.INVALID_URL_HELP
)


def validate_bucket_url(url: str):
if not re.match(consts.VALID_BUCKET_URL_REGEX, url):
raise InvalidArgumentValueError(
consts.INVALID_URL_ERROR, consts.INVALID_URL_HELP
)
Expand Down

0 comments on commit 6753db5

Please sign in to comment.