@rocket.chat/houston 0.27.0-next.2 → 2.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +108 -15
- package/bin/houston +62 -0
- package/data/docs/ARCHITECTURE.md +387 -0
- package/data/docs/automation.md +107 -0
- package/data/docs/e2ee.md +76 -0
- package/data/docs/eks.md +258 -0
- package/data/docs/gui.md +282 -0
- package/data/docs/guide.md +669 -0
- package/data/docs/kubernetes.md +138 -0
- package/data/docs/troubleshooting.md +193 -0
- package/data/docs/upgrading.md +84 -0
- package/data/docs/voice.md +253 -0
- package/data/iam/engineer-policy.json +11 -0
- package/data/iam/engineer-rbac.yaml +42 -0
- package/data/iam/operator-policy.json +211 -0
- package/data/loadtest/badbot.js +22 -0
- package/data/loadtest/common.js +168 -0
- package/data/loadtest/custom.js +20 -0
- package/data/loadtest/journey.js +58 -0
- package/data/loadtest/login.js +18 -0
- package/data/loadtest/messages.js +13 -0
- package/data/loadtest/mixed.js +39 -0
- package/data/loadtest/read.js +12 -0
- package/data/loadtest/webhook.js +20 -0
- package/data/monitoring/houston-logs.json +98 -0
- package/data/monitoring/k6-loadtest.json +610 -0
- package/data/monitoring/mongodb-exporter.json +3007 -0
- package/data/monitoring/node-exporter-full.json +16114 -0
- package/data/monitoring/rocketchat-metrics.json +6471 -0
- package/data/presets/airgapped.yaml +16 -0
- package/data/presets/default.yaml +9 -0
- package/data/seed/avatar-0.png +0 -0
- package/data/seed/avatar-1.png +0 -0
- package/data/seed/avatar-2.png +0 -0
- package/data/seed/avatar-3.png +0 -0
- package/data/seed/avatar-4.png +0 -0
- package/data/seed/avatar-5.png +0 -0
- package/data/seed/avatar-6.png +0 -0
- package/data/seed/avatar-7.png +0 -0
- package/data/seed/seed-metrics.csv +3 -0
- package/data/seed/seed-preview.png +0 -0
- package/data/seed/seed-report.pdf +32 -0
- package/data/seed/seed-runbook.txt +3 -0
- package/data/skill/SKILL.md +399 -0
- package/data/versions.yaml +55 -0
- package/data/voice/freeswitch/drachtio.xml +52 -0
- package/data/voice/freeswitch/public.xml +87 -0
- package/data/webui/app.css +2003 -0
- package/data/webui/index.html +678 -0
- package/data/webui/js/api.js +561 -0
- package/data/webui/js/boot.js +44 -0
- package/data/webui/js/clusters-tail.js +1377 -0
- package/data/webui/js/clusters.js +896 -0
- package/data/webui/js/create.js +276 -0
- package/data/webui/js/detail.js +507 -0
- package/data/webui/js/jobs.js +636 -0
- package/data/webui/js/logs.js +634 -0
- package/data/webui/js/main.js +14 -0
- package/data/webui/js/options.js +408 -0
- package/data/webui/js/people.js +438 -0
- package/data/webui/js/perf.js +303 -0
- package/data/webui/js/render.js +438 -0
- package/data/webui/js/shared.js +3 -0
- package/data/webui/js/state.js +583 -0
- package/data/webui/js/theme.js +123 -0
- package/data/webui/js/util.js +584 -0
- package/dist/cli/app.js +593 -0
- package/dist/cli/capabilities.js +76 -0
- package/dist/cli/cell-widths.js +495 -0
- package/dist/cli/entry.js +10 -0
- package/dist/cli/error-panel.js +492 -0
- package/dist/cli/handlers/actor.js +84 -0
- package/dist/cli/handlers/https.js +614 -0
- package/dist/cli/handlers/prompt.js +299 -0
- package/dist/cli/handlers/users.js +226 -0
- package/dist/cli/handlers.js +3678 -0
- package/dist/cli/help.js +183 -0
- package/dist/cli/index.js +15 -0
- package/dist/cli/perf.js +810 -0
- package/dist/cli/render.js +254 -0
- package/dist/cli/serve.js +858 -0
- package/dist/cli/spec-data.js +2593 -0
- package/dist/cli/spec-types.js +8 -0
- package/dist/cli/usage-error.js +520 -0
- package/dist/cli/wire.js +70 -0
- package/dist/core/audit.js +346 -0
- package/dist/core/backup-compat.js +85 -0
- package/dist/core/backup.js +1639 -0
- package/dist/core/cluster.js +1398 -0
- package/dist/core/configimport.js +344 -0
- package/dist/core/data.js +267 -0
- package/dist/core/database-of.js +82 -0
- package/dist/core/diagnose.js +159 -0
- package/dist/core/doctor.js +1518 -0
- package/dist/core/edge/acme.js +338 -0
- package/dist/core/edge/engine.js +500 -0
- package/dist/core/edge/networks.js +174 -0
- package/dist/core/edge/paths.js +107 -0
- package/dist/core/edge/routes.js +251 -0
- package/dist/core/edge/routeyml.js +195 -0
- package/dist/core/edge/runtime.js +493 -0
- package/dist/core/edge/spec.js +305 -0
- package/dist/core/edge/types.js +202 -0
- package/dist/core/eks-docs.js +135 -0
- package/dist/core/eks.js +2240 -0
- package/dist/core/envvars.js +562 -0
- package/dist/core/eventlog.js +541 -0
- package/dist/core/firstrun.js +153 -0
- package/dist/core/journal.js +691 -0
- package/dist/core/kube/ask.js +231 -0
- package/dist/core/kube/capabilities.js +269 -0
- package/dist/core/kube/certmanager.js +452 -0
- package/dist/core/kube/chart.js +413 -0
- package/dist/core/kube/cluster.js +196 -0
- package/dist/core/kube/context.js +545 -0
- package/dist/core/kube/endpoints.js +448 -0
- package/dist/core/kube/exec.js +195 -0
- package/dist/core/kube/forward.js +732 -0
- package/dist/core/kube/grafana-ingress.js +110 -0
- package/dist/core/kube/ingress.js +218 -0
- package/dist/core/kube/manifests.js +361 -0
- package/dist/core/kube/monitoring.js +768 -0
- package/dist/core/kube/names.js +237 -0
- package/dist/core/kube/namespace.js +678 -0
- package/dist/core/kube/operator.js +941 -0
- package/dist/core/kube/probe.js +202 -0
- package/dist/core/kube/reachability.js +163 -0
- package/dist/core/kube/release.js +439 -0
- package/dist/core/kube/replicaset.js +180 -0
- package/dist/core/kube/scale.js +398 -0
- package/dist/core/kube/status.js +633 -0
- package/dist/core/kube/tools.js +238 -0
- package/dist/core/kube/transfer.js +173 -0
- package/dist/core/kube/workspace.js +423 -0
- package/dist/core/lifecycle/admin.js +64 -0
- package/dist/core/lifecycle/create-docker.js +1533 -0
- package/dist/core/lifecycle/create-k8s.js +939 -0
- package/dist/core/lifecycle/destroy.js +339 -0
- package/dist/core/lifecycle/inspect.js +635 -0
- package/dist/core/lifecycle/naming.js +298 -0
- package/dist/core/lifecycle/notes.js +726 -0
- package/dist/core/lifecycle/ownership.js +313 -0
- package/dist/core/lifecycle/payload.js +401 -0
- package/dist/core/lifecycle/ports.js +388 -0
- package/dist/core/lifecycle/preconditions.js +367 -0
- package/dist/core/lifecycle/pruning.js +551 -0
- package/dist/core/lifecycle/ready.js +753 -0
- package/dist/core/lifecycle/request.js +525 -0
- package/dist/core/lifecycle/seeding.js +281 -0
- package/dist/core/lifecycle/sizing.js +392 -0
- package/dist/core/lifecycle/state.js +286 -0
- package/dist/core/lifecycle/tlsplan.js +704 -0
- package/dist/core/lifecycle/types.js +232 -0
- package/dist/core/loadtest.js +995 -0
- package/dist/core/migrate.js +612 -0
- package/dist/core/monitor.js +494 -0
- package/dist/core/postready.js +969 -0
- package/dist/core/promote.js +804 -0
- package/dist/core/rcapi.js +1285 -0
- package/dist/core/recovery.js +75 -0
- package/dist/core/runner/engine.js +394 -0
- package/dist/core/runner/host.js +298 -0
- package/dist/core/runner/locks.js +129 -0
- package/dist/core/runner/paths.js +173 -0
- package/dist/core/runner/ports.js +172 -0
- package/dist/core/runner/queries.js +612 -0
- package/dist/core/runner/record.js +563 -0
- package/dist/core/scaleseed.js +271 -0
- package/dist/core/seed-db.js +208 -0
- package/dist/core/sessions.js +472 -0
- package/dist/core/skill.js +326 -0
- package/dist/core/sso-identities.js +172 -0
- package/dist/core/sso.js +703 -0
- package/dist/core/tls-local.js +311 -0
- package/dist/core/tls.js +878 -0
- package/dist/core/topology.js +482 -0
- package/dist/core/upgrade.js +868 -0
- package/dist/core/users.js +739 -0
- package/dist/lib/audit.js +100 -0
- package/dist/lib/close-matches.js +238 -0
- package/dist/lib/compose.js +672 -0
- package/dist/lib/config.js +1398 -0
- package/dist/lib/context.js +18 -0
- package/dist/lib/errors.js +305 -0
- package/dist/lib/events.js +208 -0
- package/dist/lib/float-text.js +126 -0
- package/dist/lib/format.js +1320 -0
- package/dist/lib/home.js +325 -0
- package/dist/lib/journal.js +122 -0
- package/dist/lib/json-text.js +733 -0
- package/dist/lib/jsonout.js +382 -0
- package/dist/lib/lock.js +516 -0
- package/dist/lib/login-name.js +202 -0
- package/dist/lib/map-limit.js +29 -0
- package/dist/lib/mkdir-parents.js +90 -0
- package/dist/lib/mongo-kernel.js +67 -0
- package/dist/lib/monitoring.js +492 -0
- package/dist/lib/platform-name.js +59 -0
- package/dist/lib/presets/common.js +390 -0
- package/dist/lib/presets/composite.js +326 -0
- package/dist/lib/presets/e2ee.js +143 -0
- package/dist/lib/presets/email.js +172 -0
- package/dist/lib/presets/guides.js +27 -0
- package/dist/lib/presets/index.js +490 -0
- package/dist/lib/presets/keycloak.js +369 -0
- package/dist/lib/presets/ldap.js +1285 -0
- package/dist/lib/presets/livechat.js +220 -0
- package/dist/lib/presets/multi-instance.js +124 -0
- package/dist/lib/presets/oidc.js +161 -0
- package/dist/lib/presets/public.js +496 -0
- package/dist/lib/presets/registry.js +64 -0
- package/dist/lib/presets/s3-minio.js +300 -0
- package/dist/lib/presets/saml.js +149 -0
- package/dist/lib/presets/scenario.js +51 -0
- package/dist/lib/presets/types.js +86 -0
- package/dist/lib/presets/voice-pbx.js +320 -0
- package/dist/lib/presets/voice.js +345 -0
- package/dist/lib/presets/webhooks.js +182 -0
- package/dist/lib/redact.js +106 -0
- package/dist/lib/seed-options.js +68 -0
- package/dist/lib/seed-profiles.js +59 -0
- package/dist/lib/targz.js +1383 -0
- package/dist/lib/teammode.js +25 -0
- package/dist/lib/tempdir.js +162 -0
- package/dist/lib/unicode-tables.js +2335 -0
- package/dist/lib/version-spec.js +357 -0
- package/dist/lib/versions.js +463 -0
- package/dist/lib/yaml-dump.js +1361 -0
- package/dist/perf/baseline.js +168 -0
- package/dist/perf/constrain.js +296 -0
- package/dist/perf/k6.js +235 -0
- package/dist/perf/mongoprof.js +171 -0
- package/dist/perf/rcmetrics.js +178 -0
- package/dist/perf/report.js +386 -0
- package/dist/perf/resources.js +224 -0
- package/dist/perf/slo.js +111 -0
- package/dist/perf/timeline.js +187 -0
- package/dist/perf/timings.js +85 -0
- package/dist/perf/verdict.js +201 -0
- package/dist/seams/aws-cli.js +126 -0
- package/dist/seams/docker-cli.js +280 -0
- package/dist/seams/exec.js +861 -0
- package/dist/seams/fs/lock.js +528 -0
- package/dist/seams/kubectl.js +83 -0
- package/dist/seams/rc-http.js +921 -0
- package/dist/seed/apply.js +190 -0
- package/dist/seed/assets.js +76 -0
- package/dist/seed/check.js +391 -0
- package/dist/seed/client.js +172 -0
- package/dist/seed/executor.js +726 -0
- package/dist/seed/history.js +281 -0
- package/dist/seed/kinds.js +91 -0
- package/dist/seed/plan.js +432 -0
- package/dist/seed/receipt.js +339 -0
- package/dist/seed/repairs.js +75 -0
- package/dist/seed/verification.js +363 -0
- package/dist/seed/wording.js +248 -0
- package/dist/web/app.js +326 -0
- package/dist/web/jobs.js +510 -0
- package/dist/web/route-deps.js +211 -0
- package/dist/web/route-table.js +213 -0
- package/dist/web/routes/backup.js +244 -0
- package/dist/web/routes/clusters.js +303 -0
- package/dist/web/routes/data.js +187 -0
- package/dist/web/routes/edge.js +82 -0
- package/dist/web/routes/index.js +63 -0
- package/dist/web/routes/jobs.js +95 -0
- package/dist/web/routes/logs.js +297 -0
- package/dist/web/routes/meta.js +191 -0
- package/dist/web/routes/people.js +151 -0
- package/dist/web/routes/perf.js +95 -0
- package/dist/web/routes/repros.js +550 -0
- package/dist/web/routes/session.js +361 -0
- package/dist/web/routes/spa.js +198 -0
- package/dist/web/security.js +475 -0
- package/dist/web/signin.js +246 -0
- package/package.json +67 -32
- package/.eslintrc +0 -94
- package/.swcrc +0 -18
- package/dist/bin.js +0 -3
- package/dist/cli.js +0 -184
- package/dist/commands/createBranch.js +0 -192
- package/src/bin.js +0 -4
- package/src/cli.ts +0 -25
- package/src/commands/createBranch.ts +0 -25
- package/src-old/logs.js +0 -323
- package/src-old/md.js +0 -191
- package/src-old/release-table.js +0 -60
- package/src-old/set-version.js +0 -614
- package/src-old/utils.js +0 -15
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.namespaceFor = namespaceFor;
|
|
4
|
+
exports.labelsFor = labelsFor;
|
|
5
|
+
exports.refuseForeignNamespace = refuseForeignNamespace;
|
|
6
|
+
exports.namespaceLabels = namespaceLabels;
|
|
7
|
+
exports.namespaceOwner = namespaceOwner;
|
|
8
|
+
exports.madeHere = madeHere;
|
|
9
|
+
exports.assertNamespaceAvailable = assertNamespaceAvailable;
|
|
10
|
+
exports.ensureNamespace = ensureNamespace;
|
|
11
|
+
exports.workspacePvcs = workspacePvcs;
|
|
12
|
+
exports.deleteStray = deleteStray;
|
|
13
|
+
exports.deleteNamespace = deleteNamespace;
|
|
14
|
+
exports.requireOwnNamespace = requireOwnNamespace;
|
|
15
|
+
exports.waitNamespaceGone = waitNamespaceGone;
|
|
16
|
+
/**
|
|
17
|
+
* One namespace per workspace, and the rules for taking one away.
|
|
18
|
+
*
|
|
19
|
+
* Tier k3. Selection is BY LABEL: `namespaceLabels` reads what the cluster says rather than
|
|
20
|
+
* matching a name prefix, because a prefix match once made a namespace somebody else created
|
|
21
|
+
* eligible for teardown. A namespace that cannot be CONFIRMED gone is not gone.
|
|
22
|
+
*/
|
|
23
|
+
const errors_1 = require("../../lib/errors");
|
|
24
|
+
const config_1 = require("../../lib/config");
|
|
25
|
+
const events_1 = require("../../lib/events");
|
|
26
|
+
const teammode_1 = require("../../lib/teammode");
|
|
27
|
+
const record_1 = require("../runner/record");
|
|
28
|
+
const ask_1 = require("./ask");
|
|
29
|
+
const context_1 = require("./context");
|
|
30
|
+
const exec_1 = require("./exec");
|
|
31
|
+
const names_1 = require("./names");
|
|
32
|
+
const realSleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds * 1000));
|
|
33
|
+
/**
|
|
34
|
+
* The namespace this workspace's objects ACTUALLY live in.
|
|
35
|
+
*
|
|
36
|
+
* RECORDED, not derived, for the same reason as `projectName`: a namespace cannot be
|
|
37
|
+
* renamed in place, so once objects exist the prefix that minted them is history rather
|
|
38
|
+
* than a description. Deriving it everywhere meant a changed prefix would leave `down`
|
|
39
|
+
* deleting a namespace that was never created while the real one kept its pods, its PVCs
|
|
40
|
+
* and its port-forward.
|
|
41
|
+
*/
|
|
42
|
+
async function namespaceFor(name) {
|
|
43
|
+
return (await (0, record_1.recorded)(name, 'namespace')) || (0, names_1.mintNamespace)(name);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The labels every resource houston creates carries.
|
|
47
|
+
*
|
|
48
|
+
* `managed-by` is what teardown selects on. `workspace` and `owner` are what make a
|
|
49
|
+
* refusal specific -- "the cluster still holds 2 namespaces" is actionable only if it can
|
|
50
|
+
* say whose.
|
|
51
|
+
*/
|
|
52
|
+
function labelsFor(name, owner = '') {
|
|
53
|
+
const out = {
|
|
54
|
+
[names_1.OWNER_LABEL_KEY]: names_1.OWNER_LABEL_VALUE,
|
|
55
|
+
[names_1.WORKSPACE_LABEL]: name,
|
|
56
|
+
};
|
|
57
|
+
if (owner !== '')
|
|
58
|
+
out[names_1.OWNER_OF_LABEL] = owner;
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Explain why a namespace owned by another user cannot be adopted.
|
|
63
|
+
*
|
|
64
|
+
* `owner` EMPTY IS NOT A MATCH. This was `heldBy && owner && ...`, so a box with no
|
|
65
|
+
* accounts -- where the CLI actor is '' because team mode is opt-in -- skipped the
|
|
66
|
+
* comparison entirely and adopted a namespace stamped with somebody else's name. Found by
|
|
67
|
+
* doing it on a live cluster: the label survived and Rocket.Chat was installed into their
|
|
68
|
+
* namespace anyway.
|
|
69
|
+
*
|
|
70
|
+
* Not knowing who you are is not evidence that you are the owner. And TEAM MODE OFF IS
|
|
71
|
+
* NOT THE SAME AS THE VARIABLE BEING UNSET: on a box with accounts the message said
|
|
72
|
+
* "HOUSTON_USER is not set" while it WAS, and the remedy it named produced the identical
|
|
73
|
+
* refusal.
|
|
74
|
+
*/
|
|
75
|
+
function refuseForeignOwner(ns, heldBy, owner) {
|
|
76
|
+
let mine;
|
|
77
|
+
let remedy;
|
|
78
|
+
if (owner !== '') {
|
|
79
|
+
mine = `you are '${owner}'`;
|
|
80
|
+
remedy =
|
|
81
|
+
`Use a different --name, or identify yourself with \`HOUSTON_USER=${heldBy} ` +
|
|
82
|
+
'houston ...` if it is yours';
|
|
83
|
+
}
|
|
84
|
+
else if ((0, teammode_1.teamMode)()) {
|
|
85
|
+
mine = 'houston cannot tell who you are, because HOUSTON_USER is not set';
|
|
86
|
+
remedy =
|
|
87
|
+
`Use a different --name, or identify yourself with \`HOUSTON_USER=${heldBy} ` +
|
|
88
|
+
'houston ...` if it is yours';
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
mine =
|
|
92
|
+
'this box has no houston accounts, so it has no way to say who you are — ' +
|
|
93
|
+
'HOUSTON_USER alone is not enough, because names are only honoured once accounts exist';
|
|
94
|
+
remedy =
|
|
95
|
+
`Use a different --name, or make the account and claim it: \`houston users add ` +
|
|
96
|
+
`${heldBy}\` then \`HOUSTON_USER=${heldBy} houston ...\``;
|
|
97
|
+
}
|
|
98
|
+
throw new errors_1.ConflictError(`namespace ${ns} belongs to '${heldBy}' on this cluster and ${mine}. Adopting it ` +
|
|
99
|
+
`would make \`down --volumes\` delete their data. ${remedy}`);
|
|
100
|
+
}
|
|
101
|
+
/** Raise unless `labels` prove this namespace is this workspace's. */
|
|
102
|
+
function refuseForeignNamespace(ns, name, labels, owner) {
|
|
103
|
+
if (labels[names_1.OWNER_LABEL_KEY] !== names_1.OWNER_LABEL_VALUE) {
|
|
104
|
+
throw new errors_1.ConflictError(`namespace ${ns} already exists and is not managed by houston, so it will not be ` +
|
|
105
|
+
'adopted -- `down --volumes` deletes a namespace and its volumes, and houston ' +
|
|
106
|
+
'cannot tell a namespace an older houston made from one somebody else made. Use ' +
|
|
107
|
+
`a different --name, or delete it yourself first: kubectl delete namespace ${ns}`);
|
|
108
|
+
}
|
|
109
|
+
const theirs = labels[names_1.WORKSPACE_LABEL] ?? '';
|
|
110
|
+
if (theirs === '') {
|
|
111
|
+
throw new errors_1.ConflictError(`namespace ${ns} records no houston workspace, so its ownership cannot be proved. ` +
|
|
112
|
+
'houston will not adopt, change or delete it; inspect its labels and data yourself first');
|
|
113
|
+
}
|
|
114
|
+
if (theirs !== name) {
|
|
115
|
+
throw new errors_1.ConflictError(`namespace ${ns} belongs to houston workspace '${theirs}', not '${name}'. Two ` +
|
|
116
|
+
'workspaces cannot share a namespace; use a different --name');
|
|
117
|
+
}
|
|
118
|
+
const heldBy = labels[names_1.OWNER_OF_LABEL] ?? '';
|
|
119
|
+
if (heldBy !== '' && heldBy !== owner)
|
|
120
|
+
refuseForeignOwner(ns, heldBy, owner);
|
|
121
|
+
if (heldBy === '' && owner !== '') {
|
|
122
|
+
throw new errors_1.ConflictError(`namespace ${ns} records no owner, but '${name}' belongs to '${owner}'. ` +
|
|
123
|
+
'Its ownership cannot be proved, so houston will not adopt, change or delete it');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Raise unless this namespace is provably THIS home's.
|
|
128
|
+
*
|
|
129
|
+
* Everything `refuseForeignNamespace` reads is the same in two homes on one cluster: the
|
|
130
|
+
* workspace name derives from the version, and the owner label is an account name a laptop
|
|
131
|
+
* and the server share, or '' on both. So `up` in one home installed over the other's
|
|
132
|
+
* namespace, and either `down --volumes` deleted the other's data. The recorded UID proves
|
|
133
|
+
* identity on its own; without one, the home label has to be this home's, and a namespace
|
|
134
|
+
* made before the label existed is taken only by a home that already has its record.
|
|
135
|
+
*/
|
|
136
|
+
function refuseAnotherHome(ns, labels, { recorded, uid }) {
|
|
137
|
+
if (uid !== '')
|
|
138
|
+
return; // `requireSameNamespace` decides
|
|
139
|
+
const theirs = labels[names_1.HOME_LABEL] ?? '';
|
|
140
|
+
const mine = (0, config_1.homeId)();
|
|
141
|
+
if (theirs === mine)
|
|
142
|
+
return;
|
|
143
|
+
const claim = `\`kubectl label namespace ${ns} ${names_1.HOME_LABEL}=${mine} --overwrite\``;
|
|
144
|
+
if (theirs !== '') {
|
|
145
|
+
throw new errors_1.ConflictError(`namespace ${ns} was made by another houston home on this cluster (${names_1.HOME_LABEL}=` +
|
|
146
|
+
`${theirs}; this one is ${mine}), so houston will not adopt, change or delete it: either home's ` +
|
|
147
|
+
"`down --volumes` would delete the other's data. Use a different --name; if that home " +
|
|
148
|
+
`is gone for good and the data is yours, claim it first with ${claim}`);
|
|
149
|
+
}
|
|
150
|
+
if (recorded)
|
|
151
|
+
return;
|
|
152
|
+
throw new errors_1.ConflictError(`namespace ${ns} already exists and this houston has no record of it: another houston ` +
|
|
153
|
+
'home made it, or this one did before its record was lost. It will not be adopted, ' +
|
|
154
|
+
"because adopting another home's namespace lets either home delete the other's data. " +
|
|
155
|
+
`Use a different --name; if it is this home's, claim it first with ${claim}`);
|
|
156
|
+
}
|
|
157
|
+
function metadataOf(doc) {
|
|
158
|
+
const meta = doc?.metadata;
|
|
159
|
+
return meta !== null && typeof meta === 'object' ? meta : {};
|
|
160
|
+
}
|
|
161
|
+
function phaseOf(doc) {
|
|
162
|
+
const status = doc?.status;
|
|
163
|
+
const phase = status !== null && typeof status === 'object'
|
|
164
|
+
? status.phase
|
|
165
|
+
: undefined;
|
|
166
|
+
return String(phase ?? '');
|
|
167
|
+
}
|
|
168
|
+
/** Identity comes from one authoritative object, never a filtered namespace list. */
|
|
169
|
+
function identityOf(ns, doc) {
|
|
170
|
+
const metadata = metadataOf(doc);
|
|
171
|
+
if (metadata.name !== ns ||
|
|
172
|
+
typeof metadata.uid !== 'string' ||
|
|
173
|
+
metadata.uid === '' ||
|
|
174
|
+
typeof metadata.resourceVersion !== 'string' ||
|
|
175
|
+
metadata.resourceVersion === '') {
|
|
176
|
+
throw new errors_1.ConflictError(`could not confirm namespace ${ns}'s UID and resource version — refusing to guess about its identity`);
|
|
177
|
+
}
|
|
178
|
+
const raw = metadata.labels;
|
|
179
|
+
const labels = {};
|
|
180
|
+
if (raw !== null && typeof raw === 'object' && !Array.isArray(raw)) {
|
|
181
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
182
|
+
if (typeof value === 'string')
|
|
183
|
+
labels[key] = value;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return { uid: metadata.uid, resourceVersion: metadata.resourceVersion, labels };
|
|
187
|
+
}
|
|
188
|
+
async function namespaceIdentity(ns, context) {
|
|
189
|
+
const seen = (0, ask_1.require_)(await (0, ask_1.askObject)('namespace', ns, { context }), `namespace ${ns}`, {
|
|
190
|
+
context,
|
|
191
|
+
});
|
|
192
|
+
return (0, ask_1.present)(seen) ? identityOf(ns, seen.doc) : null;
|
|
193
|
+
}
|
|
194
|
+
async function recordedNamespaceClaim(name) {
|
|
195
|
+
let meta;
|
|
196
|
+
try {
|
|
197
|
+
meta = await (0, record_1.readMeta)(name);
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
if (error instanceof errors_1.MissingRecordError)
|
|
201
|
+
return { owner: '', uid: '', recorded: false };
|
|
202
|
+
throw error;
|
|
203
|
+
}
|
|
204
|
+
const extra = meta.extra;
|
|
205
|
+
if (extra === null || typeof extra !== 'object' || Array.isArray(extra)) {
|
|
206
|
+
throw new errors_1.ConflictError(`'${name}' has no readable namespace ownership record — refusing to act on its namespace`);
|
|
207
|
+
}
|
|
208
|
+
const fields = extra;
|
|
209
|
+
// Namespace labels identify the creating actor. A handover changes local `owner`
|
|
210
|
+
// without relabelling Kubernetes, so its current owner is checked by lifecycle's
|
|
211
|
+
// mayDestroy gate and its namespace identity still follows immutable created_by.
|
|
212
|
+
const owner = Object.hasOwn(fields, 'namespace_owner')
|
|
213
|
+
? fields.namespace_owner
|
|
214
|
+
: (fields.created_by ?? '');
|
|
215
|
+
const uid = Object.hasOwn(fields, 'namespace_uid') ? fields.namespace_uid : '';
|
|
216
|
+
if (typeof owner !== 'string' || typeof uid !== 'string') {
|
|
217
|
+
throw new errors_1.ConflictError(`'${name}' has an invalid namespace ownership record — refusing to act on its namespace`);
|
|
218
|
+
}
|
|
219
|
+
return { owner, uid, recorded: true };
|
|
220
|
+
}
|
|
221
|
+
function requireSameNamespace(ns, identity, expectedUid) {
|
|
222
|
+
if (expectedUid !== '' && identity.uid !== expectedUid) {
|
|
223
|
+
throw new errors_1.ConflictError(`namespace ${ns} has a different UID from this workspace's record, so houston will not ` +
|
|
224
|
+
'adopt, change or delete it. A replacement namespace is not the workspace that was recorded');
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* A namespace's labels, null if it does not exist -- and RAISES if the cluster could not
|
|
229
|
+
* be asked.
|
|
230
|
+
*
|
|
231
|
+
* The three answers are different and were being collapsed into two. "I asked and it is
|
|
232
|
+
* not there" is safe to act on; "I could not ask" is not, and a wrong kube-context, an
|
|
233
|
+
* expired credential or an RBAC denial all produce the second while looking like the
|
|
234
|
+
* first.
|
|
235
|
+
*
|
|
236
|
+
* NO LONGER MATCHED ON THE SERVER'S PROSE: `--ignore-not-found` makes the API server
|
|
237
|
+
* state absence in the PAYLOAD, so there is no wording to get wrong.
|
|
238
|
+
*/
|
|
239
|
+
async function namespaceLabels(ns, { context }) {
|
|
240
|
+
const seen = (0, ask_1.require_)(await (0, ask_1.askObject)('namespace', ns, { context }), `namespace ${ns}`, {
|
|
241
|
+
context,
|
|
242
|
+
});
|
|
243
|
+
if ((0, ask_1.absent)(seen))
|
|
244
|
+
return null;
|
|
245
|
+
const labels = metadataOf((0, ask_1.present)(seen) ? seen.doc : null).labels;
|
|
246
|
+
if (labels === null || typeof labels !== 'object' || Array.isArray(labels))
|
|
247
|
+
return {};
|
|
248
|
+
const out = {};
|
|
249
|
+
for (const [key, value] of Object.entries(labels))
|
|
250
|
+
out[String(key)] = String(value);
|
|
251
|
+
return out;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Who a namespace says it belongs to. `''` = it names nobody. null = unreadable.
|
|
255
|
+
*
|
|
256
|
+
* THREE ANSWERS, because the caller is `prune --orphans`, which deletes. Collapsing "it
|
|
257
|
+
* names nobody" and "I could not read it" into one empty string is what makes a
|
|
258
|
+
* colleague's namespace look unowned, and the label is the only evidence there is that
|
|
259
|
+
* somebody else made it -- their `repro.json` is on their machine, not here.
|
|
260
|
+
*
|
|
261
|
+
* A namespace that is GONE answers `''` rather than null, and the distinction is the
|
|
262
|
+
* point: nobody owns a namespace that does not exist, and deleting it is a no-op -- so
|
|
263
|
+
* that case must not be treated as the unreadable one, or a namespace that vanished
|
|
264
|
+
* between the listing and the sweep would block the sweep.
|
|
265
|
+
*/
|
|
266
|
+
async function namespaceOwner(ns, { context }) {
|
|
267
|
+
let labels;
|
|
268
|
+
try {
|
|
269
|
+
labels = await namespaceLabels(ns, { context });
|
|
270
|
+
}
|
|
271
|
+
catch {
|
|
272
|
+
return null; // could not ask: the only null answer
|
|
273
|
+
}
|
|
274
|
+
if (labels === null)
|
|
275
|
+
return ''; // asked, and it is not there
|
|
276
|
+
return labels[names_1.OWNER_OF_LABEL] ?? '';
|
|
277
|
+
}
|
|
278
|
+
/** Whether a namespace says THIS home made it; false when it says otherwise or cannot be read. */
|
|
279
|
+
async function madeHere(ns, { context }) {
|
|
280
|
+
try {
|
|
281
|
+
return (await namespaceLabels(ns, { context }))?.[names_1.HOME_LABEL] === (0, config_1.homeId)();
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Refuse now if this workspace's namespace belongs to somebody else.
|
|
289
|
+
*
|
|
290
|
+
* A PREFLIGHT, so it runs before the write-ahead `repro.json`. A refusal that created
|
|
291
|
+
* nothing should leave nothing behind, and a refused create once left an `incomplete`
|
|
292
|
+
* record that `prune` then offered to delete.
|
|
293
|
+
*
|
|
294
|
+
* IT ALSO READS THE PHASE. A namespace that is `Terminating` still carries its labels, so
|
|
295
|
+
* an ownership-only check passed and the create died several steps later on "unable to
|
|
296
|
+
* create new content in namespace X because it is being terminated" -- at exit 7, after
|
|
297
|
+
* emitting "namespace X" as a completed step for a namespace it had not created. The
|
|
298
|
+
* everyday trigger is `down --volumes` followed by re-creating under the same ticket name.
|
|
299
|
+
*
|
|
300
|
+
* Refused rather than waited out: a finalizer can wedge indefinitely, and a preflight that
|
|
301
|
+
* sometimes blocks for a minute is a worse contract than one that says retry.
|
|
302
|
+
*/
|
|
303
|
+
async function assertNamespaceAvailable(name, { context, owner = '' }) {
|
|
304
|
+
const ns = await namespaceFor(name);
|
|
305
|
+
const seen = (0, ask_1.require_)(await (0, ask_1.askObject)('namespace', ns, { context }), `namespace ${ns}`, {
|
|
306
|
+
context,
|
|
307
|
+
});
|
|
308
|
+
if ((0, ask_1.absent)(seen))
|
|
309
|
+
return;
|
|
310
|
+
const doc = (0, ask_1.present)(seen) ? seen.doc : null;
|
|
311
|
+
if (phaseOf(doc) === 'Terminating') {
|
|
312
|
+
throw new errors_1.ConflictError(`namespace ${ns} is still Terminating, so nothing can be created in it yet. ` +
|
|
313
|
+
`Kubernetes will finish on its own -- \`kubectl get ns ${ns}\` shows when -- then ` +
|
|
314
|
+
'run this again. A workspace just torn down with `--volumes` takes a few seconds to go.');
|
|
315
|
+
}
|
|
316
|
+
const labels = metadataOf(doc).labels;
|
|
317
|
+
const flat = {};
|
|
318
|
+
if (labels !== null && typeof labels === 'object' && !Array.isArray(labels)) {
|
|
319
|
+
for (const [key, value] of Object.entries(labels))
|
|
320
|
+
flat[String(key)] = String(value);
|
|
321
|
+
}
|
|
322
|
+
const claim = await recordedNamespaceClaim(name);
|
|
323
|
+
refuseForeignNamespace(ns, name, flat, claim.recorded ? claim.owner : owner);
|
|
324
|
+
refuseAnotherHome(ns, flat, claim);
|
|
325
|
+
if (claim.uid !== '' && doc !== null)
|
|
326
|
+
requireSameNamespace(ns, identityOf(ns, doc), claim.uid);
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Create the workspace's namespace with its ownership labels.
|
|
330
|
+
*
|
|
331
|
+
* REFUSES A NAMESPACE THAT IS NOT OURS. This used to create (ignoring the result, so an
|
|
332
|
+
* existing one was fine) and then label `--overwrite` whatever was there -- which stamped
|
|
333
|
+
* houston's ownership onto a namespace it did not make, after which `down --volumes` would
|
|
334
|
+
* delete it and its PVCs. The realistic trigger is not a stranger: it is two houston users
|
|
335
|
+
* on one adopted cluster.
|
|
336
|
+
*
|
|
337
|
+
* An UNLABELLED namespace is refused too, and that is a deliberate change of intent:
|
|
338
|
+
* houston cannot tell one an older houston made from somebody else's, and being wrong
|
|
339
|
+
* deletes data.
|
|
340
|
+
*/
|
|
341
|
+
async function ensureNamespace(name, { context, owner = '', emit = events_1.nullEmit, receipt, }) {
|
|
342
|
+
const ns = await namespaceFor(name);
|
|
343
|
+
const existing = await namespaceIdentity(ns, context);
|
|
344
|
+
if (existing !== null) {
|
|
345
|
+
const claim = await recordedNamespaceClaim(name);
|
|
346
|
+
const expectedOwner = claim.recorded ? claim.owner : owner;
|
|
347
|
+
refuseForeignNamespace(ns, name, existing.labels, expectedOwner);
|
|
348
|
+
refuseAnotherHome(ns, existing.labels, claim);
|
|
349
|
+
requireSameNamespace(ns, existing, claim.uid);
|
|
350
|
+
// MARKED NOW, one made before the mark existed: its labels alone are the same in every home
|
|
351
|
+
// that ever adopted it, so from here on another home is refused. Never `--overwrite`: a
|
|
352
|
+
// mark another home set in between stays theirs, and that home keeps the namespace.
|
|
353
|
+
if ((existing.labels[names_1.HOME_LABEL] ?? '') === '') {
|
|
354
|
+
const marked = await exec_1.kube.run(['kubectl', '--context', context, 'label', 'namespace', ns, `${names_1.HOME_LABEL}=${(0, config_1.homeId)()}`], { timeout: names_1.APPLY_TIMEOUT, own: (0, names_1.isOurs)(context) });
|
|
355
|
+
if (marked.returncode !== 0) {
|
|
356
|
+
(0, events_1.warn)(emit, `could not mark namespace ${ns} as this houston home's (${(0, ask_1.why)(marked)}) — it works as ` +
|
|
357
|
+
'before, without the protection the mark gives', { phase: 'provision' });
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
if (receipt !== undefined) {
|
|
361
|
+
Object.assign(receipt, { uid: existing.uid, owner: expectedOwner, createdUid: '' });
|
|
362
|
+
}
|
|
363
|
+
return ns;
|
|
364
|
+
}
|
|
365
|
+
// CREATE is atomic and includes the ownership labels. An AlreadyExists race is a
|
|
366
|
+
// refusal, never permission to label somebody else's namespace with --overwrite.
|
|
367
|
+
const res = await exec_1.kube.run(['kubectl', '--context', context, 'create', '-f', '-', '-o', 'json'], {
|
|
368
|
+
timeout: names_1.APPLY_TIMEOUT,
|
|
369
|
+
own: (0, names_1.isOurs)(context),
|
|
370
|
+
input: JSON.stringify({
|
|
371
|
+
apiVersion: 'v1',
|
|
372
|
+
kind: 'Namespace',
|
|
373
|
+
metadata: { name: ns, labels: { ...labelsFor(name, owner), [names_1.HOME_LABEL]: (0, config_1.homeId)() } },
|
|
374
|
+
}),
|
|
375
|
+
});
|
|
376
|
+
if (res.returncode !== 0) {
|
|
377
|
+
throw new errors_1.CreateFailedError(`could not create namespace ${ns}: ${(0, ask_1.why)(res)} — nothing else was adopted`);
|
|
378
|
+
}
|
|
379
|
+
let created;
|
|
380
|
+
try {
|
|
381
|
+
created = JSON.parse(res.stdout);
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
throw new errors_1.CreateFailedError(`namespace ${ns}'s create returned no readable identity — refusing to guess`);
|
|
385
|
+
}
|
|
386
|
+
if (created === null || typeof created !== 'object' || Array.isArray(created)) {
|
|
387
|
+
throw new errors_1.CreateFailedError(`namespace ${ns}'s create returned no readable identity — refusing to guess`);
|
|
388
|
+
}
|
|
389
|
+
const identity = identityOf(ns, created);
|
|
390
|
+
refuseForeignNamespace(ns, name, identity.labels, owner);
|
|
391
|
+
if (receipt !== undefined) {
|
|
392
|
+
Object.assign(receipt, { uid: identity.uid, owner, createdUid: identity.uid });
|
|
393
|
+
}
|
|
394
|
+
(0, events_1.info)(emit, `created namespace ${ns}`, { phase: 'provision' });
|
|
395
|
+
return ns;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* PVCs belonging to a workspace, so `prune` can prove what it reclaimed.
|
|
399
|
+
*
|
|
400
|
+
* Still `[]` on a refused read, and deliberately: the one caller uses this to say how many
|
|
401
|
+
* volumes are about to go, so being wrong understates a count in a message. Nothing
|
|
402
|
+
* branches on it. Named here so the next reader knows it was considered rather than missed.
|
|
403
|
+
*
|
|
404
|
+
* EVERY claim in the namespace, not only houston-labelled ones: the namespace is the
|
|
405
|
+
* workspace's alone, and the MongoDB operator's claims carry its labels, not houston's, so
|
|
406
|
+
* an operator workspace's teardown said "0 volume(s)" while it deleted two.
|
|
407
|
+
*/
|
|
408
|
+
async function workspacePvcs(name, { context }) {
|
|
409
|
+
const res = await exec_1.kube.run(['kubectl', '--context', context, '-n', await namespaceFor(name), 'get', 'pvc', '-o', 'name'], { own: (0, names_1.isOurs)(context) });
|
|
410
|
+
if (res.returncode !== 0)
|
|
411
|
+
return [];
|
|
412
|
+
return res.stdout
|
|
413
|
+
.split('\n')
|
|
414
|
+
.filter((line) => line.trim() !== '')
|
|
415
|
+
.map((line) => {
|
|
416
|
+
const parts = line.split('/');
|
|
417
|
+
return (parts[parts.length - 1] ?? '').trim();
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
/** Whether an unreachable namespace belonged to a provably deleted cluster. */
|
|
421
|
+
async function clusterLossProvesNamespaceAbsent(ns, context, emit) {
|
|
422
|
+
if ((0, names_1.isOurs)(context)) {
|
|
423
|
+
const [names, probeFailed] = await (0, context_1.clusters)();
|
|
424
|
+
if (probeFailed === '' && !names.includes(names_1.CLUSTER_NAME)) {
|
|
425
|
+
(0, events_1.info)(emit, `cluster '${names_1.CLUSTER_NAME}' no longer exists, so namespace ${ns} went with it — ` +
|
|
426
|
+
'removing the local record', { phase: 'teardown' });
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
/** Delete a namespace and confirm that its pods and volumes are gone. */
|
|
433
|
+
/** Delete `ns` only as `identity` read it: the API server refuses a replacement or a change. */
|
|
434
|
+
async function deleteExactly(ns, context, own, identity) {
|
|
435
|
+
return await exec_1.kube.run([
|
|
436
|
+
'kubectl',
|
|
437
|
+
'--context',
|
|
438
|
+
context,
|
|
439
|
+
'delete',
|
|
440
|
+
'--raw',
|
|
441
|
+
`/api/v1/namespaces/${encodeURIComponent(ns)}`,
|
|
442
|
+
'-f',
|
|
443
|
+
'-',
|
|
444
|
+
], {
|
|
445
|
+
timeout: names_1.APPLY_TIMEOUT,
|
|
446
|
+
own,
|
|
447
|
+
// Ordinary `kubectl delete -f` does not use metadata.uid as a precondition.
|
|
448
|
+
// --raw sends this DeleteOptions body to the API server unchanged.
|
|
449
|
+
input: JSON.stringify({
|
|
450
|
+
apiVersion: 'v1',
|
|
451
|
+
kind: 'DeleteOptions',
|
|
452
|
+
preconditions: { uid: identity.uid, resourceVersion: identity.resourceVersion },
|
|
453
|
+
}),
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Delete a namespace no record here explains, exactly as `stillStray` judges it NOW.
|
|
458
|
+
*
|
|
459
|
+
* `prune --orphans` read its labels, then deleted it by name: a namespace another home had
|
|
460
|
+
* made in its place between the two went instead. The labels are judged again on the one
|
|
461
|
+
* read the delete is then bound to, by UID and resourceVersion. A refused read raises; gone
|
|
462
|
+
* already is a removal, not a refusal.
|
|
463
|
+
*/
|
|
464
|
+
async function deleteStray(ns, { context, stillStray, }) {
|
|
465
|
+
const identity = await namespaceIdentity(ns, context);
|
|
466
|
+
if (identity === null)
|
|
467
|
+
return { deleted: true, why: '' };
|
|
468
|
+
if (!stillStray(identity.labels)) {
|
|
469
|
+
return { deleted: false, why: 'it changed after it was read, so it is left alone' };
|
|
470
|
+
}
|
|
471
|
+
const res = await deleteExactly(ns, context, (0, names_1.isOurs)(context), identity);
|
|
472
|
+
return res.returncode === 0 ? { deleted: true, why: '' } : { deleted: false, why: (0, ask_1.why)(res) };
|
|
473
|
+
}
|
|
474
|
+
async function deleteNamespaceAndVolumes(name, ns, context, own, identity, emit, sleep) {
|
|
475
|
+
const pvcs = await workspacePvcs(name, { context });
|
|
476
|
+
(0, events_1.info)(emit, `deleting namespace ${ns}: ${String(pvcs.length)} volume(s), pods and the release`, {
|
|
477
|
+
phase: 'teardown',
|
|
478
|
+
pct: 10,
|
|
479
|
+
});
|
|
480
|
+
const res = await deleteExactly(ns, context, own, identity);
|
|
481
|
+
if (res.returncode !== 0)
|
|
482
|
+
throw new errors_1.DockerError(`could not delete namespace ${ns}: ${(0, ask_1.why)(res)}`);
|
|
483
|
+
let seen = null;
|
|
484
|
+
for (let attempt = 0; attempt < names_1.NS_GONE_TRIES; attempt += 1) {
|
|
485
|
+
seen = await (0, ask_1.askObject)('namespace', ns, { context });
|
|
486
|
+
if ((0, ask_1.absent)(seen)) {
|
|
487
|
+
(0, events_1.info)(emit, `namespace ${ns} and its volume(s) are gone`, { phase: 'teardown', pct: 100 });
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
if ((0, ask_1.present)(seen)) {
|
|
491
|
+
const phase = phaseOf(seen.doc) || 'Terminating';
|
|
492
|
+
if (attempt % 4 === 0) {
|
|
493
|
+
(0, events_1.info)(emit, `namespace ${ns} is ${phase} — waiting for the pods and volume(s) to go`, {
|
|
494
|
+
phase: 'teardown',
|
|
495
|
+
pct: Math.min(90, 20 + attempt * 5),
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
else if (attempt % 4 === 0) {
|
|
500
|
+
(0, events_1.info)(emit, `cannot reach cluster '${context}' to confirm ${ns} is gone — still trying`, {
|
|
501
|
+
phase: 'teardown',
|
|
502
|
+
pct: Math.min(90, 20 + attempt * 5),
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
await sleep(names_1.NS_GONE_INTERVAL);
|
|
506
|
+
}
|
|
507
|
+
if (seen !== null && seen.state === 'refused') {
|
|
508
|
+
(0, events_1.warn)(emit, `could not confirm that namespace ${ns} is gone: ${(0, ask_1.answerWhy)(seen)} — the local ` +
|
|
509
|
+
`record is KEPT. Check the cluster ('${context}'), then \`houston down --name ` +
|
|
510
|
+
`${name} --volumes\` again.`, { phase: 'teardown' });
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
(0, events_1.warn)(emit, `namespace ${ns} is still terminating after ` +
|
|
514
|
+
`${String(Math.trunc(names_1.NS_GONE_TRIES * names_1.NS_GONE_INTERVAL))}s — Kubernetes will finish on ` +
|
|
515
|
+
`its own. The local record is KEPT so you can retry: \`kubectl get ns ${ns}\`, then ` +
|
|
516
|
+
`\`houston down --name ${name} --volumes\` again once it is gone.`, { phase: 'teardown' });
|
|
517
|
+
return false;
|
|
518
|
+
}
|
|
519
|
+
/** Remove the release and hand-written MongoDB while retaining its PVC. */
|
|
520
|
+
async function uninstallWorkspaceWorkloads(ns, context, own, emit) {
|
|
521
|
+
(0, events_1.info)(emit, `uninstalling ${names_1.RELEASE} from ${ns} — the volume is kept`, { phase: 'teardown' });
|
|
522
|
+
const rel = await exec_1.kube.run(['helm', 'uninstall', names_1.RELEASE, '--kube-context', context, '-n', ns], {
|
|
523
|
+
timeout: names_1.APPLY_TIMEOUT,
|
|
524
|
+
own,
|
|
525
|
+
});
|
|
526
|
+
if (rel.returncode !== 0 && !(0, ask_1.why)(rel).toLowerCase().includes('not found')) {
|
|
527
|
+
throw new errors_1.DockerError(`could not uninstall ${names_1.RELEASE} from ${ns}: ${(0, ask_1.why)(rel)}`);
|
|
528
|
+
}
|
|
529
|
+
// MongoDB BY NAME, and still only houston's. The owner label alone is on every preset
|
|
530
|
+
// object too, so this took every preset's Service while their Deployments ran on; `up` then
|
|
531
|
+
// made them anew at new addresses, under a minio-ui nginx still proxying to the old one.
|
|
532
|
+
// The label stays: an operator's own `mongodb` StatefulSet is not houston's to delete.
|
|
533
|
+
const gone = await exec_1.kube.run([
|
|
534
|
+
'kubectl',
|
|
535
|
+
'--context',
|
|
536
|
+
context,
|
|
537
|
+
'-n',
|
|
538
|
+
ns,
|
|
539
|
+
'delete',
|
|
540
|
+
'statefulset,service',
|
|
541
|
+
'-l',
|
|
542
|
+
names_1.OWNER_SELECTOR,
|
|
543
|
+
'--field-selector',
|
|
544
|
+
`metadata.name=${names_1.MONGO_SERVICE}`,
|
|
545
|
+
], { timeout: names_1.APPLY_TIMEOUT, own });
|
|
546
|
+
if (gone.returncode !== 0) {
|
|
547
|
+
throw new errors_1.DockerError(`could not remove MongoDB from ${ns}: ${(0, ask_1.why)(gone)}`);
|
|
548
|
+
}
|
|
549
|
+
return true;
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Remove a workspace. Returns false if there was nothing there.
|
|
553
|
+
*
|
|
554
|
+
* `helm uninstall` does not delete the PVCs a StatefulSet created; Kubernetes retains them
|
|
555
|
+
* deliberately. Deleting the NAMESPACE does remove them -- which is why `down` without
|
|
556
|
+
* `--volumes` cannot just delete the namespace, or it would silently destroy data that
|
|
557
|
+
* Compose's `down` keeps.
|
|
558
|
+
*
|
|
559
|
+
* down uninstall the release, keep the namespace and its PVCs
|
|
560
|
+
* down --volumes delete the namespace, taking the PVCs with it
|
|
561
|
+
*/
|
|
562
|
+
async function deleteNamespace(name, { context, volumes = false, emit = events_1.nullEmit, sleep = realSleep, expectedUid, owner, }) {
|
|
563
|
+
const ns = await namespaceFor(name);
|
|
564
|
+
const own = (0, names_1.isOurs)(context);
|
|
565
|
+
// `namespaceLabels`, not `workspaceNamespaces`: that one returns [] both when there is
|
|
566
|
+
// nothing there and when the cluster could not be asked, and this used the second as
|
|
567
|
+
// proof of the first. A wrong kube-context, an expired credential or an RBAC denial
|
|
568
|
+
// therefore reported "nothing to remove" -- after which the caller deleted the local
|
|
569
|
+
// record and told the reader the namespace and its PersistentVolumeClaim were gone,
|
|
570
|
+
// while all of it went on running with the only record that knew about it destroyed.
|
|
571
|
+
let identity;
|
|
572
|
+
try {
|
|
573
|
+
identity = await namespaceIdentity(ns, context);
|
|
574
|
+
}
|
|
575
|
+
catch (error) {
|
|
576
|
+
// A DELETED CLUSTER IS NOT AN UNREACHABLE ONE. Its kubeconfig entry survives, but a
|
|
577
|
+
// namespace cannot outlive a cluster that is provably absent. A failed kind probe
|
|
578
|
+
// remains "I could not ask" and is re-raised.
|
|
579
|
+
if (error instanceof errors_1.DockerError &&
|
|
580
|
+
(await clusterLossProvesNamespaceAbsent(ns, context, emit))) {
|
|
581
|
+
return true;
|
|
582
|
+
}
|
|
583
|
+
throw error;
|
|
584
|
+
}
|
|
585
|
+
// ABSENT IS THE MOST CONFIRMED-GONE STATE THERE IS, and this returned false for it -- so
|
|
586
|
+
// teardown read "not confirmed gone", kept the local record, and said so. A Kubernetes
|
|
587
|
+
// record whose namespace has already been removed could then NEVER be deleted.
|
|
588
|
+
if (identity === null)
|
|
589
|
+
return true;
|
|
590
|
+
if (identity.labels[names_1.OWNER_LABEL_KEY] !== names_1.OWNER_LABEL_VALUE) {
|
|
591
|
+
// Refused rather than deleted. Reaching a namespace houston does not own is either a
|
|
592
|
+
// half-labelled adoption or the wrong cluster -- and neither is a reason to delete
|
|
593
|
+
// somebody's namespace.
|
|
594
|
+
throw new errors_1.ConflictError(`namespace ${ns} exists but is not labelled as houston's, so it will not be ` +
|
|
595
|
+
`deleted. Check you are on the right cluster ('${context}'), then remove it ` +
|
|
596
|
+
'yourself if you meant to');
|
|
597
|
+
}
|
|
598
|
+
const claim = await recordedNamespaceClaim(name);
|
|
599
|
+
refuseForeignNamespace(ns, name, identity.labels, owner ?? claim.owner);
|
|
600
|
+
// `recorded`, whatever the file says: every caller deletes a workspace this home holds a
|
|
601
|
+
// record of, or a namespace the failing create just made. Another home's mark still stops it.
|
|
602
|
+
refuseAnotherHome(ns, identity.labels, { recorded: true, uid: expectedUid ?? claim.uid });
|
|
603
|
+
requireSameNamespace(ns, identity, expectedUid ?? claim.uid);
|
|
604
|
+
if (volumes) {
|
|
605
|
+
// Reported as it happens, and WAITED for. `--wait=false` returned instantly and `down`
|
|
606
|
+
// said "removed" while the namespace was still Terminating.
|
|
607
|
+
return await deleteNamespaceAndVolumes(name, ns, context, own, identity, emit, sleep);
|
|
608
|
+
}
|
|
609
|
+
// CHECKED. Both of these had their return codes dropped, so an RBAC denial or an
|
|
610
|
+
// unreachable API server left the release installed and the pods running while `down`
|
|
611
|
+
// reported success. "already uninstalled" is not a failure -- a repeated `down` is
|
|
612
|
+
// normal -- so that one case is allowed through by name.
|
|
613
|
+
return await uninstallWorkspaceWorkloads(ns, context, own, emit);
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Raise unless the namespace this workspace's record names is still the one this home made.
|
|
617
|
+
*
|
|
618
|
+
* What `deleteNamespace` asks before deleting, asked by everything else that changes a
|
|
619
|
+
* Kubernetes workspace: start, stop, backup, restore, upgrade, ready. They acted on the
|
|
620
|
+
* namespace by its NAME, so once another home had removed this one's (`prune --orphans
|
|
621
|
+
* --force`) and made its own of that name, a restore here dropped that home's collections.
|
|
622
|
+
* A namespace that is not there is the caller's to report, in its own words.
|
|
623
|
+
*/
|
|
624
|
+
async function requireOwnNamespace(name, { context }) {
|
|
625
|
+
const ns = await namespaceFor(name);
|
|
626
|
+
const identity = await namespaceIdentity(ns, context);
|
|
627
|
+
if (identity === null)
|
|
628
|
+
return;
|
|
629
|
+
if (identity.labels[names_1.OWNER_LABEL_KEY] !== names_1.OWNER_LABEL_VALUE) {
|
|
630
|
+
throw new errors_1.ConflictError(`namespace ${ns} is not labelled as houston's, so '${name}' will not act on it. Check ` +
|
|
631
|
+
`you are on the right cluster ('${context}')`);
|
|
632
|
+
}
|
|
633
|
+
const claim = await recordedNamespaceClaim(name);
|
|
634
|
+
refuseForeignNamespace(ns, name, identity.labels, claim.owner);
|
|
635
|
+
refuseAnotherHome(ns, identity.labels, { recorded: true, uid: claim.uid });
|
|
636
|
+
requireSameNamespace(ns, identity, claim.uid);
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Block until namespace `ns` is actually gone. False if it is still terminating.
|
|
640
|
+
*
|
|
641
|
+
* Extracted for `prune --orphans`, which swept namespaces with `--wait=false` and then
|
|
642
|
+
* asked for the control plane in the same call -- so `deleteCluster` refused, correctly,
|
|
643
|
+
* because the namespaces prune had just deleted were still Terminating. Reclaiming the
|
|
644
|
+
* cluster took a SECOND prune twenty seconds later.
|
|
645
|
+
*
|
|
646
|
+
* SAME CORRECTION AS `deleteNamespace`, and it mattered more here: on a cluster houston
|
|
647
|
+
* did not create, the caller passed a context that does not exist in the kubeconfig,
|
|
648
|
+
* kubectl exited non-zero, and this returned true -- so `prune --orphans` printed
|
|
649
|
+
* "removed 1 orphaned namespace(s)" with exit 0 over a namespace still Terminating.
|
|
650
|
+
*/
|
|
651
|
+
async function waitNamespaceGone(ns, { context, emit = events_1.nullEmit, sleep = realSleep }) {
|
|
652
|
+
let seen = null;
|
|
653
|
+
for (let attempt = 0; attempt < names_1.NS_GONE_TRIES; attempt += 1) {
|
|
654
|
+
seen = await (0, ask_1.askObject)('namespace', ns, { context });
|
|
655
|
+
if ((0, ask_1.absent)(seen))
|
|
656
|
+
return true;
|
|
657
|
+
if ((0, ask_1.present)(seen)) {
|
|
658
|
+
const phase = phaseOf(seen.doc) || 'Terminating';
|
|
659
|
+
if (attempt % 4 === 0) {
|
|
660
|
+
(0, events_1.info)(emit, `namespace ${ns} is ${phase} — waiting for it to go before the cluster can be reclaimed`, { phase: 'done' });
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
else if (attempt % 4 === 0) {
|
|
664
|
+
(0, events_1.info)(emit, `cannot reach cluster '${context}' to confirm ${ns} is gone — still trying`, {
|
|
665
|
+
phase: 'done',
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
await sleep(names_1.NS_GONE_INTERVAL);
|
|
669
|
+
}
|
|
670
|
+
if (seen !== null && seen.state === 'refused') {
|
|
671
|
+
(0, events_1.warn)(emit, `could not confirm that namespace ${ns} is gone: ${(0, ask_1.answerWhy)(seen)} — the cluster is ` +
|
|
672
|
+
'left alone.', { phase: 'done' });
|
|
673
|
+
return false;
|
|
674
|
+
}
|
|
675
|
+
(0, events_1.warn)(emit, `namespace ${ns} is still terminating; the cluster is left alone. \`houston prune\` ` +
|
|
676
|
+
'again once `kubectl get ns` is clear.', { phase: 'done' });
|
|
677
|
+
return false;
|
|
678
|
+
}
|