From 2327e42cdd1a26d72dcd1ece0e879c4963abe339 Mon Sep 17 00:00:00 2001 From: kasun Date: Sun, 6 Dec 2026 02:33:21 +0100 Subject: [PATCH] feature(k8s-bootstrap): remove the CI SSH key from nodes after bootstrap The key is only needed during bootstrap, but it stayed on every node with passwordless sudo. A final step now removes it once all SSH steps are done. Needs the ciRunnerPublicKey output from proxmox-infra, so deploy that first. Also moves the repeated start, SSH-wait and join commands into helpers. Resource names are unchanged, so nothing re-runs on the live cluster. --- 02-k8s-bootstrap/index.ts | 354 +++++++++++++++++++++----------------- 1 file changed, 193 insertions(+), 161 deletions(-) diff --git a/02-k8s-bootstrap/index.ts b/02-k8s-bootstrap/index.ts index 8e15b3d..9cd3f28 100644 --- a/02-k8s-bootstrap/index.ts +++ b/02-k8s-bootstrap/index.ts @@ -87,6 +87,9 @@ const vmIdsOutput = infraRef.requireOutput("vmIds") as pulumi.Output< const ciRunnerPrivateKey = infraRef.requireOutput( "ciRunnerPrivateKey", ) as pulumi.Output; +const ciRunnerPublicKey = infraRef.requireOutput( + "ciRunnerPublicKey", +) as pulumi.Output; const master1VmId = vmIdsOutput.apply((ids) => String(ids.master1)); const master2VmId = vmIdsOutput.apply((ids) => String(ids.master2)); @@ -120,42 +123,56 @@ function startVmScript( ' "$PVE_API_TOKEN"; }; is_running() { auth | curl -sf -K - --cacert "$ca" "$base/current" | grep -Eq '"status" *: *"running"'; }; is_running && exit 0; auth | curl -sf -K - --cacert "$ca" -X POST "$base/start" >/dev/null || exit 1; for i in $(seq 1 30); do is_running && exit 0; sleep 2; done; exit 1`; } -const startMaster1 = new command.local.Command("start-master-1", { - create: startVmScript(pve1Endpoint, "pve1", "pve", master1VmId), - environment: { PVE_API_TOKEN: pve1ApiToken }, - triggers: [master1VmId], - interpreter: ["/bin/bash", "-c"], -}); -const startMaster2 = new command.local.Command("start-master-2", { - create: startVmScript(pve3Endpoint, "pve3", "pve", master2VmId), - environment: { PVE_API_TOKEN: pve3ApiToken }, - triggers: [master2VmId], - interpreter: ["/bin/bash", "-c"], -}); -const startWorker1 = new command.local.Command("start-worker-1", { - create: startVmScript(pve1Endpoint, "pve1", "pve", worker1VmId), - environment: { PVE_API_TOKEN: pve1ApiToken }, - triggers: [worker1VmId], - interpreter: ["/bin/bash", "-c"], -}); -const startMaster3 = new command.local.Command("start-master-3", { - create: startVmScript(pve2Endpoint, "pve2", "pve-bckp", master3VmId), - environment: { PVE_API_TOKEN: pve2ApiToken }, - triggers: [master3VmId], - interpreter: ["/bin/bash", "-c"], -}); -const startWorker2 = new command.local.Command("start-worker-2", { - create: startVmScript(pve2Endpoint, "pve2", "pve-bckp", worker2VmId), - environment: { PVE_API_TOKEN: pve2ApiToken }, - triggers: [worker2VmId], - interpreter: ["/bin/bash", "-c"], -}); -const startWorker3 = new command.local.Command("start-worker-3", { - create: startVmScript(pve3Endpoint, "pve3", "pve", worker3VmId), - environment: { PVE_API_TOKEN: pve3ApiToken }, - triggers: [worker3VmId], - interpreter: ["/bin/bash", "-c"], -}); +function startVm( + name: string, + create: pulumi.Input, + apiToken: pulumi.Input, + vmId: pulumi.Input, +): command.local.Command { + return new command.local.Command(name, { + create, + environment: { PVE_API_TOKEN: apiToken }, + triggers: [vmId], + interpreter: ["/bin/bash", "-c"], + }); +} + +const startMaster1 = startVm( + "start-master-1", + startVmScript(pve1Endpoint, "pve1", "pve", master1VmId), + pve1ApiToken, + master1VmId, +); +const startMaster2 = startVm( + "start-master-2", + startVmScript(pve3Endpoint, "pve3", "pve", master2VmId), + pve3ApiToken, + master2VmId, +); +const startWorker1 = startVm( + "start-worker-1", + startVmScript(pve1Endpoint, "pve1", "pve", worker1VmId), + pve1ApiToken, + worker1VmId, +); +const startMaster3 = startVm( + "start-master-3", + startVmScript(pve2Endpoint, "pve2", "pve-bckp", master3VmId), + pve2ApiToken, + master3VmId, +); +const startWorker2 = startVm( + "start-worker-2", + startVmScript(pve2Endpoint, "pve2", "pve-bckp", worker2VmId), + pve2ApiToken, + worker2VmId, +); +const startWorker3 = startVm( + "start-worker-3", + startVmScript(pve3Endpoint, "pve3", "pve", worker3VmId), + pve3ApiToken, + worker3VmId, +); const allStarts = [ startMaster1, @@ -170,14 +187,29 @@ const allStarts = [ // Step 2: Wait for SSH on master-1, install k3s // --------------------------------------------------------------------------- -const waitMaster1Ssh = new command.local.Command( +// Succeeds once port 22 answers, polling every 5 s for up to 5 min. +function waitSsh( + name: string, + ip: pulumi.Input, + vmId: pulumi.Input, + dependsOn: pulumi.ResourceOptions["dependsOn"], +): command.local.Command { + return new command.local.Command( + name, + { + create: pulumi.interpolate`for i in $(seq 1 60); do (timeout 5 bash -c "echo > /dev/tcp/${ip}/22") 2>/dev/null && exit 0; sleep 5; done; exit 1`, + triggers: [vmId], + interpreter: ["/bin/bash", "-c"], + }, + { dependsOn }, + ); +} + +const waitMaster1Ssh = waitSsh( "wait-ssh-master-1", - { - create: pulumi.interpolate`for i in $(seq 1 60); do (timeout 5 bash -c "echo > /dev/tcp/${master1Ip}/22") 2>/dev/null && exit 0; sleep 5; done; exit 1`, - triggers: [master1VmId], - interpreter: ["/bin/bash", "-c"], - }, - { dependsOn: allStarts }, + master1Ip, + master1VmId, + allStarts, ); const installMaster1 = new command.remote.Command( @@ -233,138 +265,105 @@ const waitControlPlaneVipReady = new command.remote.Command( // Step 4: Wait for SSH on remaining masters, join cluster via the VIP // --------------------------------------------------------------------------- -const waitMaster2Ssh = new command.local.Command( - "wait-ssh-master-2", - { - create: pulumi.interpolate`for i in $(seq 1 60); do (timeout 5 bash -c "echo > /dev/tcp/${master2Ip}/22") 2>/dev/null && exit 0; sleep 5; done; exit 1`, - triggers: [master2VmId], - interpreter: ["/bin/bash", "-c"], - }, - { dependsOn: [waitK3sMaster1Ready] }, -); +const waitMaster2Ssh = waitSsh("wait-ssh-master-2", master2Ip, master2VmId, [ + waitK3sMaster1Ready, +]); -const waitMaster3Ssh = new command.local.Command( - "wait-ssh-master-3", - { - create: pulumi.interpolate`for i in $(seq 1 60); do (timeout 5 bash -c "echo > /dev/tcp/${master3Ip}/22") 2>/dev/null && exit 0; sleep 5; done; exit 1`, - triggers: [master3VmId], - interpreter: ["/bin/bash", "-c"], - }, - { dependsOn: [waitK3sMaster1Ready] }, -); +const waitMaster3Ssh = waitSsh("wait-ssh-master-3", master3Ip, master3VmId, [ + waitK3sMaster1Ready, +]); -const joinMaster2 = new command.remote.Command( - "join-k3s-master-2", - { - connection: conn(master2Ip), - create: k3sInstallScript( - "", - pulumi.interpolate`server --server https://${controlPlaneVipAddress}:6443 --disable servicelb ${secretsEncryptionArgs} --node-name k3s-master-2`, - ), - stdin: k3sToken, - triggers: [master2VmId], - }, - { dependsOn: [waitMaster2Ssh, waitControlPlaneVipReady] }, -); +// Joins an extra control-plane node through the VIP. Every master must get the +// same server flags, so they live here, not at the call site. +function joinMaster( + nodeName: string, + ip: pulumi.Input, + vmId: pulumi.Input, + dependsOn: pulumi.ResourceOptions["dependsOn"], +): command.remote.Command { + return new command.remote.Command( + `join-${nodeName}`, + { + connection: conn(ip), + create: k3sInstallScript( + "", + pulumi.interpolate`server --server https://${controlPlaneVipAddress}:6443 --disable servicelb ${secretsEncryptionArgs} --node-name ${nodeName}`, + ), + stdin: k3sToken, + triggers: [vmId], + }, + { dependsOn }, + ); +} -const joinMaster3 = new command.remote.Command( - "join-k3s-master-3", - { - connection: conn(master3Ip), - create: k3sInstallScript( - "", - pulumi.interpolate`server --server https://${controlPlaneVipAddress}:6443 --disable servicelb ${secretsEncryptionArgs} --node-name k3s-master-3`, - ), - stdin: k3sToken, - triggers: [master3VmId], - }, - { dependsOn: [waitMaster3Ssh, waitControlPlaneVipReady, joinMaster2] }, -); +const joinMaster2 = joinMaster("k3s-master-2", master2Ip, master2VmId, [ + waitMaster2Ssh, + waitControlPlaneVipReady, +]); + +const joinMaster3 = joinMaster("k3s-master-3", master3Ip, master3VmId, [ + waitMaster3Ssh, + waitControlPlaneVipReady, + joinMaster2, +]); // --------------------------------------------------------------------------- // Step 5: Join worker nodes via the VIP // --------------------------------------------------------------------------- -const waitWorker1Ssh = new command.local.Command( - "wait-ssh-worker-1", - { - create: pulumi.interpolate`for i in $(seq 1 60); do (timeout 5 bash -c "echo > /dev/tcp/${worker1Ip}/22") 2>/dev/null && exit 0; sleep 5; done; exit 1`, - triggers: [worker1VmId], - interpreter: ["/bin/bash", "-c"], - }, - { dependsOn: [joinMaster3] }, -); +const waitWorker1Ssh = waitSsh("wait-ssh-worker-1", worker1Ip, worker1VmId, [ + joinMaster3, +]); -const waitWorker2Ssh = new command.local.Command( - "wait-ssh-worker-2", - { - create: pulumi.interpolate`for i in $(seq 1 60); do (timeout 5 bash -c "echo > /dev/tcp/${worker2Ip}/22") 2>/dev/null && exit 0; sleep 5; done; exit 1`, - triggers: [worker2VmId], - interpreter: ["/bin/bash", "-c"], - }, - { dependsOn: [joinMaster3] }, -); +const waitWorker2Ssh = waitSsh("wait-ssh-worker-2", worker2Ip, worker2VmId, [ + joinMaster3, +]); -const waitWorker3Ssh = new command.local.Command( - "wait-ssh-worker-3", - { - create: pulumi.interpolate`for i in $(seq 1 60); do (timeout 5 bash -c "echo > /dev/tcp/${worker3Ip}/22") 2>/dev/null && exit 0; sleep 5; done; exit 1`, - triggers: [worker3VmId], - interpreter: ["/bin/bash", "-c"], - }, - { dependsOn: [joinMaster3] }, -); +const waitWorker3Ssh = waitSsh("wait-ssh-worker-3", worker3Ip, worker3VmId, [ + joinMaster3, +]); -const joinWorker1 = new command.remote.Command( - "join-k3s-worker-1", - { - connection: conn(worker1Ip), - create: k3sInstallScript( - pulumi.interpolate`K3S_URL=https://${controlPlaneVipAddress}:6443`, - "--node-name k3s-worker-1", - ), - stdin: k3sToken, - triggers: [worker1VmId], - }, - { dependsOn: [waitWorker1Ssh] }, -); +function joinWorker( + nodeName: string, + ip: pulumi.Input, + vmId: pulumi.Input, + dependsOn: pulumi.ResourceOptions["dependsOn"], +): command.remote.Command { + return new command.remote.Command( + `join-${nodeName}`, + { + connection: conn(ip), + create: k3sInstallScript( + pulumi.interpolate`K3S_URL=https://${controlPlaneVipAddress}:6443`, + `--node-name ${nodeName}`, + ), + stdin: k3sToken, + triggers: [vmId], + }, + { dependsOn }, + ); +} -const joinWorker2 = new command.remote.Command( - "join-k3s-worker-2", - { - connection: conn(worker2Ip), - create: k3sInstallScript( - pulumi.interpolate`K3S_URL=https://${controlPlaneVipAddress}:6443`, - "--node-name k3s-worker-2", - ), - stdin: k3sToken, - triggers: [worker2VmId], - }, - { dependsOn: [waitWorker2Ssh] }, -); +const joinWorker1 = joinWorker("k3s-worker-1", worker1Ip, worker1VmId, [ + waitWorker1Ssh, +]); -const joinWorker3 = new command.remote.Command( - "join-k3s-worker-3", - { - connection: conn(worker3Ip), - create: k3sInstallScript( - pulumi.interpolate`K3S_URL=https://${controlPlaneVipAddress}:6443`, - "--node-name k3s-worker-3", - ), - stdin: k3sToken, - triggers: [worker3VmId], - }, - { dependsOn: [waitWorker3Ssh] }, -); +const joinWorker2 = joinWorker("k3s-worker-2", worker2Ip, worker2VmId, [ + waitWorker2Ssh, +]); + +const joinWorker3 = joinWorker("k3s-worker-3", worker3Ip, worker3VmId, [ + waitWorker3Ssh, +]); // --------------------------------------------------------------------------- // Step 6: Format and mount Longhorn disks // --------------------------------------------------------------------------- const workers = [worker1Ip, worker2Ip, worker3Ip]; -const workerSshReader = [waitWorker1Ssh, waitWorker2Ssh, waitWorker3Ssh]; +const workerSshReady = [waitWorker1Ssh, waitWorker2Ssh, waitWorker3Ssh]; -workers.forEach((worker, i) => { +const longhornMounts = workers.map((worker, i) => { return new command.remote.Command( `mount-longhorn-disk-${i}`, { @@ -382,7 +381,7 @@ workers.forEach((worker, i) => { mountpoint -q /mnt/longhorn-extra; df -h /mnt/longhorn-extra`, }, - { dependsOn: workerSshReader[i] }, + { dependsOn: workerSshReady[i] }, ); }); @@ -446,16 +445,49 @@ const allVms = [ }, ]; -allVms.forEach(({ id, ip, name, readiness }) => { +const packageInstalls = allVms.map( + ({ id, ip, name, readiness }) => + new command.remote.Command( + `install-packages-${name}`, + { + connection: conn(ip), + // cloud-init exit code 2 means it finished with recoverable errors. + create: pulumi.interpolate`{ sudo cloud-init status --wait >/dev/null || [ $? -eq 2 ]; } && sudo apt-get -o DPkg::Lock::Timeout=300 update && sudo apt-get -o DPkg::Lock::Timeout=300 install -y qemu-guest-agent && sudo systemctl enable --now qemu-guest-agent`, + triggers: [id], + }, + { dependsOn: readiness }, + ), +); + +// --------------------------------------------------------------------------- +// Step 9: Remove the CI runner's SSH key from every node +// --------------------------------------------------------------------------- + +// The key is only needed for the steps above, so it shouldn't stay valid on +// the nodes afterwards. A rebuilt VM gets it again from cloud-init, so a fresh +// bootstrap still works. +const sshSteps = [getKubeconfig, ...longhornMounts, ...packageInstalls]; + +const revokeCiKeyScript = `set -euo pipefail +IFS= read -r KEY || true +[ -n "$KEY" ] +f="$HOME/.ssh/authorized_keys" +grep -vxF -- "$KEY" "$f" > "$f.new" || [ $? -eq 1 ] +[ -s "$f.new" ] +chmod 600 "$f.new" +mv "$f.new" "$f" +if grep -qxF -- "$KEY" "$f"; then exit 1; fi`; + +allVms.forEach(({ id, ip, name }) => { new command.remote.Command( - `install-packages-${name}`, + `revoke-ci-key-${name}`, { connection: conn(ip), - // cloud-init exit code 2 means it finished with recoverable errors. - create: pulumi.interpolate`{ sudo cloud-init status --wait >/dev/null || [ $? -eq 2 ]; } && sudo apt-get -o DPkg::Lock::Timeout=300 update && sudo apt-get -o DPkg::Lock::Timeout=300 install -y qemu-guest-agent && sudo systemctl enable --now qemu-guest-agent`, + create: revokeCiKeyScript, + stdin: ciRunnerPublicKey, triggers: [id], }, - { dependsOn: readiness }, + { dependsOn: sshSteps }, ); }); -- 2.54.1