|
20 | 20 | from azure.cli.core.util import send_raw_request
|
21 | 21 | from azure.cli.core import telemetry
|
22 | 22 | from azure.core.exceptions import ResourceNotFoundError
|
23 |
| -from msrest.exceptions import AuthenticationError, HttpOperationError, TokenExpiredError, ValidationError |
| 23 | +from msrest.exceptions import AuthenticationError, HttpOperationError, TokenExpiredError |
| 24 | +from msrest.exceptions import ValidationError as MSRestValidationError |
24 | 25 | from msrestazure.azure_exceptions import CloudError
|
25 | 26 | from kubernetes.client.rest import ApiException
|
26 |
| -from azext_connectedk8s._client_factory import _resource_client_factory |
| 27 | +from azext_connectedk8s._client_factory import _resource_client_factory, _resource_providers_client |
27 | 28 | import azext_connectedk8s._constants as consts
|
28 | 29 | from kubernetes import client as kube_client
|
29 | 30 | from azure.cli.core.azclierror import CLIInternalError, ClientRequestError, ArgumentUsageError, ManualInterrupt, AzureResponseError, AzureInternalError, ValidationError
|
@@ -183,7 +184,7 @@ def arm_exception_handler(ex, fault_type, summary, return_if_not_found=False):
|
183 | 184 | raise AzureInternalError("Http operation error occured while making ARM request: " + str(ex) + "\nSummary: {}".format(summary))
|
184 | 185 | raise AzureResponseError("Http operation error occured while making ARM request: " + str(ex) + "\nSummary: {}".format(summary))
|
185 | 186 |
|
186 |
| - if isinstance(ex, ValidationError): |
| 187 | + if isinstance(ex, MSRestValidationError): |
187 | 188 | telemetry.set_exception(exception=ex, fault_type=fault_type, summary=summary)
|
188 | 189 | raise AzureResponseError("Validation error occured while making ARM request: " + str(ex) + "\nSummary: {}".format(summary))
|
189 | 190 |
|
@@ -395,3 +396,38 @@ def names(self, names):
|
395 | 396 | V1ContainerImage.names = V1ContainerImage.names.setter(names)
|
396 | 397 | except Exception as ex:
|
397 | 398 | logger.debug("Error while trying to monkey patch the fix for list_node(): {}".format(str(ex)))
|
| 399 | + |
| 400 | + |
| 401 | +def check_provider_registrations(cli_ctx): |
| 402 | + try: |
| 403 | + rp_client = _resource_providers_client(cli_ctx) |
| 404 | + cc_registration_state = rp_client.get(consts.Connected_Cluster_Provider_Namespace).registration_state |
| 405 | + if cc_registration_state != "Registered": |
| 406 | + telemetry.set_exception(exception="{} provider is not registered".format(consts.Connected_Cluster_Provider_Namespace), fault_type=consts.CC_Provider_Namespace_Not_Registered_Fault_Type, |
| 407 | + summary="{} provider is not registered".format(consts.Connected_Cluster_Provider_Namespace)) |
| 408 | + raise ValidationError("{} provider is not registered. Please register it using 'az provider register -n 'Microsoft.Kubernetes' before running the connect command.".format(consts.Connected_Cluster_Provider_Namespace)) |
| 409 | + kc_registration_state = rp_client.get(consts.Kubernetes_Configuration_Provider_Namespace).registration_state |
| 410 | + if kc_registration_state != "Registered": |
| 411 | + telemetry.set_user_fault() |
| 412 | + logger.warning("{} provider is not registered".format(consts.Kubernetes_Configuration_Provider_Namespace)) |
| 413 | + except ValidationError as e: |
| 414 | + raise e |
| 415 | + except Exception as ex: |
| 416 | + logger.warning("Couldn't check the required provider's registration status. Error: {}".format(str(ex))) |
| 417 | + |
| 418 | + |
| 419 | +def can_create_clusterrolebindings(configuration): |
| 420 | + try: |
| 421 | + api_instance = kube_client.AuthorizationV1Api(kube_client.ApiClient(configuration)) |
| 422 | + access_review = kube_client.V1SelfSubjectAccessReview(spec={ |
| 423 | + "resourceAttributes": { |
| 424 | + "verb": "create", |
| 425 | + "resource": "clusterrolebindings", |
| 426 | + "group": "rbac.authorization.k8s.io" |
| 427 | + } |
| 428 | + }) |
| 429 | + response = api_instance.create_self_subject_access_review(access_review) |
| 430 | + return response.status.allowed |
| 431 | + except Exception as ex: |
| 432 | + logger.warning("Couldn't check for the permission to create clusterrolebindings on this k8s cluster. Error: {}".format(str(ex))) |
| 433 | + return "Unknown" |
0 commit comments