util.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #!/usr/bin/env bash
  2. # Copyright 2018 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/../../..
  16. # Creates a new kube-spawn cluster
  17. function create-clusters {
  18. # shellcheck disable=SC2154 # Color defined in sourced script
  19. echo -e "${color_yellow}CHECKING CLUSTERS${color_norm}"
  20. if ibmcloud ks clusters | grep -Fq 'deleting'; then
  21. echo -n "Deleting old clusters"
  22. fi
  23. while ibmcloud ks clusters | grep -Fq 'deleting'
  24. do
  25. echo -n "."
  26. sleep 10
  27. done
  28. echo ""
  29. ibmcloud ks region-set us-east >/dev/null
  30. ibmcloud ks vlans wdc06 >/dev/null
  31. PRIVLAN=$(ibmcloud ks vlans wdc06 --json | jq '. | .[] | select(.type == "private") | .id' | sed -e "s/\"//g")
  32. PUBVLAN=$(ibmcloud ks vlans wdc06 --json | jq '. | .[] | select(.type == "public") | .id' | sed -e "s/\"//g")
  33. if ! ibmcloud ks clusters | grep -Fq 'kubeSpawnTester'; then
  34. echo "Creating spawning cluster"
  35. # make number and spec of node workers configurable
  36. # otherwise it can't afford tests like kubemark-5000
  37. # TODO: dynamically adjust the number and spec
  38. ibmcloud ks cluster-create --location "${CLUSTER_LOCATION}" --public-vlan "${PUBVLAN}" --private-vlan "${PRIVLAN}" --workers "${NUM_NODES:-2}" --machine-type "${NODE_SIZE}" --name kubeSpawnTester
  39. fi
  40. if ! ibmcloud ks clusters | grep -Fq 'kubeMasterTester'; then
  41. echo "Creating master cluster"
  42. # if we can't make it a bare master (workers = 0)
  43. # then make workers = 1 with the smallest machine spec
  44. ibmcloud ks cluster-create --location "${CLUSTER_LOCATION}" --public-vlan "${PUBVLAN}" --private-vlan "${PRIVLAN}" --workers 1 --machine-type u2c.2x4 --name kubeMasterTester
  45. fi
  46. push-image
  47. if ! ibmcloud ks clusters | grep 'kubeSpawnTester' | grep -Fq 'normal'; then
  48. # shellcheck disable=SC2154 # Color defined in sourced script
  49. echo -e "${color_cyan}Warning: new clusters may take up to 60 minutes to be ready${color_norm}"
  50. echo -n "Clusters loading"
  51. fi
  52. while ! ibmcloud ks clusters | grep 'kubeSpawnTester' | grep -Fq 'normal'
  53. do
  54. echo -n "."
  55. sleep 5
  56. done
  57. while ! ibmcloud ks clusters | grep 'kubeMasterTester' | grep -Fq 'normal'
  58. do
  59. echo -n "."
  60. sleep 5
  61. done
  62. echo -e "${color_yellow}CLUSTER CREATION COMPLETE${color_norm}"
  63. }
  64. # Builds and pushes image to registry
  65. function push-image {
  66. if [[ "${ISBUILD}" = "y" ]]; then
  67. if ! ibmcloud cr namespaces | grep -Fq "${KUBE_NAMESPACE}"; then
  68. echo "Creating registry namespace"
  69. ibmcloud cr namespace-add "${KUBE_NAMESPACE}"
  70. echo "ibmcloud cr namespace-rm ${KUBE_NAMESPACE}" >> "${RESOURCE_DIRECTORY}/iks-namespacelist.sh"
  71. fi
  72. docker build -t "${KUBEMARK_INIT_TAG}" "${KUBEMARK_IMAGE_LOCATION}"
  73. docker tag "${KUBEMARK_INIT_TAG}" "${KUBEMARK_IMAGE_REGISTRY}${KUBE_NAMESPACE}/${PROJECT}:${KUBEMARK_IMAGE_TAG}"
  74. docker push "${KUBEMARK_IMAGE_REGISTRY}${KUBE_NAMESPACE}/${PROJECT}:${KUBEMARK_IMAGE_TAG}"
  75. echo "Image pushed"
  76. else
  77. KUBEMARK_IMAGE_REGISTRY="${KUBEMARK_IMAGE_REGISTRY:-brandondr96}"
  78. KUBE_NAMESPACE=""
  79. fi
  80. }
  81. # Allow user to use existing clusters if desired
  82. function choose-clusters {
  83. echo -n -e "Do you want to use custom clusters? [y/N]${color_cyan}>${color_norm} "
  84. read -r USE_EXISTING
  85. if [[ "${USE_EXISTING}" = "y" ]]; then
  86. echo -e "${color_yellow}Enter path for desired hollow-node spawning cluster kubeconfig file:${color_norm}"
  87. read -r CUSTOM_SPAWN_CONFIG
  88. echo -e "${color_yellow}Enter path for desired hollow-node hosting cluster kubeconfig file:${color_norm}"
  89. read -r CUSTOM_MASTER_CONFIG
  90. push-image
  91. elif [[ "${USE_EXISTING}" = "N" ]]; then
  92. create-clusters
  93. else
  94. # shellcheck disable=SC2154 # Color defined in sourced script
  95. echo -e "${color_red}Invalid response, please try again:${color_norm}"
  96. choose-clusters
  97. fi
  98. }
  99. # Ensure secrets are correctly set
  100. function set-registry-secrets {
  101. spawn-config
  102. kubectl get secret bluemix-default-secret-regional -o yaml | sed 's/default/kubemark/g' | kubectl -n kubemark create -f -
  103. kubectl patch serviceaccount -n kubemark default -p '{"imagePullSecrets": [{"name": "bluemix-kubemark-secret"}]}'
  104. kubectl -n kubemark get serviceaccounts default -o json | jq 'del(.metadata.resourceVersion)' | jq 'setpath(["imagePullSecrets"];[{"name":"bluemix-kubemark-secret-regional"}])' | kubectl -n kubemark replace serviceaccount default -f -
  105. }
  106. # Sets the hollow-node master
  107. # Exported variables:
  108. # MASTER_IP - IP Address of the Kubemark master
  109. function set-hollow-master {
  110. echo -e "${color_yellow}CONFIGURING MASTER${color_norm}"
  111. master-config
  112. MASTER_IP=$(grep server "$KUBECONFIG" | awk -F "/" '{print $3}')
  113. export MASTER_IP
  114. }
  115. # Set up master cluster environment
  116. # Exported variables:
  117. # KUBECONFIG - Overrides default kube config for the purpose of setting up the Kubemark master components.
  118. function master-config {
  119. if [[ "${USE_EXISTING}" = "y" ]]; then
  120. export KUBECONFIG=${CUSTOM_MASTER_CONFIG}
  121. else
  122. eval "$(ibmcloud ks cluster-config kubeMasterTester --admin | grep export)"
  123. fi
  124. }
  125. # Set up spawn cluster environment
  126. # Exported variables:
  127. # KUBECONFIG - Overrides default kube config for the purpose of setting up the hollow-node cluster.
  128. function spawn-config {
  129. if [[ "${USE_EXISTING}" = "y" ]]; then
  130. export KUBECONFIG=${CUSTOM_SPAWN_CONFIG}
  131. else
  132. eval "$(ibmcloud ks cluster-config kubeSpawnTester --admin | grep export)"
  133. fi
  134. }
  135. # Deletes existing clusters
  136. function delete-clusters {
  137. echo "DELETING CLUSTERS"
  138. ibmcloud ks cluster-rm kubeSpawnTester
  139. ibmcloud ks cluster-rm kubeMasterTester
  140. while ! ibmcloud ks clusters | grep 'kubeSpawnTester' | grep -Fq 'deleting'
  141. do
  142. sleep 5
  143. done
  144. while ! ibmcloud ks clusters | grep 'kubeMasterTester' | grep -Fq 'deleting'
  145. do
  146. sleep 5
  147. done
  148. kubectl delete ns kubemark
  149. }
  150. # Login to cloud services
  151. function complete-login {
  152. echo -e "${color_yellow}LOGGING INTO CLOUD SERVICES${color_norm}"
  153. echo -n -e "Do you have a federated IBM cloud login? [y/N]${color_cyan}>${color_norm} "
  154. read -r ISFED
  155. if [[ "${ISFED}" = "y" ]]; then
  156. ibmcloud login --sso -a "${REGISTRY_LOGIN_URL}"
  157. elif [[ "${ISFED}" = "N" ]]; then
  158. ibmcloud login -a "${REGISTRY_LOGIN_URL}"
  159. else
  160. echo -e "${color_red}Invalid response, please try again:${color_norm}"
  161. complete-login
  162. fi
  163. ibmcloud cr login
  164. }
  165. # Generate values to fill the hollow-node configuration templates.
  166. # Exported variables:
  167. # KUBECTL - The name or path to the kubernetes client binary.
  168. # TEST_CLUSTER_API_CONTENT_TYPE - Defines the content-type of the requests used by the Kubemark components.
  169. function generate-values {
  170. echo "Generating values"
  171. master-config
  172. KUBECTL=kubectl
  173. export KUBECTL
  174. KUBEMARK_DIRECTORY="${KUBE_ROOT}/test/kubemark"
  175. RESOURCE_DIRECTORY="${KUBEMARK_DIRECTORY}/resources"
  176. TEST_CLUSTER_API_CONTENT_TYPE="bluemix" #Determine correct usage of this
  177. export TEST_CLUSTER_API_CONTENT_TYPE
  178. CONFIGPATH=${KUBECONFIG%/*}
  179. KUBELET_CERT_BASE64="${KUBELET_CERT_BASE64:-$(base64 "${CONFIGPATH}/admin.pem" | tr -d '\r\n')}"
  180. KUBELET_KEY_BASE64="${KUBELET_KEY_BASE64:-$(base64 "${CONFIGPATH}/admin-key.pem" | tr -d '\r\n')}"
  181. CA_CERT_BASE64="${CA_CERT_BASE64:-$( base64 "$(find "${CONFIGPATH}" -name "*ca*" | head -n 1)" | tr -d '\r\n')}"
  182. }
  183. # Build image for kubemark
  184. function build-kubemark-image {
  185. echo -n -e "Do you want to build the kubemark image? [y/N]${color_cyan}>${color_norm} "
  186. read -r ISBUILD
  187. if [[ "${ISBUILD}" = "y" ]]; then
  188. echo -e "${color_yellow}BUILDING IMAGE${color_norm}"
  189. "${KUBE_ROOT}/build/run.sh" make kubemark
  190. cp "${KUBE_ROOT}/_output/dockerized/bin/linux/amd64/kubemark" "${KUBEMARK_IMAGE_LOCATION}"
  191. elif [[ "${ISBUILD}" = "N" ]]; then
  192. echo -n ""
  193. else
  194. echo -e "${color_red}Invalid response, please try again:${color_norm}"
  195. build-kubemark-image
  196. fi
  197. }
  198. # Clean up repository
  199. function clean-repo {
  200. echo -n -e "Do you want to remove build output and binary? [y/N]${color_cyan}>${color_norm} "
  201. read -r ISCLEAN
  202. if [[ "${ISCLEAN}" = "y" ]]; then
  203. echo -e "${color_yellow}CLEANING REPO${color_norm}"
  204. rm -rf "${KUBE_ROOT}/_output"
  205. rm -f "${KUBEMARK_IMAGE_LOCATION}/kubemark"
  206. elif [[ "${ISCLEAN}" = "N" ]]; then
  207. echo -n ""
  208. else
  209. echo -e "${color_red}Invalid response, please try again:${color_norm}"
  210. clean-repo
  211. fi
  212. }