| aguilard | 8c0a054 | 2023-11-13 13:16:32 +0000 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | ####################################################################################### |
| 3 | # Copyright ETSI Contributors and Others. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | ####################################################################################### |
| 18 | |
| 19 | # Delete a VM and its resources (nic, disk, nsg, public IP) on Azure. |
| 20 | # To do this it reads the following environment variables: |
| 21 | # - K8S_NAME: name of the VM |
| 22 | # - RESOURCE_GROUP: name of the resource-group where the VM is |
| 23 | function delete_azure_vm { |
| 24 | set -eux |
| 25 | INTERFACE_ID=$(az vm show --resource-group ${RESOURCE_GROUP} --name ${K8S_NAME} --query networkProfile.networkInterfaces[0].id) |
| 26 | INTERFACE_ID=${INTERFACE_ID:1:-1} |
| 27 | OS_DISK_ID=$(az vm show --resource-group ${RESOURCE_GROUP} --name ${K8S_NAME} --query storageProfile.osDisk.managedDisk.id) |
| 28 | OS_DISK_ID=${OS_DISK_ID:1:-1} |
| 29 | SECURITY_GROUP_ID=$(az network nic show --id ${INTERFACE_ID} --query networkSecurityGroup.id) |
| 30 | SECURITY_GROUP_ID=${SECURITY_GROUP_ID:1:-1} |
| 31 | PUBLIC_IP_ID=$(az network nic show --id ${INTERFACE_ID} --query ipConfigurations[0].publicIpAddress.id) |
| 32 | PUBLIC_IP_ID=${PUBLIC_IP_ID:1:-1} |
| 33 | az vm delete --resource-group ${RESOURCE_GROUP} --name ${K8S_NAME} --yes |
| 34 | az network nic delete --id ${INTERFACE_ID} |
| 35 | az disk delete --id ${OS_DISK_ID} --yes |
| 36 | az network nsg delete --id ${SECURITY_GROUP_ID} |
| 37 | if [ -n "${PUBLIC_IP_ID}" ]; then |
| 38 | az network public-ip delete --id ${PUBLIC_IP_ID} |
| 39 | fi |
| 40 | } |
| 41 | |
| 42 | # Delete an existing AKS cluster and its subnet. |
| 43 | # Required the following environment variables: |
| 44 | # - K8S_NAME: name of the AKS |
| 45 | # - RESOURCE_GROUP: name of the resource-group where the AKS was created |
| 46 | function delete_azure_aks { |
| 47 | SUBNET_ID=$(az aks show --resource-group "${RESOURCE_GROUP}" --name ${K8S_NAME} --query agentPoolProfiles[].vnetSubnetId -o tsv) |
| 48 | az aks delete -y --resource-group "${RESOURCE_GROUP}" --name "${K8S_NAME}" |
| 49 | az network vnet subnet delete --ids "${SUBNET_ID}" |
| 50 | } |
| 51 | |
| 52 | # Delete a VM on GCP. |
| 53 | # To do this it reads the following environment variables: |
| 54 | # - K8S_NAME: name of the VM |
| 55 | # - GCP_PROJECT: name of project where the VM is |
| 56 | # - GCP_ZONE: name of the zone |
| 57 | function delete_gcp_vm { |
| 58 | gcloud compute instances delete "${K8S_NAME}" --project="${GCP_PROJECT}" --zone="${GCP_ZONE}" --delete-disks all -q |
| 59 | } |
| 60 | |
| 61 | # Delete an existing GKE cluster. |
| 62 | # Required the following environment variables: |
| 63 | # - K8S_NAME: name of the AKS |
| 64 | # - GCP_PROJECT: name of project where the VM is |
| 65 | # - GCP_ZONE: name of the zone |
| 66 | function delete_gcp_gke { |
| 67 | gcloud container clusters delete "${K8S_NAME}" --project="${GCP_PROJECT}" --zone="${GCP_ZONE}" -q |
| 68 | } |
| 69 | |
| 70 | # If there is an argument, it must be the cluster name |
| 71 | if [ -n "${1:-}" ]; then |
| 72 | K8S_NAME=$1 |
| 73 | else |
| 74 | K8S_NAME="${K8S_IMAGE_NAME}" |
| 75 | fi |
| 76 | # Default USE_PAAS_K8S is "FALSE" |
| 77 | if [ -z "${USE_PAAS_K8S}" ]; then |
| 78 | USE_PAAS_K8S="FALSE" |
| 79 | fi |
| 80 | # Branch by USE_PAAS_K8S and CLOUD_TYPE values |
| 81 | if [ "${USE_PAAS_K8S}" == "FALSE" ]; then |
| 82 | # Deletes k8s cluster in a cloud's VM |
| 83 | echo "Deleting IaaS k8s cluster in ${CLOUD_TYPE}" |
| 84 | if [ "${CLOUD_TYPE}" == "azure" ]; then |
| 85 | # Azure VIM |
| 86 | delete_azure_vm |
| 87 | elif [ "${CLOUD_TYPE}" == "gcp" ]; then |
| 88 | # GCP VIM |
| 89 | delete_gcp_vm |
| 90 | else |
| 91 | echo "Invalid cloud type: ${CLOUD_TYPE}" |
| 92 | fi |
| 93 | else |
| 94 | # Deletes k8s cluster as PaaS in cloud |
| 95 | if [ "${CLOUD_TYPE}" == "azure" ]; then |
| 96 | echo "Deleting PaaS k8s cluster in Azure" |
| 97 | delete_azure_aks |
| 98 | elif [ "${CLOUD_TYPE}" == "gcp" ]; then |
| 99 | echo "Deleting PaaS k8s cluster in GCP" |
| 100 | delete_gcp_gke |
| 101 | else |
| 102 | echo "Invalid cloud type: ${CLOUD_TYPE}" |
| 103 | fi |
| 104 | |
| 105 | fi |
| 106 | |