...
1#!/usr/bin/env bash
2
3# Copyright 2015 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Calls gcloud to print out a variety of Google Cloud Platform resources used by
18# Kubernetes. Can be run before/after test runs and compared to track leaking
19# resources.
20
21# PROJECT must be set in the environment.
22# If ZONE, KUBE_GCE_INSTANCE_PREFIX, CLUSTER_NAME, KUBE_GCE_NETWORK, or
23# KUBE_GKE_NETWORK is set, they will be used to filter the results.
24
25set -o errexit
26set -o nounset
27set -o pipefail
28
29ZONE=${ZONE:-}
30REGION=${ZONE%-*}
31INSTANCE_PREFIX=${KUBE_GCE_INSTANCE_PREFIX:-${CLUSTER_NAME:-}}
32NETWORK=${KUBE_GCE_NETWORK:-${KUBE_GKE_NETWORK:-default}}
33
34# In GKE the instance prefix starts with "gke-".
35if [[ "${KUBERNETES_PROVIDER:-}" == "gke" ]]; then
36 INSTANCE_PREFIX="gke-${CLUSTER_NAME}"
37 # Truncate to 26 characters for route prefix matching.
38 INSTANCE_PREFIX="${INSTANCE_PREFIX:0:26}"
39fi
40
41# Usage: gcloud-list <group> <resource> <additional parameters to gcloud...>
42# GREP_REGEX is applied to the output of gcloud if set
43GREP_REGEX=""
44function gcloud-list() {
45 local -r group=$1
46 local -r resource=$2
47 local -r filter=${3:-}
48 echo -e "\n\n[ ${group} ${resource} ]"
49 local attempt=1
50 local result=""
51 while true; do
52 if result=$(gcloud "${group}" "${resource}" list --project="${PROJECT}" ${filter:+--filter="$filter"} "${@:4}"); then
53 if [[ -n "${GREP_REGEX:-}" ]]; then
54 result=$(echo "${result}" | grep "${GREP_REGEX}" || true)
55 fi
56 echo "${result}"
57 return
58 fi
59 echo -e "Attempt ${attempt} failed to list ${resource}. Retrying." >&2
60 attempt=$((attempt + 1))
61 if [[ ${attempt} -gt 5 ]]; then
62 echo -e "List ${resource} failed!" >&2
63 exit 2
64 fi
65 sleep $((5 * attempt))
66 done
67}
68
69echo "Project: ${PROJECT}"
70echo "Region: ${REGION}"
71echo "Zone: ${ZONE}"
72echo "Instance prefix: ${INSTANCE_PREFIX:-}"
73echo "Network: ${NETWORK}"
74echo "Provider: ${KUBERNETES_PROVIDER:-}"
75
76# List resources related to instances, filtering by the instance prefix if
77# provided.
78
79set +e # do not stop on error
80
81gcloud-list compute instance-templates "name ~ '${INSTANCE_PREFIX}.*'"
82gcloud-list compute instance-groups "${ZONE:+"zone:(${ZONE}) AND "}name ~ '${INSTANCE_PREFIX}.*'"
83gcloud-list compute instances "${ZONE:+"zone:(${ZONE}) AND "}name ~ '${INSTANCE_PREFIX}.*'"
84
85# List disk resources, filtering by instance prefix if provided.
86gcloud-list compute disks "${ZONE:+"zone:(${ZONE}) AND "}name ~ '${INSTANCE_PREFIX}.*'"
87
88# List network resources. We include names starting with "a", corresponding to
89# those that Kubernetes creates.
90gcloud-list compute addresses "${REGION:+"region=(${REGION}) AND "}name ~ 'a.*|${INSTANCE_PREFIX}.*'"
91# Match either the header or a line with the specified e2e network.
92# This assumes that the network name is the second field in the output.
93GREP_REGEX="^NAME\|^[^ ]\+[ ]\+\(${NETWORK}\) "
94gcloud-list compute routes "name ~ 'default.*|${INSTANCE_PREFIX}.*'"
95gcloud-list compute firewall-rules "name ~ 'default.*|k8s-fw.*|${INSTANCE_PREFIX}.*'"
96GREP_REGEX=""
97gcloud-list compute forwarding-rules ${REGION:+"region=(${REGION})"}
98gcloud-list compute target-pools ${REGION:+"region=(${REGION})"}
99
100gcloud-list logging sinks
101
102set -e
View as plain text