Skip to content

Commit 1c919fb

Browse files
fix(setup): only manage k8s lifecycle on a verified-local context
Greptile: sim down/reset used the ambient kube-context, so switching context after setup could uninstall a same-named sim-dev release from the wrong cluster. Gate k8sInstall on the same locality check the wizard uses (API server is loopback/docker-internal) via a shared isLocalKubeContext helper — the wizard only ever deploys locally, so a remote current-context is never treated as a Sim install.
1 parent 2136a03 commit 1c919fb

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

scripts/setup/lifecycle.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { spawnSync } from 'node:child_process'
22
import { DB_CONTAINER, type Detection, REDIS_CONTAINER, runDetection } from './detect.ts'
33
import { archiveEnvFile, ROOT } from './env-files.ts'
44
import { SetupError } from './errors.ts'
5+
import { isLocalKubeContext } from './modes/k8s.ts'
56
import { httpHealth } from './probes.ts'
67
import * as p from './prompter.ts'
78
import { glyph, theme } from './theme.ts'
@@ -93,7 +94,11 @@ function devInstall(detection: Detection): DevInstall | null {
9394

9495
function k8sInstall(detection: Detection): K8sInstall | null {
9596
const context = detection.kubeContext
96-
if (!context) return null
97+
// Only manage k8s on a verified-local context. The ambient current-context can
98+
// change after setup, and the wizard only ever deploys to a local cluster — so
99+
// gating on locality stops `down`/`reset` from uninstalling a same-named release
100+
// from a remote cluster the user happens to be pointed at.
101+
if (!context || !isLocalKubeContext(context)) return null
97102
const status = spawnSync(
98103
'helm',
99104
['status', K8S_RELEASE, '--kube-context', context, '-n', K8S_NAMESPACE],

scripts/setup/modes/k8s.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ const LOCAL_SERVER_HOSTS = new Set([
3838
'host.docker.internal',
3939
])
4040

41+
/** True when a context's API server is a loopback/host address — i.e. a local cluster. */
42+
export function isLocalKubeContext(context: string): boolean {
43+
const server = contextServerHost(context)
44+
return server !== null && LOCAL_SERVER_HOSTS.has(server)
45+
}
46+
4147
/** The API server host a context points at, or null if kubectl can't resolve it. */
4248
function contextServerHost(context: string): string | null {
4349
const result = spawnSync(

0 commit comments

Comments
 (0)