lib.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/bin/bash
  2. # Copyright 2018 Istio 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. # Output a message, with a timestamp matching istio log format
  16. function log() {
  17. echo -e "$(date -u '+%Y-%m-%dT%H:%M:%S.%NZ')\t$*"
  18. }
  19. # Trace runs the provided command and records additional timing information
  20. # NOTE: to avoid spamming the logs, we disable xtrace and re-enable it before executing the function
  21. # and after completion. If xtrace was never set, this will result in xtrace being enabled.
  22. # Ideally we would restore the old xtrace setting, but I don't think its possible to do that without also log-spamming
  23. # If we need to call it from a context without xtrace we can just make a new function.
  24. function trace() {
  25. { set +x; } 2>/dev/null
  26. log "Running '${1}'"
  27. start="$(date -u +%s)"
  28. { set -x; } 2>/dev/null
  29. "${@:2}"
  30. { set +x; } 2>/dev/null
  31. end="$(date -u +%s)"
  32. elapsed=$((end - start))
  33. log "Command '${1}' complete in ${elapsed}s"
  34. # Write to YAML file as well for easy reading by tooling
  35. echo "'${1}': $elapsed" >> "${ARTIFACTS}/trace.yaml"
  36. { set -x; } 2>/dev/null
  37. }
  38. function setup_and_export_git_sha() {
  39. if [[ -n "${CI:-}" ]]; then
  40. if [ -z "${PULL_PULL_SHA:-}" ]; then
  41. if [ -z "${PULL_BASE_SHA:-}" ]; then
  42. GIT_SHA="$(git rev-parse --verify HEAD)"
  43. export GIT_SHA
  44. else
  45. export GIT_SHA="${PULL_BASE_SHA}"
  46. fi
  47. else
  48. export GIT_SHA="${PULL_PULL_SHA}"
  49. fi
  50. else
  51. # Use the current commit.
  52. GIT_SHA="$(git rev-parse --verify HEAD)"
  53. export GIT_SHA
  54. fi
  55. GIT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
  56. export GIT_BRANCH
  57. }
  58. # Creates a local registry for kind nodes to pull images from. Expects that the "kind" network already exists.
  59. function setup_kind_registry() {
  60. # create a registry container if it not running already
  61. running="$(docker inspect -f '{{.State.Running}}' "${KIND_REGISTRY_NAME}" 2>/dev/null || true)"
  62. if [[ "${running}" != 'true' ]]; then
  63. docker run \
  64. -d --restart=always -p "${KIND_REGISTRY_PORT}:5000" --name "${KIND_REGISTRY_NAME}" \
  65. registry:2
  66. # Allow kind nodes to reach the registry
  67. docker network connect "kind" "${KIND_REGISTRY_NAME}"
  68. fi
  69. # https://docs.tilt.dev/choosing_clusters.html#discovering-the-registry
  70. for cluster in $(kind get clusters); do
  71. # TODO get context/config from existing variables
  72. kind export kubeconfig --name="${cluster}"
  73. for node in $(kind get nodes --name="${cluster}"); do
  74. kubectl annotate node "${node}" "kind.x-k8s.io/registry=localhost:${KIND_REGISTRY_PORT}" --overwrite;
  75. done
  76. done
  77. }
  78. # The setup_cluster_reg is used to set up a cluster registry for multicluster testing
  79. function setup_cluster_reg () {
  80. MAIN_CONFIG=""
  81. for context in "${CLUSTERREG_DIR}"/*; do
  82. if [[ -z "${MAIN_CONFIG}" ]]; then
  83. MAIN_CONFIG="${context}"
  84. fi
  85. export KUBECONFIG="${context}"
  86. kubectl delete ns istio-system-multi --ignore-not-found
  87. kubectl delete clusterrolebinding istio-multi-test --ignore-not-found
  88. kubectl create ns istio-system-multi
  89. kubectl create sa istio-multi-test -n istio-system-multi
  90. kubectl create clusterrolebinding istio-multi-test --clusterrole=cluster-admin --serviceaccount=istio-system-multi:istio-multi-test
  91. CLUSTER_NAME=$(kubectl config view --minify=true -o "jsonpath={.clusters[].name}")
  92. gen_kubeconf_from_sa istio-multi-test "${context}"
  93. done
  94. export KUBECONFIG="${MAIN_CONFIG}"
  95. }
  96. function gen_kubeconf_from_sa () {
  97. local service_account=$1
  98. local filename=$2
  99. SERVER=$(kubectl config view --minify=true -o "jsonpath={.clusters[].cluster.server}")
  100. SECRET_NAME=$(kubectl get sa "${service_account}" -n istio-system-multi -o jsonpath='{.secrets[].name}')
  101. CA_DATA=$(kubectl get secret "${SECRET_NAME}" -n istio-system-multi -o "jsonpath={.data['ca\\.crt']}")
  102. TOKEN=$(kubectl get secret "${SECRET_NAME}" -n istio-system-multi -o "jsonpath={.data['token']}" | base64 --decode)
  103. cat <<EOF > "${filename}"
  104. apiVersion: v1
  105. clusters:
  106. - cluster:
  107. certificate-authority-data: ${CA_DATA}
  108. server: ${SERVER}
  109. name: ${CLUSTER_NAME}
  110. contexts:
  111. - context:
  112. cluster: ${CLUSTER_NAME}
  113. user: ${CLUSTER_NAME}
  114. name: ${CLUSTER_NAME}
  115. current-context: ${CLUSTER_NAME}
  116. kind: Config
  117. preferences: {}
  118. users:
  119. - name: ${CLUSTER_NAME}
  120. user:
  121. token: ${TOKEN}
  122. EOF
  123. }
  124. # Gives a copy of a given topology JSON editing the given key on the entry with the given cluster name
  125. function set_topology_value() {
  126. local JSON="$1"
  127. local CLUSTER_NAME="$2"
  128. local KEY="$3"
  129. local VALUE="$4"
  130. VALUE=$(echo "${VALUE}" | awk '{$1=$1};1')
  131. echo "${JSON}" | jq '(.[] | select(.clusterName =="'"${CLUSTER_NAME}"'") | .'"${KEY}"') |="'"${VALUE}"'"'
  132. }