Skip to content

Commit 6bffabf

Browse files
feat: enhance _k8s_file function to use a shared temp directory for remote downloads
1 parent bbb38e0 commit 6bffabf

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

lib/run-mode.sh

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
# _lib <name> Source a file from lib/ (e.g. _lib log.sh)
2222
# _k8s <rel-path> Echo an absolute path or URL to a kubernetes/ manifest
2323
# (safe to pass to `kubectl apply -f`)
24-
# _helm_val <rel-path> Echo a local path to a helm values file; in remote
25-
# mode the file is downloaded to a temp path first.
26-
# Temp files are cleaned up automatically on EXIT.
24+
# _k8s_file <rel-path> Echo a local path to a kubernetes/ file; in remote
25+
# mode the file is downloaded to a shared temp directory.
26+
# The temp directory is cleaned up automatically on EXIT.
2727
#
2828
# Prerequisites
2929
# ─────────────
@@ -77,27 +77,36 @@ _k8s() {
7777
# _k8s_file <rel-path>
7878
# Return a local filesystem path to any kubernetes/ file.
7979
# Local: absolute filesystem path (no download needed).
80-
# Remote: download to a temp file and return its path.
81-
# All temp files are registered for cleanup on EXIT.
80+
# Remote: download into a shared temp directory and return the path.
81+
# The temp directory is created once in the main shell so the EXIT trap
82+
# can clean it up reliably — even though callers use $() subshells.
8283
#
8384
# Use this (not _k8s) when you need a real file on disk:
8485
# helm upgrade --values "$(_k8s_file ingress/traefik-values.yaml)"
8586
# envsubst < "$(_k8s_file cert-manager/clusterissuer.yaml)" | kubectl apply -f -
86-
_run_tmp_files=()
87+
#
88+
# macOS note: BSD mktemp requires X's at the very end of the template (no suffix).
89+
# Using a directory sidesteps the issue entirely — files are named by their path.
90+
if [[ "${_RUN_REMOTE}" -eq 1 ]]; then
91+
_run_tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/k8s-XXXXXXXX")"
92+
else
93+
_run_tmp_dir=""
94+
fi
95+
8796
_k8s_file() {
8897
if [[ "${_RUN_REMOTE}" -eq 0 ]]; then
8998
echo "${_RUN_REPO}/kubernetes/$1"
9099
else
91-
local tmpf
92-
tmpf="$(mktemp /tmp/k8s-file-XXXXXXXX.yaml)"
93-
_run_tmp_files+=("${tmpf}")
94-
curl -fsSL "${K3S_LAB_RAW}/kubernetes/$1" -o "${tmpf}"
95-
echo "${tmpf}"
100+
# Flatten the relative path to a safe filename (e.g. ingress/foo.yaml → ingress-foo.yaml)
101+
local dst
102+
dst="${_run_tmp_dir}/$(echo "$1" | tr '/' '-')"
103+
curl -fsSL "${K3S_LAB_RAW}/kubernetes/$1" -o "${dst}"
104+
echo "${dst}"
96105
fi
97106
}
98107

99-
# Register cleanup of any temp files created by _helm_val.
108+
# Cleanup: remove the shared temp directory on script exit.
100109
_run_mode_cleanup() {
101-
[[ "${#_run_tmp_files[@]}" -gt 0 ]] && rm -f "${_run_tmp_files[@]}" 2>/dev/null || true
110+
[[ -n "${_run_tmp_dir:-}" ]] && rm -rf "${_run_tmp_dir}" 2>/dev/null || true
102111
}
103112
trap _run_mode_cleanup EXIT

0 commit comments

Comments
 (0)