#!/bin/bash # Copyright 2015 The Kubernetes Authors. # # 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 global ZONE := $(ZONE:-) global REGION := $(ZONE%-*) global INSTANCE_PREFIX := $(KUBE_GCE_INSTANCE_PREFIX:-${CLUSTER_NAME:-}) global NETWORK := $(KUBE_GCE_NETWORK:-${KUBE_GKE_NETWORK:-}) # In GKE the instance prefix starts with "gke-". if [[ "${KUBERNETES_PROVIDER:-}" == "gke" ]] { global INSTANCE_PREFIX := ""gke-$(CLUSTER_NAME)"" # Truncate to 26 characters for route prefix matching. global INSTANCE_PREFIX := $(INSTANCE_PREFIX:0:26) } # Usage: gcloud-compute-list # GREP_REGEX is applied to the output of gcloud if set global GREP_REGEX := ''"" proc gcloud-compute-list { var -r resource = $1 var -r filter = $(2:-) echo -e "\n\n[ $(resource) ]" var attempt = '1' var result = ''"" while true { if result := $[gcloud compute $(resource) list --project=$(PROJECT) $(filter:+--filter="$filter") $(@:3)] { if [[ ! -z "${GREP_REGEX}" ]] { result := $[echo $(result) | grep $(GREP_REGEX) || true] } echo $(result) return } echo -e "Attempt $(attempt) failed to list $(resource). Retrying." > !2 attempt := $($attempt+1) if [[ ${attempt} > 5 ]] { echo -e "List $(resource) failed!" > !2 exit 2 } sleep $(5*${attempt}) } } echo "Project: $(PROJECT)" echo "Region: $(REGION)" echo "Zone: $(ZONE)" echo "Instance prefix: $(INSTANCE_PREFIX:-)" echo "Network: $(NETWORK)" echo "Provider: $(KUBERNETES_PROVIDER:-)" # List resources related to instances, filtering by the instance prefix if # provided. gcloud-compute-list instance-templates "name ~ '$(INSTANCE_PREFIX).*'" gcloud-compute-list instance-groups "$(ZONE:+"zone:(${ZONE}) AND ")name ~ '$(INSTANCE_PREFIX).*'" gcloud-compute-list instances "$(ZONE:+"zone:(${ZONE}) AND ")name ~ '$(INSTANCE_PREFIX).*'" # List disk resources, filtering by instance prefix if provided. gcloud-compute-list disks "$(ZONE:+"zone:(${ZONE}) AND ")name ~ '$(INSTANCE_PREFIX).*'" # List network resources. We include names starting with "a", corresponding to # those that Kubernetes creates. gcloud-compute-list addresses "$(REGION:+"region=(${REGION}) AND ")name ~ 'a.*|$(INSTANCE_PREFIX).*'" # 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. global GREP_REGEX := ""^NAME\|^[^ ]\+[ ]\+\(default\|$(NETWORK)\) "" gcloud-compute-list routes "name ~ 'default.*|$(INSTANCE_PREFIX).*'" gcloud-compute-list firewall-rules "name ~ 'default.*|k8s-fw.*|$(INSTANCE_PREFIX).*'" global GREP_REGEX := ''"" gcloud-compute-list forwarding-rules $(REGION:+"region=(${REGION})") gcloud-compute-list target-pools $(REGION:+"region=(${REGION})") (CommandList children: [ (C {(set)} {(-o)} {(errexit)}) (C {(set)} {(-o)} {(nounset)}) (C {(set)} {(-o)} {(pipefail)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:ZONE) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{}) spids: [84 87] ) } spids: [83] ) ] spids: [83] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:REGION) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{("-*")}) spids: [90 94] ) } spids: [89] ) ] spids: [89] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:INSTANCE_PREFIX) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonHyphen arg_word: { (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{}) spids: [100 103] ) } ) spids: [97 104] ) } spids: [96] ) ] spids: [96] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:NETWORK) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonHyphen arg_word: { (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{}) spids: [110 113] ) } ) spids: [107 114] ) } spids: [106] ) ] spids: [106] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: { (DQ (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{(SQ )}) spids: [125 128] ) ) } right: {(DQ (gke))} ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:INSTANCE_PREFIX) op: Equal rhs: {(DQ (gke-) (${ VSub_Name CLUSTER_NAME))} spids: [143] ) ] spids: [143] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:INSTANCE_PREFIX) op: Equal rhs: { (DQ (BracedVarSub token: suffix_op: (Slice begin: (ArithWord w:{(Lit_Digits 0)}) length: (ArithWord w:{(Lit_Digits 26)}) ) spids: [158 164] ) ) } spids: [156] ) ] spids: [156] ) ] spids: [-1 140] ) ] spids: [-1 167] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:GREP_REGEX) op:Equal rhs:{(DQ )} spids:[176])] spids: [176] ) (FuncDef name: gcloud-compute-list body: (BraceGroup children: [ (Assignment keyword: Assign_Local flags: ["'-r'"] pairs: [ (assign_pair lhs: (LhsName name:resource) op: Equal rhs: {($ VSub_Number "$1")} spids: [193] ) ] spids: [189] ) (Assignment keyword: Assign_Local flags: ["'-r'"] pairs: [ (assign_pair lhs: (LhsName name:filter) op: Equal rhs: { (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{}) spids: [202 205] ) } spids: [201] ) ] spids: [197] ) (C {(echo)} {(-e)} { (DQ (EscapedLiteralPart token:) (EscapedLiteralPart token:) ("[ ") (${ VSub_Name resource) (" ]") ) } ) (Assignment keyword: Assign_Local pairs: [(assign_pair lhs:(LhsName name:attempt) op:Equal rhs:{(1)} spids:[225])] spids: [223] ) (Assignment keyword: Assign_Local pairs: [(assign_pair lhs:(LhsName name:result) op:Equal rhs:{(DQ )} spids:[231])] spids: [229] ) (While cond: [(Sentence child:(C {(true)}) terminator:)] body: (DoGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:result) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (C {(gcloud)} {(compute)} {(${ VSub_Name resource)} {(list)} {(--project) (Lit_Other "=") (${ VSub_Name PROJECT)} { (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonPlus arg_word: {("--filter=") (DQ ($ VSub_Name "$filter")) } ) spids: [264 271] ) } { (BracedVarSub token: suffix_op: (Slice begin: (ArithWord w:{(Lit_Digits 3)}) ) spids: [273 277] ) } ) ] ) left_token: spids: [247 278] ) } spids: [246] ) ] spids: [246] ) terminator: ) ] action: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (LogicalNot child: (BoolUnary op_id: BoolUnary_z child: {(DQ (${ VSub_Name GREP_REGEX))} ) ) ) terminator: ) ] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:result) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (AndOr children: [ (Pipeline children: [ (C {(echo)} {(DQ (${ VSub_Name result))}) (C {(grep)} {(DQ (${ VSub_Name GREP_REGEX))} ) ] negated: False ) (C {(true)}) ] op_id: Op_DPipe ) ] ) left_token: spids: [305 327] ) } spids: [304] ) ] spids: [304] ) ] spids: [-1 301] ) ] spids: [-1 330] ) (C {(echo)} {(DQ (${ VSub_Name result))}) (ControlFlow token: ) ] spids: [-1 281] ) ] spids: [-1 345] ) (SimpleCommand words: [ {(echo)} {(-e)} { (DQ ("Attempt ") (${ VSub_Name attempt) (" failed to list ") (${ VSub_Name resource) (". Retrying.") ) } ] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[364])] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:attempt) op: Equal rhs: { (ArithSubPart anode: (ArithBinary op_id: Arith_Plus left: (ArithWord w:{($ VSub_Name "$attempt")}) right: (ArithWord w:{(Lit_Digits 1)}) ) spids: [369 374] ) } spids: [368] ) ] spids: [368] ) (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: Redir_Great left: {(${ VSub_Name attempt)} right: {(5)} ) ) terminator: ) ] action: [ (SimpleCommand words: [ {(echo)} {(-e)} {(DQ ("List ") (${ VSub_Name resource) (" failed!"))} ] redirects: [ (Redir op_id: Redir_GreatAnd fd: -1 arg_word: {(2)} spids: [407] ) ] ) (C {(exit)} {(2)}) ] spids: [-1 392] ) ] spids: [-1 416] ) (C {(sleep)} { (ArithSubPart anode: (ArithBinary op_id: Arith_Star left: (ArithWord w:{(Lit_Digits 5)}) right: (ArithWord w:{(${ VSub_Name attempt)}) ) spids: [421 428] ) } ) ] spids: [241 431] ) ) ] spids: [186] ) spids: [180 185] ) (C {(echo)} {(DQ ("Project: ") (${ VSub_Name PROJECT))}) (C {(echo)} {(DQ ("Region: ") (${ VSub_Name REGION))}) (C {(echo)} {(DQ ("Zone: ") (${ VSub_Name ZONE))}) (C {(echo)} { (DQ ("Instance prefix: ") (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{(SQ )}) spids: [467 470] ) ) } ) (C {(echo)} {(DQ ("Network: ") (${ VSub_Name NETWORK))}) (C {(echo)} { (DQ ("Provider: ") (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_ColonHyphen arg_word:{(SQ )}) spids: [486 489] ) ) } ) (C {(gcloud-compute-list)} {(instance-templates)} {(DQ ("name ~ '") (${ VSub_Name INSTANCE_PREFIX) (".*'"))} ) (C {(gcloud-compute-list)} {(instance-groups)} { (DQ (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonPlus arg_word: {(DQ ("zone:(") (${ VSub_Name ZONE) (") AND "))} ) spids: [516 526] ) ("name ~ '") (${ VSub_Name INSTANCE_PREFIX) (".*'") ) } ) (C {(gcloud-compute-list)} {(instances)} { (DQ (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonPlus arg_word: {(DQ ("zone:(") (${ VSub_Name ZONE) (") AND "))} ) spids: [539 549] ) ("name ~ '") (${ VSub_Name INSTANCE_PREFIX) (".*'") ) } ) (C {(gcloud-compute-list)} {(disks)} { (DQ (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonPlus arg_word: {(DQ ("zone:(") (${ VSub_Name ZONE) (") AND "))} ) spids: [566 576] ) ("name ~ '") (${ VSub_Name INSTANCE_PREFIX) (".*'") ) } ) (C {(gcloud-compute-list)} {(addresses)} { (DQ (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonPlus arg_word: {(DQ ("region=(") (${ VSub_Name REGION) (") AND "))} ) spids: [596 606] ) ("name ~ 'a.*|") (${ VSub_Name INSTANCE_PREFIX) (".*'") ) } ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:GREP_REGEX) op: Equal rhs: { (DQ ("^NAME") (EscapedLiteralPart token:) ("^[^ ]") (EscapedLiteralPart token:) ("[ ]") (EscapedLiteralPart token:) (EscapedLiteralPart token:) (default) (EscapedLiteralPart token:) (${ VSub_Name NETWORK) (EscapedLiteralPart token:) (" ") ) } spids: [620] ) ] spids: [620] ) (C {(gcloud-compute-list)} {(routes)} {(DQ ("name ~ 'default.*|") (${ VSub_Name INSTANCE_PREFIX) (".*'"))} ) (C {(gcloud-compute-list)} {(firewall-rules)} {(DQ ("name ~ 'default.*|k8s-fw.*|") (${ VSub_Name INSTANCE_PREFIX) (".*'"))} ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:GREP_REGEX) op:Equal rhs:{(DQ )} spids:[662])] spids: [662] ) (C {(gcloud-compute-list)} {(forwarding-rules)} { (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonPlus arg_word: {(DQ ("region=(") (${ VSub_Name REGION) (")"))} ) spids: [670 680] ) } ) (C {(gcloud-compute-list)} {(target-pools)} { (BracedVarSub token: suffix_op: (StringUnary op_id: VTest_ColonPlus arg_word: {(DQ ("region=(") (${ VSub_Name REGION) (")"))} ) spids: [686 696] ) } ) ] )