blob: f453135e50b1aa767a48b35f1f30037cf4ce48ab [file] [log] [blame]
Jack Lucasa1647b22019-08-13 09:51:16 -04001#!/bin/sh
2# ============LICENSE_START=======================================================
3# org.onap.dcae
4# ================================================================================
5# Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
6# ================================================================================
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18# ============LICENSE_END=========================================================
19
20# Clean up k8s Services and Deployments created by the DCAE k8s plugin
21
22# Cleanup ontainer has access to the Kubernetes CA cert and
23# an access token for the API -- need these to make API calls
24CREDDIR=/var/run/secrets/kubernetes.io/serviceaccount
25TOKEN=$(cat ${CREDDIR}/token)
26AUTH="Authorization: Bearer $TOKEN"
27CACERT=${CREDDIR}/ca.crt
28
29# Namespace is also available
30NS=$(cat ${CREDDIR}/namespace)
31
32# The k8s plugin labels all of the k8s it deploys
33# with a label called "cfydeployment". The value
34# of the label is the name of Cloudify deployment
35# that caused the entity to be deployed.
36# For cleanup purposes, the value of the label doesn't
37# matter. The existence of the label on an entity
38# marks the entity as having been deployed by the
39# k8s plugin and therefore in need of cleanup.
40SELECTOR="labelSelector=cfydeployment"
41
42# Set up the API endpoints
43API="https://kubernetes.default"
44SVC=${API}/api/v1/namespaces/${NS}/services
45DEP=${API}/apis/apps/v1beta1/namespaces/${NS}/deployments
46
47# Find all of the k8s Services labeled with the Cloudify label
48SERVICES=$(curl -Ss --cacert ${CACERT} -H "${AUTH}" ${SVC}?${SELECTOR} | /jq .items[].metadata.name | tr -d '"')
49
50# Find all of the k8s Deployments labeled with the Cloudify label
51DEPLOYS=$(curl -Ss --cacert ${CACERT} -H "${AUTH}" ${DEP}?${SELECTOR} | /jq .items[].metadata.name | tr -d '"')
52
53# Delete all of the k8s Services with the Cloudify label
54for s in ${SERVICES}
55do
56 echo Deleting service $s
57 curl -Ss --cacert ${CACERT} -H "${AUTH}" -X DELETE ${SVC}/$s
58done
59
60# Delete all of the k8s Deployments with the Cloudify label
61# "propagationPolicy=Foreground" tells k8s to delete any children
62# of the Deployment (ReplicaSets, Pods) and to hold off on deleting
63# the Deployment itself until the children have been deleted
64for d in ${DEPLOYS}
65do
66 echo Deleting deployment $d
67 curl -Ss --cacert ${CACERT} -H "${AUTH}" -X DELETE ${DEP}/$d?propagationPolicy=Foreground
68done