fix: bridge docker k3s adapter to WSL API
This commit is contained in:
@@ -16,8 +16,11 @@ WORKDIR /app/src/components/microservices/k3sctl-adapter
|
||||
COPY src/components/shared /app/src/components/shared
|
||||
COPY src/components/microservices/k3sctl-adapter/package.json ./package.json
|
||||
COPY src/components/microservices/k3sctl-adapter/tsconfig.json ./tsconfig.json
|
||||
COPY src/components/microservices/k3sctl-adapter/entrypoint.sh ./entrypoint.sh
|
||||
COPY src/components/microservices/k3sctl-adapter/src ./src
|
||||
COPY src/components/microservices/k3sctl-adapter/k3s ./k3s
|
||||
RUN chmod 755 ./entrypoint.sh
|
||||
|
||||
EXPOSE 4266
|
||||
ENTRYPOINT ["./entrypoint.sh"]
|
||||
CMD ["bun", "--smol", "run", "src/index.ts"]
|
||||
|
||||
@@ -8,10 +8,11 @@ services:
|
||||
K3SCTL_ADAPTER_BASE_IMAGE: ${K3SCTL_ADAPTER_BASE_IMAGE:-oven/bun:1-debian}
|
||||
container_name: k3sctl-adapter
|
||||
restart: unless-stopped
|
||||
network_mode: host
|
||||
env_file:
|
||||
- path: ${K3SCTL_ADAPTER_ENV_FILE:-../../../../.state/k3sctl-adapter-d601.env}
|
||||
required: false
|
||||
ports:
|
||||
- "127.0.0.1:${K3SCTL_ADAPTER_HOST_PORT:-4266}:4266"
|
||||
environment:
|
||||
HOST: "0.0.0.0"
|
||||
PORT: "4266"
|
||||
@@ -22,14 +23,33 @@ services:
|
||||
K3SCTL_KUBE_API_PROXY_ENABLED: "${K3SCTL_KUBE_API_PROXY_ENABLED:-true}"
|
||||
K3SCTL_KUBECONFIG_PATH: "/var/lib/unidesk/k3s/kubeconfig"
|
||||
K3SCTL_KUBE_API_CONNECT_HOST: "${K3SCTL_KUBE_API_CONNECT_HOST:-127.0.0.1}"
|
||||
K3SCTL_KUBE_API_SSH_TUNNEL_ENABLED: "${K3SCTL_KUBE_API_SSH_TUNNEL_ENABLED:-true}"
|
||||
K3SCTL_KUBE_API_SSH_HOST: "${K3SCTL_KUBE_API_SSH_HOST:-host.docker.internal}"
|
||||
K3SCTL_KUBE_API_SSH_USER: "${K3SCTL_KUBE_API_SSH_USER:-ubuntu}"
|
||||
K3SCTL_KUBE_API_SSH_KEY: "${K3SCTL_KUBE_API_SSH_KEY:-/run/host-ssh/id_ed25519}"
|
||||
K3SCTL_KUBE_API_LOCAL_HOST: "${K3SCTL_KUBE_API_LOCAL_HOST:-127.0.0.1}"
|
||||
K3SCTL_KUBE_API_LOCAL_PORT: "${K3SCTL_KUBE_API_LOCAL_PORT:-6443}"
|
||||
K3SCTL_KUBE_API_REMOTE_HOST: "${K3SCTL_KUBE_API_REMOTE_HOST:-127.0.0.1}"
|
||||
K3SCTL_KUBE_API_REMOTE_PORT: "${K3SCTL_KUBE_API_REMOTE_PORT:-6443}"
|
||||
K3SCTL_MANIFEST_PATHS: "${K3SCTL_MANIFEST_PATHS:-k3s/code-queue.k3s.json,k3s/mdtodo.k3s.json}"
|
||||
K3SCTL_SERVICES_JSON: "${K3SCTL_SERVICES_JSON:-[]}"
|
||||
UNIDESK_LOG_RETENTION_BYTES: "${UNIDESK_LOG_RETENTION_BYTES:-512MiB}"
|
||||
volumes:
|
||||
- ${K3SCTL_ADAPTER_LOG_DIR:-../../../../.state/k3sctl-adapter/logs}:/var/log/unidesk
|
||||
- ${K3SCTL_KUBECONFIG_HOST_PATH:-/etc/rancher/k3s/k3s.yaml}:/var/lib/unidesk/k3s/kubeconfig:ro
|
||||
- ${K3SCTL_HOST_SSH_KEY_DIR:-/home/ubuntu/.web-terminal/wsl-ssh}:/run/host-ssh:ro
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
networks:
|
||||
- default
|
||||
- provider-gateway
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "curl -fsS --max-time 2 http://127.0.0.1:4266/health >/dev/null"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
|
||||
networks:
|
||||
provider-gateway:
|
||||
external: true
|
||||
name: ${K3SCTL_PROVIDER_GATEWAY_NETWORK:-unidesk-provider-d601_default}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
log() {
|
||||
printf '%s\n' "$*" >&2
|
||||
}
|
||||
|
||||
is_true() {
|
||||
case "$(printf '%s' "${1:-}" | tr '[:upper:]' '[:lower:]')" in
|
||||
1|true|yes|on) return 0 ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
wait_for_kube_api_tunnel() {
|
||||
local_host="${K3SCTL_KUBE_API_LOCAL_HOST:-127.0.0.1}"
|
||||
local_port="${K3SCTL_KUBE_API_LOCAL_PORT:-6443}"
|
||||
attempts="${K3SCTL_KUBE_API_SSH_TUNNEL_WAIT_ATTEMPTS:-30}"
|
||||
index=1
|
||||
while [ "$index" -le "$attempts" ]; do
|
||||
status="$(curl -ksS --connect-timeout 2 --max-time 4 -o /dev/null -w '%{http_code}' "https://${local_host}:${local_port}/version" 2>/dev/null || true)"
|
||||
case "$status" in
|
||||
200|401|403)
|
||||
log "k3sctl-adapter: kube api tunnel reachable status=${status}"
|
||||
return 0
|
||||
;;
|
||||
esac
|
||||
sleep 1
|
||||
index=$((index + 1))
|
||||
done
|
||||
log "k3sctl-adapter: kube api tunnel did not become reachable at ${local_host}:${local_port}"
|
||||
return 1
|
||||
}
|
||||
|
||||
start_kube_api_ssh_tunnel() {
|
||||
if ! is_true "${K3SCTL_KUBE_API_SSH_TUNNEL_ENABLED:-false}"; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
ssh_host="${K3SCTL_KUBE_API_SSH_HOST:-host.docker.internal}"
|
||||
ssh_user="${K3SCTL_KUBE_API_SSH_USER:-ubuntu}"
|
||||
ssh_key="${K3SCTL_KUBE_API_SSH_KEY:-/run/host-ssh/id_ed25519}"
|
||||
local_host="${K3SCTL_KUBE_API_LOCAL_HOST:-127.0.0.1}"
|
||||
local_port="${K3SCTL_KUBE_API_LOCAL_PORT:-6443}"
|
||||
remote_host="${K3SCTL_KUBE_API_REMOTE_HOST:-127.0.0.1}"
|
||||
remote_port="${K3SCTL_KUBE_API_REMOTE_PORT:-6443}"
|
||||
restart_delay="${K3SCTL_KUBE_API_SSH_RESTART_DELAY_SECONDS:-2}"
|
||||
|
||||
if [ ! -r "$ssh_key" ]; then
|
||||
log "k3sctl-adapter: SSH tunnel key is not readable: ${ssh_key}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p /tmp/k3sctl-ssh
|
||||
(
|
||||
while :; do
|
||||
log "k3sctl-adapter: starting kube api SSH tunnel ${local_host}:${local_port}->${remote_host}:${remote_port} via ${ssh_user}@${ssh_host}"
|
||||
ssh \
|
||||
-i "$ssh_key" \
|
||||
-o BatchMode=yes \
|
||||
-o StrictHostKeyChecking=no \
|
||||
-o UserKnownHostsFile=/tmp/k3sctl-ssh/known_hosts \
|
||||
-o ExitOnForwardFailure=yes \
|
||||
-o ServerAliveInterval=15 \
|
||||
-o ServerAliveCountMax=3 \
|
||||
-N \
|
||||
-L "${local_host}:${local_port}:${remote_host}:${remote_port}" \
|
||||
"${ssh_user}@${ssh_host}" || true
|
||||
log "k3sctl-adapter: kube api SSH tunnel exited; restarting in ${restart_delay}s"
|
||||
sleep "$restart_delay"
|
||||
done
|
||||
) &
|
||||
|
||||
wait_for_kube_api_tunnel
|
||||
}
|
||||
|
||||
start_kube_api_ssh_tunnel
|
||||
exec "$@"
|
||||
Reference in New Issue
Block a user