| 1 | #!/bin/sh |
| 2 | set -eu |
| 3 | |
| 4 | # Environment Variables |
| 5 | # LIMA_INSTANCE: Specifies the name of the Lima instance to use. Default is empty. |
| 6 | |
| 7 | : "${LIMA_INSTANCE:=}" |
| 8 | : "${KUBECTL:=kubectl}" |
| 9 | |
| 10 | if [ -z "$LIMA_INSTANCE" ]; then |
| 11 | if [ "$(limactl ls -q k3s 2>/dev/null)" = "k3s" ]; then |
| 12 | LIMA_INSTANCE=k3s |
| 13 | elif [ "$(limactl ls -q k8s 2>/dev/null)" = "k8s" ]; then |
| 14 | LIMA_INSTANCE=k8s |
| 15 | else |
| 16 | echo "No k3s or k8s existing instances found. Either create one with" >&2 |
| 17 | echo "limactl create --name=k3s template:k3s" >&2 |
| 18 | echo "limactl create --name=k8s template:k8s" >&2 |
| 19 | echo "or set LIMA_INSTANCE to the name of your Kubernetes instance" >&2 |
| 20 | exit 1 |
| 21 | fi |
| 22 | if [ "$(limactl ls -f '{{.Status}}' "$LIMA_INSTANCE" 2>/dev/null)" != "Running" ]; then |
| 23 | echo "instance \"$LIMA_INSTANCE\" is not running, run \`limactl start $LIMA_INSTANCE\` to start the existing instance" >&2 |
| 24 | exit 1 |
| 25 | fi |
| 26 | elif [ "$(limactl ls -q "$LIMA_INSTANCE" 2>/dev/null)" != "$LIMA_INSTANCE" ]; then |
| 27 | echo "instance \"$LIMA_INSTANCE\" does not exist, run \`limactl create --name=$LIMA_INSTANCE\` to create a new instance" >&2 |
| 28 | exit 1 |
| 29 | elif [ "$(limactl ls -f '{{ .Status }}' "$LIMA_INSTANCE" 2>/dev/null)" != "Running" ]; then |
| 30 | echo "instance \"$LIMA_INSTANCE\" is not running, run \`limactl start $LIMA_INSTANCE\` to start the existing instance" >&2 |
| 31 | exit 1 |
| 32 | fi |
| 33 | KUBECTL=$(command -v "$KUBECTL" || true) |
| 34 | if [ -n "$KUBECTL" ]; then |
| 35 | KUBECONFIG=$(limactl list "$LIMA_INSTANCE" --format '{{.Dir}}/copied-from-guest/kubeconfig.yaml') |
| 36 | export KUBECONFIG |
| 37 | exec "$KUBECTL" "$@" |
| 38 | else |
| 39 | export LIMA_INSTANCE |
| 40 | exec lima sudo kubectl "$@" |
| 41 | fi |