Skip to content

Commit 0bc32f0

Browse files
JackS1237Peter Steinberger
andauthored
fix: add Kubernetes resource-only teardown (#114953)
* fix: add Kubernetes resource-only teardown * fix(k8s): preserve shared namespaces on teardown --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 1c9e78a commit 0bc32f0

3 files changed

Lines changed: 233 additions & 5 deletions

File tree

‎docs/install/kubernetes.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,23 @@ This applies all manifests and restarts the pod to pick up any config or secret
163163
./scripts/k8s/deploy.sh --delete
164164
```
165165

166-
This deletes the namespace and all resources in it, including the PVC.
166+
For the default `openclaw` namespace, this deletes the namespace and everything in it, including the PVC.
167+
168+
For a custom namespace, `--delete` removes only OpenClaw resources and preserves the namespace and unrelated workloads:
169+
170+
```bash
171+
OPENCLAW_NAMESPACE=my-namespace ./scripts/k8s/deploy.sh --delete
172+
```
173+
174+
Use `--delete-resources` to request this scoped teardown explicitly in any namespace. Both scoped modes delete the OpenClaw Deployment, Service, PVC, ConfigMap, and generated Secret. Deleting the PVC removes OpenClaw's claim and access to its persisted data; whether the backing volume and data are deleted depends on the PersistentVolume or StorageClass reclaim policy (`Delete` or `Retain`).
175+
176+
To delete a custom namespace and every workload in it, explicitly opt in:
177+
178+
```bash
179+
OPENCLAW_NAMESPACE=my-namespace ./scripts/k8s/deploy.sh --delete-namespace
180+
```
181+
182+
This also deletes unrelated workloads and the PVC.
167183

168184
## Architecture notes
169185

‎scripts/k8s/deploy.sh‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
# ./scripts/k8s/deploy.sh # Deploy (requires API key in env or secret already in cluster)
99
# ./scripts/k8s/deploy.sh --create-secret # Create or update the K8s Secret from env vars
1010
# ./scripts/k8s/deploy.sh --show-token # Print the gateway token after deploy
11-
# ./scripts/k8s/deploy.sh --delete # Tear down
11+
# ./scripts/k8s/deploy.sh --delete # Tear down safely for the selected namespace
12+
# ./scripts/k8s/deploy.sh --delete-resources # Delete OpenClaw resources only
13+
# ./scripts/k8s/deploy.sh --delete-namespace # Delete the namespace and all resources
1214
#
1315
# Environment:
1416
# OPENCLAW_NAMESPACE Kubernetes namespace (default: openclaw)
@@ -34,7 +36,11 @@ Usage: ./scripts/k8s/deploy.sh [OPTION]
3436
(no args) Deploy OpenClaw (creates secret from env if needed)
3537
--create-secret Create or update the K8s Secret from env vars without deploying
3638
--show-token Print the gateway token after deploy or secret creation
37-
--delete Delete the namespace and all resources
39+
--delete Delete the default namespace, or resources only in a custom namespace
40+
--delete-resources
41+
Delete OpenClaw resources from the namespace
42+
--delete-namespace
43+
Delete the namespace and all resources in it
3844
-h, --help Show this help
3945
4046
Environment:
@@ -57,6 +63,12 @@ while [[ $# -gt 0 ]]; do
5763
--delete)
5864
MODE="delete"
5965
;;
66+
--delete-resources)
67+
MODE="delete-resources"
68+
;;
69+
--delete-namespace)
70+
MODE="delete-namespace"
71+
;;
6072
--show-token)
6173
SHOW_TOKEN=true
6274
;;
@@ -70,15 +82,30 @@ while [[ $# -gt 0 ]]; do
7082
done
7183

7284
# ---------------------------------------------------------------------------
73-
# --delete
85+
# --delete / --delete-namespace
7486
# ---------------------------------------------------------------------------
75-
if [[ "$MODE" == "delete" ]]; then
87+
if [[ "$MODE" == "delete" && "$NS" != "openclaw" ]]; then
88+
MODE="delete-resources"
89+
fi
90+
91+
if [[ "$MODE" == "delete" || "$MODE" == "delete-namespace" ]]; then
7692
echo "Deleting namespace '$NS' and all resources..."
7793
kubectl delete namespace "$NS" --ignore-not-found
7894
echo "Done."
7995
exit 0
8096
fi
8197

98+
# ---------------------------------------------------------------------------
99+
# --delete-resources
100+
# ---------------------------------------------------------------------------
101+
if [[ "$MODE" == "delete-resources" ]]; then
102+
echo "Deleting OpenClaw resources from namespace '$NS'..."
103+
kubectl delete -k "$MANIFESTS" -n "$NS" --ignore-not-found
104+
kubectl delete secret openclaw-secrets -n "$NS" --ignore-not-found
105+
echo "Done."
106+
exit 0
107+
fi
108+
82109
# ---------------------------------------------------------------------------
83110
# Create and apply Secret to the cluster
84111
# ---------------------------------------------------------------------------

‎test/scripts/k8s-deploy.test.ts‎

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
import { spawnSync } from "node:child_process";
2+
import { chmodSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
3+
import path from "node:path";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js";
6+
7+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
8+
9+
function writeExecutable(filePath: string, contents: string): void {
10+
writeFileSync(filePath, contents);
11+
chmodSync(filePath, 0o755);
12+
}
13+
14+
function runWithStubbedKubectl(
15+
args: string[],
16+
namespace: string,
17+
options: {
18+
deleteKustomizeStatus?: number;
19+
deleteNamespaceStatus?: number;
20+
deleteSecretStatus?: number;
21+
} = {},
22+
) {
23+
const root = tempDirs.make("openclaw-k8s-delete-");
24+
const binDir = path.join(root, "bin");
25+
const logPath = path.join(root, "kubectl.log");
26+
mkdirSync(binDir);
27+
28+
writeExecutable(
29+
path.join(binDir, "kubectl"),
30+
`#!/usr/bin/env bash
31+
set -euo pipefail
32+
printf '%s\\n' "$*" >> "$OPENCLAW_KUBECTL_LOG"
33+
if [[ "$1" == "delete" && "$2" == "-k" ]]; then
34+
exit "\${OPENCLAW_KUBECTL_DELETE_KUSTOMIZE_STATUS:-0}"
35+
fi
36+
if [[ "$1" == "delete" && "$2" == "namespace" ]]; then
37+
exit "\${OPENCLAW_KUBECTL_DELETE_NAMESPACE_STATUS:-0}"
38+
fi
39+
if [[ "$1" == "delete" && "$2" == "secret" ]]; then
40+
exit "\${OPENCLAW_KUBECTL_DELETE_SECRET_STATUS:-0}"
41+
fi
42+
if [[ "$1" == "get" && "$2" == "namespace" ]]; then
43+
exit 99
44+
fi
45+
case "$1" in
46+
cluster-info|delete) exit 0 ;;
47+
*) exit 99 ;;
48+
esac
49+
`,
50+
);
51+
writeExecutable(path.join(binDir, "openssl"), "#!/usr/bin/env bash\nexit 0\n");
52+
53+
const result = spawnSync("bash", ["scripts/k8s/deploy.sh", ...args], {
54+
cwd: process.cwd(),
55+
encoding: "utf8",
56+
env: {
57+
...process.env,
58+
OPENCLAW_KUBECTL_DELETE_KUSTOMIZE_STATUS: String(options.deleteKustomizeStatus ?? 0),
59+
OPENCLAW_KUBECTL_DELETE_NAMESPACE_STATUS: String(options.deleteNamespaceStatus ?? 0),
60+
OPENCLAW_KUBECTL_DELETE_SECRET_STATUS: String(options.deleteSecretStatus ?? 0),
61+
OPENCLAW_KUBECTL_LOG: logPath,
62+
OPENCLAW_NAMESPACE: namespace,
63+
PATH: `${binDir}:${process.env.PATH ?? ""}`,
64+
},
65+
});
66+
67+
return {
68+
calls: readFileSync(logPath, "utf8").trim().split("\n"),
69+
output: `${result.stdout}\n${result.stderr}`,
70+
result,
71+
};
72+
}
73+
74+
describe("scripts/k8s/deploy.sh", () => {
75+
function runDeleteResourcesWithStubbedKubectl(
76+
namespace: string,
77+
options: {
78+
deleteKustomizeStatus?: number;
79+
} = {},
80+
) {
81+
return runWithStubbedKubectl(["--delete-resources"], namespace, options);
82+
}
83+
84+
it("keeps the default namespace delete mode as a full namespace teardown", () => {
85+
const { calls, output, result } = runWithStubbedKubectl(["--delete"], "openclaw");
86+
87+
expect(result.status, output).toBe(0);
88+
expect(output).toContain("Deleting namespace 'openclaw' and all resources");
89+
expect(calls).toEqual(["cluster-info", "delete namespace openclaw --ignore-not-found"]);
90+
});
91+
92+
it("keeps a custom namespace and unrelated workloads when the legacy delete mode is used", () => {
93+
const { calls, output, result } = runWithStubbedKubectl(["--delete"], "my-namespace");
94+
95+
expect(result.status, output).toBe(0);
96+
expect(output).toContain("Deleting OpenClaw resources from namespace 'my-namespace'");
97+
expect(calls).toEqual([
98+
"cluster-info",
99+
`delete -k ${path.resolve("scripts/k8s/manifests")} -n my-namespace --ignore-not-found`,
100+
"delete secret openclaw-secrets -n my-namespace --ignore-not-found",
101+
]);
102+
expect(calls).not.toContain("delete namespace my-namespace --ignore-not-found");
103+
expect(calls).not.toContain("get namespace my-namespace");
104+
});
105+
106+
it("deletes OpenClaw resources without deleting the namespace", () => {
107+
const { calls, output, result } = runDeleteResourcesWithStubbedKubectl("my-namespace");
108+
109+
expect(result.status, output).toBe(0);
110+
expect(output).toContain("Deleting OpenClaw resources from namespace 'my-namespace'");
111+
112+
expect(calls).toEqual([
113+
"cluster-info",
114+
`delete -k ${path.resolve("scripts/k8s/manifests")} -n my-namespace --ignore-not-found`,
115+
"delete secret openclaw-secrets -n my-namespace --ignore-not-found",
116+
]);
117+
expect(calls).not.toContain("delete namespace my-namespace --ignore-not-found");
118+
expect(calls).not.toContain("get namespace my-namespace");
119+
});
120+
121+
it("surfaces resource delete failures instead of reporting teardown success", () => {
122+
const { calls, output, result } = runDeleteResourcesWithStubbedKubectl("restricted-namespace", {
123+
deleteKustomizeStatus: 17,
124+
});
125+
126+
expect(result.status, output).toBe(17);
127+
expect(output).toContain("Deleting OpenClaw resources from namespace 'restricted-namespace'");
128+
expect(calls).toEqual([
129+
"cluster-info",
130+
`delete -k ${path.resolve("scripts/k8s/manifests")} -n restricted-namespace --ignore-not-found`,
131+
]);
132+
expect(output).not.toContain("Done.");
133+
});
134+
135+
it("stops custom namespace teardown when deleting managed manifests fails", () => {
136+
const { calls, output, result } = runWithStubbedKubectl(["--delete"], "shared-namespace", {
137+
deleteKustomizeStatus: 17,
138+
});
139+
140+
expect(result.status, output).toBe(17);
141+
expect(calls).toEqual([
142+
"cluster-info",
143+
`delete -k ${path.resolve("scripts/k8s/manifests")} -n shared-namespace --ignore-not-found`,
144+
]);
145+
expect(output).not.toContain("Done.");
146+
});
147+
148+
it.each(["--delete", "--delete-resources"])(
149+
"surfaces generated Secret deletion failures for %s",
150+
(mode) => {
151+
const { calls, output, result } = runWithStubbedKubectl([mode], "shared-namespace", {
152+
deleteSecretStatus: 23,
153+
});
154+
155+
expect(result.status, output).toBe(23);
156+
expect(calls).toEqual([
157+
"cluster-info",
158+
`delete -k ${path.resolve("scripts/k8s/manifests")} -n shared-namespace --ignore-not-found`,
159+
"delete secret openclaw-secrets -n shared-namespace --ignore-not-found",
160+
]);
161+
expect(output).not.toContain("Done.");
162+
},
163+
);
164+
165+
it("surfaces namespace deletion failures instead of reporting teardown success", () => {
166+
const { calls, output, result } = runWithStubbedKubectl(["--delete-namespace"], "shared", {
167+
deleteNamespaceStatus: 29,
168+
});
169+
170+
expect(result.status, output).toBe(29);
171+
expect(calls).toEqual(["cluster-info", "delete namespace shared --ignore-not-found"]);
172+
expect(output).not.toContain("Done.");
173+
});
174+
175+
it("deletes the namespace only when the explicit namespace teardown mode is selected", () => {
176+
const { calls, output, result } = runWithStubbedKubectl(
177+
["--delete-namespace"],
178+
"shared-namespace",
179+
);
180+
181+
expect(result.status, output).toBe(0);
182+
expect(output).toContain("Deleting namespace 'shared-namespace' and all resources");
183+
expect(calls).toEqual(["cluster-info", "delete namespace shared-namespace --ignore-not-found"]);
184+
});
185+
});

0 commit comments

Comments
 (0)