util.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env bash
  2. # Copyright 2017 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. source "${KUBE_ROOT}/test/kubemark/common/util.sh"
  17. # Wrapper for gcloud compute, running it $RETRIES times in case of failures.
  18. # Args:
  19. # $@: all stuff that goes after 'gcloud compute'
  20. function run-gcloud-compute-with-retries {
  21. run-cmd-with-retries gcloud compute "$@"
  22. }
  23. function authenticate-docker {
  24. echo "Configuring registry authentication"
  25. mkdir -p "${HOME}/.docker"
  26. gcloud beta auth configure-docker -q
  27. }
  28. function create-kubemark-master {
  29. # We intentionally override env vars in subshell to preserve original values.
  30. # shellcheck disable=SC2030,SC2031
  31. (
  32. # All calls to e2e-grow-cluster must share temp dir with initial e2e-up.sh.
  33. kube::util::ensure-temp-dir
  34. export KUBE_TEMP="${KUBE_TEMP}"
  35. export KUBECONFIG="${RESOURCE_DIRECTORY}/kubeconfig.kubemark"
  36. export KUBECONFIG_INTERNAL="${RESOURCE_DIRECTORY}/kubeconfig-internal.kubemark"
  37. export CLUSTER_NAME="${CLUSTER_NAME}-kubemark"
  38. export KUBE_CREATE_NODES=false
  39. export KUBE_GCE_INSTANCE_PREFIX="${KUBE_GCE_INSTANCE_PREFIX}-kubemark"
  40. # Quite tricky cidr setup: we set KUBE_GCE_ENABLE_IP_ALIASES=true to avoid creating
  41. # cloud routes and RangeAllocator to assign cidrs by kube-controller-manager.
  42. export KUBE_GCE_ENABLE_IP_ALIASES=true
  43. export KUBE_GCE_NODE_IPAM_MODE=RangeAllocator
  44. # Disable all addons. They are running outside of the kubemark cluster.
  45. export KUBE_ENABLE_CLUSTER_AUTOSCALER=false
  46. export KUBE_ENABLE_CLUSTER_DNS=false
  47. export KUBE_ENABLE_NODE_LOGGING=false
  48. export KUBE_ENABLE_METRICS_SERVER=false
  49. export KUBE_ENABLE_L7_LOADBALANCING="none"
  50. # Unset env variables set by kubetest for 'root cluster'. We need recompute them
  51. # for kubemark master.
  52. # TODO(mborsz): Figure out some better way to filter out such env variables than
  53. # listing them here.
  54. unset MASTER_SIZE MASTER_DISK_SIZE MASTER_ROOT_DISK_SIZE
  55. # Set kubemark-specific overrides:
  56. # for each defined env KUBEMARK_X=Y call export X=Y.
  57. for var in ${!KUBEMARK_*}; do
  58. dst_var=${var#KUBEMARK_}
  59. val=${!var}
  60. echo "Setting ${dst_var} to '${val}'"
  61. export "${dst_var}"="${val}"
  62. done
  63. "${KUBE_ROOT}/hack/e2e-internal/e2e-up.sh"
  64. if [[ "${KUBEMARK_HA_MASTER:-}" == "true" && -n "${KUBEMARK_MASTER_ADDITIONAL_ZONES:-}" ]]; then
  65. for KUBE_GCE_ZONE in ${KUBEMARK_MASTER_ADDITIONAL_ZONES}; do
  66. KUBE_GCE_ZONE="${KUBE_GCE_ZONE}" KUBE_REPLICATE_EXISTING_MASTER=true \
  67. "${KUBE_ROOT}/hack/e2e-internal/e2e-grow-cluster.sh"
  68. done
  69. fi
  70. # The e2e-up.sh script is not sourced, so we don't have access to variables that
  71. # it sets. Instead, we read data which was written to the KUBE_TEMP directory.
  72. # The cluster-location is either ZONE (say us-east1-a) or REGION (say us-east1).
  73. # To get REGION from location, only first two parts are matched.
  74. REGION=$(grep -o "^[a-z]*-[a-z0-9]*" "${KUBE_TEMP}"/cluster-location.txt)
  75. MASTER_NAME="${KUBE_GCE_INSTANCE_PREFIX}"-master
  76. if [[ ${GCE_PRIVATE_CLUSTER:-} == "true" ]]; then
  77. MASTER_INTERNAL_IP=$(gcloud compute addresses describe "${MASTER_NAME}-internal-ip" \
  78. --project "${PROJECT}" --region "${REGION}" -q --format='value(address)')
  79. fi
  80. MASTER_IP=$(gcloud compute addresses describe "${MASTER_NAME}-ip" \
  81. --project "${PROJECT}" --region "${REGION}" -q --format='value(address)')
  82. # If cluster uses private master IP, two kubeconfigs are created:
  83. # - kubeconfig with public IP, which will be used to connect to the cluster
  84. # from outside of the cluster network
  85. # - kubeconfig with private IP (called internal kubeconfig), which will be
  86. # used to create hollow nodes.
  87. #
  88. # Note that hollow nodes might use either of these kubeconfigs, but
  89. # using internal one is better from performance and cost perspective, since
  90. # traffic does not need to go through Cloud NAT.
  91. if [[ -n "${MASTER_INTERNAL_IP:-}" ]]; then
  92. echo "Writing internal kubeconfig to '${KUBECONFIG_INTERNAL}'"
  93. ip_regexp=${MASTER_IP//./\\.} # escape ".", so that sed won't treat it as "any char"
  94. sed "s/${ip_regexp}/${MASTER_INTERNAL_IP}/g" "${KUBECONFIG}" > "${KUBECONFIG_INTERNAL}"
  95. fi
  96. )
  97. }
  98. function delete-kubemark-master {
  99. # We intentionally override env vars in subshell to preserve original values.
  100. # shellcheck disable=SC2030,SC2031
  101. (
  102. export CLUSTER_NAME="${CLUSTER_NAME}-kubemark"
  103. export KUBE_GCE_INSTANCE_PREFIX="${KUBE_GCE_INSTANCE_PREFIX}-kubemark"
  104. export KUBE_DELETE_NETWORK=false
  105. if [[ "${KUBEMARK_HA_MASTER:-}" == "true" && -n "${KUBEMARK_MASTER_ADDITIONAL_ZONES:-}" ]]; then
  106. for KUBE_GCE_ZONE in ${KUBEMARK_MASTER_ADDITIONAL_ZONES}; do
  107. KUBE_GCE_ZONE="${KUBE_GCE_ZONE}" KUBE_REPLICATE_EXISTING_MASTER=true \
  108. "${KUBE_ROOT}/hack/e2e-internal/e2e-shrink-cluster.sh"
  109. done
  110. fi
  111. "${KUBE_ROOT}/hack/e2e-internal/e2e-down.sh"
  112. )
  113. }
  114. function calculate-node-labels {
  115. echo "cloud.google.com/metadata-proxy-ready=true"
  116. }