-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkustomize-build
executable file
·149 lines (123 loc) · 3.62 KB
/
kustomize-build
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bash
# Load background jobs script
# shellcheck disable=SC1090
source <(wget -qO- https://raw.githubusercontent.com/shakefu/sh-snippets/main/bash/jobs.sh)
# Constants
# Suffix where we place files after building them
BUILD_DIR_SUFFIX="build/kustomize"
KUSTOMIZE="kustomize build --enable-helm"
# Execute kustomize build with nice logging
function kustomizeBuild {
local kustomizeFile=$1
local output=$2
echo "Building $kustomizeFile"
{
local -
set +x
set -e
} 2>/dev/null
echo -e " ➢ $KUSTOMIZE ${kustomizeFile} > ${BUILD_DIR_SUFFIX}/${output}"
$KUSTOMIZE "${SRC_DIR}/${kustomizeFile}" >"${BUILD_DIR}/${output}"
return $?
}
# Check for external script dependencies and provide sensible errors
function check_dependencies {
if ! command -v wget >/dev/null; then
echo "Missing required command: wget"
exit 1
fi
# If we are here, then we want to try for the standalone kustomize command
# just in case as a back up
if ! command -v kustomize >/dev/null; then
echo "Missing required command: kubectl kustomize OR kustomize"
exit 1
fi
# And if we reach here we have a `kustomize` command, so we can use it
# Ouptut the version for debugging's sake
log_job "version" kustomize version --short 2>/dev/null
}
# Display help and exit
function usage {
echo "usage: kustomize-build [-f|--filter <filter-string>] [-o|--output-dir <output_dir>]" >&2
echo "Error: Unsupported argument $1" >&2
}
function main {
local log="logger kustomize-build"
$log "🚀 Kustomize build..."
check_dependencies
# Create globals used by subcommands
SRC_DIR="$(dirname "${BASH_SOURCE[0]}")"
BUILD_DIR="${SRC_DIR}/${BUILD_DIR_SUFFIX}"
# filter from --filter argument
while (("$#")); do
case "$1" in
-f | --filter)
shift
filter="$1"
shift
;;
-o | --output-dir)
shift
BUILD_DIR="$1"
shift
;;
--clean)
if [[ -d "$BUILD_DIR" ]]; then
echo "Cleaning build directory: $BUILD_DIR"
rm -rf "$BUILD_DIR"
else
echo "Skipping clean, directory does not exist: $BUILD_DIR"
fi
shift
;;
-*) # unsupported flags
usage "$1"
;;
*) # preserve positional arguments
usage "$1"
;;
esac
done
mkdir -p "${BUILD_DIR}"
cd "$SRC_DIR"
$log "🛠 started"
KUSTOMIZATION_FILES_BASE=$(find . -name "kustomization.yaml" | grep -i -v component | grep -i base | grep -i "$filter")
$log "BASE:"
$log "${KUSTOMIZATION_FILES_BASE}"
KUSTOMIZATION_FILES=$(find . -name "kustomization.yaml" | grep -i -v component | grep -i -v base | grep -i "$filter")
$log "CLUSTERS:"
$log "${KUSTOMIZATION_FILES}"
local kustomize_dir
local counter
local file
counter=1
for file in $KUSTOMIZATION_FILES_BASE; do
kustomize_dir=$(dirname "$file")
kustomize_dir=${kustomize_dir//\.\//}
$log "🛠 ${counter}: ${file} - ${kustomize_dir}"
# skip if kustomization.yaml is a component
if grep -q "kind: Component" "$file"; then
echo "kind: Component >> Skipping..."
else
log_job "${kustomize_dir}" kustomizeBuild "${kustomize_dir}" "${kustomize_dir//\//_}.yaml" &
fi
((counter += 1))
done
for i in {1..50} ; do
for file in $KUSTOMIZATION_FILES; do
kustomize_dir=$(dirname "$file")
kustomize_dir=${kustomize_dir//\.\//}
$log "🛠 ${counter}: ${file} - ${kustomize_dir}"
# skip if kustomization.yaml is a component
if grep -q "kind: Component" "$file"; then
echo "{$i} kind: Component >> Skipping..."
else
log_job "{$i} ${kustomize_dir}" kustomizeBuild "${kustomize_dir}" "${kustomize_dir//\//_}.yaml" &
fi
((counter += 1))
done
done
wait_for_jobs || exit $?
echo -e "✅ [kustomize] Kustomize build: Done\n"
}
main "$@"