Skip to content

Commit ec44ca9

Browse files
authored
fix(cluster): run helm/kubectl inside container via docker exec (#255)
cluster-deploy-fast.sh ran helm and kubectl directly on the host, which requires a local kubeconfig pointing at the k3s API server. Since the cluster container does not expose port 6443, these commands always fail with 'connection refused'. Install helm in the cluster container image and run all helm/kubectl commands via docker exec with KUBECONFIG pre-configured. The local chart directory is copied into the container before helm upgrade.
1 parent 3936234 commit ec44ca9

2 files changed

Lines changed: 41 additions & 13 deletions

File tree

deploy/docker/Dockerfile.cluster

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
ARG K3S_VERSION=v1.35.2-k3s1
3333
ARG K9S_VERSION=v0.50.18
34+
ARG HELM_VERSION=v3.17.3
3435
ARG NVIDIA_CONTAINER_TOOLKIT_VERSION=1.18.2-1
3536

3637
# ---------------------------------------------------------------------------
@@ -50,6 +51,18 @@ RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certifi
5051
chmod +x /tmp/k9s && \
5152
rm -rf /var/lib/apt/lists/*
5253

54+
# ---------------------------------------------------------------------------
55+
# Stage 1c: Download helm binary for in-container chart upgrades
56+
# ---------------------------------------------------------------------------
57+
FROM ubuntu:24.04 AS helm
58+
ARG HELM_VERSION
59+
ARG TARGETARCH
60+
RUN apt-get update && apt-get install -y --no-install-recommends curl ca-certificates && \
61+
curl -fsSL "https://get.helm.sh/helm-${HELM_VERSION}-linux-${TARGETARCH}.tar.gz" \
62+
| tar xz --strip-components=1 -C /tmp "linux-${TARGETARCH}/helm" && \
63+
chmod +x /tmp/helm && \
64+
rm -rf /var/lib/apt/lists/*
65+
5366
# ---------------------------------------------------------------------------
5467
# Stage 2: Install NVIDIA container toolkit on Ubuntu
5568
# ---------------------------------------------------------------------------
@@ -99,6 +112,9 @@ COPY --from=k3s /bin/ /bin/
99112
# Copy k9s binary for interactive cluster debugging via `openshell doctor exec -- k9s`
100113
COPY --from=k9s /tmp/k9s /usr/local/bin/k9s
101114

115+
# Copy helm binary for in-container chart upgrades (used by cluster-deploy-fast.sh)
116+
COPY --from=helm /tmp/helm /usr/local/bin/helm
117+
102118
# Copy iptables/nftables tooling (xtables-nft-multi, iptables-detect.sh, etc.)
103119
# These are in /bin/aux/ in the k3s image and must be on PATH.
104120
# Note: the Ubuntu iptables package provides /usr/sbin/iptables, but k3s

tasks/scripts/cluster-deploy-fast.sh

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ if ! docker ps -q --filter "name=^${CONTAINER_NAME}$" --filter "health=healthy"
3636
exit 1
3737
fi
3838

39+
# Run a command inside the cluster container with KUBECONFIG pre-configured.
40+
cluster_exec() {
41+
docker exec "${CONTAINER_NAME}" sh -c "KUBECONFIG=/etc/rancher/k3s/k3s.yaml $*"
42+
}
43+
44+
# Path inside the container where the chart is copied for helm upgrades.
45+
CONTAINER_CHART_DIR=/tmp/openshell-chart
46+
3947
build_gateway=0
4048
build_sandbox=0
4149
needs_helm_upgrade=0
@@ -376,23 +384,27 @@ fi
376384
if [[ "${needs_helm_upgrade}" == "1" ]]; then
377385
helm_start=$(date +%s)
378386
echo "Upgrading helm release..."
379-
helm_wait_args=()
387+
helm_wait_args=""
380388
if [[ "${DEPLOY_FAST_HELM_WAIT}" == "1" ]]; then
381-
helm_wait_args+=(--wait)
389+
helm_wait_args="--wait"
382390
fi
383391

392+
# Copy the local chart source into the container so helm can read it.
393+
docker exec "${CONTAINER_NAME}" rm -rf "${CONTAINER_CHART_DIR}"
394+
docker cp deploy/helm/openshell "${CONTAINER_NAME}:${CONTAINER_CHART_DIR}"
395+
384396
# grpcEndpoint must be explicitly set to https:// because the chart always
385397
# terminates mTLS (there is no server.tls.enabled toggle). Without this,
386398
# a prior Helm override or chart default change could silently regress
387399
# sandbox callbacks to plaintext.
388400
# Retrieve the existing handshake secret from the running release, or generate
389401
# a new one if this is the first deploy with the mandatory secret.
390-
EXISTING_SECRET=$(helm get values openshell -n openshell -o json 2>/dev/null \
391-
| grep -o '"sshHandshakeSecret":"[^"]*"' \
392-
| cut -d'"' -f4) || true
402+
EXISTING_SECRET=$(cluster_exec "helm get values openshell -n openshell -o json 2>/dev/null \
403+
| grep -o '\"sshHandshakeSecret\":\"[^\"]*\"' \
404+
| cut -d'\"' -f4") || true
393405
SSH_HANDSHAKE_SECRET="${EXISTING_SECRET:-$(openssl rand -hex 32)}"
394406

395-
helm upgrade openshell deploy/helm/openshell \
407+
cluster_exec "helm upgrade openshell ${CONTAINER_CHART_DIR} \
396408
--namespace openshell \
397409
--set image.repository=${IMAGE_REPO_BASE}/gateway \
398410
--set image.tag=${IMAGE_TAG} \
@@ -403,20 +415,20 @@ if [[ "${needs_helm_upgrade}" == "1" ]]; then
403415
--set server.tls.clientCaSecretName=openshell-server-client-ca \
404416
--set server.tls.clientTlsSecretName=openshell-client-tls \
405417
--set server.sshHandshakeSecret=${SSH_HANDSHAKE_SECRET} \
406-
"${helm_wait_args[@]}"
418+
${helm_wait_args}"
407419
helm_end=$(date +%s)
408420
log_duration "Helm upgrade" "${helm_start}" "${helm_end}"
409421
fi
410422

411423
if [[ "${#pushed_images[@]}" -gt 0 ]]; then
412424
rollout_start=$(date +%s)
413425
echo "Restarting deployment to pick up updated images..."
414-
if kubectl get statefulset/openshell -n openshell >/dev/null 2>&1; then
415-
kubectl rollout restart statefulset/openshell -n openshell
416-
kubectl rollout status statefulset/openshell -n openshell
417-
elif kubectl get deployment/openshell -n openshell >/dev/null 2>&1; then
418-
kubectl rollout restart deployment/openshell -n openshell
419-
kubectl rollout status deployment/openshell -n openshell
426+
if cluster_exec "kubectl get statefulset/openshell -n openshell" >/dev/null 2>&1; then
427+
cluster_exec "kubectl rollout restart statefulset/openshell -n openshell"
428+
cluster_exec "kubectl rollout status statefulset/openshell -n openshell"
429+
elif cluster_exec "kubectl get deployment/openshell -n openshell" >/dev/null 2>&1; then
430+
cluster_exec "kubectl rollout restart deployment/openshell -n openshell"
431+
cluster_exec "kubectl rollout status deployment/openshell -n openshell"
420432
else
421433
echo "Warning: no openshell workload found to roll out in namespace 'openshell'."
422434
fi

0 commit comments

Comments
 (0)