Skip to content

Commit

Permalink
Add kubectl kubernetes client wrapper
Browse files Browse the repository at this point in the history
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 <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Jan 16, 2023
1 parent e4ede4f commit d16e4b2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ binaries: clean \
_output/bin/apptainer.lima \
_output/bin/docker.lima \
_output/bin/podman.lima \
_output/bin/kubectl.lima \
_output/share/lima/lima-guestagent.Linux-x86_64 \
_output/share/lima/lima-guestagent.Linux-aarch64 \
_output/share/lima/lima-guestagent.Linux-riscv64
Expand Down Expand Up @@ -87,6 +88,10 @@ _output/bin/podman.lima: ./cmd/podman.lima
@mkdir -p _output/bin
cp -a $^ $@

_output/bin/kubectl.lima: ./cmd/kubectl.lima
@mkdir -p _output/bin
cp -a $^ $@

.PHONY: _output/bin/limactl$(exe)
_output/bin/limactl$(exe):
# The hostagent must be compiled with CGO_ENABLED=1 so that net.LookupIP() in the DNS server
Expand Down Expand Up @@ -132,6 +137,7 @@ uninstall:
"$(DEST)/bin/apptainer.lima" \
"$(DEST)/bin/docker.lima" \
"$(DEST)/bin/podman.lima" \
"$(DEST)/bin/kubectl.lima" \
"$(DEST)/share/lima" "$(DEST)/share/doc/lima"
if [ "$$(readlink "$(DEST)/bin/nerdctl")" = "nerdctl.lima" ]; then rm "$(DEST)/bin/nerdctl"; fi
if [ "$$(readlink "$(DEST)/bin/apptainer")" = "apptainer.lima" ]; then rm "$(DEST)/bin/apptainer"; fi
Expand Down
27 changes: 27 additions & 0 deletions cmd/kubectl.lima
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/sh
set -eu
: "${LIMA_INSTANCE:=}"
: "${KUBECTL:=kubectl}"

if [ -z "$LIMA_INSTANCE" ]; then
if [ "$(limactl ls -f '{{.Status}}' k3s 2>/dev/null)" = "Running" ]; then
LIMA_INSTANCE=k3s
elif [ "$(limactl ls -f '{{.Status}}' k8s 2>/dev/null)" = "Running" ]; then
LIMA_INSTANCE=k8s
else
echo "no running instance found, run \`limactl start\` to start an instance" >&2
exit 1
fi
elif [ "$(limactl ls -q "$LIMA_INSTANCE" 2>/dev/null)" != "$LIMA_INSTANCE" ]; then
echo "instance \"$LIMA_INSTANCE\" does not exist, run \`limactl start --name=$LIMA_INSTANCE\` to create a new instance" >&2
exit 1
fi
KUBECTL=$(command -v "$KUBECTL" || true)
if [ -n "$KUBECTL" ]; then
KUBECONFIG=$(limactl list "$LIMA_INSTANCE" --format '{{.Dir}}/conf/kubeconfig.yaml')
export KUBECONFIG
exec "$KUBECTL" "$@"
else
export LIMA_INSTANCE
exec lima sudo kubectl "$@"
fi

0 comments on commit d16e4b2

Please sign in to comment.