list-resources.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash
  2. # Copyright 2015 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. # Calls gcloud to print out a variety of Google Cloud Platform resources used by
  16. # Kubernetes. Can be run before/after test runs and compared to track leaking
  17. # resources.
  18. # PROJECT must be set in the environment.
  19. # If ZONE, KUBE_GCE_INSTANCE_PREFIX, CLUSTER_NAME, KUBE_GCE_NETWORK, or
  20. # KUBE_GKE_NETWORK is set, they will be used to filter the results.
  21. set -o errexit
  22. set -o nounset
  23. set -o pipefail
  24. ZONE=${ZONE:-}
  25. REGION=${ZONE%-*}
  26. INSTANCE_PREFIX=${KUBE_GCE_INSTANCE_PREFIX:-${CLUSTER_NAME:-}}
  27. NETWORK=${KUBE_GCE_NETWORK:-${KUBE_GKE_NETWORK:-}}
  28. # In GKE the instance prefix starts with "gke-".
  29. if [[ "${KUBERNETES_PROVIDER:-}" == "gke" ]]; then
  30. INSTANCE_PREFIX="gke-${CLUSTER_NAME}"
  31. # Truncate to 26 characters for route prefix matching.
  32. INSTANCE_PREFIX="${INSTANCE_PREFIX:0:26}"
  33. fi
  34. # Usage: gcloud-compute-list <resource> <additional parameters to gcloud...>
  35. # GREP_REGEX is applied to the output of gcloud if set
  36. GREP_REGEX=""
  37. function gcloud-compute-list() {
  38. local -r resource=$1
  39. echo -e "\n\n[ ${resource} ]"
  40. local attempt=1
  41. local result=""
  42. while true; do
  43. if result=$(gcloud compute ${resource} list --project=${PROJECT} ${@:2}); then
  44. if [[ ! -z "${GREP_REGEX}" ]]; then
  45. result=$(echo "${result}" | grep "${GREP_REGEX}")
  46. fi
  47. echo "${result}"
  48. return
  49. fi
  50. echo -e "Attempt ${attempt} failed to list ${resource}. Retrying." >&2
  51. attempt=$(($attempt+1))
  52. if [[ ${attempt} > 5 ]]; then
  53. echo -e "List ${resource} failed!" >&2
  54. exit 2
  55. fi
  56. sleep $((5*${attempt}))
  57. done
  58. }
  59. echo "Project: ${PROJECT}"
  60. echo "Region: ${REGION}"
  61. echo "Zone: ${ZONE}"
  62. echo "Instance prefix: ${INSTANCE_PREFIX:-}"
  63. echo "Network: ${NETWORK}"
  64. echo "Provider: ${KUBERNETES_PROVIDER:-}"
  65. # List resources related to instances, filtering by the instance prefix if
  66. # provided.
  67. gcloud-compute-list instance-templates --regexp="${INSTANCE_PREFIX}.*"
  68. gcloud-compute-list instance-groups ${ZONE:+"--zones=${ZONE}"} --regexp="${INSTANCE_PREFIX}.*"
  69. gcloud-compute-list instances ${ZONE:+"--zones=${ZONE}"} --regexp="${INSTANCE_PREFIX}.*"
  70. # List disk resources, filterying by instance prefix if provided.
  71. gcloud-compute-list disks ${ZONE:+"--zones=${ZONE}"} --regexp="${INSTANCE_PREFIX}.*"
  72. # List network resources. We include names starting with "a", corresponding to
  73. # those that Kubernetes creates.
  74. gcloud-compute-list addresses ${REGION:+"--regions=${REGION}"} --regexp="a.*|${INSTANCE_PREFIX}.*"
  75. # Match either the header or a line with the specified e2e network.
  76. # This assumes that the network name is the second field in the output.
  77. GREP_REGEX="^NAME\|^[^ ]\+[ ]\+\(default\|${NETWORK}\) "
  78. gcloud-compute-list routes --regexp="default.*|${INSTANCE_PREFIX}.*"
  79. gcloud-compute-list firewall-rules --regexp="default.*|k8s-fw.*|${INSTANCE_PREFIX}.*"
  80. GREP_REGEX=""
  81. gcloud-compute-list forwarding-rules ${REGION:+"--regions=${REGION}"}
  82. gcloud-compute-list target-pools ${REGION:+"--regions=${REGION}"}