From a1647b2d90cbfd3fc4ccedddf876fba2cf031a8a Mon Sep 17 00:00:00 2001 From: Jack Lucas Date: Tue, 13 Aug 2019 09:51:16 -0400 Subject: Add DCAE cleanup container Issue-ID: DCAEGEN2-1317 Change-Id: I1c834346d99c62f036e14724674a447966032ec8 Signed-off-by: Jack Lucas --- .gitignore | 1 + dcae-k8s-cleanup-container/Dockerfile | 25 +++++ dcae-k8s-cleanup-container/README.md | 28 +++++ dcae-k8s-cleanup-container/dcae-cleanup.sh | 68 ++++++++++++ dcae-k8s-cleanup-container/pom.xml | 172 +++++++++++++++++++++++++++++ mvn-phase-script.sh | 2 +- pom.xml | 1 + 7 files changed, 296 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 dcae-k8s-cleanup-container/Dockerfile create mode 100644 dcae-k8s-cleanup-container/README.md create mode 100644 dcae-k8s-cleanup-container/dcae-cleanup.sh create mode 100644 dcae-k8s-cleanup-container/pom.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d74e21 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/ diff --git a/dcae-k8s-cleanup-container/Dockerfile b/dcae-k8s-cleanup-container/Dockerfile new file mode 100644 index 0000000..7d5187b --- /dev/null +++ b/dcae-k8s-cleanup-container/Dockerfile @@ -0,0 +1,25 @@ +# ============LICENSE_START======================================================= +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +# Alpine-based container with curl and jq, needed to access the k8s API +FROM alpine:3.10 +COPY dcae-cleanup.sh / +RUN apk add --no-cache curl && \ + wget -O /jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 && \ + chmod 755 /jq && \ + chmod 755 /dcae-cleanup.sh +ENTRYPOINT ["/dcae-cleanup.sh"] diff --git a/dcae-k8s-cleanup-container/README.md b/dcae-k8s-cleanup-container/README.md new file mode 100644 index 0000000..8506885 --- /dev/null +++ b/dcae-k8s-cleanup-container/README.md @@ -0,0 +1,28 @@ +# DCAE Cleanup Container +## Purpose +DCAE platform components (inventory, deployment handler, policy handler, etc.) are +deployed and undeployed using Helm. DCAE service components--data collectors and +data analytics modules--are deployed using Cloudify, with the DCAE k8s plugin. +When DCAE is undeployed, Helm +has no way to undeploy the service components. The artifacts in this directory +build a Docker image that can be run as a Kubernetes Job, using a Helm pre-delete hook. +The image runs a script that deletes the Kubernetes Services and Kubernetes Deployments +(and all of the ReplicaSets and Pods created as children of the Deployments) that were +created by the k8s plugin. + +The script relies on the fact that Services and Deployments created by the k8s +plugin have a unique label ("cfydeployment"). The script finds Services and +Deployments with that label and deletes them. + +## Running the container +The image is intended to be run as Kubernetes Job in a Helm pre-delete hook associated +with the OOM dcaegen2 charts. A Helm template in the OOM dcaegen2 tree defines the Job. +The Job will start a container. The container will execute the `dcae-cleanup.sh` script +and then exit. The intent is that using a `helm undeploy` command will automatically +delete all of the DCAE service components, so that no additional cleanup is needed. + +The container can be run manually using the `kubectl run` command. For example: +``` +kubectl -n onap run --restart='OnFailure' --image dcae-cleanup:0.0.0 cleanup +``` +The `--restart='OnFailure'` parameter causes kubectl to create a Job. \ No newline at end of file diff --git a/dcae-k8s-cleanup-container/dcae-cleanup.sh b/dcae-k8s-cleanup-container/dcae-cleanup.sh new file mode 100644 index 0000000..f453135 --- /dev/null +++ b/dcae-k8s-cleanup-container/dcae-cleanup.sh @@ -0,0 +1,68 @@ +#!/bin/sh +# ============LICENSE_START======================================================= +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= + +# Clean up k8s Services and Deployments created by the DCAE k8s plugin + +# Cleanup ontainer has access to the Kubernetes CA cert and +# an access token for the API -- need these to make API calls +CREDDIR=/var/run/secrets/kubernetes.io/serviceaccount +TOKEN=$(cat ${CREDDIR}/token) +AUTH="Authorization: Bearer $TOKEN" +CACERT=${CREDDIR}/ca.crt + +# Namespace is also available +NS=$(cat ${CREDDIR}/namespace) + +# The k8s plugin labels all of the k8s it deploys +# with a label called "cfydeployment". The value +# of the label is the name of Cloudify deployment +# that caused the entity to be deployed. +# For cleanup purposes, the value of the label doesn't +# matter. The existence of the label on an entity +# marks the entity as having been deployed by the +# k8s plugin and therefore in need of cleanup. +SELECTOR="labelSelector=cfydeployment" + +# Set up the API endpoints +API="https://kubernetes.default" +SVC=${API}/api/v1/namespaces/${NS}/services +DEP=${API}/apis/apps/v1beta1/namespaces/${NS}/deployments + +# Find all of the k8s Services labeled with the Cloudify label +SERVICES=$(curl -Ss --cacert ${CACERT} -H "${AUTH}" ${SVC}?${SELECTOR} | /jq .items[].metadata.name | tr -d '"') + +# Find all of the k8s Deployments labeled with the Cloudify label +DEPLOYS=$(curl -Ss --cacert ${CACERT} -H "${AUTH}" ${DEP}?${SELECTOR} | /jq .items[].metadata.name | tr -d '"') + +# Delete all of the k8s Services with the Cloudify label +for s in ${SERVICES} +do + echo Deleting service $s + curl -Ss --cacert ${CACERT} -H "${AUTH}" -X DELETE ${SVC}/$s +done + +# Delete all of the k8s Deployments with the Cloudify label +# "propagationPolicy=Foreground" tells k8s to delete any children +# of the Deployment (ReplicaSets, Pods) and to hold off on deleting +# the Deployment itself until the children have been deleted +for d in ${DEPLOYS} +do + echo Deleting deployment $d + curl -Ss --cacert ${CACERT} -H "${AUTH}" -X DELETE ${DEP}/$d?propagationPolicy=Foreground +done diff --git a/dcae-k8s-cleanup-container/pom.xml b/dcae-k8s-cleanup-container/pom.xml new file mode 100644 index 0000000..3822ac7 --- /dev/null +++ b/dcae-k8s-cleanup-container/pom.xml @@ -0,0 +1,172 @@ + + + + 4.0.0 + + org.onap.dcaegen2.deployments + deployments + 1.2.0-SNAPSHOT + + org.onap.dcaegen2.deployments + dcae-k8s-cleanup-container + dcaegen2-deployments-dcae-k8s-cleanup-container + 1.0.0 + http://maven.apache.org + + UTF-8 + true + . + + + + + py + Python + **/*.py + + + + + ${project.artifactId}-${project.version} + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + clean phase script + clean + + exec + + + + ${project.artifactId} + clean + + + + + generate-sources script + generate-sources + + exec + + + + ${project.artifactId} + generate-sources + + + + + compile script + compile + + exec + + + + ${project.artifactId} + compile + + + + + package script + package + + exec + + + + ${project.artifactId} + package + + + + + test script + test + + exec + + + + ${project.artifactId} + test + + + + + install script + install + + exec + + + + ${project.artifactId} + install + + + + + deploy script + deploy + + exec + + + + ${project.artifactId} + deploy + + + + + + + + diff --git a/mvn-phase-script.sh b/mvn-phase-script.sh index 6e18c0d..67b25e0 100755 --- a/mvn-phase-script.sh +++ b/mvn-phase-script.sh @@ -83,7 +83,7 @@ deploy) upload_files_of_extension sh build_and_push_docker ;; - k8s-bootstrap-container|tca-cdap-container|cm-container|redis-cluster-container|healthcheck-container|pnda-mirror-container|pnda-bootstrap-container|tls-init-container|consul-loader-container|multisite-init-container) + k8s-bootstrap-container|tca-cdap-container|cm-container|redis-cluster-container|healthcheck-container|pnda-mirror-container|pnda-bootstrap-container|tls-init-container|consul-loader-container|multisite-init-container|dcae-k8s-cleanup-container) build_and_push_docker ;; scripts|cloud_init|heat) diff --git a/pom.xml b/pom.xml index e0489ba..674593b 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,7 @@ limitations under the License. tls-init-container consul-loader-container multisite-init-container + dcae-k8s-cleanup-container