Add uninstall app hook

This commit is contained in:
Nikolai Rodionov 2024-05-13 13:33:36 +02:00
parent 0e2007c40c
commit 9dd997207e
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
2 changed files with 52 additions and 6 deletions

View File

@ -23,25 +23,30 @@ kubernetes:
EOF EOF
else else
export $(jq -r .[0].object.data.vars $BINDING_CONTEXT_PATH) export $(jq -r .[0].object.data.vars $BINDING_CONTEXT_PATH)
export SP_CUSTOMER_ID=$(jq -r .[0].object.metadata.namespace $BINDING_CONTEXT_PATH) export NAMESPACE=$(jq -r .[0].object.metadata.namespace $BINDING_CONTEXT_PATH)
export SP_CONFIG_NAME=$(jq -r .[0].object.metadata.name $BINDING_CONTEXT_PATH) export NAME=$(jq -r .[0].object.metadata.name $BINDING_CONTEXT_PATH)
export SP_APPLICATION=$(jq -r '.[0].object.data."values.yaml"' $BINDING_CONTEXT_PATH | base64 -d | yq '.helm.release') export SP_APPLICATION=$(jq -r '.[0].object.data."values.yaml"' $BINDING_CONTEXT_PATH | base64 -d | yq '.helm.release')
export SP_ENVIRONMENT_ID=$(jq -r .[0].object.metadata.labels.environment $BINDING_CONTEXT_PATH) export SP_ENVIRONMENT_ID=$(jq -r .[0].object.metadata.labels.environment $BINDING_CONTEXT_PATH)
kubectl patch configmap -n ${SP_CUSTOMER_ID} ${SP_ENVIRONMENT} \
-p '{"metadata":{"finalizers":["softplayer.net/bootstrapped"]}}' --type=merge
flux create source helm $SP_APPLICATION \ flux create source helm $SP_APPLICATION \
--namespace=$SP_CUSTOMER_ID \ --namespace=$NAMESPACE \
--url=oci://registry.badhouseplants.net/softplayer/helm --url=oci://registry.badhouseplants.net/softplayer/helm
flux create helmrelease $SP_APPLICATION \ flux create helmrelease $SP_APPLICATION \
--source=HelmRepository/$SP_APPLICATION \ --source=HelmRepository/$SP_APPLICATION \
--namespace=$SP_CUSTOMER_ID \ --namespace=$NAMESPACE \
--chart=helmrelease \ --chart=helmrelease \
--values-from Secret/$SP_CONFIG_NAME \ --values-from Secret/$NAME \
--kubeconfig-secret-ref=$SP_ENVIRONMENT_ID-config \ --kubeconfig-secret-ref=$SP_ENVIRONMENT_ID-config \
--target-namespace=default --export \ --target-namespace=default --export \
| yq '.spec.storageNamespace="helm-installations"' \ | yq '.spec.storageNamespace="helm-installations"' \
| kubectl apply -f - | kubectl apply -f -
flux reconcile helmrelease --namespace=$SP_CUSTOMER_ID $SP_APPLICATION
flux reconcile helmrelease --namespace=$NAMESPACE $SP_APPLICATION
fi fi

41
hooks/uninstall-application.sh Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env bash
# ---------------------------------------------------------------------
# This script should bootstrap a new environment, or update
# existing ones
# Get the configmap name and namespace to prepare the
# ---------------------------------------------------------------------
if [[ $1 == "--config" ]] ; then
cat <<EOF
configVersion: v1
kubernetes:
- name: Watch application configmaps getting created
apiVersion: v1
kind: Secret
executeHookOnSynchronization: false
executeHookOnEvent: ["Modified"]
jqFilter: ".metadata.deletionTimestamp"
labelSelector:
matchExpressions:
- key: "component"
operator: "In"
values: ["install"]
EOF
else
export $(jq -r .[0].object.data.vars $BINDING_CONTEXT_PATH)
export NAMESPACE=$(jq -r .[0].object.metadata.namespace $BINDING_CONTEXT_PATH)
export NAME=$(jq -r .[0].object.metadata.name $BINDING_CONTEXT_PATH)
export SP_APPLICATION=$(jq -r '.[0].object.data."values.yaml"' $BINDING_CONTEXT_PATH | base64 -d | yq '.helm.release')
export SP_ENVIRONMENT_ID=$(jq -r .[0].object.metadata.labels.environment $BINDING_CONTEXT_PATH)
flux delete source helm $SP_APPLICATION \
--namespace=$NAMESPACE \
flux delete helmrelease $SP_APPLICATION \
--namespace=$NAMESPACE
kubectl patch configmap -n ${SP_CUSTOMER_ID} ${SP_ENVIRONMENT} \
-p '{"metadata":{"finalizers":null}}' --type=merge
fi