From 6b92de4f7cff28fbe8de3e9c3c3000e92ae9a5bb Mon Sep 17 00:00:00 2001 From: phm07 <22707808+phm07@users.noreply.github.com> Date: Wed, 3 Jul 2024 14:43:00 +0200 Subject: [PATCH] use launchctl submit instead of writing file and use command -v instead of which --- cleanup.sh | 7 +++--- registry-port-forward.sh | 47 +++++++--------------------------------- 2 files changed, 11 insertions(+), 43 deletions(-) diff --git a/cleanup.sh b/cleanup.sh index 8caf21d..056fd26 100755 --- a/cleanup.sh +++ b/cleanup.sh @@ -3,11 +3,10 @@ set -ue -o pipefail run_launchd() { - FILEPATH="$HOME/Library/LaunchAgents/com.hetzner.cloud.k8sdev.registry-port-forward.plist" - launchctl unload "$FILEPATH" 2> /dev/null || true - rm -f "$FILEPATH" + label="k8s-registry-port-forward" + launchctl remove "$label" 2> /dev/null || true } -if which launchctl > /dev/null; then +if command -v launchctl > /dev/null; then run_launchd fi diff --git a/registry-port-forward.sh b/registry-port-forward.sh index 15c47d4..f9cf14c 100755 --- a/registry-port-forward.sh +++ b/registry-port-forward.sh @@ -18,50 +18,19 @@ run_systemd() { } run_launchd() { - FILEPATH="$HOME/Library/LaunchAgents/com.hetzner.cloud.k8sdev.registry-port-forward.plist" + label="k8s-registry-port-forward" - cat > "$FILEPATH" < - - - - Label - com.hetzner.cloud.k8sdev.registry-port-forward.plist - Program - $(which kubectl) - ProgramArguments - - kubectl - port-forward - -n - kube-system - svc/docker-registry - 30666:5000 - - EnvironmentVariables - - KUBECONFIG - $PWD/files/kubeconfig.yaml - - RunAtLoad - - KeepAlive - - SuccessfulExit - - - ThrottleInterval - 5 - - -EOF + launchctl remove "$label" 2> /dev/null || true - launchctl load "$FILEPATH" + launchctl submit \ + -l "$label" \ + -p "$(which kubectl)" \ + -- kubectl --kubeconfig="$PWD/files/kubeconfig.yaml" port-forward -n kube-system svc/docker-registry 30666:5000 } -if which systemctl > /dev/null; then +if command -v systemctl > /dev/null; then run_systemd -elif which launchctl > /dev/null; then +elif command -v launchctl > /dev/null; then run_launchd else echo "No supported init system found"