Skip to content

Commit

Permalink
feat: add directory support
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Bouyoud <jBouyoud@users.noreply.github.com>
  • Loading branch information
jBouyoud committed Jan 8, 2021
1 parent 8a5a022 commit 0b23472
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 82 deletions.
33 changes: 19 additions & 14 deletions scripts/commands/downloader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,40 @@ downloader() {
# build parameters
export_subst_args "${parent_process_cmd_line}"

idx=0
value_files="${TMP_DIR}/result"
repository_view_scheme "${scheme_name}" | while read -r file_template; do
log_info "[downloader] Looking for '${file_template}'..."
eval "file=\"${file_template}\""

eval "file=${file_template}"
# shellcheck disable=SC2154
file_content="$( (_file_get "${file}" || printf ''))"
log_info_line "[downloader] Loading values for '${file}' "

if [ -z "${file_content}" ]; then
log_info "[downloader] Ignored config source : ${file}"
# shellcheck disable=SC2154
value_files_list="$(file_get "${file}" "${TMP_DIR}")"

cat "${value_files_list}" >>"${value_files}"

count="$(wc <"${value_files_list}" -l | awk '{print $1}')"
if [ "${count}" -eq 0 ]; then
printf "skipped.\n" >&2
else
log_info "[downloader] Loaded config source : ${file}"
printf '%s\n' "${file_content}" >"${TMP_DIR}/file-${idx}"
idx=$((idx + 1))
printf "done. %s file(s) loaded.\n" "${count}" >&2
fi
rm -f "${value_files_list}"
done

if [ "$(find "${TMP_DIR}" -type f | wc -l)" -le 1 ]; then
cat "${TMP_DIR}/file-0" 2>/dev/null || printf ''
count="$(wc <"${value_files}" -l | awk '{print $1}')"
if [ "${count}" -eq 0 ]; then
printf ''
else
yq_select=""
for value_file in "${TMP_DIR}"/file-*; do
while read -r value_file; do
if [ "${yq_select}" != "" ]; then
yq_select="${yq_select} *"
fi
yq_select="${yq_select} select(filename == \"${value_file}\")"
done
done <"${value_files}"

# shellcheck disable=SC2046
_yq eval-all -M "explode(.) |${yq_select}" "${TMP_DIR}"/file-*
_yq eval-all -M "explode(.) |${yq_select}" $(cat "${value_files}")
fi
}
22 changes: 18 additions & 4 deletions scripts/lib/file.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,27 @@ _file_get_protocol() {
echo "custom"
;;
*)
echo "local"
if [ -d "$1" ]; then
echo "local_dir"
elif [ -f "$1" ]; then
echo "local_file"
else
echo "local_regex"
fi
;;
esac
}

_file_get() {
file_type=$(_file_get_protocol "${1}")
_file_tmpfile() {
mktemp "${1}/tmp.XXXXXXXXXX"
}

file_get() {
file_type="$(_file_get_protocol "${1}")"
tmpdir="${2}"
res="$(_file_tmpfile "${tmpdir}")"
touch "${res}"
echo "${res}"

_file_"${file_type}"_get "$@"
_file_"${file_type}"_get "${1}" "${tmpdir}" "${res}"
}
12 changes: 10 additions & 2 deletions scripts/lib/file/custom.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
#!/usr/bin/env sh

_file_custom_get() {
helm template "${SCRIPT_DIR}/lib/file/helm-values-getter" -f "${1}" |
sed -E -e "s/^# Source: .+$//" -e "s/^---$//"
uri="${1}"
tmpfile="$(_file_tmpfile "${2}")"
res="${3}"

helm template "${SCRIPT_DIR}/lib/file/helm-values-getter" -f "${uri}" 2>/dev/null |
sed -E -e "s/^# Source: .+$//" -e "s/^---$//" >"${tmpfile}"

if [ -s "${tmpfile}" ]; then
echo "${tmpfile}" >>"${res}"
fi
}
24 changes: 22 additions & 2 deletions scripts/lib/file/local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

set -eu

_file_local_get() {
cat "${1}" 2>/dev/null
_file_local_file_get() {
file="${1}"
res="${3}"

if [ -s "${file}" ]; then
tmpfile="$(_file_tmpfile "${2}")"

cp -f "${file}" "${tmpfile}"
echo "${tmpfile}" >>"${res}"
fi
}

_file_local_regex_get() {
find "$(dirname "${1}")" -type f 2>/dev/null | grep -E "${1}" | sort | while read -r valueFile; do
_file_local_file_get "${valueFile}" "${2}" "${3}"
done
}

_file_local_dir_get() {
find "${1}" -maxdepth 1 -type f -name '*yaml' -or -name '*yml' | sort | while read -r valueFile; do
_file_local_file_get "${valueFile}" "${2}" "${3}"
done
}
6 changes: 6 additions & 0 deletions scripts/lib/log.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ log_info() {
fi
}

log_info_line() {
if [ "${QUIET}" = "false" ]; then
printf "[config-scheme]%s" "$@" >&2
fi
}

log_error() {
echo
printf "[config-scheme]%s\n" "$@"
Expand Down
7 changes: 3 additions & 4 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ _parent_process_command() {
if [ "$(uname)" = "Darwin" ]; then
ps -p "${PPID}" -o command=
elif [ "$(uname)" = "Linux" ]; then
if command -v apk >/dev/null; then
# alpine based
# shellcheck disable=SC2009
ps -o pid,args= | grep -E "^\s+${PPID}" | awk '!($1="")'
if [ -f /proc/${PPID}/cmdline ]; then
xargs -0 </proc/${PPID}/cmdline
echo
else
ps -p "${PPID}" -o command=
fi
Expand Down
3 changes: 3 additions & 0 deletions tests/assets/config-dir/a.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ingress:
annotations:
config-dir/a.yaml: 'true'
3 changes: 3 additions & 0 deletions tests/assets/config-dir/b.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ingress:
annotations:
config-dir/b.yaml: 'true'
3 changes: 3 additions & 0 deletions tests/assets/config-dir/c.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ingress:
annotations:
config-dir/c.yaml: 'true'
20 changes: 10 additions & 10 deletions tests/it/install.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ load '../lib/assert-downloader'

run helm install --create-namespace --wait "${RELEASE}" "${TEST_TEMP_DIR}/chart" -f config://test -n namespace 2>&1
assert_success
assert-downloader-output-base
assert_output --partial "[config-scheme][downloader] Ignored config source : ${TEST_TEMP_DIR}/rand-assets/values.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/values.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/chart.yaml"
assert_output --partial "[config-scheme][downloader] Ignored config source : ${TEST_TEMP_DIR}/assets/${RELEASE}.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/test.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/namespace/values.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/namespace/chart.yaml"
assert_output --partial "[config-scheme][downloader] Ignored config source : ${TEST_TEMP_DIR}/assets/namespace/${RELEASE}.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/namespace/test.yaml"
assert_file_not_exist "${HELM_CONFIG_SCHEME_TMP_DIR}"
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/rand-assets/values.yaml' skipped."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/values.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/chart.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/${RELEASE}.yaml' skipped."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/test.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/namespace/values.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/namespace/chart.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/namespace/${RELEASE}.yaml' skipped."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/namespace/test.yaml' done. 1 file(s) loaded."
assert_output --partial 'STATUS: deployed'

run helm ls -n namespace
Expand Down
20 changes: 10 additions & 10 deletions tests/it/upgrade.bats
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ load '../lib/assert-downloader'

run helm upgrade -i --create-namespace --wait "${RELEASE}" "${TEST_TEMP_DIR}/chart" -f config://test -n namespace 2>&1
assert_success
assert-downloader-output-base
assert_output --partial "[config-scheme][downloader] Ignored config source : ${TEST_TEMP_DIR}/rand-assets/values.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/values.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/chart.yaml"
assert_output --partial "[config-scheme][downloader] Ignored config source : ${TEST_TEMP_DIR}/assets/${RELEASE}.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/test.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/namespace/values.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/namespace/chart.yaml"
assert_output --partial "[config-scheme][downloader] Ignored config source : ${TEST_TEMP_DIR}/assets/namespace/${RELEASE}.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/namespace/test.yaml"
assert_file_not_exist "${HELM_CONFIG_SCHEME_TMP_DIR}"
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/rand-assets/values.yaml' skipped."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/values.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/chart.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/${RELEASE}.yaml' skipped."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/test.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/namespace/values.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/namespace/chart.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/namespace/${RELEASE}.yaml' skipped."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/namespace/test.yaml' done. 1 file(s) loaded."
assert_output --partial 'STATUS: deployed'

run helm ls -n namespace
Expand Down
34 changes: 10 additions & 24 deletions tests/lib/assert-downloader.bash
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@

assert-downloader-output-base() {
assert_output --partial "[config-scheme][downloader] Looking for '${TEST_TEMP_DIR}/rand-assets/values.yaml'..."
assert_output --partial "[config-scheme][downloader] Looking for '${TEST_TEMP_DIR}/assets/values.yaml'..."
assert_output --partial "[config-scheme][downloader] Looking for '${TEST_TEMP_DIR}/assets/\${chart}.yaml'..."
assert_output --partial "[config-scheme][downloader] Looking for '${TEST_TEMP_DIR}/assets/\${release}.yaml'..."
assert_output --partial "[config-scheme][downloader] Looking for '${TEST_TEMP_DIR}/assets/\${CUSTOM_ENV_VAR}.yaml'..."
assert_output --partial "[config-scheme][downloader] Looking for '${TEST_TEMP_DIR}/assets/\${namespace}/values.yaml'..."
assert_output --partial "[config-scheme][downloader] Looking for '${TEST_TEMP_DIR}/assets/\${namespace}/\${chart}.yaml'..."
assert_output --partial "[config-scheme][downloader] Looking for '${TEST_TEMP_DIR}/assets/\${namespace}/\${release}.yaml'..."
assert_output --partial "[config-scheme][downloader] Looking for '${TEST_TEMP_DIR}/assets/\${namespace}/\${CUSTOM_ENV_VAR}.yaml'..."

assert_file_not_exist "${HELM_CONFIG_SCHEME_TMP_DIR}"
}

assert-downloader-output() {
assert-downloader-output-base
assert_file_not_exist "${HELM_CONFIG_SCHEME_TMP_DIR}"

namespace="${1}"
chart="${2}"
release="${3}"
env="${4}"
assert_output --partial "[config-scheme][downloader] Ignored config source : ${TEST_TEMP_DIR}/rand-assets/values.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/values.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/${chart}.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/${release}.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/${env}.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/${namespace}/values.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/${namespace}/${chart}.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/${namespace}/${release}.yaml"
assert_output --partial "[config-scheme][downloader] Loaded config source : ${TEST_TEMP_DIR}/assets/${namespace}/${env}.yaml"
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/rand-assets/values.yaml' skipped."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/values.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/${chart}.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/${release}.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/${env}.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/${namespace}/values.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/${namespace}/${chart}.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/${namespace}/${release}.yaml' done. 1 file(s) loaded."
assert_output --partial "[config-scheme][downloader] Loading values for '${TEST_TEMP_DIR}/assets/${namespace}/${env}.yaml' done. 1 file(s) loaded."
}
Loading

0 comments on commit 0b23472

Please sign in to comment.