master-helper.sh 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. # Copyright 2016 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. # A library of helper functions and constant for GCI distro
  16. source "${KUBE_ROOT}/cluster/gce/gci/helper.sh"
  17. # create-master-instance creates the master instance. If called with
  18. # an argument, the argument is used as the name to a reserved IP
  19. # address for the master. (In the case of upgrade/repair, we re-use
  20. # the same IP.)
  21. #
  22. # It requires a whole slew of assumed variables, partially due to to
  23. # the call to write-master-env. Listing them would be rather
  24. # futile. Instead, we list the required calls to ensure any additional
  25. #
  26. # variables are set:
  27. # ensure-temp-dir
  28. # detect-project
  29. # get-bearer-token
  30. function create-master-instance {
  31. local address_opt=""
  32. [[ -n ${1:-} ]] && address_opt="--address ${1}"
  33. write-master-env
  34. ensure-gci-metadata-files
  35. create-master-instance-internal "${MASTER_NAME}" "${address_opt}"
  36. }
  37. function replicate-master-instance() {
  38. local existing_master_zone="${1}"
  39. local existing_master_name="${2}"
  40. local existing_master_replicas="${3}"
  41. local kube_env="$(get-metadata "${existing_master_zone}" "${existing_master_name}" kube-env)"
  42. # Substitute INITIAL_ETCD_CLUSTER to enable etcd clustering.
  43. kube_env="$(echo "${kube_env}" | grep -v "INITIAL_ETCD_CLUSTER")"
  44. kube_env="$(echo -e "${kube_env}\nINITIAL_ETCD_CLUSTER: '${existing_master_replicas},${REPLICA_NAME}'")"
  45. echo "${kube_env}" > ${KUBE_TEMP}/master-kube-env.yaml
  46. get-metadata "${existing_master_zone}" "${existing_master_name}" cluster-name > "${KUBE_TEMP}/cluster-name.txt"
  47. get-metadata "${existing_master_zone}" "${existing_master_name}" gci-update-strategy > "${KUBE_TEMP}/gci-update.txt"
  48. get-metadata "${existing_master_zone}" "${existing_master_name}" gci-ensure-gke-docker > "${KUBE_TEMP}/gci-ensure-gke-docker.txt"
  49. get-metadata "${existing_master_zone}" "${existing_master_name}" gci-docker-version > "${KUBE_TEMP}/gci-docker-version.txt"
  50. create-master-instance-internal "${REPLICA_NAME}"
  51. }
  52. function create-master-instance-internal() {
  53. local -r master_name="${1}"
  54. local -r address_option="${2:-}"
  55. gcloud compute instances create "${master_name}" \
  56. ${address_option} \
  57. --project "${PROJECT}" \
  58. --zone "${ZONE}" \
  59. --machine-type "${MASTER_SIZE}" \
  60. --image-project="${MASTER_IMAGE_PROJECT}" \
  61. --image "${MASTER_IMAGE}" \
  62. --tags "${MASTER_TAG}" \
  63. --network "${NETWORK}" \
  64. --scopes "storage-ro,compute-rw,monitoring,logging-write" \
  65. --can-ip-forward \
  66. --metadata-from-file \
  67. "kube-env=${KUBE_TEMP}/master-kube-env.yaml,user-data=${KUBE_ROOT}/cluster/gce/gci/master.yaml,configure-sh=${KUBE_ROOT}/cluster/gce/gci/configure.sh,cluster-name=${KUBE_TEMP}/cluster-name.txt,gci-update-strategy=${KUBE_TEMP}/gci-update.txt,gci-ensure-gke-docker=${KUBE_TEMP}/gci-ensure-gke-docker.txt,gci-docker-version=${KUBE_TEMP}/gci-docker-version.txt" \
  68. --disk "name=${master_name}-pd,device-name=master-pd,mode=rw,boot=no,auto-delete=no" \
  69. --boot-disk-size "${MASTER_ROOT_DISK_SIZE:-10}"
  70. }
  71. function get-metadata() {
  72. local zone="${1}"
  73. local name="${2}"
  74. local key="${3}"
  75. gcloud compute ssh "${name}" \
  76. --project "${PROJECT}" \
  77. --zone "${zone}" \
  78. --command "curl \"http://metadata.google.internal/computeMetadata/v1/instance/attributes/${key}\" -H \"Metadata-Flavor: Google\"" 2>/dev/null
  79. }