@rocket.chat/houston 0.27.0-next.3 → 2.1.9
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 +109 -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 +642 -0
- package/data/docs/kubernetes.md +138 -0
- package/data/docs/loadtest.md +264 -0
- package/data/docs/troubleshooting.md +203 -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 +170 -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 +57 -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 +3694 -0
- package/dist/cli/help.js +183 -0
- package/dist/cli/index.js +15 -0
- package/dist/cli/perf.js +814 -0
- package/dist/cli/render.js +254 -0
- package/dist/cli/serve.js +858 -0
- package/dist/cli/spec-data.js +2599 -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 +345 -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 +694 -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 +1549 -0
- package/dist/core/lifecycle/create-k8s.js +960 -0
- package/dist/core/lifecycle/destroy.js +339 -0
- package/dist/core/lifecycle/inspect.js +636 -0
- package/dist/core/lifecycle/naming.js +298 -0
- package/dist/core/lifecycle/notes.js +862 -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 +818 -0
- package/dist/core/lifecycle/request.js +510 -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 +1051 -0
- package/dist/core/migrate.js +612 -0
- package/dist/core/monitor.js +494 -0
- package/dist/core/postready.js +976 -0
- package/dist/core/promote.js +804 -0
- package/dist/core/rcapi.js +1336 -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 +877 -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 +371 -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 +299 -0
- package/dist/perf/k6.js +235 -0
- package/dist/perf/mongoprof.js +171 -0
- package/dist/perf/rcmetrics.js +191 -0
- package/dist/perf/report.js +386 -0
- package/dist/perf/resources.js +236 -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 +70 -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 -194
- package/dist/outputCI.js +0 -59
- package/src/bin.js +0 -4
- package/src/cli.ts +0 -25
- package/src/commands/createBranch.ts +0 -28
- package/src/outputCI.ts +0 -12
- 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
package/dist/core/eks.js
ADDED
|
@@ -0,0 +1,2240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.aws = exports.STORAGE_CLASS = exports.STORAGE_CLASS_NAME = exports.METRICS_SERVER_MANIFEST = exports.METRICS_SERVER_VERSION = exports.SWEEP_WORKERS = exports.SWEEP_TIMEOUT = exports.DEFAULT_REGION_SWEEP = exports.MAX_NODES = exports.DEFAULT_NODES = exports.DEFAULT_VPC_CIDR = exports.EBS_CSI_ADDON_VERSION = exports.NODE_INSTANCE_TYPE = exports.K8S_VERSION = exports.CLUSTER_DELETE_TIMEOUT = exports.CLUSTER_CREATE_TIMEOUT = exports.DEFAULT_EXPIRY_DAYS = exports.ZONE_TAG_KEY = exports.EXPIRY_TAG_KEY = exports.OWNED_TAG_VALUE = exports.OWNED_TAG_KEY = exports.IDENTITY_TIMEOUT = exports.IDENTITY_TOOL = exports.iamDocument = exports.guidePath = exports.guide = exports.IAM_DOCUMENTS = exports.GUIDE_FILE = void 0;
|
|
37
|
+
exports.identity = identity;
|
|
38
|
+
exports.identityKnown = identityKnown;
|
|
39
|
+
exports.identityPretty = identityPretty;
|
|
40
|
+
exports.whoami = whoami;
|
|
41
|
+
exports.region = region;
|
|
42
|
+
exports.profiles = profiles;
|
|
43
|
+
exports.shadowedProfile = shadowedProfile;
|
|
44
|
+
exports.blockers = blockers;
|
|
45
|
+
exports.cloudReady = cloudReady;
|
|
46
|
+
exports.preflight = preflight;
|
|
47
|
+
exports.connectExisting = connectExisting;
|
|
48
|
+
exports.expiryDate = expiryDate;
|
|
49
|
+
exports.daysUntil = daysUntil;
|
|
50
|
+
exports.clusterConfig = clusterConfig;
|
|
51
|
+
exports.streamTool = streamTool;
|
|
52
|
+
exports.eksctlProgress = eksctlProgress;
|
|
53
|
+
exports.describeCluster = describeCluster;
|
|
54
|
+
exports.clusterExists = clusterExists;
|
|
55
|
+
exports.ownsCluster = ownsCluster;
|
|
56
|
+
exports.oneLine = oneLine;
|
|
57
|
+
exports.enabledRegions = enabledRegions;
|
|
58
|
+
exports.sweepClusters = sweepClusters;
|
|
59
|
+
exports.clusterDir = clusterDir;
|
|
60
|
+
exports.keepManifest = keepManifest;
|
|
61
|
+
exports.remember = remember;
|
|
62
|
+
exports.zoneOf = zoneOf;
|
|
63
|
+
exports.rememberZone = rememberZone;
|
|
64
|
+
exports.forget = forget;
|
|
65
|
+
exports.rememberedRegion = rememberedRegion;
|
|
66
|
+
exports.remembered = remembered;
|
|
67
|
+
exports.createCluster = createCluster;
|
|
68
|
+
exports.deleteCluster = deleteCluster;
|
|
69
|
+
exports.approveAccount = approveAccount;
|
|
70
|
+
exports.keepCluster = keepCluster;
|
|
71
|
+
exports.shareCluster = shareCluster;
|
|
72
|
+
exports.sweepLeftovers = sweepLeftovers;
|
|
73
|
+
exports.installMetricsServer = installMetricsServer;
|
|
74
|
+
exports.prepareCluster = prepareCluster;
|
|
75
|
+
/**
|
|
76
|
+
* The AWS seam's domain half. Everything that speaks to AWS rather than to a cluster.
|
|
77
|
+
*
|
|
78
|
+
* `core/kube` stays the only Kubernetes seam: this module asks AWS who you are and, later,
|
|
79
|
+
* asks `eksctl` to build a cluster, and everything that talks to a cluster once it exists
|
|
80
|
+
* goes through `core/kube` unchanged. The same relationship `runner` has with Docker and
|
|
81
|
+
* `rcapi` has with Rocket.Chat.
|
|
82
|
+
*
|
|
83
|
+
* **Credentials are never houston's to hold.** Being logged in to AWS is a PREREQUISITE,
|
|
84
|
+
* exactly as having Docker running is: the operator arranges it with `aws configure`,
|
|
85
|
+
* SSO, an instance profile or environment variables, and houston reads the ambient result
|
|
86
|
+
* and names what it found. Nothing here stores, prompts for, refreshes or writes a
|
|
87
|
+
* credential. That is also what makes an adopted EKS cluster work at all -- `aws eks
|
|
88
|
+
* update-kubeconfig` writes a kubeconfig whose user runs `aws eks get-token`, so kubectl
|
|
89
|
+
* itself needs those same ambient credentials, and the kube seam passes the environment
|
|
90
|
+
* through untouched for any cluster houston does not own.
|
|
91
|
+
*
|
|
92
|
+
* **The identity is reported, not just checked.** A refusal that says "not logged in" is
|
|
93
|
+
* half an answer; a confirmation that says "ready" is worse. `cluster support` exists in
|
|
94
|
+
* more than one AWS account, and the whole failure mode of this feature is acting on the
|
|
95
|
+
* wrong one -- so the account id and the caller ARN are carried on every answer and
|
|
96
|
+
* belong in every message that follows.
|
|
97
|
+
*
|
|
98
|
+
* ## NOTHING CAN CHECK THIS MODULE AT ANY REASONABLE PRICE
|
|
99
|
+
*
|
|
100
|
+
* That is the reason for how the module is shaped rather than a warning attached to it.
|
|
101
|
+
* No test can validate `eksctl create cluster` without spending twenty minutes and real
|
|
102
|
+
* money, so this module draws the line where it was always drawn: every DECISION is
|
|
103
|
+
* pure and testable -- the config document, the ownership rule, the blocker list, the
|
|
104
|
+
* expiry arithmetic, the sweep's bookkeeping -- and what is left unverifiable is the
|
|
105
|
+
* invocation itself. The ownership rule is asserted directly, because a delete authorised
|
|
106
|
+
* by a read that failed is the one thing that must never happen here.
|
|
107
|
+
*/
|
|
108
|
+
const fs = __importStar(require("node:fs"));
|
|
109
|
+
const path = __importStar(require("node:path"));
|
|
110
|
+
const config = __importStar(require("../lib/config"));
|
|
111
|
+
const errors_1 = require("../lib/errors");
|
|
112
|
+
const events_1 = require("../lib/events");
|
|
113
|
+
const map_limit_1 = require("../lib/map-limit");
|
|
114
|
+
const home_1 = require("../lib/home");
|
|
115
|
+
const journal_1 = require("../lib/journal");
|
|
116
|
+
const mkdir_parents_1 = require("../lib/mkdir-parents");
|
|
117
|
+
const tempdir_1 = require("../lib/tempdir");
|
|
118
|
+
const yaml_dump_1 = require("../lib/yaml-dump");
|
|
119
|
+
const aws_cli_1 = require("../seams/aws-cli");
|
|
120
|
+
const ask_1 = require("./kube/ask");
|
|
121
|
+
const capabilities_1 = require("./kube/capabilities");
|
|
122
|
+
const certmanager_1 = require("./kube/certmanager");
|
|
123
|
+
const context_1 = require("./kube/context");
|
|
124
|
+
const endpoints_1 = require("./kube/endpoints");
|
|
125
|
+
const exec_1 = require("./kube/exec");
|
|
126
|
+
const ingress_1 = require("./kube/ingress");
|
|
127
|
+
const names_1 = require("./kube/names");
|
|
128
|
+
const namespace_1 = require("./kube/namespace");
|
|
129
|
+
const reachability_1 = require("./kube/reachability");
|
|
130
|
+
const release_1 = require("./kube/release");
|
|
131
|
+
const tools_1 = require("./kube/tools");
|
|
132
|
+
const paths_1 = require("./runner/paths");
|
|
133
|
+
const locks_1 = require("./runner/locks");
|
|
134
|
+
var eks_docs_1 = require("./eks-docs");
|
|
135
|
+
Object.defineProperty(exports, "GUIDE_FILE", { enumerable: true, get: function () { return eks_docs_1.GUIDE_FILE; } });
|
|
136
|
+
Object.defineProperty(exports, "IAM_DOCUMENTS", { enumerable: true, get: function () { return eks_docs_1.IAM_DOCUMENTS; } });
|
|
137
|
+
Object.defineProperty(exports, "guide", { enumerable: true, get: function () { return eks_docs_1.guide; } });
|
|
138
|
+
Object.defineProperty(exports, "guidePath", { enumerable: true, get: function () { return eks_docs_1.guidePath; } });
|
|
139
|
+
Object.defineProperty(exports, "iamDocument", { enumerable: true, get: function () { return eks_docs_1.iamDocument; } });
|
|
140
|
+
const eks_docs_2 = require("./eks-docs");
|
|
141
|
+
/**
|
|
142
|
+
* What talking to AWS needs, and it is not the same question as what BUILDING a cluster
|
|
143
|
+
* needs. `aws` alone is enough to authenticate against an EKS cluster somebody else made
|
|
144
|
+
* -- that is the exec credential plugin in the kubeconfig -- while `eksctl` is only ever
|
|
145
|
+
* needed to create or destroy one. Kept apart so a box set up to USE an EKS cluster is
|
|
146
|
+
* never told it is missing a provisioning tool it will never run.
|
|
147
|
+
*/
|
|
148
|
+
exports.IDENTITY_TOOL = 'aws';
|
|
149
|
+
/**
|
|
150
|
+
* Seconds. An `sts` call is one round trip to a public endpoint; a box with no route to
|
|
151
|
+
* it should answer "I could not ask" quickly rather than hang a `doctor`.
|
|
152
|
+
*/
|
|
153
|
+
exports.IDENTITY_TIMEOUT = 15;
|
|
154
|
+
const asMs = (seconds) => Math.round(seconds * 1000);
|
|
155
|
+
/** A refused AWS read keeps the only record of a possibly billable cluster. */
|
|
156
|
+
async function clusterStillThere(entry) {
|
|
157
|
+
const seen = await clusterExists(entry.workspace, String(entry.detail.region ?? ''));
|
|
158
|
+
return seen.state !== ask_1.ABSENT;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Teach recovery how to ask whether an unfinished cluster is still there.
|
|
162
|
+
*
|
|
163
|
+
* At module scope, so it is registered as soon as the module loads, and queued by the
|
|
164
|
+
* registry until the real journal is installed.
|
|
165
|
+
*/
|
|
166
|
+
(0, journal_1.registerSubjectPredicate)(journal_1.CLUSTER_UNFINISHED, clusterStillThere);
|
|
167
|
+
function identity(values = {}) {
|
|
168
|
+
return { account: '', arn: '', error: '', ...values };
|
|
169
|
+
}
|
|
170
|
+
function identityKnown(value) {
|
|
171
|
+
return value.account !== '';
|
|
172
|
+
}
|
|
173
|
+
/** How the identity is named in every message that mentions it. */
|
|
174
|
+
function identityPretty(value) {
|
|
175
|
+
if (!identityKnown(value))
|
|
176
|
+
return 'not signed in';
|
|
177
|
+
const who = value.arn.includes('/') ? value.arn.split('/').pop() : value.arn;
|
|
178
|
+
return `${String(who)} in account ${value.account}`;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Ask AWS who this box is acting as. Never throws.
|
|
182
|
+
*
|
|
183
|
+
* `sts get-caller-identity` is the right question because it is the only one that needs
|
|
184
|
+
* no permissions beyond having credentials at all -- anything else would conflate "not
|
|
185
|
+
* signed in" with "signed in and not allowed to do that", which are different problems
|
|
186
|
+
* with different fixes.
|
|
187
|
+
*/
|
|
188
|
+
async function whoami({ timeout = exports.IDENTITY_TIMEOUT } = {}) {
|
|
189
|
+
if ((0, tools_1.which)(exports.IDENTITY_TOOL) === '') {
|
|
190
|
+
return identity({ error: `${exports.IDENTITY_TOOL} is not installed` });
|
|
191
|
+
}
|
|
192
|
+
const res = await aws_cli_1.awsCli.run([exports.IDENTITY_TOOL, 'sts', 'get-caller-identity', '--output', 'json'], {
|
|
193
|
+
timeoutMs: asMs(timeout),
|
|
194
|
+
});
|
|
195
|
+
if (res.returncode !== 0) {
|
|
196
|
+
// Expired SSO, no profile, no route to STS -- all land here, and the text AWS prints
|
|
197
|
+
// is far more useful than any sentence houston could substitute.
|
|
198
|
+
return identity({ error: (0, ask_1.why)(res, { limit: 200 }) });
|
|
199
|
+
}
|
|
200
|
+
let doc;
|
|
201
|
+
try {
|
|
202
|
+
doc = JSON.parse(res.stdout || '{}');
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
return identity({ error: 'sts returned something that is not JSON' });
|
|
206
|
+
}
|
|
207
|
+
const parsed = doc !== null && typeof doc === 'object' ? doc : {};
|
|
208
|
+
return identity({
|
|
209
|
+
account: String(parsed.Account ?? ''),
|
|
210
|
+
arn: String(parsed.Arn ?? ''),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* The region this box would use, or ''.
|
|
215
|
+
*
|
|
216
|
+
* Read the way the AWS tooling itself resolves it -- environment first, then the
|
|
217
|
+
* configured profile -- so houston and `eksctl` cannot disagree about where a cluster is
|
|
218
|
+
* about to be built. A cluster in the wrong region is not a small mistake: it is
|
|
219
|
+
* invisible to every command afterwards, which all default to the same resolution.
|
|
220
|
+
*/
|
|
221
|
+
async function region() {
|
|
222
|
+
for (const name of ['AWS_REGION', 'AWS_DEFAULT_REGION']) {
|
|
223
|
+
const found = (process.env[name] ?? '').trim();
|
|
224
|
+
if (found !== '')
|
|
225
|
+
return found;
|
|
226
|
+
}
|
|
227
|
+
if ((0, tools_1.which)(exports.IDENTITY_TOOL) === '')
|
|
228
|
+
return '';
|
|
229
|
+
const res = await aws_cli_1.awsCli.run([exports.IDENTITY_TOOL, 'configure', 'get', 'region'], {
|
|
230
|
+
timeoutMs: asMs(exports.IDENTITY_TIMEOUT),
|
|
231
|
+
});
|
|
232
|
+
return res.returncode === 0 ? res.stdout.trim() : '';
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* The named profiles in this box's AWS config, or [] when the tool cannot say. What the
|
|
236
|
+
* setup page offers when the server was started without AWS_PROFILE: `aws sts` working in
|
|
237
|
+
* a shell that had it says nothing about the server that did not. Not `default`, which an
|
|
238
|
+
* unset AWS_PROFILE already uses.
|
|
239
|
+
*/
|
|
240
|
+
async function profiles() {
|
|
241
|
+
if ((0, tools_1.which)(exports.IDENTITY_TOOL) === '')
|
|
242
|
+
return [];
|
|
243
|
+
const res = await aws_cli_1.awsCli.run([exports.IDENTITY_TOOL, 'configure', 'list-profiles'], {
|
|
244
|
+
timeoutMs: asMs(exports.IDENTITY_TIMEOUT),
|
|
245
|
+
});
|
|
246
|
+
if (res.returncode !== 0)
|
|
247
|
+
return [];
|
|
248
|
+
return res.stdout
|
|
249
|
+
.split('\n')
|
|
250
|
+
.map((line) => line.trim())
|
|
251
|
+
.filter((line) => line !== '' && line !== 'default');
|
|
252
|
+
}
|
|
253
|
+
/** The `key = value` pairs of one `[section]` of an AWS ini file; {} when absent. */
|
|
254
|
+
function iniSection(file, section) {
|
|
255
|
+
let text;
|
|
256
|
+
try {
|
|
257
|
+
text = fs.readFileSync(file, 'utf8');
|
|
258
|
+
}
|
|
259
|
+
catch {
|
|
260
|
+
return {};
|
|
261
|
+
}
|
|
262
|
+
const out = {};
|
|
263
|
+
let inside = false;
|
|
264
|
+
for (const raw of text.split(/\r?\n/u)) {
|
|
265
|
+
const line = raw.trim();
|
|
266
|
+
const header = /^\[\s*(.+?)\s*\]$/u.exec(line);
|
|
267
|
+
if (header !== null) {
|
|
268
|
+
// `[profile name]` is `[profile name]`, as the AWS tools read it.
|
|
269
|
+
inside = (header[1] ?? '').replace(/\s+/gu, ' ') === section;
|
|
270
|
+
}
|
|
271
|
+
else if (inside && line !== '' && !line.startsWith('#') && !line.startsWith(';')) {
|
|
272
|
+
const at = line.indexOf('=');
|
|
273
|
+
if (at > 0)
|
|
274
|
+
out[line.slice(0, at).trim().toLowerCase()] = line.slice(at + 1).trim();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return out;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Keys in the credentials file that eksctl signs in with INSTEAD of the profile's SSO.
|
|
281
|
+
*
|
|
282
|
+
* For one profile with both, the aws CLI reads the SSO settings first and the Go SDK in
|
|
283
|
+
* eksctl reads the credentials file first. So houston's identity check (the aws CLI)
|
|
284
|
+
* passed, and a second later eksctl was refused -- `ExpiredToken`, on keys pasted into
|
|
285
|
+
* ~/.aws/credentials long before -- and with keys that still worked it would build as a
|
|
286
|
+
* different identity from the one houston checked and the spending gate approved.
|
|
287
|
+
* Returns the sentence that says which section to remove, or ''.
|
|
288
|
+
*/
|
|
289
|
+
function shadowedProfile(env = process.env) {
|
|
290
|
+
const named = (key) => (env[key] ?? '').trim();
|
|
291
|
+
const profile = [named('AWS_PROFILE'), named('AWS_DEFAULT_PROFILE')].find((v) => v !== '');
|
|
292
|
+
const name = profile ?? 'default';
|
|
293
|
+
const configFile = named('AWS_CONFIG_FILE') || (0, home_1.joinPath)((0, home_1.pathHome)(), '.aws', 'config');
|
|
294
|
+
const keysFile = named('AWS_SHARED_CREDENTIALS_FILE') || (0, home_1.joinPath)((0, home_1.pathHome)(), '.aws', 'credentials');
|
|
295
|
+
// `[profile default]` is the default profile too.
|
|
296
|
+
const settings = {
|
|
297
|
+
...(name === 'default' ? iniSection(configFile, 'default') : {}),
|
|
298
|
+
...iniSection(configFile, `profile ${name}`),
|
|
299
|
+
};
|
|
300
|
+
const sso = ['sso_session', 'sso_start_url', 'sso_account_id'].some((key) => key in settings);
|
|
301
|
+
// Keys beside the SSO settings in the config file shadow it the same way.
|
|
302
|
+
const keysHere = 'aws_access_key_id' in settings;
|
|
303
|
+
if (!sso || (!keysHere && !('aws_access_key_id' in iniSection(keysFile, name))))
|
|
304
|
+
return '';
|
|
305
|
+
const where = keysHere ? configFile : keysFile;
|
|
306
|
+
const section = keysHere && name !== 'default' ? `profile ${name}` : name;
|
|
307
|
+
return (`${where} holds keys under [${section}], and eksctl signs in with those instead of ` +
|
|
308
|
+
`the profile's SSO sign-in that the aws CLI uses -- so eksctl would act as a different, ` +
|
|
309
|
+
`or an expired, identity from the one houston checked. Remove ` +
|
|
310
|
+
(keysHere
|
|
311
|
+
? `the aws_access_key_id, aws_secret_access_key and aws_session_token lines from [${section}] in ${where}`
|
|
312
|
+
: `the [${section}] section from ${where}`) +
|
|
313
|
+
` (keep a copy), then \`aws sso login --profile ${name}\` if it asks`);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Everything that would stop a create, each phrased as its own remedy.
|
|
317
|
+
*
|
|
318
|
+
* Ordered by what to do first: a missing tool cannot be diagnosed further, and asking
|
|
319
|
+
* about credentials before `aws` exists produces a confusing second complaint about the
|
|
320
|
+
* same absence.
|
|
321
|
+
*/
|
|
322
|
+
function blockers(pre, { toBuild = false } = {}) {
|
|
323
|
+
const out = [];
|
|
324
|
+
// kubectl and helm are for BUILDING (prepare runs them after the money is spent), so a
|
|
325
|
+
// create refuses without them. A delete never needs helm, and needs kubectl only to look
|
|
326
|
+
// inside first -- `deleteCluster` asks for it there, and `--force` does without, so a
|
|
327
|
+
// missing tool is never what stops somebody stopping a bill.
|
|
328
|
+
const coreTools = new Set(Object.keys(names_1.CORE_TOOLS));
|
|
329
|
+
for (const name of Object.keys(pre.tools).sort()) {
|
|
330
|
+
if (!toBuild && coreTools.has(name))
|
|
331
|
+
continue;
|
|
332
|
+
const found = pre.tools[name];
|
|
333
|
+
if (!(0, tools_1.toolPresent)(found)) {
|
|
334
|
+
out.push(`${name} is not installed`);
|
|
335
|
+
}
|
|
336
|
+
else if (!(0, tools_1.newEnough)(found)) {
|
|
337
|
+
const floor = (0, names_1.tools)()[name] ?? [];
|
|
338
|
+
const want = floor.length > 0 ? floor.join('.') : 'a newer release';
|
|
339
|
+
out.push(`${name} ${(0, tools_1.pretty)(found)} is older than ${want}`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (out.some((line) => line.includes('not installed')))
|
|
343
|
+
return out;
|
|
344
|
+
if (!identityKnown(pre.identity)) {
|
|
345
|
+
out.push('not signed in to AWS' +
|
|
346
|
+
(pre.identity.error !== '' ? ` (${pre.identity.error})` : '') +
|
|
347
|
+
'. houston never holds AWS credentials — arrange them with `aws configure`, ' +
|
|
348
|
+
'`aws sso login`, an instance profile or the standard environment variables, ' +
|
|
349
|
+
'and it will use what it finds.');
|
|
350
|
+
}
|
|
351
|
+
if ((pre.shadowed ?? '') !== '')
|
|
352
|
+
out.push(pre.shadowed);
|
|
353
|
+
if (pre.region === '') {
|
|
354
|
+
out.push('no AWS region resolved. Set one with `aws configure set region <region>` or ' +
|
|
355
|
+
'`AWS_REGION`, so houston and eksctl cannot disagree about where a cluster is ' +
|
|
356
|
+
'being built.');
|
|
357
|
+
}
|
|
358
|
+
return out;
|
|
359
|
+
}
|
|
360
|
+
/** Whether this box could BUILD a cluster: the four tools, an identity, a region. */
|
|
361
|
+
function cloudReady(pre) {
|
|
362
|
+
return blockers(pre, { toBuild: true }).length === 0;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Everything the cloud path needs, in one pass, changing nothing.
|
|
366
|
+
*
|
|
367
|
+
* CONCURRENTLY, because this is the slowest thing `doctor` does and almost all of it is
|
|
368
|
+
* waiting. MEASURED: `aws sts get-caller-identity` 1191ms, `aws
|
|
369
|
+
* configure get region` 301ms, `aws --version` 291ms -- 1783ms of a 2485ms report, and
|
|
370
|
+
* every one of them a separate process start or network round trip that has nothing to
|
|
371
|
+
* say to the others.
|
|
372
|
+
*
|
|
373
|
+
* Two waves, because `whoami` needs to know whether `aws` is installed before it is worth
|
|
374
|
+
* asking who you are. Within each wave nothing depends on anything else. The calls are
|
|
375
|
+
* already asynchronous, so `Promise.all` states it with nothing to join.
|
|
376
|
+
*/
|
|
377
|
+
async function preflight() {
|
|
378
|
+
// ALL FOUR TOOLS, not only the two that start the build. kubectl and helm are needed
|
|
379
|
+
// twenty minutes later, to prepare the cluster, and a create that checked only aws and
|
|
380
|
+
// eksctl left a cluster that billed and could not run a workspace yet.
|
|
381
|
+
const names = [
|
|
382
|
+
...new Set([
|
|
383
|
+
...Object.keys(names_1.tuning.CLOUD_PROVISION_TOOLS),
|
|
384
|
+
exports.IDENTITY_TOOL,
|
|
385
|
+
...Object.keys(names_1.CORE_TOOLS),
|
|
386
|
+
]),
|
|
387
|
+
];
|
|
388
|
+
const probed = Object.fromEntries(await Promise.all(names.map(async (name) => [name, await (0, tools_1.tool)(name)])));
|
|
389
|
+
// `region` does not depend on the identity, so it runs beside it rather than after it
|
|
390
|
+
// -- 301ms that used to be added to the 1191ms instead of hidden by it.
|
|
391
|
+
const [resolvedRegion, ident] = await Promise.all([
|
|
392
|
+
region(),
|
|
393
|
+
(0, tools_1.toolPresent)(probed[exports.IDENTITY_TOOL])
|
|
394
|
+
? whoami()
|
|
395
|
+
: Promise.resolve(identity({ error: `${exports.IDENTITY_TOOL} is not installed` })),
|
|
396
|
+
]);
|
|
397
|
+
return { tools: probed, identity: ident, region: resolvedRegion, shadowed: shadowedProfile() };
|
|
398
|
+
}
|
|
399
|
+
/** Add an existing EKS cluster to this server's kubeconfig without provisioning it. */
|
|
400
|
+
async function connectExisting(arn) {
|
|
401
|
+
const match = arn.match(/^arn:[^:]+:eks:([^:]+):(\d{12}):cluster\/([^/]+)$/u);
|
|
402
|
+
if (match === null)
|
|
403
|
+
throw new errors_1.ValidationError('select a full EKS cluster ARN');
|
|
404
|
+
const [, region, account, name] = match;
|
|
405
|
+
if (region === undefined || account === undefined || name === undefined) {
|
|
406
|
+
throw new errors_1.ValidationError('select a full EKS cluster ARN');
|
|
407
|
+
}
|
|
408
|
+
// Using an existing cluster needs an AWS identity, not the four provisioning tools
|
|
409
|
+
// or spending approval. Check the resolved account before writing kubeconfig.
|
|
410
|
+
const caller = await exports.aws.whoami();
|
|
411
|
+
if (!identityKnown(caller)) {
|
|
412
|
+
throw new errors_1.PreflightError(`AWS sign-in is required on the Houston server: ${caller.error}`);
|
|
413
|
+
}
|
|
414
|
+
if (caller.account !== account) {
|
|
415
|
+
throw new errors_1.AuthorityGateError(`cluster ${arn} belongs to account ${account}; this server is signed in to ${caller.account}`, {
|
|
416
|
+
kind: 'cluster',
|
|
417
|
+
subject: arn,
|
|
418
|
+
approveWith: 'sign in to the selected AWS account',
|
|
419
|
+
code: 'GATE_WRONG_ACCOUNT',
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
const seen = await exports.aws.describeCluster(name, region);
|
|
423
|
+
if (seen.state === ask_1.ABSENT)
|
|
424
|
+
throw new errors_1.NotFoundError(`EKS cluster ${arn} was not found`);
|
|
425
|
+
if (seen.state !== ask_1.PRESENT) {
|
|
426
|
+
throw new errors_1.PreflightError(`EKS cluster ${arn} could not be read with this AWS identity`);
|
|
427
|
+
}
|
|
428
|
+
if (String(seen.doc.arn ?? '') !== arn) {
|
|
429
|
+
throw new errors_1.PreflightError(`AWS returned a different cluster ARN for ${name} in ${region}`);
|
|
430
|
+
}
|
|
431
|
+
// update-kubeconfig selects its new entry as the process-wide current context. Hold
|
|
432
|
+
// one lock across read, write and restore so two GUI/CLI connections cannot leave
|
|
433
|
+
// each other's cluster selected. An initially unset context must stay unset too.
|
|
434
|
+
await (0, locks_1.reproLock)('kubeconfig-connect', async () => {
|
|
435
|
+
const current = await exec_1.kube.run(['kubectl', 'config', 'current-context']);
|
|
436
|
+
const previous = current.returncode === 0 ? current.stdout.trim() : '';
|
|
437
|
+
const env = (0, exec_1.userEnv)();
|
|
438
|
+
let wrote;
|
|
439
|
+
let updateError;
|
|
440
|
+
try {
|
|
441
|
+
wrote = await aws_cli_1.awsCli.run(['aws', 'eks', 'update-kubeconfig', '--name', name, '--region', region, '--alias', arn], { timeoutMs: asMs(exports.IDENTITY_TIMEOUT), ...(env !== undefined ? { env } : {}) });
|
|
442
|
+
}
|
|
443
|
+
catch (error) {
|
|
444
|
+
updateError = error;
|
|
445
|
+
}
|
|
446
|
+
const restore = previous === ''
|
|
447
|
+
? ['kubectl', 'config', 'unset', 'current-context']
|
|
448
|
+
: ['kubectl', 'config', 'use-context', previous];
|
|
449
|
+
const restored = await exec_1.kube.run(restore);
|
|
450
|
+
if (restored.returncode !== 0) {
|
|
451
|
+
throw new errors_1.PreflightError(`connected ${arn}, but could not restore the previous kubectl context`);
|
|
452
|
+
}
|
|
453
|
+
if (updateError !== undefined)
|
|
454
|
+
throw updateError;
|
|
455
|
+
if (wrote === undefined || wrote.returncode !== 0) {
|
|
456
|
+
throw new errors_1.PreflightError(`could not connect ${arn}: ${wrote === undefined ? 'AWS CLI did not answer' : oneLine(wrote)}`);
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
const reached = await (0, reachability_1.reach)(arn);
|
|
460
|
+
return {
|
|
461
|
+
context: arn,
|
|
462
|
+
account,
|
|
463
|
+
principal: caller.arn,
|
|
464
|
+
region,
|
|
465
|
+
reachable: (0, reachability_1.reachOk)(reached),
|
|
466
|
+
access_reason: (0, reachability_1.reachOk)(reached) ? '' : (0, reachability_1.reachWhy)(reached),
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Cluster ownership, by the same rule as namespace ownership and for the same reason.
|
|
471
|
+
* `OWNER_LABEL_KEY` says it first -- anyone can create a namespace called
|
|
472
|
+
* `houston-anything`, and a teardown that matched on the prefix would eventually delete
|
|
473
|
+
* one houston never made. A cluster NAME is likewise the operator's choice, and the
|
|
474
|
+
* stakes here are a VPC and a NAT gateway rather than a container.
|
|
475
|
+
*
|
|
476
|
+
* The tag lives ON THE CLUSTER, in AWS, and not in HOUSTON_HOME. CLAUDE.md tells anyone
|
|
477
|
+
* writing a script to `export HOUSTON_HOME=$(mktemp -d)`, so a local ownership record is
|
|
478
|
+
* one followed instruction away from leaving a billable cluster houston can no longer
|
|
479
|
+
* prove is its own. A tag survives a lost home, a different machine and a colleague.
|
|
480
|
+
*/
|
|
481
|
+
exports.OWNED_TAG_KEY = 'houston.io/managed-by';
|
|
482
|
+
exports.OWNED_TAG_VALUE = 'houston';
|
|
483
|
+
/**
|
|
484
|
+
* When somebody said they would be finished with this cluster. A REMINDER, never a
|
|
485
|
+
* policy: nothing acts on it, and in particular nothing deletes on it. Removing a cloud
|
|
486
|
+
* cluster is minutes of destructive work on somebody's AWS account, and a timer is the
|
|
487
|
+
* worst possible thing to have decided it -- on a shared cluster the namespaces may
|
|
488
|
+
* belong to colleagues on machines this box has never seen, which is the same argument
|
|
489
|
+
* the delete guards are built on.
|
|
490
|
+
*/
|
|
491
|
+
exports.EXPIRY_TAG_KEY = 'houston.io/expires';
|
|
492
|
+
/**
|
|
493
|
+
* The DNS zone workspaces get their public names under, e.g. `rc.example.com` for
|
|
494
|
+
* `ticket-1234.rc.example.com`. Known once, at build time, and previously typed again as
|
|
495
|
+
* --domain on every ticket. On the cluster (true on every machine) and in the note.
|
|
496
|
+
*/
|
|
497
|
+
exports.ZONE_TAG_KEY = 'houston.io/zone';
|
|
498
|
+
/**
|
|
499
|
+
* Days, and short on purpose. The safe case is the default: a reproduction cluster is
|
|
500
|
+
* wanted for a ticket, not a quarter, and the failure this guards against is the one
|
|
501
|
+
* nobody notices -- a cluster at roughly $0.24/hr is about $175 a month, which is a sum
|
|
502
|
+
* that turns up on a bill long after the person who made it has moved on.
|
|
503
|
+
*/
|
|
504
|
+
exports.DEFAULT_EXPIRY_DAYS = 7;
|
|
505
|
+
/**
|
|
506
|
+
* Seconds, and its own tier because nothing else in the product waits this long.
|
|
507
|
+
* `eksctl create cluster` is 15-25 minutes; the kube CREATE_TIMEOUT is 600 and would kill
|
|
508
|
+
* a create most of the way through building a VPC, which is the worst possible moment.
|
|
509
|
+
*/
|
|
510
|
+
exports.CLUSTER_CREATE_TIMEOUT = 3000;
|
|
511
|
+
/** `--wait` is not optional on delete, so this has to outlast a full teardown. */
|
|
512
|
+
exports.CLUSTER_DELETE_TIMEOUT = 2400;
|
|
513
|
+
/**
|
|
514
|
+
* Copied from Rocket.Chat's own Launchpad, which is the reference for what an EKS cluster
|
|
515
|
+
* running Rocket.Chat must PROVIDE.
|
|
516
|
+
*
|
|
517
|
+
* Managed node group, NOT EKS Auto Mode, and the reason is what houston is for. houston
|
|
518
|
+
* already runs Kubernetes on a local kind cluster -- free, about a minute. If a bug were
|
|
519
|
+
* application-level, that reproduces it. Spending twenty minutes and real money only
|
|
520
|
+
* makes sense when something ABOUT EKS matters to the ticket: an EBS volume that will not
|
|
521
|
+
* bind, an ALB behaving unlike an ingress, scheduling under node pressure. Auto Mode
|
|
522
|
+
* abstracts exactly those differently, so it would hide the class of difference this
|
|
523
|
+
* feature exists to expose.
|
|
524
|
+
*/
|
|
525
|
+
//
|
|
526
|
+
// The NEWEST version on EKS standard support, not Launchpad's 1.33: 1.33 left standard
|
|
527
|
+
// support on 2026-07-29, and from then its control plane bills $0.60 an hour instead of
|
|
528
|
+
// $0.10 -- about $12 a day for nothing a support ticket needs. `aws eks
|
|
529
|
+
// describe-cluster-versions` listed 1.37 as the newest standard-support version on
|
|
530
|
+
// 2026-10-06 (standard support to 2027-12-01). Move it when the next one is offered.
|
|
531
|
+
exports.K8S_VERSION = '1.37';
|
|
532
|
+
exports.NODE_INSTANCE_TYPE = 't3.large'; // Launchpad's stated minimum
|
|
533
|
+
// Moves with K8S_VERSION. Launchpad pins 1.47.0, which EKS offers only up to 1.36; for 1.37
|
|
534
|
+
// `aws eks describe-addon-versions` offered 1.55.0 and newer, with 1.66.0 the default.
|
|
535
|
+
exports.EBS_CSI_ADDON_VERSION = '1.66.0';
|
|
536
|
+
exports.DEFAULT_VPC_CIDR = '10.0.0.0/16';
|
|
537
|
+
/**
|
|
538
|
+
* Launchpad sizes for a PRODUCTION workspace: 5 nodes with its built-in MongoDB, 3 with
|
|
539
|
+
* an external one. A reproduction carries ONE workspace, and fidelity comes from the
|
|
540
|
+
* addons, the Kubernetes version and the storage driver rather than from how many nodes
|
|
541
|
+
* there are -- so this is the one place houston deliberately departs from the reference,
|
|
542
|
+
* and `--nodes` raises it when a ticket is about scheduling or capacity. Written down
|
|
543
|
+
* because an undocumented divergence is indistinguishable from a mistake.
|
|
544
|
+
*/
|
|
545
|
+
exports.DEFAULT_NODES = 3;
|
|
546
|
+
/** The most nodes houston will ask eksctl for; the cost estimate holds the same bound. */
|
|
547
|
+
exports.MAX_NODES = 100;
|
|
548
|
+
/**
|
|
549
|
+
* The date this cluster says it is wanted until, or '' for no expiry.
|
|
550
|
+
*
|
|
551
|
+
* `null` days means never, and that is a real answer rather than a missing one: a cluster
|
|
552
|
+
* somebody intends to keep should be able to say so, and a reminder nobody can turn off
|
|
553
|
+
* is a reminder everybody learns to ignore.
|
|
554
|
+
*/
|
|
555
|
+
function expiryDate(days, { today } = {}) {
|
|
556
|
+
if (days === null)
|
|
557
|
+
return '';
|
|
558
|
+
return new Date(localDay(today ?? new Date()) + Math.max(0, days) * 86400000)
|
|
559
|
+
.toISOString()
|
|
560
|
+
.slice(0, 10);
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Midnight UTC of the LOCAL calendar date of `base`: today's date on the machine's clock in
|
|
564
|
+
* its own timezone. An earlier version took the UTC date, so for the hours when the two
|
|
565
|
+
* differ -- 00:00-05:30 every day on an IST box -- `doctor` said a day more left than the
|
|
566
|
+
* established answer and `cluster create` recorded an expiry a day later.
|
|
567
|
+
*/
|
|
568
|
+
function localDay(base) {
|
|
569
|
+
return Date.UTC(base.getFullYear(), base.getMonth(), base.getDate());
|
|
570
|
+
}
|
|
571
|
+
/** Days left, negative once past, or null when there is no expiry to compare. */
|
|
572
|
+
function daysUntil(expires, { today } = {}) {
|
|
573
|
+
if (expires === '')
|
|
574
|
+
return null;
|
|
575
|
+
if (!/^\d{4}-\d{2}-\d{2}$/u.test(expires))
|
|
576
|
+
return null;
|
|
577
|
+
const when = Date.parse(`${expires}T00:00:00Z`);
|
|
578
|
+
if (Number.isNaN(when))
|
|
579
|
+
return null;
|
|
580
|
+
return Math.round((when - localDay(today ?? new Date())) / 86400000);
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* The eksctl ClusterConfig for a cluster houston would build.
|
|
584
|
+
*
|
|
585
|
+
* Pure: builds a document and touches nothing. That is deliberate and is what makes the
|
|
586
|
+
* expensive path testable -- every decision about the shape of a cluster can be asserted
|
|
587
|
+
* without an AWS account, leaving only the `eksctl` invocation itself unverifiable
|
|
588
|
+
* without one.
|
|
589
|
+
*
|
|
590
|
+
* **A NEW VPC BY DEFAULT, an existing one only when asked for by name.** Defaulting the
|
|
591
|
+
* other way is not available: there is no safe guess at which of an account's subnets a
|
|
592
|
+
* support workspace belongs in, and picking wrong puts a cluster somewhere it can be
|
|
593
|
+
* reached from, or cannot reach out from, in ways nobody asked for. New is the answer
|
|
594
|
+
* that is wrong in the recoverable direction -- `cluster delete` takes a VPC houston made
|
|
595
|
+
* back with it, and will not touch one it did not.
|
|
596
|
+
*/
|
|
597
|
+
function clusterConfig(name, { region: where, nodes = exports.DEFAULT_NODES, subnets, expires = '', zone = '', vpcCidr = exports.DEFAULT_VPC_CIDR, }) {
|
|
598
|
+
const doc = {
|
|
599
|
+
apiVersion: 'eksctl.io/v1alpha5',
|
|
600
|
+
kind: 'ClusterConfig',
|
|
601
|
+
// THE TAG IS SET AT CREATION, not afterwards. A separate tagging step has a window
|
|
602
|
+
// in which the cluster exists and is not yet provably houston's, and a crash inside
|
|
603
|
+
// that window leaves something billable that `cluster delete` will then correctly
|
|
604
|
+
// refuse to remove. eksctl applies `metadata.tags` to the cluster itself, so there
|
|
605
|
+
// is no window.
|
|
606
|
+
metadata: {
|
|
607
|
+
name,
|
|
608
|
+
region: where,
|
|
609
|
+
version: exports.K8S_VERSION,
|
|
610
|
+
tags: {
|
|
611
|
+
[exports.OWNED_TAG_KEY]: exports.OWNED_TAG_VALUE,
|
|
612
|
+
...(expires !== '' ? { [exports.EXPIRY_TAG_KEY]: expires } : {}),
|
|
613
|
+
...(zone !== '' ? { [exports.ZONE_TAG_KEY]: zone } : {}),
|
|
614
|
+
},
|
|
615
|
+
},
|
|
616
|
+
managedNodeGroups: [
|
|
617
|
+
{
|
|
618
|
+
name: `${name}-ng`,
|
|
619
|
+
instanceType: exports.NODE_INSTANCE_TYPE,
|
|
620
|
+
desiredCapacity: nodes,
|
|
621
|
+
privateNetworking: true,
|
|
622
|
+
iam: { withAddonPolicies: { ebs: true } },
|
|
623
|
+
},
|
|
624
|
+
],
|
|
625
|
+
addons: [{ name: 'aws-ebs-csi-driver', version: exports.EBS_CSI_ADDON_VERSION }],
|
|
626
|
+
// Every workspace PVC lands on EBS, so the driver needs to assume a role; without
|
|
627
|
+
// OIDC the addon installs and the volumes stay Pending, which is the single most
|
|
628
|
+
// confusing way for this to fail.
|
|
629
|
+
iam: { withOIDC: true },
|
|
630
|
+
};
|
|
631
|
+
if (subnets !== undefined && Object.keys(subnets).length > 0) {
|
|
632
|
+
// NAMED subnets, never discovered. houston is not going to guess which of an
|
|
633
|
+
// account's subnets it is welcome in, and a wrong guess is a cluster in the wrong
|
|
634
|
+
// place rather than an error.
|
|
635
|
+
doc.vpc = {
|
|
636
|
+
subnets: {
|
|
637
|
+
private: Object.fromEntries(Object.keys(subnets)
|
|
638
|
+
.sort()
|
|
639
|
+
.map((zone) => [zone, { id: subnets[zone] }])),
|
|
640
|
+
},
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
else {
|
|
644
|
+
doc.vpc = { cidr: vpcCidr };
|
|
645
|
+
}
|
|
646
|
+
return doc;
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Run a long command and report what it says WHILE it says it.
|
|
650
|
+
*
|
|
651
|
+
* `awsCli.run` captures output and returns it at the end, which is right for a probe that
|
|
652
|
+
* answers in a second and wrong for one that takes twenty minutes: `cluster create`
|
|
653
|
+
* printed one line and then went silent for the entire build, which is indistinguishable
|
|
654
|
+
* from a hang.
|
|
655
|
+
*
|
|
656
|
+
* eksctl prefixes its own progress with a level and a timestamp; the message is the part
|
|
657
|
+
* worth showing, and the rest is noise on a terminal that already has its own clock.
|
|
658
|
+
*/
|
|
659
|
+
/**
|
|
660
|
+
* How much of one eksctl line is shown. It was 200, and a refused build's only line,
|
|
661
|
+
* "checking AWS STS access – … StatusCode: 403, RequestID: …, api error ExpiredToken: …",
|
|
662
|
+
* lost exactly the part that said why. Bounded still, because a job keeps its events.
|
|
663
|
+
*/
|
|
664
|
+
const LINE_LIMIT = 1000;
|
|
665
|
+
async function streamTool(argv, { phase, emit, timeout, progress, }) {
|
|
666
|
+
const seen = [];
|
|
667
|
+
const env = (0, exec_1.userEnv)();
|
|
668
|
+
let best = null;
|
|
669
|
+
const res = await aws_cli_1.awsCli.stream(argv, {
|
|
670
|
+
timeoutMs: asMs(timeout),
|
|
671
|
+
...(env === undefined ? {} : { env }),
|
|
672
|
+
onLine: (raw) => {
|
|
673
|
+
const line = raw.replace(/\s+$/u, '');
|
|
674
|
+
if (line === '')
|
|
675
|
+
return;
|
|
676
|
+
seen.push(line);
|
|
677
|
+
const shown = line.startsWith('[') || line.startsWith('2')
|
|
678
|
+
? line.split(']').slice(1).join(']').trim() || line
|
|
679
|
+
: line;
|
|
680
|
+
if (progress !== undefined) {
|
|
681
|
+
// NEVER BACKWARDS: eksctl waits on the cluster stack again after the node group,
|
|
682
|
+
// and a bar that jumped from 70 back to 35 would read as a failure.
|
|
683
|
+
const read = progress(shown);
|
|
684
|
+
if (read !== null && (best === null || read > best))
|
|
685
|
+
best = read;
|
|
686
|
+
}
|
|
687
|
+
(0, events_1.info)(emit, shown.slice(0, LINE_LIMIT), best === null ? { phase } : { phase, pct: best });
|
|
688
|
+
},
|
|
689
|
+
});
|
|
690
|
+
if (res.returncode !== 0) {
|
|
691
|
+
// A launch failure has no output of its own, so the reason on stderr is the report.
|
|
692
|
+
if (seen.length === 0 && res.stderr !== '') {
|
|
693
|
+
(0, events_1.warn)(emit, `${String(argv[0])} could not be started: ${res.stderr}`, { phase });
|
|
694
|
+
}
|
|
695
|
+
for (const line of seen.slice(-6))
|
|
696
|
+
(0, events_1.warn)(emit, line.slice(0, LINE_LIMIT), { phase });
|
|
697
|
+
}
|
|
698
|
+
return res.returncode;
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Where an eksctl create is, as a percentage, from the steps it reports doing.
|
|
702
|
+
*
|
|
703
|
+
* eksctl's messages are the only progress a twenty-five-minute build offers. In the order a
|
|
704
|
+
* real build printed them (us-east-1, 2026-10-05): the cluster stack, the long wait for the
|
|
705
|
+
* control plane, the first add-ons, the node group stack, the node ready, the storage and
|
|
706
|
+
* metrics add-ons, and the cluster ready. The control plane wait is most of the time, so the
|
|
707
|
+
* bar sits at 10% through it. The last 10% is houston's own prepare.
|
|
708
|
+
*
|
|
709
|
+
* PLANS ARE NOT PROGRESS. eksctl opens with what it WILL do -- "will create a CloudFormation
|
|
710
|
+
* stack for cluster itself and 1 managed nodegroup stack(s)", "will install them as EKS
|
|
711
|
+
* addons" -- and a task tree naming every step ("create managed nodegroup", "wait for
|
|
712
|
+
* control plane to become ready", "1 task: { create addons }"). Matched as progress, those
|
|
713
|
+
* put the bar at 85% in the first minute, and it never moves backwards, so it stood there
|
|
714
|
+
* for the whole build.
|
|
715
|
+
*/
|
|
716
|
+
function eksctlProgress(line) {
|
|
717
|
+
const text = line.toLowerCase().trim();
|
|
718
|
+
if (/\bwill\b|sequential|\btasks?:|[{}]/u.test(text))
|
|
719
|
+
return null;
|
|
720
|
+
if (/region is ready/u.test(text))
|
|
721
|
+
return 90;
|
|
722
|
+
if (/aws-ebs-csi-driver|metrics-server/u.test(text) && /addon/u.test(text))
|
|
723
|
+
return 85;
|
|
724
|
+
if (/created \d+ managed nodegroup|node "[^"]*" is ready/u.test(text))
|
|
725
|
+
return 80;
|
|
726
|
+
if (/nodegroup stack|stack "eksctl-[^"]*-nodegroup-/u.test(text))
|
|
727
|
+
return 65;
|
|
728
|
+
if (/creating addon|addon "[^"]*" active/u.test(text))
|
|
729
|
+
return 55;
|
|
730
|
+
if (/waiting for cloudformation stack "eksctl-[^"]*-cluster"/u.test(text))
|
|
731
|
+
return 10;
|
|
732
|
+
if (/building cluster stack|deploying stack "eksctl-[^"]*-cluster"/u.test(text))
|
|
733
|
+
return 5;
|
|
734
|
+
return null;
|
|
735
|
+
}
|
|
736
|
+
/**
|
|
737
|
+
* What AWS knows about one cluster: PRESENT with `.doc`, ABSENT, or REFUSED.
|
|
738
|
+
*
|
|
739
|
+
* THREE answers, and this is the highest-stakes use of that contract in the product.
|
|
740
|
+
* Everything destructive below reads it, and the two negatives must not be collapsed:
|
|
741
|
+
* `ResourceNotFoundException` is AWS asserting the cluster is not there, which is safe to
|
|
742
|
+
* act on; anything else -- expired credentials, no route, a throttle, a permission
|
|
743
|
+
* boundary -- is "I could not ask", which is not.
|
|
744
|
+
*/
|
|
745
|
+
async function describeCluster(name, where) {
|
|
746
|
+
if ((0, tools_1.which)(exports.IDENTITY_TOOL) === '' || where === '')
|
|
747
|
+
return (0, ask_1.answer)(ask_1.REFUSED);
|
|
748
|
+
const res = await aws_cli_1.awsCli.run([
|
|
749
|
+
exports.IDENTITY_TOOL,
|
|
750
|
+
'eks',
|
|
751
|
+
'describe-cluster',
|
|
752
|
+
'--name',
|
|
753
|
+
name,
|
|
754
|
+
'--region',
|
|
755
|
+
where,
|
|
756
|
+
'--output',
|
|
757
|
+
'json',
|
|
758
|
+
], { timeoutMs: asMs(exports.IDENTITY_TIMEOUT) });
|
|
759
|
+
const shaped = {
|
|
760
|
+
argv: res.argv,
|
|
761
|
+
returncode: res.returncode,
|
|
762
|
+
stdout: res.stdout,
|
|
763
|
+
stderr: res.stderr,
|
|
764
|
+
};
|
|
765
|
+
if (res.returncode === 0) {
|
|
766
|
+
try {
|
|
767
|
+
const parsed = JSON.parse(res.stdout || '{}');
|
|
768
|
+
const cluster = parsed.cluster;
|
|
769
|
+
return (0, ask_1.answer)(ask_1.PRESENT, cluster !== null && typeof cluster === 'object' ? cluster : {});
|
|
770
|
+
}
|
|
771
|
+
catch {
|
|
772
|
+
return (0, ask_1.answer)(ask_1.REFUSED, null, shaped);
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
if (res.stderr.includes('ResourceNotFoundException'))
|
|
776
|
+
return (0, ask_1.answer)(ask_1.ABSENT, null, shaped);
|
|
777
|
+
return (0, ask_1.answer)(ask_1.REFUSED, null, shaped);
|
|
778
|
+
}
|
|
779
|
+
/**
|
|
780
|
+
* Whether a cluster by this name is there at all, tag or no tag.
|
|
781
|
+
*
|
|
782
|
+
* Separate from `ownsCluster` because they are different questions with different
|
|
783
|
+
* consequences. "Is it there" decides whether an interruption note is still worth
|
|
784
|
+
* keeping; "is it ours" decides whether it may be destroyed. Answering the second where
|
|
785
|
+
* the first was asked would clear the note about a half-built cluster that never got its
|
|
786
|
+
* tag on -- which is exactly the cluster the note exists for.
|
|
787
|
+
*/
|
|
788
|
+
async function clusterExists(name, where) {
|
|
789
|
+
return await exports.aws.describeCluster(name, where);
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Whether houston may DESTROY this cluster. Never a name comparison.
|
|
793
|
+
*
|
|
794
|
+
* ABSENT means "not ours, refuse". REFUSED means "could not read, ALSO refuse" -- the two
|
|
795
|
+
* behave the same way here and that is deliberate: the one thing that must never happen
|
|
796
|
+
* is a delete authorised by a read that failed. They are still distinguished in the
|
|
797
|
+
* return value, because the refusal has to say which it was.
|
|
798
|
+
*/
|
|
799
|
+
async function ownsCluster(name, where) {
|
|
800
|
+
const seen = await exports.aws.describeCluster(name, where);
|
|
801
|
+
if (seen.state !== ask_1.PRESENT)
|
|
802
|
+
return seen;
|
|
803
|
+
const tags = seen.doc.tags;
|
|
804
|
+
const table = tags !== null && typeof tags === 'object' ? tags : {};
|
|
805
|
+
if (String(table[exports.OWNED_TAG_KEY] ?? '') === exports.OWNED_TAG_VALUE)
|
|
806
|
+
return seen;
|
|
807
|
+
return (0, ask_1.answer)(ask_1.ABSENT, seen.doc);
|
|
808
|
+
}
|
|
809
|
+
/**
|
|
810
|
+
* Where to look when the account cannot be asked what it has enabled. A SHIPPED LIST
|
|
811
|
+
* rather than a probe, because the probe needs `ec2:DescribeRegions` and the whole point
|
|
812
|
+
* of this sweep is that it must work for the person who has least reason to have it --
|
|
813
|
+
* somebody checking what they are being charged for. Commercial regions only: China and
|
|
814
|
+
* GovCloud are separate partitions with their own credentials, and a sweep that pretended
|
|
815
|
+
* to cover them would report "nothing found" for an account it could never have reached.
|
|
816
|
+
*/
|
|
817
|
+
exports.DEFAULT_REGION_SWEEP = [
|
|
818
|
+
'us-east-1',
|
|
819
|
+
'us-east-2',
|
|
820
|
+
'us-west-1',
|
|
821
|
+
'us-west-2',
|
|
822
|
+
'ca-central-1',
|
|
823
|
+
'sa-east-1',
|
|
824
|
+
'eu-west-1',
|
|
825
|
+
'eu-west-2',
|
|
826
|
+
'eu-west-3',
|
|
827
|
+
'eu-central-1',
|
|
828
|
+
'eu-north-1',
|
|
829
|
+
'eu-south-1',
|
|
830
|
+
'ap-south-1',
|
|
831
|
+
'ap-southeast-1',
|
|
832
|
+
'ap-southeast-2',
|
|
833
|
+
'ap-northeast-1',
|
|
834
|
+
'ap-northeast-2',
|
|
835
|
+
'ap-northeast-3',
|
|
836
|
+
'ap-east-1',
|
|
837
|
+
'me-south-1',
|
|
838
|
+
'af-south-1',
|
|
839
|
+
];
|
|
840
|
+
/**
|
|
841
|
+
* Seconds for one region's `list-clusters`. Short: a sweep is many of these in a row and
|
|
842
|
+
* one unreachable region must not hold the whole report.
|
|
843
|
+
*/
|
|
844
|
+
exports.SWEEP_TIMEOUT = 20;
|
|
845
|
+
/** Regions asked at once. `ponytail:` six stays well under the AWS API's per-account rate; raise it if a sweep of 17 still drags. */
|
|
846
|
+
exports.SWEEP_WORKERS = 6;
|
|
847
|
+
/** The first useful line of a failed AWS call, for a report to carry. */
|
|
848
|
+
function oneLine(res) {
|
|
849
|
+
const text = (res.stderr || res.stdout).trim();
|
|
850
|
+
return text !== '' ? text.split('\n')[0].trim() : `exit ${String(res.returncode)}`;
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* `[regions to sweep, why the account could not be asked]`.
|
|
854
|
+
*
|
|
855
|
+
* Asks the account which regions it has enabled, because sweeping twenty-one when four
|
|
856
|
+
* are on is twenty-one round trips to answer a question about four. Falls back to the
|
|
857
|
+
* shipped list rather than refusing -- `ec2:DescribeRegions` is not in the minimal
|
|
858
|
+
* engineer policy, and the person most likely to be missing it is exactly the person who
|
|
859
|
+
* needs to know what is running.
|
|
860
|
+
*
|
|
861
|
+
* The second value is not decoration. A sweep that fell back covers the shipped list and
|
|
862
|
+
* NOT necessarily the account's actual regions, so a caller reporting "nothing found" has
|
|
863
|
+
* to be able to say which of the two questions it answered.
|
|
864
|
+
*/
|
|
865
|
+
async function enabledRegions() {
|
|
866
|
+
if ((0, tools_1.which)(exports.IDENTITY_TOOL) === '') {
|
|
867
|
+
return [[...exports.DEFAULT_REGION_SWEEP], `${exports.IDENTITY_TOOL} is not installed`];
|
|
868
|
+
}
|
|
869
|
+
const res = await aws_cli_1.awsCli.run([
|
|
870
|
+
exports.IDENTITY_TOOL,
|
|
871
|
+
'ec2',
|
|
872
|
+
'describe-regions',
|
|
873
|
+
'--query',
|
|
874
|
+
'Regions[].RegionName',
|
|
875
|
+
'--output',
|
|
876
|
+
'json',
|
|
877
|
+
], { timeoutMs: asMs(exports.IDENTITY_TIMEOUT) });
|
|
878
|
+
if (res.returncode !== 0)
|
|
879
|
+
return [[...exports.DEFAULT_REGION_SWEEP], oneLine(res)];
|
|
880
|
+
let names;
|
|
881
|
+
try {
|
|
882
|
+
const parsed = JSON.parse(res.stdout || '[]');
|
|
883
|
+
if (!Array.isArray(parsed))
|
|
884
|
+
throw new TypeError('not a list');
|
|
885
|
+
names = parsed.map((entry) => String(entry));
|
|
886
|
+
}
|
|
887
|
+
catch {
|
|
888
|
+
return [[...exports.DEFAULT_REGION_SWEEP], 'the region list could not be parsed'];
|
|
889
|
+
}
|
|
890
|
+
return names.length > 0
|
|
891
|
+
? [[...names].sort(), '']
|
|
892
|
+
: [[...exports.DEFAULT_REGION_SWEEP], 'the account reported no regions'];
|
|
893
|
+
}
|
|
894
|
+
/** One region's clusters, and what could not be reached in it. */
|
|
895
|
+
/** One region's answer: what it holds, and what in it could not be read. */
|
|
896
|
+
async function sweepOneRegion(where, ownedOnly) {
|
|
897
|
+
const clusters = [];
|
|
898
|
+
const unreadable = [];
|
|
899
|
+
const res = await aws_cli_1.awsCli.run([
|
|
900
|
+
exports.IDENTITY_TOOL,
|
|
901
|
+
'eks',
|
|
902
|
+
'list-clusters',
|
|
903
|
+
'--region',
|
|
904
|
+
where,
|
|
905
|
+
'--query',
|
|
906
|
+
'clusters',
|
|
907
|
+
'--output',
|
|
908
|
+
'json',
|
|
909
|
+
], { timeoutMs: asMs(exports.SWEEP_TIMEOUT) });
|
|
910
|
+
if (res.returncode !== 0) {
|
|
911
|
+
unreadable.push({ region: where, why: oneLine(res) });
|
|
912
|
+
return { clusters, unreadable };
|
|
913
|
+
}
|
|
914
|
+
let names;
|
|
915
|
+
try {
|
|
916
|
+
const parsed = JSON.parse(res.stdout || '[]');
|
|
917
|
+
if (!Array.isArray(parsed))
|
|
918
|
+
throw new TypeError('not a list');
|
|
919
|
+
names = parsed.map((entry) => String(entry));
|
|
920
|
+
}
|
|
921
|
+
catch {
|
|
922
|
+
unreadable.push({ region: where, why: 'the cluster list could not be parsed' });
|
|
923
|
+
return { clusters, unreadable };
|
|
924
|
+
}
|
|
925
|
+
for (const name of names) {
|
|
926
|
+
const seen = await exports.aws.describeCluster(name, where);
|
|
927
|
+
if (seen.state !== ask_1.PRESENT) {
|
|
928
|
+
// The cluster is THERE -- list-clusters just named it -- and could not be
|
|
929
|
+
// described. Reported as unreadable rather than dropped, because a cluster
|
|
930
|
+
// silently missing from this list is the exact failure the function exists to
|
|
931
|
+
// prevent.
|
|
932
|
+
unreadable.push({ region: where, why: `${name} is there and could not be described` });
|
|
933
|
+
continue;
|
|
934
|
+
}
|
|
935
|
+
const doc = seen.doc;
|
|
936
|
+
const rawTags = doc.tags;
|
|
937
|
+
const tags = rawTags !== null && typeof rawTags === 'object' ? rawTags : {};
|
|
938
|
+
const ours = tags[exports.OWNED_TAG_KEY] === exports.OWNED_TAG_VALUE;
|
|
939
|
+
if (ownedOnly && !ours)
|
|
940
|
+
continue;
|
|
941
|
+
clusters.push({
|
|
942
|
+
name,
|
|
943
|
+
region: where,
|
|
944
|
+
ours,
|
|
945
|
+
status: String(doc.status ?? ''),
|
|
946
|
+
arn: String(doc.arn ?? ''),
|
|
947
|
+
created: String(doc.createdAt ?? ''),
|
|
948
|
+
expires: String(tags[exports.EXPIRY_TAG_KEY] ?? ''),
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
return { clusters, unreadable };
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* Every region asked, six at a time, answers assembled in the order the regions were named.
|
|
955
|
+
*
|
|
956
|
+
* Seventeen regions one after another at up to twenty seconds each is why `cluster list`
|
|
957
|
+
* took minutes; the calls are independent reads. The ORDER of `clusters` and `unreadable`
|
|
958
|
+
* follows the argument order whatever finished first, because the CLI and the GUI print
|
|
959
|
+
* this document and a report must not vary from run to run.
|
|
960
|
+
*/
|
|
961
|
+
async function sweepTheRegions(asked, emit, out, ownedOnly) {
|
|
962
|
+
let done = 0;
|
|
963
|
+
const answers = await (0, map_limit_1.mapLimit)(asked, exports.SWEEP_WORKERS, async (where) => {
|
|
964
|
+
const answer = await sweepOneRegion(where, ownedOnly);
|
|
965
|
+
done += 1;
|
|
966
|
+
// `preflight` rather than a phase of its own: PHASES is a closed wire contract and a
|
|
967
|
+
// sweep of regions is a connectivity check, which is what that phase already names.
|
|
968
|
+
(0, events_1.info)(emit, `[${String(done)}/${String(asked.length)}] ${where}`, {
|
|
969
|
+
phase: 'preflight',
|
|
970
|
+
pct: Math.trunc((done * 100) / Math.max(asked.length, 1)),
|
|
971
|
+
});
|
|
972
|
+
return answer;
|
|
973
|
+
});
|
|
974
|
+
for (const answer of answers) {
|
|
975
|
+
out.clusters.push(...answer.clusters);
|
|
976
|
+
out.unreadable.push(...answer.unreadable);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Every cluster in the account, region by region, and what could not be read.
|
|
981
|
+
*
|
|
982
|
+
* THE COMMAND THAT ANSWERS "what am I paying for". Nothing else in houston can: `cluster
|
|
983
|
+
* status` needs a context you already have, and `doctor` reads a local pointer that a
|
|
984
|
+
* fresh HOUSTON_HOME has not got -- so a cluster in a region you have stopped thinking
|
|
985
|
+
* about is invisible to the whole tool. That is not hypothetical. A cluster built by
|
|
986
|
+
* houston sat in eu-west-1 for 16.8 hours and $2.47 while every check run that day looked
|
|
987
|
+
* only at us-east-1, and it was found by an unrelated `aws` call rather than by anything
|
|
988
|
+
* here.
|
|
989
|
+
*
|
|
990
|
+
* UNREADABLE IS NOT EMPTY, region by region. A region whose `list-clusters` failed is
|
|
991
|
+
* named in `unreadable` and contributes no clusters and no reassurance -- the one thing a
|
|
992
|
+
* report like this must never do is let "I could not ask fourteen regions" read as "you
|
|
993
|
+
* are not paying for anything".
|
|
994
|
+
*/
|
|
995
|
+
async function sweepClusters({ regions, ownedOnly = true, emit = events_1.nullEmit, } = {}) {
|
|
996
|
+
// ONE reason, not twenty-one copies of it. Without `aws` every region fails the same
|
|
997
|
+
// way, and a report whose "could not be read" list is the region list with a single
|
|
998
|
+
// cause repeated buries the one sentence that matters.
|
|
999
|
+
if ((0, tools_1.which)(exports.IDENTITY_TOOL) === '') {
|
|
1000
|
+
return {
|
|
1001
|
+
clusters: [],
|
|
1002
|
+
regions: [],
|
|
1003
|
+
owned_only: ownedOnly,
|
|
1004
|
+
fallback: '',
|
|
1005
|
+
unreadable: [
|
|
1006
|
+
{ region: '(all)', why: `${exports.IDENTITY_TOOL} is not installed, so no region could be asked` },
|
|
1007
|
+
],
|
|
1008
|
+
};
|
|
1009
|
+
}
|
|
1010
|
+
const [asked, fallbackWhy] = regions !== undefined && regions.length > 0 ? [regions, ''] : await enabledRegions();
|
|
1011
|
+
const out = {
|
|
1012
|
+
clusters: [],
|
|
1013
|
+
unreadable: [],
|
|
1014
|
+
regions: [...asked],
|
|
1015
|
+
fallback: fallbackWhy,
|
|
1016
|
+
owned_only: ownedOnly,
|
|
1017
|
+
};
|
|
1018
|
+
await sweepTheRegions(asked, emit, out, ownedOnly);
|
|
1019
|
+
return out;
|
|
1020
|
+
}
|
|
1021
|
+
/**
|
|
1022
|
+
* Where everything houston knows about one cluster lives, on this box.
|
|
1023
|
+
*
|
|
1024
|
+
* A DIRECTORY rather than a single file, because the pointer is not the only thing worth
|
|
1025
|
+
* keeping: the document eksctl was actually handed and the manifests applied afterwards
|
|
1026
|
+
* are what answer "what is this cluster, and why is it shaped like that" six weeks later,
|
|
1027
|
+
* when the cluster is the only evidence and nobody remembers which flags produced it.
|
|
1028
|
+
*/
|
|
1029
|
+
function clusterDir(name) {
|
|
1030
|
+
// `clusters/<name>` under the home, and every ledger path joined component by component,
|
|
1031
|
+
// a `..` kept (`joinPath`): `path.join` folded a `..` in the home that the kernel
|
|
1032
|
+
// resolves through a symlink.
|
|
1033
|
+
return (0, home_1.joinPath)(config.home(), 'clusters', name);
|
|
1034
|
+
}
|
|
1035
|
+
function pointerPath(name) {
|
|
1036
|
+
return (0, home_1.joinPath)(clusterDir(name), 'cluster.json');
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Write down something houston applied to a cluster. Best effort, never fatal.
|
|
1040
|
+
*
|
|
1041
|
+
* Kept on this box rather than in the cluster, and kept even when the thing it describes
|
|
1042
|
+
* failed -- a create that died half way is exactly when somebody needs to read what was
|
|
1043
|
+
* submitted, and a manifest that only survives success is a manifest that is never there
|
|
1044
|
+
* when it is wanted.
|
|
1045
|
+
*/
|
|
1046
|
+
async function keepManifest(name, filename, body) {
|
|
1047
|
+
try {
|
|
1048
|
+
const directory = clusterDir(name);
|
|
1049
|
+
// Made with its parents: 0700 on the cluster's directory alone, and the umask's mode
|
|
1050
|
+
// on `clusters/` when it is made here.
|
|
1051
|
+
(0, mkdir_parents_1.mkdirParents)(directory, 0o700);
|
|
1052
|
+
await (0, paths_1.atomicWrite)((0, home_1.joinPath)(directory, filename), body);
|
|
1053
|
+
return (0, home_1.joinPath)(directory, filename);
|
|
1054
|
+
}
|
|
1055
|
+
catch {
|
|
1056
|
+
return null;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* Note locally that this box built a cluster. Best effort, never load-bearing.
|
|
1061
|
+
*
|
|
1062
|
+
* The TAG authorises deletion; this only lets `doctor` say "you own a cluster that is
|
|
1063
|
+
* costing money" without an AWS round trip. Losing it costs a reminder, not control --
|
|
1064
|
+
* which is what makes a `mktemp -d` home survivable.
|
|
1065
|
+
*/
|
|
1066
|
+
async function remember(name, { region: where, account, expires = '', zone = '', }) {
|
|
1067
|
+
try {
|
|
1068
|
+
const target = pointerPath(name);
|
|
1069
|
+
(0, mkdir_parents_1.mkdirParents)(path.dirname(target), 0o700); // the leaf only
|
|
1070
|
+
await (0, paths_1.atomicWrite)(target, `${JSON.stringify({
|
|
1071
|
+
name,
|
|
1072
|
+
region: where,
|
|
1073
|
+
account,
|
|
1074
|
+
provider: 'eks',
|
|
1075
|
+
expires,
|
|
1076
|
+
zone,
|
|
1077
|
+
created_at: `${new Date().toISOString().slice(0, 19)}Z`,
|
|
1078
|
+
}, null, 2)}`);
|
|
1079
|
+
}
|
|
1080
|
+
catch {
|
|
1081
|
+
// A pointer is a convenience; the tag is the authority.
|
|
1082
|
+
}
|
|
1083
|
+
}
|
|
1084
|
+
/** The region an EKS ARN names, or '' for any other context. */
|
|
1085
|
+
function regionOfContext(context) {
|
|
1086
|
+
return context.startsWith('arn:aws:eks:') ? (context.split(':')[3] ?? '') : '';
|
|
1087
|
+
}
|
|
1088
|
+
/** The DNS zone the build note holds for a cluster, or '' when this box has none. */
|
|
1089
|
+
function zoneOf(name) {
|
|
1090
|
+
const record = remembered().find((entry) => entry.name === name);
|
|
1091
|
+
const zone = record?.zone;
|
|
1092
|
+
return typeof zone === 'string' ? zone : '';
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Set or change a cluster's zone after the build: in the note here, and as a tag on the
|
|
1096
|
+
* cluster so every other machine reads the same answer. Best effort on the tag -- the
|
|
1097
|
+
* note is written first, so a refused tag still leaves this box able to default a name.
|
|
1098
|
+
*/
|
|
1099
|
+
async function rememberZone(name, region, zone) {
|
|
1100
|
+
const record = remembered().find((entry) => entry.name === name);
|
|
1101
|
+
await remember(name, {
|
|
1102
|
+
region: typeof record?.region === 'string' && record.region !== '' ? record.region : region,
|
|
1103
|
+
account: typeof record?.account === 'string' ? record.account : '',
|
|
1104
|
+
expires: typeof record?.expires === 'string' ? record.expires : '',
|
|
1105
|
+
zone,
|
|
1106
|
+
});
|
|
1107
|
+
const seen = await exports.aws.describeCluster(name, region);
|
|
1108
|
+
if (seen.state !== ask_1.PRESENT)
|
|
1109
|
+
return;
|
|
1110
|
+
const arn = String(seen.doc.arn ?? '');
|
|
1111
|
+
if (arn === '')
|
|
1112
|
+
return;
|
|
1113
|
+
await aws_cli_1.awsCli.run([
|
|
1114
|
+
exports.IDENTITY_TOOL,
|
|
1115
|
+
'eks',
|
|
1116
|
+
'tag-resource',
|
|
1117
|
+
'--resource-arn',
|
|
1118
|
+
arn,
|
|
1119
|
+
'--tags',
|
|
1120
|
+
`${exports.ZONE_TAG_KEY}=${zone}`,
|
|
1121
|
+
], { timeoutMs: asMs(exports.IDENTITY_TIMEOUT) });
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Drop the local POINTER after a delete AWS confirmed, and keep the manifests.
|
|
1125
|
+
*
|
|
1126
|
+
* The pointer is what `doctor` warns from, so it has to go or the warning outlives the
|
|
1127
|
+
* cluster. The documents do not: what was built, and how, is worth more after the fact
|
|
1128
|
+
* than during. They are small, they are text, and nothing acts on them.
|
|
1129
|
+
*/
|
|
1130
|
+
function forget(name) {
|
|
1131
|
+
for (const target of [
|
|
1132
|
+
pointerPath(name),
|
|
1133
|
+
// The older flat layout, for a cluster created before manifests were kept.
|
|
1134
|
+
(0, home_1.joinPath)(config.home(), 'clusters', `${name}.json`),
|
|
1135
|
+
// AND THE KUBECONFIG, which is not part of the record. The manifests stay because
|
|
1136
|
+
// what was built is worth reading afterwards; a kubeconfig is the opposite -- it is
|
|
1137
|
+
// the only file here that stops being TRUE the moment the cluster goes, and the only
|
|
1138
|
+
// one somebody might try to USE.
|
|
1139
|
+
(0, home_1.joinPath)(clusterDir(name), 'kubeconfig'),
|
|
1140
|
+
]) {
|
|
1141
|
+
try {
|
|
1142
|
+
fs.rmSync(target, { force: true });
|
|
1143
|
+
}
|
|
1144
|
+
catch {
|
|
1145
|
+
// Best effort: a missing file, or one that cannot be removed, is not a failure.
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
/**
|
|
1150
|
+
* Every cluster this box thinks it built. May be stale; never authoritative.
|
|
1151
|
+
*
|
|
1152
|
+
* Both layouts are read: `clusters/<name>/cluster.json` is where a pointer goes now, and
|
|
1153
|
+
* `clusters/<name>.json` is where the first version put it. A box that created a cluster
|
|
1154
|
+
* before that change must not have it vanish from `doctor`'s warning -- the whole point
|
|
1155
|
+
* of that warning is a cluster nobody is thinking about any more.
|
|
1156
|
+
*/
|
|
1157
|
+
/**
|
|
1158
|
+
* The region a cluster was built in, from its local note, or '' when this box has none.
|
|
1159
|
+
*
|
|
1160
|
+
* `cluster delete` without `--region` looked in the AWS CLI's default region, found nothing,
|
|
1161
|
+
* printed "nothing to delete" and left the cluster billing where `cluster create` had put
|
|
1162
|
+
* it. The note written at build time already held the answer.
|
|
1163
|
+
*/
|
|
1164
|
+
function rememberedRegion(name) {
|
|
1165
|
+
const record = remembered().find((entry) => entry.name === name);
|
|
1166
|
+
const region = record?.region;
|
|
1167
|
+
return typeof region === 'string' ? region : '';
|
|
1168
|
+
}
|
|
1169
|
+
function remembered() {
|
|
1170
|
+
const out = [];
|
|
1171
|
+
const root = (0, home_1.joinPath)(config.home(), 'clusters');
|
|
1172
|
+
let entries;
|
|
1173
|
+
try {
|
|
1174
|
+
if (!fs.statSync(root).isDirectory())
|
|
1175
|
+
return out;
|
|
1176
|
+
entries = fs.readdirSync(root, { withFileTypes: true });
|
|
1177
|
+
}
|
|
1178
|
+
catch {
|
|
1179
|
+
return out;
|
|
1180
|
+
}
|
|
1181
|
+
const found = [];
|
|
1182
|
+
for (const entry of entries) {
|
|
1183
|
+
if (entry.isDirectory())
|
|
1184
|
+
found.push((0, home_1.joinPath)(root, entry.name, 'cluster.json'));
|
|
1185
|
+
else if (entry.isFile() && entry.name.endsWith('.json'))
|
|
1186
|
+
found.push((0, home_1.joinPath)(root, entry.name));
|
|
1187
|
+
}
|
|
1188
|
+
for (const target of found.sort()) {
|
|
1189
|
+
try {
|
|
1190
|
+
const parsed = JSON.parse(fs.readFileSync(target, 'utf8'));
|
|
1191
|
+
if (parsed !== null && typeof parsed === 'object')
|
|
1192
|
+
out.push(parsed);
|
|
1193
|
+
}
|
|
1194
|
+
catch {
|
|
1195
|
+
continue;
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
return out;
|
|
1199
|
+
}
|
|
1200
|
+
/** Every reason not to spend twenty minutes and real money on this one. */
|
|
1201
|
+
async function refuseACreateWeShouldNotMake(name, where) {
|
|
1202
|
+
const pre = await exports.aws.preflight();
|
|
1203
|
+
const resolved = where !== '' ? where : pre.region;
|
|
1204
|
+
const stopping = blockers(pre, { toBuild: true });
|
|
1205
|
+
if (stopping.length > 0) {
|
|
1206
|
+
throw new errors_1.PreflightError(`cannot build an EKS cluster from here: ${stopping.join('; ')}`);
|
|
1207
|
+
}
|
|
1208
|
+
const approved = config.approvedAwsAccounts();
|
|
1209
|
+
if (!approved.has(pre.identity.account)) {
|
|
1210
|
+
// A GATE, not a confirmation. `--force` cannot reach this and must not: force is a
|
|
1211
|
+
// reflex people develop, and this is a decision about somebody's bill.
|
|
1212
|
+
throw new errors_1.AuthorityGateError(`building an EKS cluster in account ${pre.identity.account} would cost money, and ` +
|
|
1213
|
+
'this box has not been told it may spend in that account. You are ' +
|
|
1214
|
+
`${identityPretty(pre.identity)}.`, {
|
|
1215
|
+
kind: 'cluster',
|
|
1216
|
+
subject: `${name} (${resolved})`,
|
|
1217
|
+
approveWith: 'houston config set cluster.approved_accounts ' +
|
|
1218
|
+
[...new Set([...approved, pre.identity.account])].sort().join(','),
|
|
1219
|
+
code: 'GATE_CLOUD_SPEND',
|
|
1220
|
+
});
|
|
1221
|
+
}
|
|
1222
|
+
const already = await exports.aws.describeCluster(name, resolved);
|
|
1223
|
+
if (already.state === ask_1.PRESENT) {
|
|
1224
|
+
throw new errors_1.ConflictError(`a cluster named '${name}' already exists in ${resolved} (account ` +
|
|
1225
|
+
`${pre.identity.account}). Use it with \`--context\`, or pick another name.`);
|
|
1226
|
+
}
|
|
1227
|
+
if (already.state === ask_1.REFUSED) {
|
|
1228
|
+
throw new errors_1.PreflightError(`could not check whether '${name}' already exists in ${resolved}: ` +
|
|
1229
|
+
`${already.res !== undefined ? (0, ask_1.why)(already.res, { limit: 200 }) : 'no answer'}. ` +
|
|
1230
|
+
'Refusing rather than risking a second cluster.');
|
|
1231
|
+
}
|
|
1232
|
+
return [pre, resolved];
|
|
1233
|
+
}
|
|
1234
|
+
/**
|
|
1235
|
+
* Where the user's bare `kubectl` pointed before a build, put back: a context, or none.
|
|
1236
|
+
*
|
|
1237
|
+
* houston never moves it. eksctl writes its own context and makes it current, and `aws eks
|
|
1238
|
+
* update-kubeconfig` does the same, so a person whose `kubectl` pointed at production
|
|
1239
|
+
* found it pointing at the new, billing cluster. Read AFTER eksctl, "the previous context"
|
|
1240
|
+
* was eksctl's own, and that is what got restored (measured on a real build, 2026-10-05).
|
|
1241
|
+
*/
|
|
1242
|
+
async function currentKubectlContext() {
|
|
1243
|
+
const pointed = await exec_1.kube.run(['kubectl', 'config', 'current-context']);
|
|
1244
|
+
return pointed.returncode === 0 ? pointed.stdout.trim() : '';
|
|
1245
|
+
}
|
|
1246
|
+
async function pointKubectlBack(previous) {
|
|
1247
|
+
if ((await currentKubectlContext()) === previous)
|
|
1248
|
+
return;
|
|
1249
|
+
await exec_1.kube.run(previous === ''
|
|
1250
|
+
? ['kubectl', 'config', 'unset', 'current-context']
|
|
1251
|
+
: ['kubectl', 'config', 'use-context', previous]);
|
|
1252
|
+
}
|
|
1253
|
+
/** Point kubectl at what was just built, and make it able to serve a workspace. */
|
|
1254
|
+
async function adoptTheNewCluster(emit, name, where, previous) {
|
|
1255
|
+
const described = await exports.aws.describeCluster(name, where);
|
|
1256
|
+
const context = String(described.doc?.arn ?? '');
|
|
1257
|
+
// PREPARE IS PART OF CREATE, because a cluster that cannot run a workspace is not a
|
|
1258
|
+
// finished answer to "create me a cluster". MEASURED: EKS ships no default
|
|
1259
|
+
// StorageClass, so without this the very next command refuses and the operator is left
|
|
1260
|
+
// patching a class by hand on a cluster they just paid twenty minutes for.
|
|
1261
|
+
let prepared = {};
|
|
1262
|
+
if (context === '')
|
|
1263
|
+
return [context, prepared];
|
|
1264
|
+
// `aws eks update-kubeconfig` switches the merged kubeconfig's current context to the new
|
|
1265
|
+
// cluster; `previous` was read before eksctl ran, and is put back below.
|
|
1266
|
+
await aws_cli_1.awsCli.run(['aws', 'eks', 'update-kubeconfig', '--name', name, '--region', where], {
|
|
1267
|
+
timeoutMs: asMs(exports.IDENTITY_TIMEOUT),
|
|
1268
|
+
});
|
|
1269
|
+
await pointKubectlBack(previous);
|
|
1270
|
+
// AND A COPY BESIDE THE MANIFESTS. The merged kubeconfig above is the one kubectl uses
|
|
1271
|
+
// by default, and it is also the one that gets tidied, moved between machines, or
|
|
1272
|
+
// simply not there on somebody else's laptop -- at which point the only way back to
|
|
1273
|
+
// this cluster is knowing to run `aws eks update-kubeconfig`, which is exactly the
|
|
1274
|
+
// knowledge a person new to EKS does not have.
|
|
1275
|
+
//
|
|
1276
|
+
// It holds NO credential. The file's user runs `aws eks get-token`, so it is useless
|
|
1277
|
+
// without the ambient AWS credentials -- which is what makes writing it down safe.
|
|
1278
|
+
const kubePath = (0, home_1.joinPath)(clusterDir(name), 'kubeconfig');
|
|
1279
|
+
try {
|
|
1280
|
+
(0, mkdir_parents_1.mkdirParents)(clusterDir(name), 0o700); // the leaf only
|
|
1281
|
+
const wrote = await aws_cli_1.awsCli.run([
|
|
1282
|
+
'aws',
|
|
1283
|
+
'eks',
|
|
1284
|
+
'update-kubeconfig',
|
|
1285
|
+
'--name',
|
|
1286
|
+
name,
|
|
1287
|
+
'--region',
|
|
1288
|
+
where,
|
|
1289
|
+
'--kubeconfig',
|
|
1290
|
+
kubePath,
|
|
1291
|
+
], { timeoutMs: asMs(exports.IDENTITY_TIMEOUT) });
|
|
1292
|
+
if (wrote.returncode === 0) {
|
|
1293
|
+
fs.chmodSync(kubePath, 0o600);
|
|
1294
|
+
(0, events_1.info)(emit, `kubeconfig kept at ${kubePath} — \`KUBECONFIG=${kubePath} kubectl get nodes\``, {
|
|
1295
|
+
phase: 'provision',
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
catch {
|
|
1300
|
+
// A kept copy is a convenience; never fail a create for it.
|
|
1301
|
+
}
|
|
1302
|
+
// AND IT BECOMES THIS BOX'S DEFAULT. houston has just built this cluster and knows its
|
|
1303
|
+
// ARN; making the operator read it back out of `aws` and paste it into every later
|
|
1304
|
+
// command is ceremony houston is imposing on itself. An ARN is also the worst possible
|
|
1305
|
+
// thing to ask somebody to retype.
|
|
1306
|
+
//
|
|
1307
|
+
// Announced rather than done quietly, because it changes where `up --runtime kubernetes`
|
|
1308
|
+
// lands, and a default that moved without saying so is worse than one you have to set.
|
|
1309
|
+
// It does NOT move a plain `up` off Docker (lifecycle/request.ts inferRuntime), and the
|
|
1310
|
+
// earlier wording said it did; people then ran `up` and got a Compose workspace.
|
|
1311
|
+
await (0, locks_1.updateConfig)((cfg) => {
|
|
1312
|
+
cfg[config.KUBE_CONTEXT_KEY] = context;
|
|
1313
|
+
});
|
|
1314
|
+
(0, events_1.info)(emit, "set as this box's default cluster — `houston up --runtime kubernetes` lands here " +
|
|
1315
|
+
'without --context; a plain `up` still makes a Docker workspace (change with ' +
|
|
1316
|
+
'`houston config set kubernetes.context <name>`)', { phase: 'provision' });
|
|
1317
|
+
try {
|
|
1318
|
+
prepared = await exports.aws.prepareCluster(context, { emit });
|
|
1319
|
+
}
|
|
1320
|
+
catch (error) {
|
|
1321
|
+
// The CLUSTER exists; say so and stop.
|
|
1322
|
+
(0, events_1.warn)(emit, `the cluster is up but could not be prepared: ${String(error.message)}. Run ` +
|
|
1323
|
+
`\`houston cluster prepare --context ${context}\` once it is reachable — until ` +
|
|
1324
|
+
'then `up` will refuse on storage.', { phase: 'provision' });
|
|
1325
|
+
}
|
|
1326
|
+
return [context, prepared];
|
|
1327
|
+
}
|
|
1328
|
+
/** The twenty minutes of eksctl, journalled so a kill does not leave it unowned. */
|
|
1329
|
+
async function buildTheCluster(doc, emit, expires, name, pre, where, zone) {
|
|
1330
|
+
// THE NOTE GOES DOWN BEFORE THE CALL, and is only cleared once eksctl has returned. A
|
|
1331
|
+
// note written afterwards describes nothing: the whole case it exists for is the
|
|
1332
|
+
// process not getting there.
|
|
1333
|
+
const entry = (0, journal_1.recordJournal)(journal_1.CLUSTER_UNFINISHED, name, {
|
|
1334
|
+
region: where,
|
|
1335
|
+
account: pre.identity.account,
|
|
1336
|
+
});
|
|
1337
|
+
// WRITTEN BEFORE THE CALL, and eksctl is handed the kept file rather than a copy in a
|
|
1338
|
+
// temporary directory: the document on disk is then provably the one that was
|
|
1339
|
+
// submitted, and it survives a create that fails -- which is when somebody most wants
|
|
1340
|
+
// to read it.
|
|
1341
|
+
// `safeDump(doc)`, keys in document order: the kept file is one the user is told to
|
|
1342
|
+
// read, and the `yaml` package double-quoted what the YAML 1.1 emitter single-quotes
|
|
1343
|
+
// (`version: "1.33"` against `'1.33'`, the expiry tag) -- the same value in different
|
|
1344
|
+
// bytes.
|
|
1345
|
+
const body = (0, yaml_dump_1.safeDump)(doc);
|
|
1346
|
+
let kept = await keepManifest(name, 'cluster.yaml', body);
|
|
1347
|
+
let temporary = '';
|
|
1348
|
+
if (kept === null) {
|
|
1349
|
+
// A fresh temporary directory, under the one `tempRoot()` settles on.
|
|
1350
|
+
temporary = (0, tempdir_1.makeTempDir)();
|
|
1351
|
+
kept = path.join(temporary, 'cluster.yaml');
|
|
1352
|
+
fs.writeFileSync(kept, body, 'utf8');
|
|
1353
|
+
}
|
|
1354
|
+
else {
|
|
1355
|
+
(0, events_1.info)(emit, `config kept at ${kept}`, { phase: 'provision' });
|
|
1356
|
+
}
|
|
1357
|
+
let code;
|
|
1358
|
+
try {
|
|
1359
|
+
code = await exports.aws.streamTool(['eksctl', 'create', 'cluster', '-f', kept], {
|
|
1360
|
+
phase: 'provision',
|
|
1361
|
+
progress: eksctlProgress,
|
|
1362
|
+
emit,
|
|
1363
|
+
timeout: exports.CLUSTER_CREATE_TIMEOUT,
|
|
1364
|
+
});
|
|
1365
|
+
}
|
|
1366
|
+
finally {
|
|
1367
|
+
if (temporary !== '')
|
|
1368
|
+
fs.rmSync(temporary, { recursive: true, force: true });
|
|
1369
|
+
}
|
|
1370
|
+
if (code !== 0) {
|
|
1371
|
+
// ASKED, NOT ASSUMED. A build refused at eksctl's first step -- checking who it is --
|
|
1372
|
+
// makes nothing, and "a partial create can still be billing" sent its owner looking
|
|
1373
|
+
// for a bill that did not exist. Only AWS answering "none" to every question says so.
|
|
1374
|
+
if (await nothingWasMade(name, where)) {
|
|
1375
|
+
(0, journal_1.clearJournal)(entry);
|
|
1376
|
+
throw new errors_1.CreateFailedError(`eksctl could not build '${name}' in ${where} (exit ${String(code)}); its last lines ` +
|
|
1377
|
+
'are above. AWS holds nothing of it -- no cluster, stack, volume, load balancer, ' +
|
|
1378
|
+
'NAT gateway or address -- so nothing is billing and there is nothing to delete.');
|
|
1379
|
+
}
|
|
1380
|
+
// The note STAYS. eksctl may have built a VPC, a NAT gateway and a control plane
|
|
1381
|
+
// before failing, and houston is not going to decide on its own to destroy them.
|
|
1382
|
+
throw new errors_1.CreateFailedError(`eksctl could not build '${name}' in ${where} (exit ${String(code)}); its last lines ` +
|
|
1383
|
+
'are above. A note has been kept: `houston doctor` and `houston cluster overview` ' +
|
|
1384
|
+
'report it, because a partial create can still be billing. Stop the bill with ' +
|
|
1385
|
+
`\`houston cluster delete ${name} --region ${where}\`; if that says there is no ` +
|
|
1386
|
+
`cluster, \`eksctl delete cluster --name ${name} --region ${where} --wait\` removes ` +
|
|
1387
|
+
'the stack it left.');
|
|
1388
|
+
}
|
|
1389
|
+
(0, journal_1.clearJournal)(entry);
|
|
1390
|
+
await remember(name, { region: where, account: pre.identity.account, expires, zone });
|
|
1391
|
+
}
|
|
1392
|
+
/** Whether AWS confirms a failed build left nothing. A read that failed is not "nothing". */
|
|
1393
|
+
async function nothingWasMade(name, where) {
|
|
1394
|
+
const [cluster, left] = await Promise.all([
|
|
1395
|
+
clusterExists(name, where),
|
|
1396
|
+
sweepLeftovers(name, where),
|
|
1397
|
+
]);
|
|
1398
|
+
return cluster.state === ask_1.ABSENT && left.found.length === 0 && left.unreadable.length === 0;
|
|
1399
|
+
}
|
|
1400
|
+
/**
|
|
1401
|
+
* Build an EKS cluster. Twenty minutes and real money, both on purpose.
|
|
1402
|
+
*
|
|
1403
|
+
* Never reachable from `up`. This is its own verb at its own lifecycle precisely so that
|
|
1404
|
+
* a routine per-ticket command cannot arrive here -- `planCluster` adopts an explicitly
|
|
1405
|
+
* named context and never provisions, which is what makes that structural rather than a
|
|
1406
|
+
* matter of care.
|
|
1407
|
+
*/
|
|
1408
|
+
async function createCluster(name, { region: where = '', nodes = exports.DEFAULT_NODES, subnets, expiresDays = exports.DEFAULT_EXPIRY_DAYS, zone = '', emit = events_1.nullEmit, } = {}) {
|
|
1409
|
+
// Before AWS is asked anything: `0` became 3 at both front-ends, and -1 or 250 went to
|
|
1410
|
+
// eksctl as the managed group's desiredCapacity.
|
|
1411
|
+
if (!Number.isInteger(nodes) || nodes < 1 || nodes > exports.MAX_NODES) {
|
|
1412
|
+
throw new errors_1.ValidationError(`a cluster is built with from 1 to ${String(exports.MAX_NODES)} nodes, not ${String(nodes)}`);
|
|
1413
|
+
}
|
|
1414
|
+
const [pre, resolved] = await refuseACreateWeShouldNotMake(name, where);
|
|
1415
|
+
const expires = expiryDate(expiresDays);
|
|
1416
|
+
const doc = clusterConfig(name, {
|
|
1417
|
+
region: resolved,
|
|
1418
|
+
nodes,
|
|
1419
|
+
zone,
|
|
1420
|
+
...(subnets === undefined ? {} : { subnets }),
|
|
1421
|
+
expires,
|
|
1422
|
+
});
|
|
1423
|
+
(0, events_1.info)(emit, `building EKS cluster '${name}' in ${resolved} — this takes 15–25 minutes and is ` +
|
|
1424
|
+
'billable' +
|
|
1425
|
+
(expires !== '' ? `, and is tagged as wanted until ${expires}` : ', with NO expiry set'), { phase: 'provision' });
|
|
1426
|
+
// Read BEFORE eksctl runs, which moves it; put back on a failed build too.
|
|
1427
|
+
const previous = await currentKubectlContext();
|
|
1428
|
+
try {
|
|
1429
|
+
await buildTheCluster(doc, emit, expires, name, pre, resolved, zone);
|
|
1430
|
+
}
|
|
1431
|
+
catch (error) {
|
|
1432
|
+
await pointKubectlBack(previous);
|
|
1433
|
+
throw error;
|
|
1434
|
+
}
|
|
1435
|
+
(0, events_1.info)(emit, `cluster '${name}' is up in ${resolved}`, { phase: 'provision', pct: 100 });
|
|
1436
|
+
const [context, prepared] = await adoptTheNewCluster(emit, name, resolved, previous);
|
|
1437
|
+
return {
|
|
1438
|
+
name,
|
|
1439
|
+
region: resolved,
|
|
1440
|
+
account: pre.identity.account,
|
|
1441
|
+
nodes,
|
|
1442
|
+
context,
|
|
1443
|
+
expires,
|
|
1444
|
+
prepared,
|
|
1445
|
+
};
|
|
1446
|
+
}
|
|
1447
|
+
/** What the cluster still holds, said out loud before it is destroyed. */
|
|
1448
|
+
async function confirmDeletion(emit, force, mine, where) {
|
|
1449
|
+
// GUARDS 1-3: what is INSIDE it. The context an EKS kubeconfig uses is the cluster ARN,
|
|
1450
|
+
// which is what `aws eks update-kubeconfig` writes.
|
|
1451
|
+
const context = String(mine.doc?.arn ?? '');
|
|
1452
|
+
if (force) {
|
|
1453
|
+
(0, events_1.warn)(emit, `--force: deleting ${where} without reading what it holds`, { phase: 'teardown' });
|
|
1454
|
+
return context;
|
|
1455
|
+
}
|
|
1456
|
+
const got = context !== '' ? await (0, reachability_1.reach)(context) : null;
|
|
1457
|
+
if (got === null || !(0, reachability_1.reachOk)(got)) {
|
|
1458
|
+
// GUARD 1. A cluster that does not answer is where houston knows LEAST about what it
|
|
1459
|
+
// is about to destroy -- so the refusal stands whatever the cause. What it now says
|
|
1460
|
+
// is WHICH cause: this refusal fired on an ACTIVE cluster whose only problem was
|
|
1461
|
+
// that a fresh HOUSTON_HOME had no kubeconfig entry for it, and "is not answering"
|
|
1462
|
+
// sent the reader to look at a cluster that was in perfect health.
|
|
1463
|
+
const reason = got !== null
|
|
1464
|
+
? (0, reachability_1.reachWhy)(got)
|
|
1465
|
+
: 'houston has no API endpoint for it, so there is nothing to ask';
|
|
1466
|
+
throw new errors_1.PreflightError(`what ${where} is holding cannot be read: ${reason} — and a cluster that cannot be ` +
|
|
1467
|
+
'read is not an empty one. Check it, or pass --force to delete it without ' +
|
|
1468
|
+
'looking.' +
|
|
1469
|
+
(got !== null ? (0, reachability_1.reachAdvice)(got, context) : ''));
|
|
1470
|
+
}
|
|
1471
|
+
const holding = await (0, capabilities_1.workspaceNamespaces)(context);
|
|
1472
|
+
if (holding === null) {
|
|
1473
|
+
// GUARD 2. An unreadable list is not an empty one.
|
|
1474
|
+
throw new errors_1.PreflightError(`the workspace list in ${where} could not be read, which is not the same as it ` +
|
|
1475
|
+
'being empty. Refusing.');
|
|
1476
|
+
}
|
|
1477
|
+
if (holding.length > 0) {
|
|
1478
|
+
// GUARD 3, and on a shared cloud cluster it changes character: the owners may be
|
|
1479
|
+
// colleagues on OTHER boxes whose HOUSTON_HOME this machine has never seen, so this
|
|
1480
|
+
// refusal is the only record that they exist.
|
|
1481
|
+
const owners = new Set();
|
|
1482
|
+
for (const ns of holding) {
|
|
1483
|
+
owners.add((await (0, namespace_1.namespaceOwner)(ns, { context })) || 'unknown');
|
|
1484
|
+
}
|
|
1485
|
+
throw new errors_1.ConflictError(`${where} still holds ${String(holding.length)} workspace(s), owned by ` +
|
|
1486
|
+
`${[...owners].sort().join(', ')}. Deleting the cluster takes them with it, and ` +
|
|
1487
|
+
'on a shared cluster their owners are not necessarily on this machine. Remove ' +
|
|
1488
|
+
'them first, or pass --force.');
|
|
1489
|
+
}
|
|
1490
|
+
return context;
|
|
1491
|
+
}
|
|
1492
|
+
/** Drop every kubeconfig entry that pointed at the cluster that is now gone. */
|
|
1493
|
+
async function forgetTheContexts(context, emit, name, where) {
|
|
1494
|
+
// AND THE KUBECONFIG ENTRY GOES WITH IT. Two tools wrote one -- `eksctl` and `aws eks
|
|
1495
|
+
// update-kubeconfig` -- and neither removes it, so a deleted cluster kept appearing on
|
|
1496
|
+
// the ledger for ever. Filtering it out of every report afterwards works and costs a
|
|
1497
|
+
// probe and an AWS call on every single list, which is how a "cheap" list became two
|
|
1498
|
+
// seconds. Removing it once, here, is where it belongs: houston wrote the entry and is
|
|
1499
|
+
// deleting the cluster.
|
|
1500
|
+
//
|
|
1501
|
+
// Only houston's OWN kubeconfig. The user's is theirs. Both spellings, whatever they
|
|
1502
|
+
// are called: the ARN this cluster answered to, and eksctl's
|
|
1503
|
+
// `<iam-user>@<cluster>.<region>.eksctl.io`, whose user part varies.
|
|
1504
|
+
const suffix = `@${name}.${where}.eksctl.io`;
|
|
1505
|
+
const [known] = await (0, context_1.contexts)();
|
|
1506
|
+
// An ARN with the same final name may belong to another account or region. The
|
|
1507
|
+
// eksctl spelling omits the account entirely, so leave it if another account's
|
|
1508
|
+
// ARN for this name and region is present rather than guessing its owner.
|
|
1509
|
+
const ambiguousAlias = known.some((entry) => entry !== context &&
|
|
1510
|
+
entry.startsWith('arn:aws:eks:') &&
|
|
1511
|
+
entry.split(':')[3] === where &&
|
|
1512
|
+
entry.endsWith(`:cluster/${name}`));
|
|
1513
|
+
const remove = new Set(known.filter((entry) => entry === context || (!ambiguousAlias && entry.endsWith(suffix))));
|
|
1514
|
+
for (const alias of remove) {
|
|
1515
|
+
await (0, context_1.forgetContext)(alias);
|
|
1516
|
+
}
|
|
1517
|
+
// AND THE DEFAULT GOES WITH IT. A `kubernetes.context` pointing at a cluster that no
|
|
1518
|
+
// longer exists makes the next `up` fail on a context that cannot be found, which reads
|
|
1519
|
+
// as a bug in `up` rather than as the consequence of a delete.
|
|
1520
|
+
if (config.defaultKubeContext() === context || remove.has(config.defaultKubeContext())) {
|
|
1521
|
+
await (0, locks_1.updateConfig)((cfg) => {
|
|
1522
|
+
cfg[config.KUBE_CONTEXT_KEY] = '';
|
|
1523
|
+
});
|
|
1524
|
+
(0, events_1.info)(emit, "cleared this box's default cluster — it was the one just deleted", {
|
|
1525
|
+
phase: 'teardown',
|
|
1526
|
+
});
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
/**
|
|
1530
|
+
* Destroy an EKS cluster houston built. Returns false if there was nothing to do.
|
|
1531
|
+
*
|
|
1532
|
+
* Every guard the local `deleteCluster` earned through a measured incident is reproduced
|
|
1533
|
+
* here, plus two the cloud needs. They are not defensive padding: each one is a way this
|
|
1534
|
+
* has already gone wrong once on a local cluster, where the cost was a container rather
|
|
1535
|
+
* than somebody else's afternoon and a VPC.
|
|
1536
|
+
*/
|
|
1537
|
+
async function deleteCluster(name, { region: where = '', force = false, expectedArn = '', emit = events_1.nullEmit, } = {}) {
|
|
1538
|
+
const pre = await exports.aws.preflight();
|
|
1539
|
+
// Typed region, else the region the build note remembers, else the AWS CLI's default.
|
|
1540
|
+
const resolved = where !== '' ? where : rememberedRegion(name) || pre.region;
|
|
1541
|
+
const stopping = blockers(pre);
|
|
1542
|
+
if (stopping.length > 0) {
|
|
1543
|
+
throw new errors_1.PreflightError(`cannot delete an EKS cluster from here: ${stopping.join('; ')}`);
|
|
1544
|
+
}
|
|
1545
|
+
// A delete without --force reads what the cluster holds, with kubectl. Missing, that read
|
|
1546
|
+
// failed as "nothing answered: it may be stopped..." about a cluster in perfect health.
|
|
1547
|
+
const kubectl = pre.tools.kubectl;
|
|
1548
|
+
if (!force && kubectl !== undefined && !(0, tools_1.toolPresent)(kubectl)) {
|
|
1549
|
+
throw new errors_1.PreflightError(`kubectl is not installed, so houston cannot read what '${name}' holds before ` +
|
|
1550
|
+
'deleting it. Install it (`houston doctor` names what is missing), or pass --force ' +
|
|
1551
|
+
'to delete it without looking.');
|
|
1552
|
+
}
|
|
1553
|
+
// A browser selection contains the full ARN. Refuse if the AWS identity changed
|
|
1554
|
+
// after review; a same-name cluster in another account can carry our ownership tag.
|
|
1555
|
+
const expected = expectedArn !== ''
|
|
1556
|
+
? expectedArn.match(/^arn:[^:]+:eks:([^:]+):(\d{12}):cluster\/([^/]+)$/u)
|
|
1557
|
+
: null;
|
|
1558
|
+
if (expectedArn !== '' &&
|
|
1559
|
+
(expected === null || expected[1] !== resolved || expected[3] !== name)) {
|
|
1560
|
+
throw new errors_1.ValidationError('the selected EKS cluster ARN does not match its name and region');
|
|
1561
|
+
}
|
|
1562
|
+
const saved = remembered().filter((entry) => entry.name === name && entry.region === resolved);
|
|
1563
|
+
const expectedAccount = expected?.[2] ??
|
|
1564
|
+
(saved.length === 1 && typeof saved[0]?.account === 'string'
|
|
1565
|
+
? saved[0].account
|
|
1566
|
+
: '');
|
|
1567
|
+
if (expectedAccount !== '' && expectedAccount !== pre.identity.account) {
|
|
1568
|
+
throw new errors_1.AuthorityGateError(`cannot delete '${name}' in ${resolved}: selected account ${expectedAccount} differs from ` +
|
|
1569
|
+
`the current AWS account ${pre.identity.account}. Reconnect to the selected account and re-check.`, {
|
|
1570
|
+
kind: 'cluster',
|
|
1571
|
+
subject: name,
|
|
1572
|
+
approveWith: 'use the selected AWS account',
|
|
1573
|
+
code: 'GATE_WRONG_ACCOUNT',
|
|
1574
|
+
});
|
|
1575
|
+
}
|
|
1576
|
+
const place = `'${name}' in ${resolved} (account ${pre.identity.account})`;
|
|
1577
|
+
// GUARD 5 and 6: the tag says ours, a refused read is NOT a yes, and the refusal names
|
|
1578
|
+
// the account and region -- a cluster called `support` exists in more than one account
|
|
1579
|
+
// and acting on the wrong one is the whole failure mode.
|
|
1580
|
+
const mine = await exports.aws.ownsCluster(name, resolved);
|
|
1581
|
+
if (mine.state === ask_1.REFUSED) {
|
|
1582
|
+
throw new errors_1.PreflightError(`could not read the tags on ${place}, so houston cannot prove it built it — and a ` +
|
|
1583
|
+
'delete authorised by a read that failed is the one thing that must not happen ' +
|
|
1584
|
+
`here. ${mine.res !== undefined ? (0, ask_1.why)(mine.res, { limit: 200 }) : ''}`.trim());
|
|
1585
|
+
}
|
|
1586
|
+
if (mine.state === ask_1.ABSENT) {
|
|
1587
|
+
const exists = await clusterExists(name, resolved);
|
|
1588
|
+
if (exists.state === ask_1.ABSENT)
|
|
1589
|
+
return false;
|
|
1590
|
+
throw new errors_1.AuthorityGateError(`${place} was not created by houston — it carries no ${exports.OWNED_TAG_KEY}=` +
|
|
1591
|
+
`${exports.OWNED_TAG_VALUE} tag. houston deletes only clusters it built; this one ` +
|
|
1592
|
+
'belongs to whoever made it.', {
|
|
1593
|
+
kind: 'cluster',
|
|
1594
|
+
subject: place,
|
|
1595
|
+
approveWith: `eksctl delete cluster --name ${name} --region ${resolved} --wait`,
|
|
1596
|
+
code: 'GATE_DELETE_UNOWNED',
|
|
1597
|
+
});
|
|
1598
|
+
}
|
|
1599
|
+
if (expectedArn !== '' && String(mine.doc.arn ?? '') !== expectedArn) {
|
|
1600
|
+
throw new errors_1.AuthorityGateError(`cannot delete '${name}': AWS returned ${String(mine.doc.arn ?? 'no ARN')} instead of ${expectedArn}`, {
|
|
1601
|
+
kind: 'cluster',
|
|
1602
|
+
subject: name,
|
|
1603
|
+
approveWith: 're-check the selected cluster',
|
|
1604
|
+
code: 'GATE_WRONG_CLUSTER',
|
|
1605
|
+
});
|
|
1606
|
+
}
|
|
1607
|
+
const context = await confirmDeletion(emit, force, mine, place);
|
|
1608
|
+
await releaseLoadBalancers(context, emit);
|
|
1609
|
+
(0, events_1.info)(emit, `deleting ${place} — this takes 10–20 minutes`, { phase: 'teardown' });
|
|
1610
|
+
// `--wait` is NOT optional. Without it eksctl returns as soon as the stack delete is
|
|
1611
|
+
// accepted, and the caller gets a success for a teardown that may still fail halfway
|
|
1612
|
+
// and leave the expensive parts behind.
|
|
1613
|
+
const code = await exports.aws.streamTool(['eksctl', 'delete', 'cluster', '--name', name, '--region', resolved, '--wait'], { phase: 'teardown', emit, timeout: exports.CLUSTER_DELETE_TIMEOUT });
|
|
1614
|
+
if (code !== 0) {
|
|
1615
|
+
throw new errors_1.DockerError(`eksctl could not delete ${place} (exit ${String(code)}); its last lines are above. ` +
|
|
1616
|
+
'If it says the cluster does not exist, its CloudFormation stack may have ' +
|
|
1617
|
+
'survived a rollback — check `aws cloudformation describe-stacks --stack-name ' +
|
|
1618
|
+
`eksctl-${name}-cluster --region ${resolved}\`.`);
|
|
1619
|
+
}
|
|
1620
|
+
forget(name);
|
|
1621
|
+
await forgetTheContexts(context, emit, name, resolved);
|
|
1622
|
+
return true;
|
|
1623
|
+
}
|
|
1624
|
+
/**
|
|
1625
|
+
* Every LoadBalancer Service gone before eksctl is asked to delete the VPC.
|
|
1626
|
+
*
|
|
1627
|
+
* A LoadBalancer Service holds network interfaces inside the cluster's VPC, and while it
|
|
1628
|
+
* exists the VPC delete hangs. The guide had the reader run a raw `kubectl delete svc -A`
|
|
1629
|
+
* against the pinned kubeconfig first -- step 4 of the teardown, the step most likely to
|
|
1630
|
+
* be skipped. houston installed that Service (Traefik's), so houston removes it, and waits
|
|
1631
|
+
* for AWS to let go of the load balancer before it goes on. A context that cannot be
|
|
1632
|
+
* reached skips this: the guards above have already decided a forced delete may proceed.
|
|
1633
|
+
*/
|
|
1634
|
+
async function releaseLoadBalancers(context, emit) {
|
|
1635
|
+
if (context === '')
|
|
1636
|
+
return;
|
|
1637
|
+
const res = await exec_1.kube.run([
|
|
1638
|
+
'kubectl',
|
|
1639
|
+
'--context',
|
|
1640
|
+
context,
|
|
1641
|
+
'delete',
|
|
1642
|
+
'svc',
|
|
1643
|
+
'-A',
|
|
1644
|
+
'--field-selector',
|
|
1645
|
+
'spec.type=LoadBalancer',
|
|
1646
|
+
'--ignore-not-found',
|
|
1647
|
+
], { timeout: APPLY_TIMEOUT_S });
|
|
1648
|
+
// A FAILED DELETE IS NOT YET A FAILURE. `kubectl delete` waits on each Service's
|
|
1649
|
+
// load-balancer finalizer, which AWS clears only once the load balancer is gone, so a
|
|
1650
|
+
// timeout is usually a delete that went through and is still letting go. It warned
|
|
1651
|
+
// "could not be removed" about Services that were gone, and skipped the wait that would
|
|
1652
|
+
// have said so. The read below decides, either way.
|
|
1653
|
+
if (res.returncode !== 0) {
|
|
1654
|
+
(0, events_1.info)(emit, `the load balancer Services were asked to go (${oneLine(res)}); checking whether they did`, { phase: 'teardown' });
|
|
1655
|
+
}
|
|
1656
|
+
else {
|
|
1657
|
+
(0, events_1.info)(emit, 'load balancer released — waiting for AWS to let go of it', { phase: 'teardown' });
|
|
1658
|
+
}
|
|
1659
|
+
let refused = false;
|
|
1660
|
+
for (let attempt = 0; attempt < endpoints_1.LOADBALANCER_TRIES; attempt += 1) {
|
|
1661
|
+
const read = await (0, endpoints_1.loadbalancerRead)(context);
|
|
1662
|
+
refused = read.refused;
|
|
1663
|
+
// "Could not ask" is not "gone": read as nothing left, a refused read sent the VPC to
|
|
1664
|
+
// eksctl with the load balancer still in it.
|
|
1665
|
+
if (!read.refused && read.name === '' && read.address === '' && read.pending === 0)
|
|
1666
|
+
return;
|
|
1667
|
+
await new Promise((resolve) => setTimeout(resolve, endpoints_1.LOADBALANCER_INTERVAL * 1000));
|
|
1668
|
+
}
|
|
1669
|
+
(0, events_1.warn)(emit, refused
|
|
1670
|
+
? 'the cluster could not be read to confirm its load balancer is gone; eksctl may hang on the VPC'
|
|
1671
|
+
: 'a load balancer is still there after waiting; eksctl may hang on the VPC', { phase: 'teardown' });
|
|
1672
|
+
}
|
|
1673
|
+
/**
|
|
1674
|
+
* Approve an AWS account for spending on this box, and optionally record the Let's
|
|
1675
|
+
* Encrypt contact: the same two keys `houston config set` writes, for the GUI's build
|
|
1676
|
+
* flow. Through the locked writer, like every config write, so a `serve` and a CLI run
|
|
1677
|
+
* at the same moment cannot lose each other's change. The caller decides who may.
|
|
1678
|
+
*/
|
|
1679
|
+
async function approveAccount(account, { acmeEmail = '' } = {}) {
|
|
1680
|
+
if (!/^[0-9]{12}$/u.test(account)) {
|
|
1681
|
+
throw new errors_1.ValidationError('`account` wants the 12-digit AWS account id');
|
|
1682
|
+
}
|
|
1683
|
+
if (acmeEmail !== '' && !acmeEmail.includes('@')) {
|
|
1684
|
+
throw new errors_1.ValidationError('`acme_email` wants an email address, or nothing');
|
|
1685
|
+
}
|
|
1686
|
+
let approved = [];
|
|
1687
|
+
const cfg = await (0, locks_1.updateConfig)((current) => {
|
|
1688
|
+
const known = new Set(String(current[config.CLOUD_ACCOUNTS_KEY] ?? '')
|
|
1689
|
+
.split(',')
|
|
1690
|
+
.map((part) => part.trim())
|
|
1691
|
+
.filter(Boolean));
|
|
1692
|
+
known.add(account);
|
|
1693
|
+
approved = [...known].sort();
|
|
1694
|
+
current[config.CLOUD_ACCOUNTS_KEY] = approved.join(',');
|
|
1695
|
+
if (acmeEmail !== '')
|
|
1696
|
+
current.acme_email = acmeEmail;
|
|
1697
|
+
});
|
|
1698
|
+
return { account, approved, acme_email: String(cfg.acme_email ?? '') };
|
|
1699
|
+
}
|
|
1700
|
+
/**
|
|
1701
|
+
* Move a cluster's reminder date, `days` from today: the tag on the cluster and the note.
|
|
1702
|
+
*
|
|
1703
|
+
* The date is a reminder and nothing deletes on it, but extending it took `aws eks
|
|
1704
|
+
* tag-resource` by hand, and until then doctor and the overview nagged about a cluster
|
|
1705
|
+
* still in use. Only a cluster houston built: the same tag that authorises a delete.
|
|
1706
|
+
*/
|
|
1707
|
+
async function keepCluster(name, { days, region: where = '' }) {
|
|
1708
|
+
const pre = await exports.aws.preflight();
|
|
1709
|
+
const region = where !== '' ? where : rememberedRegion(name) || pre.region;
|
|
1710
|
+
const seen = await exports.aws.describeCluster(name, region);
|
|
1711
|
+
if (seen.state === ask_1.ABSENT) {
|
|
1712
|
+
throw new errors_1.NotFoundError(`no EKS cluster named '${name}' in ${region} — \`houston cluster list\` finds it in whichever region it is`);
|
|
1713
|
+
}
|
|
1714
|
+
if (seen.state !== ask_1.PRESENT) {
|
|
1715
|
+
throw new errors_1.PreflightError(`'${name}' in ${region} could not be read, so its tags cannot be changed`);
|
|
1716
|
+
}
|
|
1717
|
+
const tags = (seen.doc.tags ?? {});
|
|
1718
|
+
if (tags[exports.OWNED_TAG_KEY] !== exports.OWNED_TAG_VALUE) {
|
|
1719
|
+
throw new errors_1.AuthorityGateError(`'${name}' in ${region} was not created by houston — it carries no ${exports.OWNED_TAG_KEY}=` +
|
|
1720
|
+
`${exports.OWNED_TAG_VALUE} tag, and houston changes only the clusters it built.`, {
|
|
1721
|
+
kind: 'cluster',
|
|
1722
|
+
subject: `${name} (${region})`,
|
|
1723
|
+
approveWith: `aws eks tag-resource --resource-arn <arn> --tags ${exports.EXPIRY_TAG_KEY}=<date>`,
|
|
1724
|
+
code: 'GATE_KEEP_UNOWNED',
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
const expires = expiryDate(days);
|
|
1728
|
+
const arn = String(seen.doc.arn ?? '');
|
|
1729
|
+
const res = await aws_cli_1.awsCli.run([
|
|
1730
|
+
exports.IDENTITY_TOOL,
|
|
1731
|
+
'eks',
|
|
1732
|
+
'tag-resource',
|
|
1733
|
+
'--resource-arn',
|
|
1734
|
+
arn,
|
|
1735
|
+
'--tags',
|
|
1736
|
+
`${exports.EXPIRY_TAG_KEY}=${expires}`,
|
|
1737
|
+
], { timeoutMs: asMs(exports.IDENTITY_TIMEOUT) });
|
|
1738
|
+
if (res.returncode !== 0)
|
|
1739
|
+
throw new errors_1.PreflightError(`the expiry tag could not be written: ${oneLine(res)}`);
|
|
1740
|
+
const record = remembered().find((entry) => entry.name === name);
|
|
1741
|
+
await remember(name, {
|
|
1742
|
+
region,
|
|
1743
|
+
account: typeof record?.account === 'string' ? record.account : pre.identity.account,
|
|
1744
|
+
expires,
|
|
1745
|
+
zone: typeof record?.zone === 'string' ? record.zone : '',
|
|
1746
|
+
});
|
|
1747
|
+
return { name, region, expires, days };
|
|
1748
|
+
}
|
|
1749
|
+
/** The cluster-scoped policy an access entry needs for Helm and namespaces. */
|
|
1750
|
+
const EKS_ADMIN_POLICY_ARN = 'arn:aws:eks::aws:cluster-access-policy/AmazonEKSAdminPolicy';
|
|
1751
|
+
/** The Kubernetes group the shipped RBAC document binds. */
|
|
1752
|
+
const ENGINEERS_GROUP = 'houston-engineers';
|
|
1753
|
+
/**
|
|
1754
|
+
* Let a colleague run workspaces on a cluster: the EKS access entry, the cluster-scoped
|
|
1755
|
+
* AWS policy, and the in-cluster RBAC the shipped document grants, in that order.
|
|
1756
|
+
*
|
|
1757
|
+
* The guide's colleague-access section: three aws commands and a kubectl apply, by hand,
|
|
1758
|
+
* and "the first alone is not enough" -- with only the IAM half every kubectl says
|
|
1759
|
+
* Unauthorized. An entry that already exists is not a failure: the policy and the RBAC
|
|
1760
|
+
* are applied again, which is what makes a second run a repair rather than an error.
|
|
1761
|
+
*/
|
|
1762
|
+
async function shareCluster(name, { user, region: where = '' }) {
|
|
1763
|
+
const pre = await exports.aws.preflight();
|
|
1764
|
+
const region = where !== '' ? where : rememberedRegion(name) || pre.region;
|
|
1765
|
+
const seen = await exports.aws.describeCluster(name, region);
|
|
1766
|
+
if (seen.state === ask_1.ABSENT) {
|
|
1767
|
+
throw new errors_1.NotFoundError(`no EKS cluster named '${name}' in ${region} — \`houston cluster list\` finds it in whichever region it is`);
|
|
1768
|
+
}
|
|
1769
|
+
if (seen.state !== ask_1.PRESENT) {
|
|
1770
|
+
throw new errors_1.PreflightError(`'${name}' in ${region} could not be read, so nothing can be granted on it`);
|
|
1771
|
+
}
|
|
1772
|
+
// THE TAG IS THE GATE, as it is for keep and delete: this grants cluster admin, and the
|
|
1773
|
+
// operator policy may create access entries on any cluster in the account.
|
|
1774
|
+
const tags = (seen.doc.tags ?? {});
|
|
1775
|
+
if (tags[exports.OWNED_TAG_KEY] !== exports.OWNED_TAG_VALUE) {
|
|
1776
|
+
throw new errors_1.AuthorityGateError(`'${name}' in ${region} was not created by houston — it carries no ${exports.OWNED_TAG_KEY}=` +
|
|
1777
|
+
`${exports.OWNED_TAG_VALUE} tag, and houston grants access only to the clusters it built.`, {
|
|
1778
|
+
kind: 'cluster',
|
|
1779
|
+
subject: `${name} (${region})`,
|
|
1780
|
+
approveWith: `aws eks create-access-entry --cluster-name ${name} --region ${region} --principal-arn <arn>`,
|
|
1781
|
+
code: 'GATE_SHARE_UNOWNED',
|
|
1782
|
+
});
|
|
1783
|
+
}
|
|
1784
|
+
const arn = String(seen.doc.arn ?? '');
|
|
1785
|
+
const principal = user.includes(':') ? user : `arn:aws:iam::${pre.identity.account}:user/${user}`;
|
|
1786
|
+
const entry = await aws_cli_1.awsCli.run([
|
|
1787
|
+
exports.IDENTITY_TOOL,
|
|
1788
|
+
'eks',
|
|
1789
|
+
'create-access-entry',
|
|
1790
|
+
'--cluster-name',
|
|
1791
|
+
name,
|
|
1792
|
+
'--region',
|
|
1793
|
+
region,
|
|
1794
|
+
'--principal-arn',
|
|
1795
|
+
principal,
|
|
1796
|
+
'--type',
|
|
1797
|
+
'STANDARD',
|
|
1798
|
+
'--kubernetes-groups',
|
|
1799
|
+
ENGINEERS_GROUP,
|
|
1800
|
+
], { timeoutMs: asMs(exports.IDENTITY_TIMEOUT) });
|
|
1801
|
+
const already = entry.returncode !== 0 && entry.stderr.includes('ResourceInUseException');
|
|
1802
|
+
if (entry.returncode !== 0 && !already) {
|
|
1803
|
+
throw new errors_1.PreflightError(`the access entry for ${principal} could not be created: ${oneLine(entry)}`);
|
|
1804
|
+
}
|
|
1805
|
+
const policy = await aws_cli_1.awsCli.run([
|
|
1806
|
+
exports.IDENTITY_TOOL,
|
|
1807
|
+
'eks',
|
|
1808
|
+
'associate-access-policy',
|
|
1809
|
+
'--cluster-name',
|
|
1810
|
+
name,
|
|
1811
|
+
'--region',
|
|
1812
|
+
region,
|
|
1813
|
+
'--principal-arn',
|
|
1814
|
+
principal,
|
|
1815
|
+
'--policy-arn',
|
|
1816
|
+
EKS_ADMIN_POLICY_ARN,
|
|
1817
|
+
'--access-scope',
|
|
1818
|
+
'type=cluster',
|
|
1819
|
+
], { timeoutMs: asMs(exports.IDENTITY_TIMEOUT) });
|
|
1820
|
+
if (policy.returncode !== 0 && !policy.stderr.includes('ResourceInUseException')) {
|
|
1821
|
+
throw new errors_1.PreflightError(`the cluster policy could not be associated: ${oneLine(policy)}`);
|
|
1822
|
+
}
|
|
1823
|
+
const rbac = await exec_1.kube.run(['kubectl', '--context', arn, 'apply', '-f', '-'], {
|
|
1824
|
+
input: (0, eks_docs_2.iamDocument)('rbac'),
|
|
1825
|
+
timeout: APPLY_TIMEOUT_S,
|
|
1826
|
+
});
|
|
1827
|
+
if (rbac.returncode !== 0) {
|
|
1828
|
+
// HALF A GRANT is still a grant: the access entry and its AmazonEKSAdminPolicy are in
|
|
1829
|
+
// place. Say so, and how to take them back, rather than leave a silent admin behind.
|
|
1830
|
+
throw new errors_1.PreflightError(`the in-cluster RBAC could not be applied (${oneLine(rbac)}). ${principal} already has ` +
|
|
1831
|
+
`an access entry on '${name}' with AmazonEKSAdminPolicy; run this again once kubectl ` +
|
|
1832
|
+
`reaches the cluster, or take it back with \`aws eks delete-access-entry --cluster-name ` +
|
|
1833
|
+
`${name} --region ${region} --principal-arn ${principal}\`.`);
|
|
1834
|
+
}
|
|
1835
|
+
return {
|
|
1836
|
+
name,
|
|
1837
|
+
region,
|
|
1838
|
+
user,
|
|
1839
|
+
principal,
|
|
1840
|
+
already,
|
|
1841
|
+
lines: [
|
|
1842
|
+
`aws eks update-kubeconfig --name ${name} --region ${region}`,
|
|
1843
|
+
`houston up --version <version> --name <ticket> --context ${name}`,
|
|
1844
|
+
],
|
|
1845
|
+
};
|
|
1846
|
+
}
|
|
1847
|
+
/**
|
|
1848
|
+
* What outlives a deleted cluster and keeps billing, read from AWS with the operator
|
|
1849
|
+
* policy's own rights. "The command returned" is not "nothing is billing": a volume left
|
|
1850
|
+
* by a workspace deleted with the cluster, a load balancer, a NAT gateway, an Elastic IP,
|
|
1851
|
+
* a CloudFormation stack that rolled back half way.
|
|
1852
|
+
*
|
|
1853
|
+
* ONLY THIS CLUSTER'S. A region holds colleagues' clusters too, and a region-wide list of
|
|
1854
|
+
* load balancers and NAT gateways, each with its delete command, hands out commands that
|
|
1855
|
+
* break someone else's running cluster. So each resource is tied to `name` by a tag:
|
|
1856
|
+
* CloudFormation's `aws:cloudformation:stack-name` on the network eksctl built (the NAT
|
|
1857
|
+
* gateway, the Elastic IP), `kubernetes.io/cluster/<name>` on what Kubernetes created
|
|
1858
|
+
* (the load balancer, and a volume when the EBS driver tags it). A Rocket.Chat volume
|
|
1859
|
+
* that carries no cluster tag at all is shown as a candidate WITHOUT a command, and one
|
|
1860
|
+
* tagged for another cluster is not shown.
|
|
1861
|
+
*/
|
|
1862
|
+
async function sweepLeftovers(name, region) {
|
|
1863
|
+
const out = { found: [], unreadable: [] };
|
|
1864
|
+
const ownTag = `kubernetes.io/cluster/${name}`;
|
|
1865
|
+
const stackFilter = `Name=tag:aws:cloudformation:stack-name,Values=eksctl-${name}-cluster`;
|
|
1866
|
+
const ask = async (kind, args) => {
|
|
1867
|
+
const res = await aws_cli_1.awsCli.run([exports.IDENTITY_TOOL, ...args, '--region', region, '--output', 'json'], {
|
|
1868
|
+
timeoutMs: asMs(exports.SWEEP_TIMEOUT),
|
|
1869
|
+
});
|
|
1870
|
+
if (res.returncode !== 0) {
|
|
1871
|
+
out.unreadable.push({ kind, why: oneLine(res) });
|
|
1872
|
+
return null;
|
|
1873
|
+
}
|
|
1874
|
+
try {
|
|
1875
|
+
return JSON.parse(res.stdout || 'null');
|
|
1876
|
+
}
|
|
1877
|
+
catch {
|
|
1878
|
+
out.unreadable.push({ kind, why: 'the answer could not be parsed' });
|
|
1879
|
+
return null;
|
|
1880
|
+
}
|
|
1881
|
+
};
|
|
1882
|
+
const rows = (value) => Array.isArray(value)
|
|
1883
|
+
? value
|
|
1884
|
+
.filter(Array.isArray)
|
|
1885
|
+
.map((row) => row.map((cell) => (cell === null || cell === undefined ? '' : String(cell))))
|
|
1886
|
+
: [];
|
|
1887
|
+
const taggedOwn = (value, idKey) => {
|
|
1888
|
+
const owned = new Set();
|
|
1889
|
+
const doc = (value ?? {});
|
|
1890
|
+
for (const entry of doc.TagDescriptions ?? []) {
|
|
1891
|
+
const tags = entry.Tags ?? [];
|
|
1892
|
+
const mine = tags.some((tag) => tag.Key === ownTag || (tag.Key === 'elbv2.k8s.aws/cluster' && tag.Value === name));
|
|
1893
|
+
const id = entry[idKey];
|
|
1894
|
+
if (mine && typeof id === 'string')
|
|
1895
|
+
owned.add(id);
|
|
1896
|
+
}
|
|
1897
|
+
return owned;
|
|
1898
|
+
};
|
|
1899
|
+
// Volumes: the Rocket.Chat ones (the EBS driver's created-for tag), then by cluster tag.
|
|
1900
|
+
for (const [id, size, created, keys] of rows(await ask('volume', [
|
|
1901
|
+
'ec2',
|
|
1902
|
+
'describe-volumes',
|
|
1903
|
+
'--filters',
|
|
1904
|
+
'Name=status,Values=available',
|
|
1905
|
+
'Name=tag-key,Values=kubernetes.io/created-for/pvc/name',
|
|
1906
|
+
'--query',
|
|
1907
|
+
"Volumes[].[VolumeId,Size,CreateTime,join(',',Tags[].Key)]",
|
|
1908
|
+
]))) {
|
|
1909
|
+
const tagKeys = (keys ?? '').split(',');
|
|
1910
|
+
const detail = `${size ?? '?'} GB, created ${created ?? '?'}`;
|
|
1911
|
+
if (tagKeys.includes(ownTag)) {
|
|
1912
|
+
out.found.push({
|
|
1913
|
+
kind: 'volume',
|
|
1914
|
+
id: id ?? '',
|
|
1915
|
+
detail: `${detail} — an IAM administrator deletes it`,
|
|
1916
|
+
fix: `aws ec2 delete-volume --region ${region} --volume-id ${id ?? ''}`,
|
|
1917
|
+
certain: true,
|
|
1918
|
+
});
|
|
1919
|
+
}
|
|
1920
|
+
else if (!tagKeys.some((key) => key.startsWith('kubernetes.io/cluster/'))) {
|
|
1921
|
+
out.found.push({
|
|
1922
|
+
kind: 'volume',
|
|
1923
|
+
id: id ?? '',
|
|
1924
|
+
certain: false,
|
|
1925
|
+
fix: '',
|
|
1926
|
+
detail: `${detail} — a Rocket.Chat volume not tagged with any cluster; check it is not another cluster's before deleting`,
|
|
1927
|
+
});
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
// Load balancers: listed, then kept only when their tags name this cluster.
|
|
1931
|
+
const classic = rows(await ask('load balancer', [
|
|
1932
|
+
'elb',
|
|
1933
|
+
'describe-load-balancers',
|
|
1934
|
+
'--query',
|
|
1935
|
+
'LoadBalancerDescriptions[].[LoadBalancerName,DNSName]',
|
|
1936
|
+
]));
|
|
1937
|
+
if (classic.length > 0) {
|
|
1938
|
+
const owned = taggedOwn(await ask('load balancer', [
|
|
1939
|
+
'elb',
|
|
1940
|
+
'describe-tags',
|
|
1941
|
+
'--load-balancer-names',
|
|
1942
|
+
...classic.slice(0, 20).map((row) => row[0] ?? ''),
|
|
1943
|
+
]), 'LoadBalancerName');
|
|
1944
|
+
for (const [id, dns] of classic) {
|
|
1945
|
+
if (!owned.has(id ?? ''))
|
|
1946
|
+
continue;
|
|
1947
|
+
out.found.push({
|
|
1948
|
+
kind: 'load balancer',
|
|
1949
|
+
id: id ?? '',
|
|
1950
|
+
detail: dns ?? '',
|
|
1951
|
+
certain: true,
|
|
1952
|
+
fix: `aws elb delete-load-balancer --region ${region} --load-balancer-name ${id ?? ''}`,
|
|
1953
|
+
});
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
const modern = rows(await ask('load balancer', [
|
|
1957
|
+
'elbv2',
|
|
1958
|
+
'describe-load-balancers',
|
|
1959
|
+
'--query',
|
|
1960
|
+
'LoadBalancers[].[LoadBalancerName,DNSName,LoadBalancerArn]',
|
|
1961
|
+
]));
|
|
1962
|
+
if (modern.length > 0) {
|
|
1963
|
+
const owned = taggedOwn(await ask('load balancer', [
|
|
1964
|
+
'elbv2',
|
|
1965
|
+
'describe-tags',
|
|
1966
|
+
'--resource-arns',
|
|
1967
|
+
...modern.slice(0, 20).map((row) => row[2] ?? ''),
|
|
1968
|
+
]), 'ResourceArn');
|
|
1969
|
+
for (const [id, dns, arn] of modern) {
|
|
1970
|
+
if (!owned.has(arn ?? ''))
|
|
1971
|
+
continue;
|
|
1972
|
+
out.found.push({
|
|
1973
|
+
kind: 'load balancer',
|
|
1974
|
+
id: id ?? '',
|
|
1975
|
+
detail: dns ?? '',
|
|
1976
|
+
certain: true,
|
|
1977
|
+
fix: `aws elbv2 delete-load-balancer --region ${region} --load-balancer-arn ${arn ?? ''}`,
|
|
1978
|
+
});
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
// The network eksctl built: tagged with its stack by CloudFormation.
|
|
1982
|
+
for (const [id, vpc] of rows(await ask('nat gateway', [
|
|
1983
|
+
'ec2',
|
|
1984
|
+
'describe-nat-gateways',
|
|
1985
|
+
'--filter',
|
|
1986
|
+
'Name=state,Values=available',
|
|
1987
|
+
stackFilter,
|
|
1988
|
+
'--query',
|
|
1989
|
+
'NatGateways[].[NatGatewayId,VpcId]',
|
|
1990
|
+
]))) {
|
|
1991
|
+
out.found.push({
|
|
1992
|
+
kind: 'nat gateway',
|
|
1993
|
+
id: id ?? '',
|
|
1994
|
+
detail: `in ${vpc ?? '?'}`,
|
|
1995
|
+
certain: true,
|
|
1996
|
+
fix: `aws ec2 delete-nat-gateway --region ${region} --nat-gateway-id ${id ?? ''}`,
|
|
1997
|
+
});
|
|
1998
|
+
}
|
|
1999
|
+
for (const [id, ip] of rows(await ask('elastic ip', [
|
|
2000
|
+
'ec2',
|
|
2001
|
+
'describe-addresses',
|
|
2002
|
+
'--filters',
|
|
2003
|
+
stackFilter,
|
|
2004
|
+
'--query',
|
|
2005
|
+
'Addresses[?AssociationId==null].[AllocationId,PublicIp]',
|
|
2006
|
+
]))) {
|
|
2007
|
+
out.found.push({
|
|
2008
|
+
kind: 'elastic ip',
|
|
2009
|
+
id: id ?? '',
|
|
2010
|
+
detail: ip ?? '',
|
|
2011
|
+
certain: true,
|
|
2012
|
+
fix: `aws ec2 release-address --region ${region} --allocation-id ${id ?? ''}`,
|
|
2013
|
+
});
|
|
2014
|
+
}
|
|
2015
|
+
for (const [id, status] of rows(await ask('stack', [
|
|
2016
|
+
'cloudformation',
|
|
2017
|
+
'list-stacks',
|
|
2018
|
+
'--query',
|
|
2019
|
+
`StackSummaries[?starts_with(StackName, 'eksctl-${name}-') && StackStatus!='DELETE_COMPLETE'].[StackName,StackStatus]`,
|
|
2020
|
+
]))) {
|
|
2021
|
+
out.found.push({
|
|
2022
|
+
kind: 'stack',
|
|
2023
|
+
id: id ?? '',
|
|
2024
|
+
detail: status ?? '',
|
|
2025
|
+
certain: true,
|
|
2026
|
+
fix: `eksctl delete cluster --name ${name} --region ${region} --wait, or if it says the cluster ` +
|
|
2027
|
+
`does not exist: aws cloudformation delete-stack --region ${region} --stack-name ${id ?? ''}`,
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
2030
|
+
return out;
|
|
2031
|
+
}
|
|
2032
|
+
/**
|
|
2033
|
+
* The StorageClass a fresh EKS cluster does NOT have, and the reason `cluster create` on
|
|
2034
|
+
* its own hands back something that cannot run a workspace.
|
|
2035
|
+
*
|
|
2036
|
+
* MEASURED on EKS 1.33: the cluster ships exactly one class, `gp2`, on the IN-TREE
|
|
2037
|
+
* provisioner `kubernetes.io/aws-ebs` -- deprecated, and not the driver the
|
|
2038
|
+
* `aws-ebs-csi-driver` addon installs. Nothing references `ebs.csi.aws.com`, so the addon
|
|
2039
|
+
* pinned at Launchpad's version sits there doing nothing, and no class is marked default,
|
|
2040
|
+
* so every PVC would stay Pending and Rocket.Chat would never start.
|
|
2041
|
+
*
|
|
2042
|
+
* `WaitForFirstConsumer` because the volume must be created in the availability zone the
|
|
2043
|
+
* pod lands in. Binding immediately puts an EBS volume in one zone and then lets the
|
|
2044
|
+
* scheduler put the pod in another, which fails in a way that reads as a scheduling
|
|
2045
|
+
* problem rather than a storage one.
|
|
2046
|
+
*/
|
|
2047
|
+
/**
|
|
2048
|
+
* The upstream metrics-server manifest, pinned. `stats` and the GUI's Containers tab read
|
|
2049
|
+
* `kubectl top`, which needs it, and `cluster create` did not install it: the guide had
|
|
2050
|
+
* the reader apply this by hand. kubectl fetches it, which is the kubectl seam's business.
|
|
2051
|
+
*/
|
|
2052
|
+
/** Seconds a `kubectl apply` of a small manifest may take over the network. */
|
|
2053
|
+
const APPLY_TIMEOUT_S = 120;
|
|
2054
|
+
exports.METRICS_SERVER_VERSION = 'v0.7.2';
|
|
2055
|
+
exports.METRICS_SERVER_MANIFEST = 'https://github.com/kubernetes-sigs/metrics-server/releases/download/' +
|
|
2056
|
+
`${exports.METRICS_SERVER_VERSION}/components.yaml`;
|
|
2057
|
+
/**
|
|
2058
|
+
* metrics-server on a managed cluster that lacks it; adopted where it answers already.
|
|
2059
|
+
*
|
|
2060
|
+
* ONLY on a managed cluster. kind needs an insecure-TLS patch this manifest does not carry
|
|
2061
|
+
* (and `stats` says so there), and k3s ships its own. A refused install is reported, not
|
|
2062
|
+
* fatal: the cluster runs workspaces without it, only `stats` goes quiet.
|
|
2063
|
+
*/
|
|
2064
|
+
async function installMetricsServer(context, distribution, emit, out) {
|
|
2065
|
+
if (distribution !== 'eks')
|
|
2066
|
+
return;
|
|
2067
|
+
if (await (0, capabilities_1.metricsAvailable)(context)) {
|
|
2068
|
+
out.already.push('metrics-server');
|
|
2069
|
+
out.metrics_server = true;
|
|
2070
|
+
return;
|
|
2071
|
+
}
|
|
2072
|
+
const res = await exec_1.kube.run(['kubectl', '--context', context, 'apply', '-f', exports.METRICS_SERVER_MANIFEST], {
|
|
2073
|
+
timeout: APPLY_TIMEOUT_S,
|
|
2074
|
+
});
|
|
2075
|
+
if (res.returncode !== 0) {
|
|
2076
|
+
(0, events_1.warn)(emit, `metrics-server could not be installed: ${oneLine(res)} — \`houston stats\` will say how`, {
|
|
2077
|
+
phase: 'provision',
|
|
2078
|
+
});
|
|
2079
|
+
return;
|
|
2080
|
+
}
|
|
2081
|
+
(0, events_1.info)(emit, 'installing metrics-server — `houston stats` and the Containers tab read it', {
|
|
2082
|
+
phase: 'provision',
|
|
2083
|
+
});
|
|
2084
|
+
out.metrics_server = true;
|
|
2085
|
+
}
|
|
2086
|
+
exports.STORAGE_CLASS_NAME = 'gp3';
|
|
2087
|
+
exports.STORAGE_CLASS = `apiVersion: storage.k8s.io/v1
|
|
2088
|
+
kind: StorageClass
|
|
2089
|
+
metadata:
|
|
2090
|
+
name: ${exports.STORAGE_CLASS_NAME}
|
|
2091
|
+
annotations:
|
|
2092
|
+
storageclass.kubernetes.io/is-default-class: "true"
|
|
2093
|
+
provisioner: ebs.csi.aws.com
|
|
2094
|
+
volumeBindingMode: WaitForFirstConsumer
|
|
2095
|
+
allowVolumeExpansion: true
|
|
2096
|
+
parameters:
|
|
2097
|
+
type: gp3
|
|
2098
|
+
`;
|
|
2099
|
+
/** The address a DNS CNAME has to point at, once one exists. */
|
|
2100
|
+
async function loadbalancerAddress(context, emit, out) {
|
|
2101
|
+
// Read rather than assumed, and empty is a real answer -- reporting a blank as though
|
|
2102
|
+
// it were a hostname would send somebody to make a broken record.
|
|
2103
|
+
//
|
|
2104
|
+
// WAITED FOR, because this runs immediately after a twenty-minute `cluster create` and
|
|
2105
|
+
// an ELB commonly takes one to three minutes to publish a hostname. Asking once made a
|
|
2106
|
+
// blank CNAME target the NORMAL outcome of a successful create -- on the one line an
|
|
2107
|
+
// operator needs in order to finish the job. The wait returns the moment nothing is
|
|
2108
|
+
// pending, so a cluster with no LoadBalancer Service at all costs one read rather than
|
|
2109
|
+
// three minutes.
|
|
2110
|
+
const [, address] = await (0, endpoints_1.waitForLoadbalancer)(context, { emit });
|
|
2111
|
+
out.cname_target = address;
|
|
2112
|
+
// WHICH KIND OF BLANK, so the CLI can say. "No ingress controller published one" and
|
|
2113
|
+
// "it was still coming when we stopped waiting" need different next actions.
|
|
2114
|
+
out.cname_pending = address === '' && (await (0, endpoints_1.loadbalancerRead)(context)).pending > 0;
|
|
2115
|
+
// EMITTED, not merely returned. This is the one line an operator has to act on -- the
|
|
2116
|
+
// whole point of running `prepare` -- and it lived only in the returned document, so
|
|
2117
|
+
// the CLI printed it and the GUI did not. A fact that reaches only the return value is
|
|
2118
|
+
// invisible in the GUI, the mirror of this project's rule about printing instead of
|
|
2119
|
+
// emitting.
|
|
2120
|
+
if (address !== '') {
|
|
2121
|
+
(0, events_1.info)(emit, `point your name at: ${address}`, { phase: 'tls' });
|
|
2122
|
+
}
|
|
2123
|
+
else if (out.cname_pending) {
|
|
2124
|
+
(0, events_1.warn)(emit, 'the load balancer still has no address — it can take a few minutes. Run prepare ' +
|
|
2125
|
+
'again to read it, then make the DNS record.', { phase: 'tls' });
|
|
2126
|
+
}
|
|
2127
|
+
else {
|
|
2128
|
+
(0, events_1.warn)(emit, 'no LoadBalancer Service is waiting for an address, so nothing is going to publish ' +
|
|
2129
|
+
'one. This cluster has no load-balancing ingress — workspaces here reach you by ' +
|
|
2130
|
+
'port-forward, and `--domain` needs one.', { phase: 'tls' });
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
/** The storage class, the ingress controller and cert-manager this cluster needs. */
|
|
2134
|
+
async function storageAndIngress(context, emit, out) {
|
|
2135
|
+
const seen = await (0, capabilities_1.storageClasses)(context);
|
|
2136
|
+
if (seen === null) {
|
|
2137
|
+
throw new errors_1.PreflightError(`could not read the StorageClasses in '${context}', so houston cannot tell whether ` +
|
|
2138
|
+
'a volume would bind. This is a permissions problem on the credential in your ' +
|
|
2139
|
+
'kubeconfig, not a missing provisioner.');
|
|
2140
|
+
}
|
|
2141
|
+
const [names, byDefault] = seen;
|
|
2142
|
+
if (byDefault !== '') {
|
|
2143
|
+
// ADOPT. A cluster that already has a default has been set up by somebody, and
|
|
2144
|
+
// replacing their choice is not this command's business -- `up` only needs ONE to
|
|
2145
|
+
// exist, and a second default is an error state in Kubernetes anyway.
|
|
2146
|
+
out.storage_class = byDefault;
|
|
2147
|
+
out.already.push(`storage class '${byDefault}'`);
|
|
2148
|
+
}
|
|
2149
|
+
else {
|
|
2150
|
+
(0, events_1.info)(emit, `installing the '${exports.STORAGE_CLASS_NAME}' storage class — EKS ships only ` +
|
|
2151
|
+
`${names.join(', ') || 'none'}, and none of them default`, { phase: 'provision' });
|
|
2152
|
+
await (0, release_1.apply)(exports.STORAGE_CLASS, { namespace: 'default', context });
|
|
2153
|
+
out.storage_class = exports.STORAGE_CLASS_NAME;
|
|
2154
|
+
// Kept beside the ClusterConfig. `prepare` runs against a context, which may name a
|
|
2155
|
+
// cluster houston did not build and therefore has no directory for -- the ARN's last
|
|
2156
|
+
// segment is that cluster's name, and is what a person would look under.
|
|
2157
|
+
out.kept =
|
|
2158
|
+
(await keepManifest(context.split('/').pop() ?? context, 'storageclass.yaml', exports.STORAGE_CLASS)) ?? '';
|
|
2159
|
+
}
|
|
2160
|
+
out.ingress_class = await (0, ingress_1.ensureIngress)({
|
|
2161
|
+
context,
|
|
2162
|
+
distribution: 'eks',
|
|
2163
|
+
invited: true,
|
|
2164
|
+
emit,
|
|
2165
|
+
});
|
|
2166
|
+
await (0, certmanager_1.ensureCertManager)({ context, distribution: 'eks', invited: true, emit });
|
|
2167
|
+
out.cert_manager = true;
|
|
2168
|
+
}
|
|
2169
|
+
/**
|
|
2170
|
+
* Make a cluster able to serve a workspace over HTTPS. Idempotent.
|
|
2171
|
+
*
|
|
2172
|
+
* Separate from `createCluster` as a VERB but run by it, because the two answer different
|
|
2173
|
+
* questions and only one of them costs money. `create` builds infrastructure; `prepare`
|
|
2174
|
+
* configures it, is free, and is safe to run again on a cluster somebody else built or on
|
|
2175
|
+
* one whose ingress was removed.
|
|
2176
|
+
*
|
|
2177
|
+
* Returns what a human still has to do. The DNS record is the honest boundary of this
|
|
2178
|
+
* tool: the ingress gets a load balancer hostname, and something has to point the
|
|
2179
|
+
* workspace's name at it before Let's Encrypt will validate.
|
|
2180
|
+
*/
|
|
2181
|
+
async function prepareCluster(context, { emit = events_1.nullEmit, zone = '' } = {}) {
|
|
2182
|
+
// THE SHORT NAME, RESOLVED. `--context support` is the form `cluster create` prints in
|
|
2183
|
+
// its own closing line and the form the guide teaches, and an EKS context is really
|
|
2184
|
+
// `arn:aws:eks:<region>:<account>:cluster/support`, which nobody types. Passing the
|
|
2185
|
+
// short name straight to kubectl made every read refuse, and the refusal it produced
|
|
2186
|
+
// was "could not read the StorageClasses ... This is a permissions problem on the
|
|
2187
|
+
// credential in your kubeconfig" -- a confident, wrong diagnosis that sends somebody to
|
|
2188
|
+
// fix IAM when the context simply had not been expanded. MEASURED on a freshly created
|
|
2189
|
+
// cluster: `cluster prepare --context support` failed in 0.27s, which is far too fast
|
|
2190
|
+
// to have asked AWS anything.
|
|
2191
|
+
const [resolved] = await (0, context_1.assertContextKnown)(context);
|
|
2192
|
+
const out = {
|
|
2193
|
+
context: resolved,
|
|
2194
|
+
storage_class: '',
|
|
2195
|
+
ingress_class: '',
|
|
2196
|
+
cert_manager: false,
|
|
2197
|
+
cname_target: '',
|
|
2198
|
+
cname_pending: false,
|
|
2199
|
+
zone: '',
|
|
2200
|
+
metrics_server: false,
|
|
2201
|
+
already: [],
|
|
2202
|
+
kept: '',
|
|
2203
|
+
};
|
|
2204
|
+
await storageAndIngress(resolved, emit, out);
|
|
2205
|
+
await loadbalancerAddress(resolved, emit, out);
|
|
2206
|
+
await installMetricsServer(resolved, await (0, context_1.distribution)(resolved), emit, out);
|
|
2207
|
+
// The zone, set or changed after the build: noted here and tagged on the cluster.
|
|
2208
|
+
const short = resolved.split('/').pop() ?? resolved;
|
|
2209
|
+
if (zone !== '') {
|
|
2210
|
+
await rememberZone(short, regionOfContext(resolved), zone);
|
|
2211
|
+
(0, events_1.info)(emit, `zone ${zone} noted — workspaces here are named <name>.${zone} by default`, {
|
|
2212
|
+
phase: 'provision',
|
|
2213
|
+
});
|
|
2214
|
+
}
|
|
2215
|
+
out.zone = zone !== '' ? zone : zoneOf(short);
|
|
2216
|
+
return out;
|
|
2217
|
+
}
|
|
2218
|
+
/**
|
|
2219
|
+
* One exported property bag keeps every replaceable AWS operation injectable.
|
|
2220
|
+
*
|
|
2221
|
+
* A module-local function binding cannot be replaced in the emitted TypeScript, because swc
|
|
2222
|
+
* turns an export into a non-configurable getter. Intra-module calls go THROUGH this object
|
|
2223
|
+
* wherever a test needs to replace one -- `describeCluster` above all, since every
|
|
2224
|
+
* destructive decision in this file reads it.
|
|
2225
|
+
*/
|
|
2226
|
+
exports.aws = {
|
|
2227
|
+
whoami,
|
|
2228
|
+
region,
|
|
2229
|
+
preflight,
|
|
2230
|
+
clusterConfig,
|
|
2231
|
+
streamTool,
|
|
2232
|
+
describeCluster,
|
|
2233
|
+
clusterExists,
|
|
2234
|
+
ownsCluster,
|
|
2235
|
+
enabledRegions,
|
|
2236
|
+
sweepClusters,
|
|
2237
|
+
createCluster,
|
|
2238
|
+
deleteCluster,
|
|
2239
|
+
prepareCluster,
|
|
2240
|
+
};
|