-
Notifications
You must be signed in to change notification settings - Fork 1
/
export-argocd-resources.sh
executable file
·78 lines (63 loc) · 1.92 KB
/
export-argocd-resources.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
#!/bin/bash
# Colorize terminal
red='\e[0;31m'
no_color='\033[0m'
# Default
RESOURCES_KIND="all"
NAMESPACE="argocd"
EXPORT_DIR="./argocd-export"
# Declare script helper
TEXT_HELPER="\nThis script aims to export ready-to-apply argocd projects, applications and applicationsets.
It uses kubectl-neat to clean up manifests metadata so it is required to work correctly.
Following flags are available:
-k Kind of resources to export, available kinds are 'apps', 'appsets', 'projects' and 'all'.
Default is '$RESOURCES_KIND'.
-n Kubernetes namespace target to copy argocd resources.
Default is '$NAMESPACE'
-o Output directory where to export files.
Default is '$EXPORT_DIR'
-h Print script help.\n\n"
print_help() {
printf "$TEXT_HELPER"
}
# Parse options
while getopts hk:n:o: flag; do
case "${flag}" in
k)
RESOURCES_KIND=${OPTARG};;
n)
NAMESPACE=${OPTARG};;
o)
EXPORT_DIR=${OPTARG};;
h | *)
print_help
exit 0;;
esac
done
mkdir -p "$EXPORT_DIR"
export_argocd_resources () {
EXPORT_FILE="$EXPORT_DIR/$1.yaml"
# Clear existing content in the projects file
> "$EXPORT_FILE"
RESOURCES=$(kubectl get $1 -n $NAMESPACE --no-headers -o custom-columns=":metadata.name")
for RESOURCE in $RESOURCES; do
echo "---" >> "$EXPORT_FILE"
kubectl get $1 $RESOURCE -n $NAMESPACE -o yaml | kubectl neat >> "$EXPORT_FILE"
echo "" >> "$EXPORT_FILE"
done
}
if [[ "$RESOURCES_KIND" == "projects" ]]; then
export_argocd_resources appprojects
elif [[ "$RESOURCES_KIND" == "apps" ]]; then
export_argocd_resources apps
elif [[ "$RESOURCES_KIND" == "appsets" ]]; then
export_argocd_resources appsets
elif [[ "$RESOURCES_KIND" == "all" ]]; then
export_argocd_resources appprojects
export_argocd_resources apps
export_argocd_resources appsets
else
printf "Wrong arguments, you need to specify a valid kind of resources.\n"
print_help
exit 1
fi