#!/bin/bash # Copyright 2014 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. set -o errexit set -o nounset set -o pipefail global KUBE_ROOT := $[readlink -m $[dirname $(BASH_SOURCE)]/../../] # For $FEDERATION_NAME, $FEDERATION_NAMESPACE, $FEDERATION_KUBE_CONTEXT, # and $HOST_CLUSTER_CONTEXT. source "$(KUBE_ROOT)/federation/cluster/common.sh" # federation_clusters returns a list of all the clusters in # federation, if at all the federation control plane exists # and there are any clusters registered. proc federation_clusters { if global clusters := $["$(KUBE_ROOT)/cluster/kubectl.sh" \ --context="$(FEDERATION_KUBE_CONTEXT)" \ -o jsonpath --template '{.items[*].metadata.name}' \ get clusters] { echo $(clusters) return } echo "" } # unjoin_clusters unjoins all the clusters from federation. proc unjoin_clusters { # Unjoin only those clusters that are registered with the # given federation. This is slightly different than # joining clusters where we join all the clusters in the # current kubeconfig with the "federation" prefix. for context in [$[federation_clusters]] { kube::log::status "Unjoining cluster \"$(context)\" from federation \"$(FEDERATION_NAME)\"" "$(KUBE_ROOT)/federation/develop/kubefed.sh" unjoin \ $(context) \ --federation-system-namespace=$(FEDERATION_NAMESPACE) \ --context="$(FEDERATION_KUBE_CONTEXT)" \ --host-cluster-context="$(HOST_CLUSTER_CONTEXT)" \ --v=4 } } unjoin_clusters if cleanup-federation-api-objects { # TODO(madhusudancs): This is an arbitrary amount of sleep to give # Kubernetes clusters enough time to delete the underlying cloud # provider resources corresponding to the Kubernetes resources we # deleted as part of the test tear downs. It is shameful that we # are doing this, but this is just a bandage to stop the bleeding. # Please don't use this pattern anywhere. Remove this when proper # cloud provider cleanups are implemented in the individual test # `AfterEach` blocks. # Also, we wait only if the cleanup succeeds. kube::log::status "Waiting for 2 minutes to allow controllers to clean up federation components..." sleep 2m } else { echo "Couldn't cleanup federation api objects" } (CommandList children: [ (C {(set)} {(-o)} {(errexit)}) (C {(set)} {(-o)} {(nounset)}) (C {(set)} {(-o)} {(pipefail)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:KUBE_ROOT) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (C {(readlink)} {(-m)} { (CommandSubPart command_list: (CommandList children: [(C {(dirname)} {(DQ (${ VSub_Name BASH_SOURCE))})] ) left_token: spids: [69 77] ) (/../../) } ) ] ) left_token: spids: [64 79] ) } spids: [63] ) ] spids: [63] ) (C {(source)} {(DQ (${ VSub_Name KUBE_ROOT) (/federation/cluster/common.sh))}) (FuncDef name: federation_clusters body: (BraceGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:clusters) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (C {(DQ (${ VSub_Name KUBE_ROOT) (/cluster/kubectl.sh))} {(--context) (Lit_Other "=") (DQ (${ VSub_Name FEDERATION_KUBE_CONTEXT)) } {(-o)} {(jsonpath)} {(--template)} {(SQ <"{.items[*].metadata.name}">)} {(get)} {(clusters)} ) ] ) left_token: spids: [119 154] ) } spids: [118] ) ] spids: [118] ) terminator: ) ] action: [ (C {(echo)} {(${ VSub_Name clusters)}) (ControlFlow token:) ] spids: [-1 158] ) ] spids: [-1 171] ) (C {(echo)} {(DQ )}) ] spids: [113] ) spids: [107 112] ) (FuncDef name: unjoin_clusters body: (BraceGroup children: [ (ForEach iter_name: context iter_words: [ { (CommandSubPart command_list: (CommandList children:[(C {(federation_clusters)})]) left_token: spids: [216 218] ) } ] do_arg_iter: False body: (DoGroup children: [ (C {(kube) (Lit_Other ":") (Lit_Other ":") (log) (Lit_Other ":") (Lit_Other ":") (status) } { (DQ ("Unjoining cluster ") (EscapedLiteralPart token:) (${ VSub_Name context) (EscapedLiteralPart token:) (" from federation ") (EscapedLiteralPart token: ) (${ VSub_Name FEDERATION_NAME) (EscapedLiteralPart token:) ) } ) (C {(DQ (${ VSub_Name KUBE_ROOT) (/federation/develop/kubefed.sh))} {(unjoin)} {(DQ (${ VSub_Name context))} {(--federation-system-namespace) (Lit_Other "=") (${ VSub_Name FEDERATION_NAMESPACE)} {(--context) (Lit_Other "=") (DQ (${ VSub_Name FEDERATION_KUBE_CONTEXT))} {(--host-cluster-context) (Lit_Other "=") (DQ (${ VSub_Name HOST_CLUSTER_CONTEXT))} {(--v) (Lit_Other "=") (4)} ) ] spids: [221 301] ) spids: [215 219] ) ] spids: [191] ) spids: [185 190] ) (C {(unjoin_clusters)}) (If arms: [ (if_arm cond: [(Sentence child:(C {(cleanup-federation-api-objects)}) terminator:)] action: [ (C {(kube) (Lit_Other ":") (Lit_Other ":") (log) (Lit_Other ":") (Lit_Other ":") (status)} { (DQ ( "Waiting for 2 minutes to allow controllers to clean up federation components..." ) ) } ) (C {(sleep)} {(2m)}) ] spids: [-1 314] ) ] else_action: [(C {(echo)} {(DQ ("Couldn't cleanup federation api objects"))})] spids: [370 379] ) ] )