|
21 | 21 | # _lib <name> Source a file from lib/ (e.g. _lib log.sh) |
22 | 22 | # _k8s <rel-path> Echo an absolute path or URL to a kubernetes/ manifest |
23 | 23 | # (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. |
27 | 27 | # |
28 | 28 | # Prerequisites |
29 | 29 | # ───────────── |
@@ -77,27 +77,36 @@ _k8s() { |
77 | 77 | # _k8s_file <rel-path> |
78 | 78 | # Return a local filesystem path to any kubernetes/ file. |
79 | 79 | # 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. |
82 | 83 | # |
83 | 84 | # Use this (not _k8s) when you need a real file on disk: |
84 | 85 | # helm upgrade --values "$(_k8s_file ingress/traefik-values.yaml)" |
85 | 86 | # 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 | + |
87 | 96 | _k8s_file() { |
88 | 97 | if [[ "${_RUN_REMOTE}" -eq 0 ]]; then |
89 | 98 | echo "${_RUN_REPO}/kubernetes/$1" |
90 | 99 | 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}" |
96 | 105 | fi |
97 | 106 | } |
98 | 107 |
|
99 | | -# Register cleanup of any temp files created by _helm_val. |
| 108 | +# Cleanup: remove the shared temp directory on script exit. |
100 | 109 | _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 |
102 | 111 | } |
103 | 112 | trap _run_mode_cleanup EXIT |
0 commit comments