list-resources.sh 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env 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-list <group> <resource> <additional parameters to gcloud...>
  35. # GREP_REGEX is applied to the output of gcloud if set
  36. GREP_REGEX=""
  37. function gcloud-list() {
  38. local -r group=$1
  39. local -r resource=$2
  40. local -r filter=${3:-}
  41. echo -e "\n\n[ ${group} ${resource} ]"
  42. local attempt=1
  43. local result=""
  44. while true; do
  45. if result=$(gcloud "${group}" "${resource}" list --project="${PROJECT}" ${filter:+--filter="$filter"} "${@:4}"); then
  46. if [[ -n "${GREP_REGEX:-}" ]]; then
  47. result=$(echo "${result}" | grep "${GREP_REGEX}" || true)
  48. fi
  49. echo "${result}"
  50. return
  51. fi
  52. echo -e "Attempt ${attempt} failed to list ${resource}. Retrying." >&2
  53. attempt=$((attempt + 1))
  54. if [[ ${attempt} -gt 5 ]]; then
  55. echo -e "List ${resource} failed!" >&2
  56. exit 2
  57. fi
  58. sleep $((5 * attempt))
  59. done
  60. }
  61. echo "Project: ${PROJECT}"
  62. echo "Region: ${REGION}"
  63. echo "Zone: ${ZONE}"
  64. echo "Instance prefix: ${INSTANCE_PREFIX:-}"
  65. echo "Network: ${NETWORK}"
  66. echo "Provider: ${KUBERNETES_PROVIDER:-}"
  67. # List resources related to instances, filtering by the instance prefix if
  68. # provided.
  69. gcloud-list compute instance-templates "name ~ '${INSTANCE_PREFIX}.*'"
  70. gcloud-list compute instance-groups "${ZONE:+"zone:(${ZONE}) AND "}name ~ '${INSTANCE_PREFIX}.*'"
  71. gcloud-list compute instances "${ZONE:+"zone:(${ZONE}) AND "}name ~ '${INSTANCE_PREFIX}.*'"
  72. # List disk resources, filtering by instance prefix if provided.
  73. gcloud-list compute disks "${ZONE:+"zone:(${ZONE}) AND "}name ~ '${INSTANCE_PREFIX}.*'"
  74. # List network resources. We include names starting with "a", corresponding to
  75. # those that Kubernetes creates.
  76. gcloud-list compute addresses "${REGION:+"region=(${REGION}) AND "}name ~ 'a.*|${INSTANCE_PREFIX}.*'"
  77. # Match either the header or a line with the specified e2e network.
  78. # This assumes that the network name is the second field in the output.
  79. GREP_REGEX="^NAME\|^[^ ]\+[ ]\+\(default\|${NETWORK}\) "
  80. gcloud-list compute routes "name ~ 'default.*|${INSTANCE_PREFIX}.*'"
  81. gcloud-list compute firewall-rules "name ~ 'default.*|k8s-fw.*|${INSTANCE_PREFIX}.*'"
  82. GREP_REGEX=""
  83. gcloud-list compute forwarding-rules ${REGION:+"region=(${REGION})"}
  84. gcloud-list compute target-pools ${REGION:+"region=(${REGION})"}
  85. gcloud-list logging sinks