Skip to content

Commit 543fd65

Browse files
committed
Add kubectl kubernetes client wrapper
Will connect to a running "k3s" or "k8s" instance. Similar to other wrappers, but uses conf not sock. Signed-off-by: Anders F Björklund <[email protected]>
1 parent 74ee986 commit 543fd65

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ binaries: clean \
4646
_output/bin/apptainer.lima \
4747
_output/bin/docker.lima \
4848
_output/bin/podman.lima \
49+
_output/bin/kubectl.lima \
4950
_output/share/lima/lima-guestagent.Linux-x86_64 \
5051
_output/share/lima/lima-guestagent.Linux-aarch64 \
5152
_output/share/lima/lima-guestagent.Linux-riscv64
@@ -87,6 +88,10 @@ _output/bin/podman.lima: ./cmd/podman.lima
8788
@mkdir -p _output/bin
8889
cp -a $^ $@
8990

91+
_output/bin/kubectl.lima: ./cmd/kubectl.lima
92+
@mkdir -p _output/bin
93+
cp -a $^ $@
94+
9095
.PHONY: _output/bin/limactl$(exe)
9196
_output/bin/limactl$(exe):
9297
# The hostagent must be compiled with CGO_ENABLED=1 so that net.LookupIP() in the DNS server
@@ -132,6 +137,7 @@ uninstall:
132137
"$(DEST)/bin/apptainer.lima" \
133138
"$(DEST)/bin/docker.lima" \
134139
"$(DEST)/bin/podman.lima" \
140+
"$(DEST)/bin/kubectl.lima" \
135141
"$(DEST)/share/lima" "$(DEST)/share/doc/lima"
136142
if [ "$$(readlink "$(DEST)/bin/nerdctl")" = "nerdctl.lima" ]; then rm "$(DEST)/bin/nerdctl"; fi
137143
if [ "$$(readlink "$(DEST)/bin/apptainer")" = "apptainer.lima" ]; then rm "$(DEST)/bin/apptainer"; fi

cmd/kubectl.lima

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
set -eu
3+
: "${LIMA_INSTANCE:=}"
4+
: "${KUBECTL:=kubectl}"
5+
6+
if [ -z "$LIMA_INSTANCE" ]; then
7+
if [ "$(limactl ls -f '{{.Status}}' k3s 2>/dev/null)" = "Running" ]; then
8+
LIMA_INSTANCE=k3s
9+
elif [ "$(limactl ls -f '{{.Status}}' k8s 2>/dev/null)" = "Running" ]; then
10+
LIMA_INSTANCE=k8s
11+
else
12+
echo "No k3s or k8s running instances found. Either start one with" >&2
13+
echo "limactl start --name=k3s template://k3s" >&2
14+
echo "limactl start --name=k8s template://k8s" >&2
15+
echo "or set LIMA_INSTANCE to the name of your Kubernetes instance" >&2
16+
exit 1
17+
fi
18+
elif [ "$(limactl ls -q "$LIMA_INSTANCE" 2>/dev/null)" != "$LIMA_INSTANCE" ]; then
19+
echo "instance \"$LIMA_INSTANCE\" does not exist, run \`limactl start --name=$LIMA_INSTANCE\` to create a new instance" >&2
20+
exit 1
21+
fi
22+
KUBECTL=$(command -v "$KUBECTL" || true)
23+
if [ -n "$KUBECTL" ]; then
24+
KUBECONFIG=$(limactl list "$LIMA_INSTANCE" --format '{{.Dir}}/copied-from-guest/kubeconfig.yaml')
25+
export KUBECONFIG
26+
exec "$KUBECTL" "$@"
27+
else
28+
export LIMA_INSTANCE
29+
exec lima sudo kubectl "$@"
30+
fi

0 commit comments

Comments
 (0)