-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[ConnectedK8s] Added update command (#3) #2091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| Invalid_Location_Fault_Type = 'location-validation-error' | ||
| Load_Kubeconfig_Fault_Type = 'kubeconfig-load-error' | ||
| Read_ConfigMap_Fault_Type = 'configmap-read-error' | ||
| Create_ConnectedCluster_Fault_Type = 'connected-cluster-create-error' | ||
| Delete_ConnectedCluster_Fault_Type = 'connected-cluster-delete-error' | ||
| Bad_DeleteRequest_Fault_Type = 'bad-delete-request-error' | ||
| Cluster_Already_Onboarded_Fault_Type = 'cluster-already-onboarded-error' | ||
| Resource_Already_Exists_Fault_Type = 'resource-already-exists-error' | ||
| Resource_Does_Not_Exist_Fault_Type = 'resource-does-not-exist-error' | ||
| Create_ResourceGroup_Fault_Type = 'resource-group-creation-error' | ||
| Add_HelmRepo_Fault_Type = 'helm-repo-add-error' | ||
| List_HelmRelease_Fault_Type = 'helm-list-release-error' | ||
| KeyPair_Generate_Fault_Type = 'keypair-generation-error' | ||
| PublicKey_Export_Fault_Type = 'publickey-export-error' | ||
| PrivateKey_Export_Fault_Type = 'privatekey-export-error' | ||
| Install_HelmRelease_Fault_Type = 'helm-release-install-error' | ||
| Delete_HelmRelease_Fault_Type = 'helm-release-delete-error' | ||
| Check_PodStatus_Fault_Type = 'check-pod-status-error' | ||
| Kubernetes_Connectivity_FaultType = 'kubernetes-cluster-connection-error' | ||
| Helm_Version_Fault_Type = 'helm-not-updated-error' | ||
| Check_HelmVersion_Fault_Type = 'helm-version-check-error' | ||
| Helm_Installation_Fault_Type = 'helm-not-installed-error' | ||
| Check_HelmInstallation_Fault_Type = 'check-helm-installed-error' | ||
| Get_HelmRegistery_Path_Fault_Type = 'helm-registry-path-fetch-error' | ||
| Pull_HelmChart_Fault_Type = 'helm-chart-pull-error' | ||
| Export_HelmChart_Fault_Type = 'helm-chart-export-error' | ||
| Get_Kubernetes_Version_Fault_Type = 'kubernetes-get-version-error' | ||
| Get_Kubernetes_Distro_Fault_Type = 'kubernetes-get-distribution-error' | ||
| Update_Agent_Success = 'Agents for Connected Cluster {} have been updated successfully' | ||
| Update_Agent_Failure = 'Error while updating agents. Please run \"kubectl get pods -n azure-arc\" to check the pods in case of timeout error. Error: {}' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import os | ||
| import subprocess | ||
| from subprocess import Popen, PIPE | ||
| import requests | ||
|
|
||
| from knack.util import CLIError | ||
| from azure.cli.core.commands.client_factory import get_subscription_id | ||
| from azure.cli.core import telemetry | ||
| from azext_connectedk8s._client_factory import _resource_client_factory | ||
| import azext_connectedk8s._constants as consts | ||
|
|
||
|
|
||
| def validate_location(cmd, location): | ||
| subscription_id = get_subscription_id(cmd.cli_ctx) | ||
| rp_locations = [] | ||
| resourceClient = _resource_client_factory(cmd.cli_ctx, subscription_id=subscription_id) | ||
| providerDetails = resourceClient.providers.get('Microsoft.Kubernetes') | ||
| for resourceTypes in providerDetails.resource_types: | ||
| if resourceTypes.resource_type == 'connectedClusters': | ||
| rp_locations = [location.replace(" ", "").lower() for location in resourceTypes.locations] | ||
| if location.lower() not in rp_locations: | ||
| telemetry.set_user_fault() | ||
| telemetry.set_exception(exception='Location not supported', fault_type=consts.Invalid_Location_Fault_Type, | ||
| summary='Provided location is not supported for creating connected clusters') | ||
| raise CLIError("Connected cluster resource creation is supported only in the following locations: " + | ||
| ', '.join(map(str, rp_locations)) + | ||
| ". Use the --location flag to specify one of these locations.") | ||
| break | ||
|
|
||
|
|
||
| def get_chart_path(registry_path, kube_config, kube_context): | ||
| # Pulling helm chart from registry | ||
| os.environ['HELM_EXPERIMENTAL_OCI'] = '1' | ||
| pull_helm_chart(registry_path, kube_config, kube_context) | ||
|
|
||
| # Exporting helm chart | ||
| chart_export_path = os.path.join(os.path.expanduser('~'), '.azure', 'AzureArcCharts') | ||
| export_helm_chart(registry_path, chart_export_path, kube_config, kube_context) | ||
| # Helm Install | ||
| helm_chart_path = os.path.join(chart_export_path, 'azure-arc-k8sagents') | ||
| chart_path = os.getenv('HELMCHART') if os.getenv('HELMCHART') else helm_chart_path | ||
| return chart_path | ||
|
|
||
|
|
||
| def pull_helm_chart(registry_path, kube_config, kube_context): | ||
| cmd_helm_chart_pull = ["helm", "chart", "pull", registry_path, "--kubeconfig", kube_config] | ||
| if kube_context: | ||
| cmd_helm_chart_pull.extend(["--kube-context", kube_context]) | ||
| response_helm_chart_pull = subprocess.Popen(cmd_helm_chart_pull, stdout=PIPE, stderr=PIPE) | ||
| _, error_helm_chart_pull = response_helm_chart_pull.communicate() | ||
| if response_helm_chart_pull.returncode != 0: | ||
| telemetry.set_exception(exception=error_helm_chart_pull.decode("ascii"), fault_type=consts.Pull_HelmChart_Fault_Type, | ||
| summary='Unable to pull helm chart from the registry') | ||
| raise CLIError("Unable to pull helm chart from the registry '{}': ".format(registry_path) + error_helm_chart_pull.decode("ascii")) | ||
|
|
||
|
|
||
| def export_helm_chart(registry_path, chart_export_path, kube_config, kube_context): | ||
| chart_export_path = os.path.join(os.path.expanduser('~'), '.azure', 'AzureArcCharts') | ||
| cmd_helm_chart_export = ["helm", "chart", "export", registry_path, "--destination", chart_export_path, "--kubeconfig", kube_config] | ||
| if kube_context: | ||
| cmd_helm_chart_export.extend(["--kube-context", kube_context]) | ||
| response_helm_chart_export = subprocess.Popen(cmd_helm_chart_export, stdout=PIPE, stderr=PIPE) | ||
| _, error_helm_chart_export = response_helm_chart_export.communicate() | ||
| if response_helm_chart_export.returncode != 0: | ||
| telemetry.set_exception(exception=error_helm_chart_export.decode("ascii"), fault_type=consts.Export_HelmChart_Fault_Type, | ||
| summary='Unable to export helm chart from the registry') | ||
| raise CLIError("Unable to export helm chart from the registry '{}': ".format(registry_path) + error_helm_chart_export.decode("ascii")) | ||
|
|
||
|
|
||
| def add_helm_repo(kube_config, kube_context): | ||
| repo_name = os.getenv('HELMREPONAME') | ||
| repo_url = os.getenv('HELMREPOURL') | ||
| cmd_helm_repo = ["helm", "repo", "add", repo_name, repo_url, "--kubeconfig", kube_config] | ||
| if kube_context: | ||
| cmd_helm_repo.extend(["--kube-context", kube_context]) | ||
| response_helm_repo = Popen(cmd_helm_repo, stdout=PIPE, stderr=PIPE) | ||
| _, error_helm_repo = response_helm_repo.communicate() | ||
| if response_helm_repo.returncode != 0: | ||
| telemetry.set_exception(exception=error_helm_repo.decode("ascii"), fault_type=consts.Add_HelmRepo_Fault_Type, | ||
| summary='Failed to add helm repository') | ||
| raise CLIError("Unable to add repository {} to helm: ".format(repo_url) + error_helm_repo.decode("ascii")) | ||
|
|
||
|
|
||
| def get_helm_registry(profile, location): | ||
| cred, _, _ = profile.get_login_credentials( | ||
| resource='https://management.core.windows.net/') | ||
| token = cred._token_retriever()[2].get('accessToken') # pylint: disable=protected-access | ||
|
|
||
| get_chart_location_url = "https://{}.dp.kubernetesconfiguration.azure.com/{}/GetLatestHelmPackagePath?api-version=2019-11-01-preview".format(location, 'azure-arc-k8sagents') | ||
| query_parameters = {} | ||
| query_parameters['releaseTrain'] = os.getenv('RELEASETRAIN') if os.getenv('RELEASETRAIN') else 'stable' | ||
| header_parameters = {} | ||
| header_parameters['Authorization'] = "Bearer {}".format(str(token)) | ||
| try: | ||
| response = requests.post(get_chart_location_url, params=query_parameters, headers=header_parameters) | ||
| except Exception as e: | ||
| telemetry.set_exception(exception=e, fault_type=consts.Get_HelmRegistery_Path_Fault_Type, | ||
| summary='Error while fetching helm chart registry path') | ||
| raise CLIError("Error while fetching helm chart registry path: " + str(e)) | ||
| if response.status_code == 200: | ||
| return response.json().get('repositoryPath') | ||
| telemetry.set_exception(exception=str(response.json()), fault_type=consts.Get_HelmRegistery_Path_Fault_Type, | ||
| summary='Error while fetching helm chart registry path') | ||
| raise CLIError("Error while fetching helm chart registry path: {}".format(str(response.json()))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.