2 #######################################################################################
3 # Copyright ETSI Contributors and Others.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #######################################################################################
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
{
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}
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}"
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
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
70 # If there is an argument, it must be the cluster name
71 if [ -n "${1:-}" ]; then
74 K8S_NAME
="${K8S_IMAGE_NAME}"
76 # Default USE_PAAS_K8S is "FALSE"
77 if [ -z "${USE_PAAS_K8S}" ]; then
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
87 elif [ "${CLOUD_TYPE}" == "gcp" ]; then
91 echo "Invalid cloud type: ${CLOUD_TYPE}"
94 # Deletes k8s cluster as PaaS in cloud
95 if [ "${CLOUD_TYPE}" == "azure" ]; then
96 echo "Deleting PaaS k8s cluster in Azure"
98 elif [ "${CLOUD_TYPE}" == "gcp" ]; then
99 echo "Deleting PaaS k8s cluster in GCP"
102 echo "Invalid cloud type: ${CLOUD_TYPE}"