2018-04-16 16:31:44 +00:00
|
|
|
#!/usr/bin/env bash
|
2015-10-06 21:53:38 +00:00
|
|
|
|
2016-06-03 00:25:58 +00:00
|
|
|
# Copyright 2015 The Kubernetes Authors.
|
2015-10-06 21:53:38 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
# Calls gcloud to print out a variety of Google Cloud Platform resources used by
|
|
|
|
# Kubernetes. Can be run before/after test runs and compared to track leaking
|
|
|
|
# resources.
|
|
|
|
|
|
|
|
# PROJECT must be set in the environment.
|
|
|
|
# If ZONE, KUBE_GCE_INSTANCE_PREFIX, CLUSTER_NAME, KUBE_GCE_NETWORK, or
|
|
|
|
# KUBE_GKE_NETWORK is set, they will be used to filter the results.
|
|
|
|
|
|
|
|
set -o errexit
|
|
|
|
set -o nounset
|
|
|
|
set -o pipefail
|
|
|
|
|
|
|
|
ZONE=${ZONE:-}
|
|
|
|
REGION=${ZONE%-*}
|
|
|
|
INSTANCE_PREFIX=${KUBE_GCE_INSTANCE_PREFIX:-${CLUSTER_NAME:-}}
|
|
|
|
NETWORK=${KUBE_GCE_NETWORK:-${KUBE_GKE_NETWORK:-}}
|
|
|
|
|
2016-01-26 05:28:54 +00:00
|
|
|
# In GKE the instance prefix starts with "gke-".
|
|
|
|
if [[ "${KUBERNETES_PROVIDER:-}" == "gke" ]]; then
|
|
|
|
INSTANCE_PREFIX="gke-${CLUSTER_NAME}"
|
|
|
|
# Truncate to 26 characters for route prefix matching.
|
|
|
|
INSTANCE_PREFIX="${INSTANCE_PREFIX:0:26}"
|
|
|
|
fi
|
|
|
|
|
2018-03-16 23:22:46 +00:00
|
|
|
# Usage: gcloud-list <group> <resource> <additional parameters to gcloud...>
|
2015-10-06 21:53:38 +00:00
|
|
|
# GREP_REGEX is applied to the output of gcloud if set
|
|
|
|
GREP_REGEX=""
|
2018-03-16 23:22:46 +00:00
|
|
|
function gcloud-list() {
|
|
|
|
local -r group=$1
|
|
|
|
local -r resource=$2
|
|
|
|
local -r filter=${3:-}
|
|
|
|
echo -e "\n\n[ ${group} ${resource} ]"
|
2015-10-23 20:57:13 +00:00
|
|
|
local attempt=1
|
|
|
|
local result=""
|
|
|
|
while true; do
|
2019-04-25 15:47:30 +00:00
|
|
|
if result=$(gcloud "${group}" "${resource}" list --project="${PROJECT}" ${filter:+--filter="$filter"} "${@:4}"); then
|
2019-04-20 00:03:29 +00:00
|
|
|
if [[ -n "${GREP_REGEX:-}" ]]; then
|
2017-02-22 22:55:57 +00:00
|
|
|
result=$(echo "${result}" | grep "${GREP_REGEX}" || true)
|
2016-04-08 19:19:24 +00:00
|
|
|
fi
|
2015-12-08 19:27:49 +00:00
|
|
|
echo "${result}"
|
2015-10-23 20:57:13 +00:00
|
|
|
return
|
|
|
|
fi
|
2016-01-16 01:24:42 +00:00
|
|
|
echo -e "Attempt ${attempt} failed to list ${resource}. Retrying." >&2
|
2019-04-20 00:03:29 +00:00
|
|
|
attempt=$((attempt + 1))
|
2018-02-02 09:17:01 +00:00
|
|
|
if [[ ${attempt} -gt 5 ]]; then
|
2016-01-16 01:24:42 +00:00
|
|
|
echo -e "List ${resource} failed!" >&2
|
2015-10-23 20:57:13 +00:00
|
|
|
exit 2
|
|
|
|
fi
|
2019-04-20 00:03:29 +00:00
|
|
|
sleep $((5 * attempt))
|
2015-10-23 20:57:13 +00:00
|
|
|
done
|
2015-10-06 21:53:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
echo "Project: ${PROJECT}"
|
|
|
|
echo "Region: ${REGION}"
|
|
|
|
echo "Zone: ${ZONE}"
|
|
|
|
echo "Instance prefix: ${INSTANCE_PREFIX:-}"
|
2016-01-26 05:28:54 +00:00
|
|
|
echo "Network: ${NETWORK}"
|
|
|
|
echo "Provider: ${KUBERNETES_PROVIDER:-}"
|
2015-10-06 21:53:38 +00:00
|
|
|
|
|
|
|
# List resources related to instances, filtering by the instance prefix if
|
|
|
|
# provided.
|
2018-03-16 23:22:46 +00:00
|
|
|
gcloud-list compute instance-templates "name ~ '${INSTANCE_PREFIX}.*'"
|
|
|
|
gcloud-list compute instance-groups "${ZONE:+"zone:(${ZONE}) AND "}name ~ '${INSTANCE_PREFIX}.*'"
|
|
|
|
gcloud-list compute instances "${ZONE:+"zone:(${ZONE}) AND "}name ~ '${INSTANCE_PREFIX}.*'"
|
2015-10-06 21:53:38 +00:00
|
|
|
|
2017-08-09 16:45:44 +00:00
|
|
|
# List disk resources, filtering by instance prefix if provided.
|
2018-03-16 23:22:46 +00:00
|
|
|
gcloud-list compute disks "${ZONE:+"zone:(${ZONE}) AND "}name ~ '${INSTANCE_PREFIX}.*'"
|
2015-10-06 21:53:38 +00:00
|
|
|
|
|
|
|
# List network resources. We include names starting with "a", corresponding to
|
|
|
|
# those that Kubernetes creates.
|
2018-03-16 23:22:46 +00:00
|
|
|
gcloud-list compute addresses "${REGION:+"region=(${REGION}) AND "}name ~ 'a.*|${INSTANCE_PREFIX}.*'"
|
2015-10-06 21:53:38 +00:00
|
|
|
# Match either the header or a line with the specified e2e network.
|
|
|
|
# This assumes that the network name is the second field in the output.
|
2016-01-26 05:28:54 +00:00
|
|
|
GREP_REGEX="^NAME\|^[^ ]\+[ ]\+\(default\|${NETWORK}\) "
|
2018-03-16 23:22:46 +00:00
|
|
|
gcloud-list compute routes "name ~ 'default.*|${INSTANCE_PREFIX}.*'"
|
|
|
|
gcloud-list compute firewall-rules "name ~ 'default.*|k8s-fw.*|${INSTANCE_PREFIX}.*'"
|
2015-10-06 21:53:38 +00:00
|
|
|
GREP_REGEX=""
|
2018-03-16 23:22:46 +00:00
|
|
|
gcloud-list compute forwarding-rules ${REGION:+"region=(${REGION})"}
|
|
|
|
gcloud-list compute target-pools ${REGION:+"region=(${REGION})"}
|
|
|
|
|
|
|
|
gcloud-list logging sinks
|