4adcf8f91c1dcd54f562ae2522128a18d85bcb19
[releng/builder.git] / jjb / packaging / openstack-k8s-create-with-template.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2021 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 # shellcheck disable=SC2153,SC2034
12 echo "---> Create K8S cluster with pre-existing template"
13
14 set -eux -o pipefail
15 # shellcheck disable=SC1090
16 . ~/lf-env.sh
17 lf-activate-venv python-openstackclient python-magnumclient
18
19 OS_TIMEOUT=20       # Wait time in minutes for OpenStack cluster to come up.
20 CLUSTER_RETRIES=3   # Number of times to retry creating a cluster.
21 CLUSTER_SUCCESSFUL=false
22
23 mkdir -p "$WORKSPACE/archives"
24
25 boot_volume_size="${BOOT_VOLUME_SIZE}"
26 cluster_name="${CLUSTER_NAME}"
27 cluster_settle_time="${CLUSTER_SETTLE_TIME:-1m}"
28 cluster_template_name="${CLUSTER_TEMPLATE_NAME}"
29 cloud_provider_tag="${CLOUD_PROVIDER_TAG}"
30 container_infra_prefix="${CONTAINER_INFRA_PREFIX}"
31 etcd_volume_size="${ETCD_VOLUME_SIZE}"
32 k8s_version="${K8S_VERSION}"
33 keypair="${KEYPAIR}"
34 kube_tag="${KUBE_TAG}"
35 helm_client_url="${HELM_CLIENT_URL}"
36 helm_sha256="${HELM_SHA256}"
37 helm_version="${HELM_VERSION}"
38 master_count="${MASTER_COUNT:-1}"
39 master_flavor="${MASTER_FLAVOR}"
40 master_lb_floating_ip_enabled="${MASTER_LB_FLOATING_IP_ENABLED:-false}"
41 node_count="${NODE_COUNT:-2}"
42 node_flavor="${NODE_FLAVOR}"
43 os_cloud="${OS_CLOUD:-vex}"
44
45 echo "INFO: Create a Cluster:${CLUSTER_NAME} for attempts:${CLUSTER_RETRIES}."
46 for try in $(seq $CLUSTER_RETRIES); do
47     # shellcheck disable=SC1083
48
49     # Create the cluster using pre-defined template. Returns the status which includes the $cluster_uuid
50     cluster_status=$(openstack --os-cloud "${os_cloud}" coe cluster create "${cluster_name}" \
51         --cluster-template "${cluster_template_name}" \
52         --keypair "${keypair}" \
53         --master-count "${master_count}" \
54         --node-count "${node_count}" \
55         --master-flavor "${master_flavor}" \
56         --flavor "${node_flavor}" \
57         --labels \
58 boot_volume_size="${boot_volume_size}",\
59 container_infra_prefix="${container_infra_prefix}",\
60 cloud_provider_tag="${cloud_provider_tag}",\
61 helm_client_sha256="${helm_sha256}",\
62 helm_client_tag="${helm_version}",\
63 etcd_volume_size="${etcd_volume_size}",\
64 kube_tag="${kube_tag}",\
65 master_lb_floating_ip_enabled=false,\
66 helm_client_url="${helm_client_url}" \
67         --floating-ip-disabled)
68
69     # Check return status and extract the $cluster_uuid from return status
70     if [[ -z "$cluster_status" ]]; then
71         echo "ERROR: Failed to create coe cluster ${cluster_name}"
72         exit 1
73     elif [[ "${cluster_status}" =~ .*accepted.* ]]; then
74         cluster_uuid=$(echo "${cluster_status}" | awk -F' ' '{print $5}')
75     fi
76
77     echo "INFO $try: Wait until ${OS_TIMEOUT} (in minutes) to rollout ${cluster_name}."
78     for i in $(seq $OS_TIMEOUT); do
79         sleep 90
80
81         CLUSTER_STATUS=$(openstack --os-cloud "$os_cloud" coe cluster show "$cluster_uuid" -c status -f value)
82         echo "$i: ${CLUSTER_STATUS}"
83
84         case "${CLUSTER_STATUS}" in
85             CREATE_COMPLETE)
86                 echo "INFO: Cluster ${cluster_name} initialized on infrastructure successful."
87                 CLUSTER_SUCCESSFUL=true
88                 break
89             ;;
90             CREATE_FAILED)
91                 reason=$(openstack coe cluster show "${cluster_name}" -f value -c health_status_reason)
92                 echo "ERROR: Failed to initialize infrastructure. Reason: ${reason}"
93                 openstack ceo cluster show "${cluster_name}"
94
95                 echo "INFO: Deleting cluster and re-try to create the cluster again ..."
96                 openstack coe cluster delete "${cluster_name}"
97
98                 # Post delete, poll for 5m to learn if cluster is fully removed
99                 for j in $(seq 20); do
100                     sleep 30
101                     delete_status=$(openstack coe cluster show "${cluster_name}" -f value -c status)
102                     echo "$j: ${delete_status}"
103                     if [[ ${delete_status} == "DELETE_FAILED" ]]; then
104                         reason=$(openstack coe cluster show "${cluster_name}" -f value -c health_status_reason)
105                         echo "ERROR: Failed to delete ${cluster_name}. Reason: ${reason}"
106
107                         echo "INFO: Deleting failed cluster again: ${cluster_name}"
108                         openstack coe cluster delete "${cluster_name}"
109                     fi
110
111                     if ! openstack coe cluster show "${cluster_name}" -f value -c status; then
112                         echo "INFO: Cluster show on ${cluster_name} came back empty. Assuming successful delete"
113                         break
114                     fi
115                 done
116
117                 # If we still see $CLUSTER_NAME in `openstack coe cluster show` this infers the delete hasn't fully
118                 # worked and we can exit forcefully
119                 if openstack coe cluster show "${cluster_name}" -f value -c stack_status; then
120                     echo "ERROR: Cluster ${cluster_name} still in cloud output after polling. Quitting!"
121                     exit 1
122                 fi
123                 break
124             ;;
125             CREATE_IN_PROGRESS)
126                 echo "INFO: Waiting to initialize cluster infrastructure ..."
127                 continue
128             ;;
129             *)
130                 echo "ERROR: Unexpected status: ${OS_STATUS}"
131                 # DO NOT exit on unexpected status. Openstack cluster sometimes returns unexpected status
132                 # before returning an expected status. Just print the message and loop until we have
133                 # a confirmed state or timeout.
134                 # exit 1
135             ;;
136         esac
137     done
138     if $CLUSTER_SUCCESSFUL; then
139         break
140     fi
141 done