-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuninstall-package.sh
executable file
·125 lines (99 loc) · 3.34 KB
/
uninstall-package.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
set -eo pipefail
# Uninstalls one or more Tanzu package(s) via kapp and kubectl CLIs
## Packages must conform to a specific directory structure
## +- application_name
## +- .init
## +- .install
## +- base
## Do not change anything below unless you know what you're doing!
# Delete applications and associated configurations
delete_applications() {
local gitops_dir=$1
local app_name=$2
local ytt_parent_dir=$3
# Handle ancillary applications
handle_ancillary "$gitops_dir" "$app_name" "$ytt_parent_dir"
# Delete main app and RBAC
if [ -d "$gitops_dir/.init" ] && [ -d "$gitops_dir/.install" ]; then
kapp delete --app $app_name --diff-changes --yes
kapp delete --app $app_name-ns-rbac --diff-changes --yes
else
echo "Expected to find .init and .install directories."
exit 1
fi
# Handle pre-requisites
if [ -d "$gitops_dir/.prereq" ]; then
kubectl delete -f $gitops_dir/.prereq
fi
}
# Handle ancillary applications based on .post-install configurations
handle_ancillary() {
local dir=$1
local app_name=$2
if [ -d "$dir/.post-install" ]; then
local files=$(find "$dir/.post-install" -type f -name "*.yml" | wc -l)
if [ $files -eq 1 ]; then
local kind=$(yq e '.kind' $dir/.post-install/*.yml)
if [ "$kind" == "App" ]; then
local ytt_paths=( $(yq e '.spec.template.[].ytt.paths.[]' $dir/.post-install/*.yml) )
if [ ${#ytt_paths[@]} -gt 0 ]; then
kapp delete --app $app_name-ancillary --diff-changes --yes
fi
fi
fi
fi
}
if [ "x${KUBECONFIG}" == "x" ]; then
echo "Workload cluster KUBECONFIG environment variable not set."
if [ -z "$1" ]; then
echo "Workload cluster name was not supplied!"
exit 1
fi
if [ -z "$2" ]; then
echo "Management cluster's KUBECONFIG base64-encoded contents was not supplied!"
exit 1
fi
WORKLOAD_CLUSTER_NAME="$1"
echo "- Decoding the management cluster's KUBECONFIG contents and saving output to /tmp/.kube-tkg/config"
mkdir -p /tmp/.kube-tkg
echo "$2" | base64 -d > /tmp/.kube-tkg/config
chmod 600 /tmp/.kube-tkg/config
cluster_name=$(cat /tmp/.kube-tkg/config | yq '.clusters[].name')
echo "- Management cluster name is [ $cluster_name ]"
echo "- Logging in to management cluster"
tanzu login --kubeconfig /tmp/.kube-tkg/config --context ${cluster_name}-admin@${cluster_name} --name ${cluster_name}
echo "- Obtaining the workload cluster's KUBECONFIG and setting the current context for kubectl"
tanzu cluster kubeconfig get ${WORKLOAD_CLUSTER_NAME} --admin
kubectl config use-context ${WORKLOAD_CLUSTER_NAME}-admin@${WORKLOAD_CLUSTER_NAME}
if [ -z "$4" ]; then
echo "Application name was not supplied!"
exit 1
else
if [ -z "$3" ]; then
echo "Path to Tanzu package was not supplied!"
exit 1
else
GITOPS_DIR=$GITHUB_WORKSPACE/$3
APP_NAME="${4}"
cd ${GITOPS_DIR}
delete_applications "$GITOPS_DIR" "$APP_NAME"
fi
fi
else
echo "Workload cluster KUBECONFIG environment variable was set."
if [ -z "$2" ]; then
echo "Application name was not supplied!"
exit 1
else
if [ -z "$1" ]; then
echo "Path to Tanzu package was not supplied!"
exit 1
else
GITOPS_DIR=$GITHUB_WORKSPACE/$1
APP_NAME="${2}"
cd ${GITOPS_DIR}
delete_applications "$GITOPS_DIR" "$APP_NAME"
fi
fi
fi