underpost 3.2.9 → 3.2.11

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.
Files changed (104) hide show
  1. package/.github/workflows/npmpkg.ci.yml +1 -0
  2. package/.github/workflows/pwa-microservices-template-test.ci.yml +1 -1
  3. package/.github/workflows/release.cd.yml +1 -0
  4. package/.vscode/extensions.json +9 -9
  5. package/.vscode/settings.json +20 -4
  6. package/CHANGELOG.md +195 -1
  7. package/CLI-HELP.md +92 -23
  8. package/README.md +38 -9
  9. package/bin/build.js +27 -7
  10. package/bin/build.template.js +187 -0
  11. package/bin/deploy.js +12 -2
  12. package/bin/index.js +2 -1
  13. package/bump.config.js +26 -0
  14. package/conf.js +20 -7
  15. package/manifests/cronjobs/dd-cron/dd-cron-backup.yaml +1 -1
  16. package/manifests/cronjobs/dd-cron/dd-cron-dns.yaml +1 -1
  17. package/manifests/deployment/dd-default-development/deployment.yaml +2 -2
  18. package/manifests/deployment/dd-test-development/deployment.yaml +4 -2
  19. package/manifests/kind-config-dev.yaml +8 -0
  20. package/manifests/lxd/lxd-admin-profile.yaml +12 -3
  21. package/manifests/mongodb/pv-pvc.yaml +44 -8
  22. package/manifests/mongodb/statefulset.yaml +55 -68
  23. package/manifests/mongodb-4.4/headless-service.yaml +10 -0
  24. package/manifests/mongodb-4.4/kustomization.yaml +3 -1
  25. package/manifests/mongodb-4.4/mongodb-nodeport.yaml +17 -0
  26. package/manifests/mongodb-4.4/pv-pvc.yaml +10 -14
  27. package/manifests/mongodb-4.4/statefulset.yaml +79 -0
  28. package/manifests/mongodb-4.4/storage-class.yaml +9 -0
  29. package/manifests/valkey/statefulset.yaml +1 -1
  30. package/manifests/valkey/valkey-nodeport.yaml +17 -0
  31. package/package.json +27 -12
  32. package/scripts/ipxe-setup.sh +52 -49
  33. package/scripts/k3s-node-setup.sh +81 -46
  34. package/scripts/lxd-vm-setup.sh +193 -8
  35. package/scripts/maas-nat-firewalld.sh +145 -0
  36. package/src/api/core/core.router.js +19 -14
  37. package/src/api/core/core.service.js +5 -5
  38. package/src/api/default/default.router.js +22 -18
  39. package/src/api/default/default.service.js +5 -5
  40. package/src/api/document/document.router.js +28 -23
  41. package/src/api/document/document.service.js +100 -23
  42. package/src/api/file/file.router.js +19 -13
  43. package/src/api/file/file.service.js +9 -7
  44. package/src/api/test/test.router.js +17 -12
  45. package/src/api/types.js +24 -0
  46. package/src/api/user/guest.service.js +5 -4
  47. package/src/api/user/user.router.js +297 -288
  48. package/src/api/user/user.service.js +100 -35
  49. package/src/cli/baremetal.js +132 -101
  50. package/src/cli/cluster.js +700 -232
  51. package/src/cli/db.js +59 -60
  52. package/src/cli/deploy.js +216 -137
  53. package/src/cli/fs.js +13 -3
  54. package/src/cli/index.js +80 -15
  55. package/src/cli/ipfs.js +4 -6
  56. package/src/cli/kubectl.js +4 -1
  57. package/src/cli/lxd.js +1099 -223
  58. package/src/cli/monitor.js +9 -3
  59. package/src/cli/release.js +334 -140
  60. package/src/cli/repository.js +68 -23
  61. package/src/cli/run.js +191 -47
  62. package/src/cli/secrets.js +11 -2
  63. package/src/cli/test.js +9 -3
  64. package/src/client/Default.index.js +9 -3
  65. package/src/client/components/core/Auth.js +5 -0
  66. package/src/client/components/core/ClientEvents.js +76 -0
  67. package/src/client/components/core/EventBus.js +4 -0
  68. package/src/client/components/core/Modal.js +82 -41
  69. package/src/client/components/core/PanelForm.js +56 -52
  70. package/src/client/components/core/Worker.js +162 -363
  71. package/src/client/sw/core.sw.js +174 -112
  72. package/src/db/DataBaseProvider.js +115 -15
  73. package/src/db/mariadb/MariaDB.js +2 -1
  74. package/src/db/mongo/MongoBootstrap.js +657 -0
  75. package/src/db/mongo/MongooseDB.js +129 -21
  76. package/src/index.js +1 -1
  77. package/src/runtime/express/Express.js +2 -2
  78. package/src/runtime/wp/Wp.js +8 -5
  79. package/src/server/auth.js +2 -2
  80. package/src/server/client-build-docs.js +1 -1
  81. package/src/server/client-build.js +94 -129
  82. package/src/server/conf.js +81 -79
  83. package/src/server/process.js +180 -19
  84. package/src/server/proxy.js +9 -2
  85. package/src/server/runtime.js +1 -1
  86. package/src/server/start.js +16 -4
  87. package/src/server/valkey.js +2 -0
  88. package/src/ws/IoInterface.js +16 -16
  89. package/src/ws/core/channels/core.ws.chat.js +11 -11
  90. package/src/ws/core/channels/core.ws.mailer.js +29 -29
  91. package/src/ws/core/channels/core.ws.stream.js +19 -19
  92. package/src/ws/core/core.ws.connection.js +8 -8
  93. package/src/ws/core/core.ws.server.js +6 -5
  94. package/src/ws/default/channels/default.ws.main.js +10 -10
  95. package/src/ws/default/default.ws.connection.js +4 -4
  96. package/src/ws/default/default.ws.server.js +4 -3
  97. package/bin/file.js +0 -202
  98. package/bin/vs.js +0 -74
  99. package/bin/zed.js +0 -84
  100. package/src/client/ssr/email/DefaultRecoverEmail.js +0 -21
  101. package/src/client/ssr/email/DefaultVerifyEmail.js +0 -17
  102. /package/src/client/ssr/{offline → views}/Maintenance.js +0 -0
  103. /package/src/client/ssr/{offline → views}/NoNetworkConnection.js +0 -0
  104. /package/src/client/ssr/{pages → views}/Test.js +0 -0
@@ -7,6 +7,8 @@
7
7
  import { getNpmRootPath } from '../server/conf.js';
8
8
  import { loggerFactory } from '../server/logger.js';
9
9
  import { shellExec } from '../server/process.js';
10
+ import { MONGODB_DEFAULT_REPLICA_COUNT } from '../db/mongo/MongooseDB.js';
11
+ import { MongoBootstrap } from '../db/mongo/MongoBootstrap.js';
10
12
  import os from 'os';
11
13
  import fs from 'fs-extra';
12
14
  import Underpost from '../index.js';
@@ -31,7 +33,7 @@ class UnderpostCluster {
31
33
  * @param {object} [options] - Configuration options for cluster initialization.
32
34
  * @param {boolean} [options.mongodb=false] - Deploy MongoDB.
33
35
  * @param {boolean} [options.mongodb4=false] - Deploy MongoDB 4.4.
34
- * @param {String} [options.mongoDbHost=''] - Set custom mongo db host
36
+ * @param {string} [options.serviceHost=''] - Set a custom host/IP for exposed MongoDB and Valkey clients.
35
37
  * @param {boolean} [options.mariadb=false] - Deploy MariaDB.
36
38
  * @param {boolean} [options.mysql=false] - Deploy MySQL.
37
39
  * @param {boolean} [options.postgresql=false] - Deploy PostgreSQL.
@@ -41,6 +43,7 @@ class UnderpostCluster {
41
43
  * @param {boolean} [options.certManager=false] - Deploy Cert-Manager for certificate management.
42
44
  * @param {boolean} [options.listPods=false] - List Kubernetes pods.
43
45
  * @param {boolean} [options.reset=false] - Perform a comprehensive reset of Kubernetes and container environments.
46
+ * @param {boolean} [options.resetMongodb=false] - Perform a targeted reset of MongoDB components without restarting the entire cluster.
44
47
  * @param {boolean} [options.dev=false] - Run in development mode (adjusts paths).
45
48
  * @param {string} [options.nsUse=''] - Set the current kubectl namespace (creates namespace if it doesn't exist).
46
49
  * @param {string} [options.namespace='default'] - Kubernetes namespace for cluster operations.
@@ -61,6 +64,12 @@ class UnderpostCluster {
61
64
  * @param {boolean} [options.removeVolumeHostPaths=false] - Remove data from host paths used by Persistent Volumes.
62
65
  * @param {string} [options.hosts] - Set custom hosts entries.
63
66
  * @param {string} [options.replicas] - Set the number of replicas for certain deployments.
67
+ * @param {boolean} [options.nodePort=false] - Expose enabled ready services (e.g. MongoDB 4.4, Valkey)
68
+ * to the host/public network via their NodePort Service manifest. The node port value lives directly
69
+ * in each manifest (mongodb-4.4/mongodb-nodeport.yaml, valkey/valkey-nodeport.yaml).
70
+ * @param {string} [options.nodeSelector=''] - Pin the just-deployed StatefulSet (MongoDB 4.4 / Valkey)
71
+ * to a specific Kubernetes node by name (e.g. 'localhost.localdomain'). Applied via a
72
+ * `kubernetes.io/hostname` nodeSelector patch once the workload reports Ready.
64
73
  * @memberof UnderpostCluster
65
74
  */
66
75
  async init(
@@ -68,7 +77,7 @@ class UnderpostCluster {
68
77
  options = {
69
78
  mongodb: false,
70
79
  mongodb4: false,
71
- mongoDbHost: '',
80
+ serviceHost: '',
72
81
  mariadb: false,
73
82
  mysql: false,
74
83
  postgresql: false,
@@ -78,6 +87,7 @@ class UnderpostCluster {
78
87
  certManager: false,
79
88
  listPods: false,
80
89
  reset: false,
90
+ resetMongodb: false,
81
91
  dev: false,
82
92
  nsUse: '',
83
93
  namespace: 'default',
@@ -98,18 +108,22 @@ class UnderpostCluster {
98
108
  removeVolumeHostPaths: false,
99
109
  hosts: '',
100
110
  replicas: '',
111
+ nodePort: false,
112
+ nodeSelector: '',
101
113
  },
102
114
  ) {
103
115
  if (options.initHost) return Underpost.cluster.initHost();
104
116
 
105
117
  if (options.uninstallHost) return Underpost.cluster.uninstallHost();
106
118
 
107
- if (options.config) return Underpost.cluster.config();
119
+ if (options.config) return options.k3s ? Underpost.cluster.configMinimalK3s() : Underpost.cluster.config();
108
120
 
109
- if (options.chown) return Underpost.cluster.chown();
121
+ if (options.chown) return Underpost.cluster.chown(options.k3s ? 'k3s' : options.kubeadm ? 'kubeadm' : 'kind');
110
122
 
111
123
  const npmRoot = getNpmRootPath();
112
124
  const underpostRoot = options.dev ? '.' : `${npmRoot}/underpost`;
125
+ const serviceHostInput = `${options.serviceHost || ''}`.trim();
126
+ const serviceHost = Underpost.cluster.resolveServiceHost(options);
113
127
 
114
128
  if (options.listPods) return console.table(Underpost.kubectl.get(podName ?? undefined));
115
129
  // Set default namespace if not specified
@@ -120,6 +134,7 @@ class UnderpostCluster {
120
134
  const namespaceExists = shellExec(`kubectl get namespace ${options.nsUse} --ignore-not-found -o name`, {
121
135
  stdout: true,
122
136
  silent: true,
137
+ silentOnError: true,
123
138
  }).trim();
124
139
 
125
140
  if (!namespaceExists) {
@@ -135,30 +150,57 @@ class UnderpostCluster {
135
150
  return;
136
151
  }
137
152
 
138
- // Reset Kubernetes cluster components (Kind/Kubeadm/K3s) and container runtimes
153
+ // Route reset to the per-type method. Each only touches what its runtime owns.
139
154
  if (options.reset) {
140
- const clusterType = options.k3s ? 'k3s' : options.kubeadm ? 'kubeadm' : 'kind';
141
- return await Underpost.cluster.safeReset({
155
+ if (options.k3s)
156
+ return await Underpost.cluster.safeResetK3s({ underpostRoot, resetMode: options.resetMode || 'full' });
157
+ if (options.kubeadm)
158
+ return await Underpost.cluster.safeResetKubeadm({
159
+ underpostRoot,
160
+ removeVolumeHostPaths: options.removeVolumeHostPaths,
161
+ });
162
+ return await Underpost.cluster.safeResetKind({
142
163
  underpostRoot,
143
164
  removeVolumeHostPaths: options.removeVolumeHostPaths,
165
+ });
166
+ }
167
+
168
+ // Targeted MongoDB-only reset (does not restart the whole node)
169
+ if (options.resetMongodb) {
170
+ const clusterType = options.k3s ? 'k3s' : options.kubeadm ? 'kubeadm' : 'kind';
171
+ return await MongoBootstrap.reset({
172
+ namespace: options.namespace,
144
173
  clusterType,
174
+ underpostRoot,
145
175
  });
146
176
  }
147
177
 
148
- // Check if a cluster (Kind, Kubeadm, or K3s) is already initialized
149
- const alreadyKubeadmCluster = Underpost.kubectl.get('calico-kube-controllers')[0];
150
- const alreadyKindCluster = Underpost.kubectl.get('kube-apiserver-kind-control-plane')[0];
151
- // K3s pods often contain 'svclb-traefik' in the kube-system namespace
152
- const alreadyK3sCluster = Underpost.kubectl.get('svclb-traefik')[0];
178
+ // Check if a cluster (Kind, Kubeadm, or K3s) is already initialized by
179
+ // inspecting nodes rather than probing add-on pods. See detectClusterRuntime.
180
+ const runtime = Underpost.cluster.detectClusterRuntime();
181
+ const alreadyCluster = runtime.type !== null;
182
+ if (alreadyCluster) {
183
+ logger.info(
184
+ `Detected existing ${runtime.type} cluster (${runtime.ready ? 'Ready' : 'NotReady'}); skipping initialization.`,
185
+ );
186
+ }
153
187
 
154
188
  // --- Kubeadm/Kind/K3s Cluster Initialization ---
155
- if (!alreadyKubeadmCluster && !alreadyKindCluster && !alreadyK3sCluster) {
156
- Underpost.cluster.config();
189
+ // Host config is applied per cluster type (not shared) so the K3s path can
190
+ // stay minimal and avoid the Docker/containerd/kubelet setup it does not need.
191
+ if (!alreadyCluster) {
157
192
  if (options.k3s) {
193
+ // K3s is self-contained (bundles containerd, kubelet, CNI, CoreDNS).
194
+ // Apply ONLY minimal host config — see configMinimalK3s.
195
+ Underpost.cluster.configMinimalK3s();
158
196
  logger.info('Initializing K3s control plane...');
159
197
  // Install K3s
160
198
  logger.info('Installing K3s...');
161
- shellExec(`curl -sfL https://get.k3s.io | sh -`);
199
+ // Disable the bundled traefik ingress and servicelb (Klipper) load
200
+ // balancer. The platform exposes services explicitly via Project
201
+ // Contour / Envoy and NodePort services (see --node-port); leaving the
202
+ // K3s built-ins enabled would bind the same host ports and conflict.
203
+ shellExec(`curl -sfL https://get.k3s.io | sh -s - --disable=traefik --disable=servicelb`);
162
204
  logger.info('K3s installation completed.');
163
205
 
164
206
  Underpost.cluster.chown('k3s');
@@ -166,7 +208,11 @@ class UnderpostCluster {
166
208
  logger.info('Waiting for K3s to be ready...');
167
209
  shellExec(`sudo systemctl is-active --wait k3s || sudo systemctl wait --for=active k3s.service`);
168
210
  logger.info('K3s service is active.');
211
+ // K3s manages its own CNI (flannel) and iptables rules. nat-iptables.sh
212
+ // is neither needed nor desirable for a single K3s node in a VM.
169
213
  } else if (options.kubeadm) {
214
+ Underpost.cluster.config();
215
+ Underpost.cluster.natSetup({ underpostRoot });
170
216
  logger.info('Initializing Kubeadm control plane...');
171
217
  // Set default values if not provided
172
218
  const podNetworkCidr = options.podNetworkCidr || '192.168.0.0/16';
@@ -202,19 +248,49 @@ class UnderpostCluster {
202
248
  // Untaint control plane node to allow scheduling pods
203
249
  const nodeName = os.hostname();
204
250
  shellExec(`kubectl taint nodes ${nodeName} node-role.kubernetes.io/control-plane:NoSchedule-`);
205
- // Install local-path-provisioner for dynamic PVCs (optional but recommended)
251
+ // Install local-path-provisioner for dynamic PVCs
206
252
  logger.info('Installing local-path-provisioner...');
207
253
  shellExec(
208
254
  `kubectl apply -f https://cdn.jsdelivr.net/gh/rancher/local-path-provisioner@master/deploy/local-path-storage.yaml`,
209
255
  );
210
256
  } else {
211
- // Kind cluster initialization (if not using kubeadm or k3s)
257
+ Underpost.cluster.config();
258
+ Underpost.cluster.natSetup({ underpostRoot });
259
+ // Kind cluster initialization (default for development)
212
260
  logger.info('Initializing Kind cluster...');
213
- shellExec(
214
- `cd ${underpostRoot}/manifests && kind create cluster --config kind-config${
215
- options.dev ? '-dev' : ''
216
- }.yaml`,
217
- );
261
+ const devReplicaCount = Math.max(Number(options.replicas) || MONGODB_DEFAULT_REPLICA_COUNT, 3);
262
+ shellExec(`sudo mkdir -p /data/mongodb`);
263
+ for (let index = 0; index < devReplicaCount; index++) {
264
+ shellExec(`sudo mkdir -p /data/mongodb/v${index}`);
265
+ }
266
+ const kindCreateCmd = `cd ${underpostRoot}/manifests && kind create cluster --config kind-config-dev.yaml`;
267
+ try {
268
+ shellExec(kindCreateCmd);
269
+ } catch (error) {
270
+ const kindCreateErrText = `${error?.message || ''}\n${error?.stderr || ''}`;
271
+ if (kindCreateErrText.includes('all predefined address pools have been fully subnetted')) {
272
+ logger.warn(
273
+ 'Docker address pool exhausted while creating Kind cluster. Running cleanup and retrying once...',
274
+ );
275
+ Underpost.cluster.recoverKindDockerNetworks();
276
+ try {
277
+ shellExec(kindCreateCmd);
278
+ } catch (retryError) {
279
+ const retryErrText = `${retryError?.message || ''}\n${retryError?.stderr || ''}`;
280
+ if (retryErrText.includes('all predefined address pools have been fully subnetted')) {
281
+ logger.warn(
282
+ 'Kind retry still failed from pool exhaustion. Applying Docker daemon address-pool config and retrying once more...',
283
+ );
284
+ Underpost.cluster.ensureDockerDefaultAddressPools();
285
+ shellExec(kindCreateCmd);
286
+ } else {
287
+ throw retryError;
288
+ }
289
+ }
290
+ } else {
291
+ throw error;
292
+ }
293
+ }
218
294
  Underpost.cluster.chown('kind'); // Pass 'kind' to chown
219
295
  }
220
296
  }
@@ -263,7 +339,23 @@ EOF
263
339
  if (options.pullImage) Underpost.cluster.pullImage('valkey/valkey:latest', options);
264
340
  shellExec(`kubectl delete statefulset valkey-service -n ${options.namespace} --ignore-not-found`);
265
341
  shellExec(`kubectl apply -k ${underpostRoot}/manifests/valkey -n ${options.namespace}`);
266
- await Underpost.test.statusMonitor('valkey-service', 'Running', 'pods', 1000, 60 * 10);
342
+ const valkeyReady = await Underpost.test.statusMonitor('valkey-service', 'Running', 'pods', 1000, 60 * 10);
343
+ // Expose valkey to the host/public network only once the pod is ready.
344
+ // The node port (32079) is set directly in the manifest.
345
+ if (valkeyReady && options.nodePort)
346
+ shellExec(`kubectl apply -f ${underpostRoot}/manifests/valkey/valkey-nodeport.yaml -n ${options.namespace}`);
347
+ if (valkeyReady && options.nodeSelector)
348
+ Underpost.cluster.pinToNode({
349
+ name: 'valkey-service',
350
+ namespace: options.namespace,
351
+ node: options.nodeSelector,
352
+ });
353
+ if (valkeyReady && serviceHost)
354
+ Underpost.cluster.syncServiceConnectionEnv({
355
+ serviceHost,
356
+ valkey: true,
357
+ options,
358
+ });
267
359
  }
268
360
  if (options.ipfs) {
269
361
  await Underpost.ipfs.deploy(options, underpostRoot);
@@ -296,54 +388,75 @@ EOF
296
388
  }
297
389
  if (options.mongodb4) {
298
390
  if (options.pullImage) Underpost.cluster.pullImage('mongo:4.4', options);
391
+ shellExec(`kubectl delete statefulset mongodb -n ${options.namespace} --ignore-not-found`);
299
392
  shellExec(`kubectl apply -k ${underpostRoot}/manifests/mongodb-4.4 -n ${options.namespace}`);
300
393
 
301
- const deploymentName = 'mongodb-deployment';
394
+ const statefulSetName = 'mongodb';
395
+ const podName = 'mongodb-0';
302
396
 
303
- const successInstance = await Underpost.test.statusMonitor(deploymentName);
397
+ const successInstance = await Underpost.test.statusMonitor(podName);
304
398
 
305
399
  if (successInstance) {
306
- if (!options.mongoDbHost) options.mongoDbHost = 'mongodb-service';
307
- const mongoConfig = {
308
- _id: 'rs0',
309
- members: [{ _id: 0, host: `${options.mongoDbHost}:27017` }],
310
- };
311
-
312
- const [pod] = Underpost.kubectl.get(deploymentName);
313
-
314
- shellExec(
315
- `sudo kubectl exec -i ${pod.NAME} -- mongo --quiet \
316
- --eval 'rs.initiate(${JSON.stringify(mongoConfig)})'`,
317
- );
400
+ // mongod only accepts a member host it can recognize as itself (the isSelf check):
401
+ // it must match a local interface IP or be reachable back at that address. A pod-external
402
+ // LAN IP / NodePort address is neither bound in the pod netns nor routable back from it,
403
+ // so reconfiguring to it fails with NodeNotFound ("...maps to this node") even under
404
+ // force. Bootstrap on localhost; only advertise a non-localhost host when the node can
405
+ // self-verify it, and tolerate the failure otherwise so bootstrap stays idempotent.
406
+ // Clients reaching the set through an external IP/NodePort must use directConnection=true,
407
+ // which ignores the advertised member host (see MongooseDB.buildUri).
408
+ const rsHost = serviceHostInput || (options.dev ? '127.0.0.1' : 'mongodb-0.mongodb-service');
409
+ const memberHost = `${rsHost}:27017`;
410
+ const initEval = [
411
+ `try { rs.initiate({ _id: "rs0", members: [{ _id: 0, host: "localhost:27017" }] }); }`,
412
+ `catch (e) { if (!String(e).includes("already initialized") && !String(e).includes("AlreadyInitialized")) throw e; }`,
413
+ `for (var i = 0; i < 30; i++) { if (db.isMaster().ismaster) break; sleep(1000); }`,
414
+ memberHost === 'localhost:27017'
415
+ ? ``
416
+ : `try { var c = rs.conf(); c.members[0].host = "${memberHost}"; rs.reconfig(c, { force: true }); print("RS_HOST_SET ${memberHost}"); } catch (e) { if (String(e).includes("NodeNotFound") || String(e).includes("maps to this node")) { print("RS_HOST_SKIPPED ${memberHost} (not self-reachable from pod; clients must use directConnection=true)"); } else { throw e; } }`,
417
+ ]
418
+ .filter(Boolean)
419
+ .join(' ');
420
+
421
+ shellExec(`sudo kubectl exec -i ${podName} -n ${options.namespace} -- mongo --quiet --eval '${initEval}'`);
422
+
423
+ // Only expose mongos to the host/public network once the instance is
424
+ // confirmed ready and the replica set is initiated. The node port (32017)
425
+ // is set directly in the manifest.
426
+ if (options.nodePort)
427
+ shellExec(
428
+ `kubectl apply -f ${underpostRoot}/manifests/mongodb-4.4/mongodb-nodeport.yaml -n ${options.namespace}`,
429
+ );
430
+ if (options.nodeSelector)
431
+ Underpost.cluster.pinToNode({
432
+ name: statefulSetName,
433
+ namespace: options.namespace,
434
+ node: options.nodeSelector,
435
+ });
436
+ if (serviceHost)
437
+ Underpost.cluster.syncServiceConnectionEnv({
438
+ serviceHost,
439
+ mongodb: true,
440
+ options,
441
+ });
318
442
  }
319
443
  } else if (options.mongodb) {
320
- if (options.pullImage) Underpost.cluster.pullImage('mongo:latest', options);
321
- shellExec(
322
- `sudo kubectl create secret generic mongodb-keyfile --from-file=/home/dd/engine/engine-private/mongodb-keyfile --dry-run=client -o yaml | kubectl apply -f - -n ${options.namespace}`,
323
- );
324
- shellExec(
325
- `sudo kubectl create secret generic mongodb-secret --from-file=username=/home/dd/engine/engine-private/mongodb-username --from-file=password=/home/dd/engine/engine-private/mongodb-password --dry-run=client -o yaml | kubectl apply -f - -n ${options.namespace}`,
326
- );
327
- shellExec(`kubectl delete statefulset mongodb -n ${options.namespace} --ignore-not-found`);
328
- shellExec(`kubectl apply -f ${underpostRoot}/manifests/mongodb/storage-class.yaml -n ${options.namespace}`);
329
- shellExec(`kubectl apply -k ${underpostRoot}/manifests/mongodb -n ${options.namespace}`);
330
-
331
- const successInstance = await Underpost.test.statusMonitor('mongodb-0', 'Running', 'pods', 1000, 60 * 10);
332
-
333
- if (successInstance) {
334
- if (!options.mongoDbHost) options.mongoDbHost = 'mongodb-0.mongodb-service';
335
- const mongoConfig = {
336
- _id: 'rs0',
337
- members: options.mongoDbHost.split(',').map((host, index) => ({ _id: index, host: `${host}:27017` })),
338
- };
339
-
340
- shellExec(
341
- `sudo kubectl exec -i mongodb-0 -- mongosh --quiet --json=relaxed \
342
- --eval 'use admin' \
343
- --eval 'rs.initiate(${JSON.stringify(mongoConfig)})' \
344
- --eval 'rs.status()'`,
345
- );
346
- }
444
+ const clusterType = options.k3s ? 'k3s' : options.kubeadm ? 'kubeadm' : 'kind';
445
+ await MongoBootstrap.initReplicaSet({
446
+ namespace: options.namespace,
447
+ replicaCount: Number(options.replicas) || MONGODB_DEFAULT_REPLICA_COUNT,
448
+ hostList: serviceHostInput,
449
+ pullImage: options.pullImage,
450
+ reset: options.reset,
451
+ clusterType,
452
+ underpostRoot,
453
+ });
454
+ if (serviceHost)
455
+ Underpost.cluster.syncServiceConnectionEnv({
456
+ serviceHost,
457
+ mongodb: true,
458
+ options,
459
+ });
347
460
  }
348
461
 
349
462
  if (options.contour) {
@@ -378,6 +491,132 @@ EOF
378
491
  }
379
492
  },
380
493
 
494
+ /**
495
+ * @method detectClusterRuntime
496
+ * @description Detects an already-initialized cluster by inspecting Kubernetes
497
+ * nodes, and classifies its runtime. Nodes are authoritative and stable, unlike
498
+ * add-on pods: the previous check keyed off pod names (`calico-kube-controllers`,
499
+ * `kube-apiserver-kind-control-plane`, `svclb-traefik`), whose presence and
500
+ * readiness are timing- and config-dependent. Disabling servicelb removes the
501
+ * `svclb-traefik` pods entirely, and CNI/controller pods report NotReady for a
502
+ * window right after init — both of which made re-runs misdetect the cluster
503
+ * state. Classification relies on stable node attributes:
504
+ * - k3s: the node VERSION carries a `+k3s` build suffix (e.g. v1.30.5+k3s1).
505
+ * - kind: kind names every node `<cluster>-control-plane` / `<cluster>-worker`.
506
+ * - kubeadm: a real control-plane node that is neither of the above.
507
+ * @returns {{ type: ('k3s'|'kubeadm'|'kind'|null), ready: boolean, nodes: Array<object> }}
508
+ * `type` is the detected runtime (null when no cluster exists); `ready` is true
509
+ * when at least one node reports STATUS=Ready.
510
+ * @memberof UnderpostCluster
511
+ */
512
+ detectClusterRuntime() {
513
+ const nodes = Underpost.kubectl.get('', 'nodes');
514
+ if (!nodes.length) return { type: null, ready: false, nodes: [] };
515
+
516
+ // STATUS can be a comma-joined list (e.g. "Ready,SchedulingDisabled").
517
+ const ready = nodes.some((n) => `${n.STATUS || ''}`.split(',').includes('Ready'));
518
+
519
+ let type;
520
+ if (nodes.some((n) => `${n.VERSION || ''}`.includes('+k3s'))) type = 'k3s';
521
+ else if (nodes.some((n) => `${n.NAME || ''}`.includes('-control-plane') || `${n.NAME || ''}`.includes('kind')))
522
+ type = 'kind';
523
+ else type = 'kubeadm';
524
+
525
+ return { type, ready, nodes };
526
+ },
527
+
528
+ /**
529
+ * @method pinToNode
530
+ * @description Pins a workload to a specific Kubernetes node by patching its
531
+ * pod template with a `kubernetes.io/hostname` nodeSelector. General-purpose;
532
+ * currently used to place the MongoDB 4.4 / Valkey StatefulSets on a chosen
533
+ * node (`--node-selector`). The patch triggers a rolling reschedule onto the
534
+ * target node.
535
+ * @param {object} params
536
+ * @param {string} [params.kind='statefulset'] - Workload kind to patch.
537
+ * @param {string} params.name - Workload name.
538
+ * @param {string} params.namespace - Target namespace.
539
+ * @param {string} params.node - Target node name (matches `kubernetes.io/hostname`).
540
+ * @memberof UnderpostCluster
541
+ */
542
+ pinToNode({ kind = 'statefulset', name, namespace, node }) {
543
+ logger.info(`Pinning ${kind}/${name} to node '${node}' (namespace: ${namespace}).`);
544
+ const patch = JSON.stringify({
545
+ spec: { template: { spec: { nodeSelector: { 'kubernetes.io/hostname': node } } } },
546
+ });
547
+ shellExec(`kubectl patch ${kind} ${name} -n ${namespace} --type merge -p '${patch}'`);
548
+ },
549
+
550
+ /**
551
+ * @method resolveServiceHost
552
+ * @description Resolves a shared single-host override used by exposed service clients.
553
+ * @param {object} [options={}] - Cluster options.
554
+ * @returns {string} A single host override, or an empty string when unset / not reusable.
555
+ * @memberof UnderpostCluster
556
+ */
557
+ resolveServiceHost(options = {}) {
558
+ const candidate = `${options.serviceHost || ''}`.trim();
559
+ return candidate && !candidate.includes(',') ? candidate : '';
560
+ },
561
+
562
+ /**
563
+ * @method upsertEnvVar
564
+ * @description Replaces or appends one env var assignment in raw env file text.
565
+ * @param {string} envText - Existing env file contents.
566
+ * @param {string} key - Env var name.
567
+ * @param {string} value - Env var value.
568
+ * @returns {string} Updated env file contents.
569
+ * @memberof UnderpostCluster
570
+ */
571
+ upsertEnvVar(envText, key, value) {
572
+ const nextEntry = `${key}=${value}`;
573
+ const envKeyPattern = new RegExp(`^${key}=.*$`, 'm');
574
+ if (envKeyPattern.test(envText)) return envText.replace(envKeyPattern, nextEntry);
575
+
576
+ const trimmedEnvText = envText.replace(/\s*$/, '');
577
+ return `${trimmedEnvText}${trimmedEnvText ? '\n' : ''}${nextEntry}\n`;
578
+ },
579
+
580
+ /**
581
+ * @method syncServiceConnectionEnv
582
+ * @description Persists exposed service connection hosts to the active deploy env files.
583
+ * Currently applies only to MongoDB (`DB_HOST`) and Valkey (`VALKEY_HOST`).
584
+ * @param {object} params
585
+ * @param {string} params.serviceHost - Shared exposed host/IP.
586
+ * @param {boolean} [params.mongodb=false] - Update MongoDB runtime host.
587
+ * @param {boolean} [params.valkey=false] - Update Valkey runtime host.
588
+ * @param {object} [params.options={}] - Cluster options used to infer the active env.
589
+ * @memberof UnderpostCluster
590
+ */
591
+ syncServiceConnectionEnv({ serviceHost, mongodb = false, valkey = false, options = {} }) {
592
+ if (!serviceHost) return;
593
+
594
+ const updates = {};
595
+ if (mongodb) updates.DB_HOST = `mongodb://${serviceHost}:27017`;
596
+ if (valkey) updates.VALKEY_HOST = serviceHost;
597
+ if (Object.keys(updates).length === 0) return;
598
+
599
+ const deployId = process.env.DEPLOY_ID || process.env.DEFAULT_DEPLOY_ID || 'dd-default';
600
+ const envName = process.env.NODE_ENV || (options.dev ? 'development' : 'production');
601
+ const envPaths = [`./engine-private/conf/${deployId}/.env.${envName}`, `./.env.${envName}`, `./.env`].filter(
602
+ (envPath, index, paths) => fs.existsSync(envPath) && paths.indexOf(envPath) === index,
603
+ );
604
+
605
+ if (envPaths.length === 0) {
606
+ logger.warn(`No env files found to persist service host override for deploy '${deployId}' (${envName}).`);
607
+ return;
608
+ }
609
+
610
+ for (const envPath of envPaths) {
611
+ let envText = fs.readFileSync(envPath, 'utf8');
612
+ for (const [key, value] of Object.entries(updates))
613
+ envText = Underpost.cluster.upsertEnvVar(envText, key, value);
614
+ fs.writeFileSync(envPath, envText, 'utf8');
615
+ }
616
+
617
+ logger.info(`Persisted service host override for ${Object.keys(updates).join(', ')} to ${envPaths.join(', ')}`);
618
+ },
619
+
381
620
  /**
382
621
  * @method pullImage
383
622
  * @description Pulls a container image using the appropriate runtime based on the cluster type.
@@ -398,7 +637,13 @@ EOF
398
637
  `for node in $(kind get nodes); do cat ${tarPath} | docker exec -i $node ctr --namespace=k8s.io images import -; done`,
399
638
  );
400
639
  shellExec(`rm -f ${tarPath}`);
401
- } else if (options.kubeadm || options.k3s) {
640
+ } else if (options.k3s) {
641
+ // K3s uses its own embedded containerd socket, not the host-level one
642
+ // used by kubeadm/containerd installations.
643
+ shellExec(
644
+ `sudo env PATH="$PATH:/usr/local/bin:/usr/bin" crictl --runtime-endpoint unix:///run/k3s/containerd/containerd.sock pull ${image}`,
645
+ );
646
+ } else if (options.kubeadm) {
402
647
  // Kubeadm / K3s: use crictl to pull directly into the active CRI runtime.
403
648
  // crictl is not in sudo's secure_path; pass full PATH through env.
404
649
  // Point crictl at CRI-O when the socket exists, otherwise fall back to containerd.
@@ -419,15 +664,20 @@ EOF
419
664
  * This method ensures proper SELinux, Docker, Containerd, and Sysctl settings
420
665
  * are applied for a healthy Kubernetes environment. It explicitly avoids
421
666
  * iptables flushing commands to prevent conflicts with Kubernetes' own network management.
422
- * @param {string} underpostRoot - The root directory of the underpost project.
667
+ * @param {object} [options] - Configuration options for host setup.
668
+ * @param {string} [options.underpostRoot] - The root path of the underpost project, used for locating scripts if needed.
423
669
  * @memberof UnderpostCluster
424
670
  */
425
671
  config(options = { underpostRoot: '.' }) {
426
672
  const { underpostRoot } = options;
427
673
  console.log('Applying host configuration: SELinux, Docker, Containerd, and Sysctl settings.');
428
674
  // Disable SELinux (permissive mode)
429
- shellExec(`sudo setenforce 0`);
430
- shellExec(`sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config`);
675
+ shellExec(`sudo setenforce 0`, {
676
+ silentOnError: true,
677
+ });
678
+ shellExec(`sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config`, {
679
+ silentOnError: true,
680
+ });
431
681
 
432
682
  // Enable and start Docker and Kubelet services
433
683
  shellExec(`sudo systemctl enable --now docker`); // Docker might not be needed for K3s
@@ -451,32 +701,89 @@ EOF
451
701
  // Reload systemd daemon to pick up new unit files/changes
452
702
  shellExec(`sudo systemctl daemon-reload`);
453
703
 
454
- // Enable bridge-nf-call-iptables for Kubernetes networking
455
- // This ensures traffic through Linux bridges is processed by iptables (crucial for CNI)
456
- for (const iptableConfPath of [
457
- `/etc/sysctl.d/k8s.conf`,
458
- `/etc/sysctl.d/99-k8s-ipforward.conf`,
459
- `/etc/sysctl.d/99-k8s.conf`,
460
- ])
461
- shellExec(
462
- `echo 'net.bridge.bridge-nf-call-iptables = 1
463
- net.bridge.bridge-nf-call-ip6tables = 1
464
- net.bridge.bridge-nf-call-arptables = 1
465
- net.ipv4.ip_forward = 1' | sudo tee ${iptableConfPath}`,
466
- { silent: true },
467
- );
468
-
469
704
  // Increase inotify limits
470
705
  shellExec(`sudo sysctl -w fs.inotify.max_user_watches=2099999999`);
471
706
  shellExec(`sudo sysctl -w fs.inotify.max_user_instances=2099999999`);
472
707
  shellExec(`sudo sysctl -w fs.inotify.max_queued_events=2099999999`);
708
+ },
709
+
710
+ /**
711
+ * @method configMinimalK3s
712
+ * @description Minimal host configuration for a K3s node. K3s is fully
713
+ * self-contained — it ships its own containerd, kubelet, CNI (flannel),
714
+ * CoreDNS and traefik. It therefore needs NONE of the Docker /
715
+ * standalone-containerd / standalone-kubelet setup that `config()` applies
716
+ * for kind and kubeadm. In a fresh LXD VM those packages do not exist, so
717
+ * `config()` there is both redundant and a source of errors.
718
+ *
719
+ * This applies only what K3s genuinely requires, and every step is guarded
720
+ * so it is a no-op when the relevant tooling is absent (e.g. minimal images
721
+ * without SELinux userspace):
722
+ * - SELinux → permissive (only if SELinux tooling is present).
723
+ * - swap off (Kubernetes best practice).
724
+ * - br_netfilter + bridge/forward sysctls (pod networking).
725
+ * - inotify limits.
726
+ * @memberof UnderpostCluster
727
+ */
728
+ configMinimalK3s() {
729
+ console.log('Applying minimal K3s host configuration (firewalld, SELinux, swap, sysctl).');
730
+
731
+ // Disable firewalld. K3s manages its own iptables rules; firewalld
732
+ // closes 6443/tcp (supervisor + API) and 8472/udp (flannel VXLAN) by
733
+ // default on RHEL/Rocky, which makes k3s-agent hang on `systemctl start`
734
+ // forever (the upstream unit ships TimeoutStartSec=0).
735
+ shellExec(`if systemctl is-active --quiet firewalld; then sudo systemctl disable --now firewalld; fi`);
736
+
737
+ // SELinux → permissive, but only when the tooling exists. Rocky has it;
738
+ // minimal LXD images may not. K3s also installs k3s-selinux for enforcing
739
+ // mode, so this is a best-effort dev convenience, not a hard requirement.
740
+ shellExec(`if command -v setenforce >/dev/null 2>&1; then sudo setenforce 0; fi`, {
741
+ silentOnError: true,
742
+ });
743
+ shellExec(
744
+ `if [ -f /etc/selinux/config ]; then sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config; fi`,
745
+ { silentOnError: true },
746
+ );
747
+
748
+ // Disable swap. `swapoff -a` is a no-op without swap; the sed only edits
749
+ // fstab when a swap line is present.
750
+ shellExec(`sudo swapoff -a`);
751
+ shellExec(`sudo sed -i '/swap/d' /etc/fstab`);
752
+
753
+ // Pod networking: ensure br_netfilter is loaded and the bridge/forward
754
+ // sysctls are set. K3s + flannel depend on these.
755
+ shellExec(
756
+ `if command -v lsmod >/dev/null 2>&1 && command -v modprobe >/dev/null 2>&1; then if ! lsmod | grep -q '^br_netfilter'; then sudo modprobe br_netfilter || true; fi; fi`,
757
+ );
758
+ shellExec(
759
+ `echo 'net.bridge.bridge-nf-call-iptables = 1
760
+ net.bridge.bridge-nf-call-ip6tables = 1
761
+ net.ipv4.ip_forward = 1' | sudo tee /etc/sysctl.d/99-k3s.conf > /dev/null`,
762
+ );
763
+ shellExec(`sudo sysctl --system`);
764
+
765
+ // inotify limits — many pods/watchers. Conservative, sane values.
766
+ shellExec(`sudo sysctl -w fs.inotify.max_user_instances=1024`);
767
+ shellExec(`sudo sysctl -w fs.inotify.max_user_watches=1048576`);
768
+ },
473
769
 
474
- // shellExec(`sudo sysctl --system`); // Apply sysctl changes immediately
475
- // Apply NAT iptables rules and configure firewalld for Kubernetes.
476
- // nat-iptables.sh enables firewalld and opens all required ports; do NOT stop it
477
- // afterwards keeping firewalld running with these rules is required for
478
- // multi-machine kubeadm inter-node communication.
479
- shellExec(`${underpostRoot}/scripts/nat-iptables.sh`, { silent: true });
770
+ /**
771
+ * @method natSetup
772
+ * @description Configures NAT and iptables settings for Kubernetes networking.
773
+ * This method enables necessary sysctl settings for bridge networking and applies iptables rules
774
+ * required for Kubernetes cluster communication. It is designed to work with kubeadm and k3s clusters, ensuring that
775
+ * traffic through Linux bridges is processed by iptables, which is crucial for CNI plugins to function correctly.
776
+ * The method also applies NAT iptables rules and configures firewalld for Kubernetes, which is required for multi-machine kubeadm inter-node communication.
777
+ * Note: This method must be called before kubeadm init / kind create so that br_netfilter is loaded and kernel networking is ready when the control plane starts.
778
+ * @param {object} [options] - Configuration options for NAT setup.
779
+ * @param {string} [options.underpostRoot] - The root path of the underpost project, used to locate the nat-iptables.sh script.
780
+ * @memberof UnderpostCluster
781
+ */
782
+ natSetup(options = { underpostRoot: '.' }) {
783
+ const { underpostRoot } = options;
784
+ // Loads br_netfilter, applies bridge/forward sysctls, opens firewall ports, enables masquerade.
785
+ // Must run before kubeadm init / kind create so kernel networking is ready.
786
+ shellExec(`${underpostRoot}/scripts/nat-iptables.sh`);
480
787
  },
481
788
 
482
789
  /**
@@ -513,157 +820,231 @@ net.ipv4.ip_forward = 1' | sudo tee ${iptableConfPath}`,
513
820
  console.log('kubectl config set up successfully.');
514
821
  },
515
822
 
823
+ // Shared reset helpers (internal — used by safeResetKind / safeResetKubeadm).
824
+
516
825
  /**
517
- * @method safeReset
518
- * @description Performs a complete reset of the Kubernetes cluster and its container environments.
519
- * This version focuses on correcting persistent permission errors (such as 'permission denied'
520
- * in coredns) by restoring SELinux security contexts and safely cleaning up cluster artifacts.
521
- * Only the uninstall/delete commands specific to the given clusterType are executed; all other
522
- * cleanup steps (log truncation, filesystem, network) are always run as generic k8s resets.
523
- * @param {object} [options] - Configuration options for the reset.
524
- * @param {string} [options.underpostRoot] - The root path of the underpost project.
525
- * @param {boolean} [options.removeVolumeHostPaths=false] - Whether to remove data from host paths used by Persistent Volumes.
526
- * @param {string} [options.clusterType='kind'] - The type of cluster to reset: 'kind', 'kubeadm', or 'k3s'.
527
- * @memberof UnderpostCluster
826
+ * @method _truncateLargeLogs
827
+ * @description Removes files >1 GiB under /var/log. Best-effort.
828
+ * @private
528
829
  */
529
- async safeReset(options = { underpostRoot: '.', removeVolumeHostPaths: false, clusterType: 'kind' }) {
530
- logger.info('Starting a safe and comprehensive reset of Kubernetes and container environments...');
830
+ _truncateLargeLogs() {
831
+ try {
832
+ const cleanPath = `/var/log/`;
833
+ const largeLogsFiles = shellExec(
834
+ `sudo du -sh ${cleanPath}* | awk '{if ($1 ~ /G$/ && ($1+0) > 1) print}' | sort -rh`,
835
+ { stdout: true },
836
+ );
837
+ for (const pathLog of largeLogsFiles
838
+ .split('\n')
839
+ .map((p) => p.split(cleanPath)[1])
840
+ .filter((p) => p)) {
841
+ shellExec(`sudo rm -rf ${cleanPath}${pathLog}`);
842
+ }
843
+ } catch (err) {
844
+ logger.warn(` -> Skipped log truncation: ${err.message}`);
845
+ }
846
+ },
531
847
 
848
+ /**
849
+ * @method _cleanHostPathPvs
850
+ * @description Wipes contents of every hostPath PV. Destroys live data —
851
+ * only call when `--remove-volume-host-paths` is set.
852
+ * @private
853
+ */
854
+ _cleanHostPathPvs() {
532
855
  try {
533
- // Phase 0: Truncate large logs under /var/log to free up immediate space
534
- logger.info('Phase 0/7: Truncating large log files under /var/log...');
535
- try {
536
- const cleanPath = `/var/log/`;
537
- const largeLogsFiles = shellExec(
538
- `sudo du -sh ${cleanPath}* | awk '{if ($1 ~ /G$/ && ($1+0) > 1) print}' | sort -rh`,
539
- {
540
- stdout: true,
541
- },
542
- );
543
- for (const pathLog of largeLogsFiles
544
- .split(`\n`)
545
- .map((p) => p.split(cleanPath)[1])
546
- .filter((p) => p)) {
547
- shellExec(`sudo rm -rf ${cleanPath}${pathLog}`);
856
+ const pvListJson = shellExec(`kubectl get pv -o json || echo '{"items":[]}'`, {
857
+ stdout: true,
858
+ silent: true,
859
+ });
860
+ const pvList = JSON.parse(pvListJson);
861
+ if (!pvList.items || pvList.items.length === 0) {
862
+ logger.info(' -> No PersistentVolumes with hostPath found.');
863
+ return;
864
+ }
865
+ for (const pv of pvList.items) {
866
+ if (pv.spec.hostPath && pv.spec.hostPath.path) {
867
+ const hostPath = pv.spec.hostPath.path;
868
+ logger.info(` -> Removing PV '${pv.metadata.name}' hostPath: ${hostPath}`);
869
+ shellExec(`sudo rm -rf ${hostPath}/*`);
548
870
  }
549
- } catch (err) {
550
- logger.warn(` -> Error truncating log files: ${err.message}. Continuing with reset.`);
551
871
  }
872
+ } catch (error) {
873
+ logger.error(` -> Failed cleaning hostPath PVs: ${error.message}`);
874
+ }
875
+ },
552
876
 
553
- // Phase 1: Clean up Persistent Volumes with hostPath
554
- // This targets data created by Kubernetes Persistent Volumes that use hostPath.
555
- logger.info('Phase 1/7: Cleaning Kubernetes hostPath volumes...');
556
- if (options.removeVolumeHostPaths)
557
- try {
558
- const pvListJson = shellExec(`kubectl get pv -o json || echo '{"items":[]}'`, {
559
- stdout: true,
560
- silent: true,
561
- });
562
- const pvList = JSON.parse(pvListJson);
563
-
564
- if (pvList.items && pvList.items.length > 0) {
565
- for (const pv of pvList.items) {
566
- // Check if the PV uses hostPath and delete its contents
567
- if (pv.spec.hostPath && pv.spec.hostPath.path) {
568
- const hostPath = pv.spec.hostPath.path;
569
- logger.info(`Removing data from host path for PV '${pv.metadata.name}': ${hostPath}`);
570
- shellExec(`sudo rm -rf ${hostPath}/*`);
571
- }
572
- }
573
- } else {
574
- logger.info('No Persistent Volumes found with hostPath to clean up.');
575
- }
576
- } catch (error) {
577
- logger.error('Failed to clean up Persistent Volumes:', error);
578
- }
579
- else logger.info(' -> Skipping hostPath volume cleanup as per configuration.');
580
- // Phase 2: Restore SELinux and stop services
581
- // This is critical for fixing the 'permission denied' error you experienced.
582
- // Enable SELinux permissive mode and restore file contexts.
583
- logger.info('Phase 2/7: Stopping services and fixing SELinux...');
584
- logger.info(' -> Ensuring SELinux is in permissive mode...');
585
- shellExec(`sudo setenforce 0`);
586
- shellExec(`sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config`);
587
- logger.info(' -> Restoring SELinux contexts for container data directories...');
588
- // The 'restorecon' command corrects file system security contexts.
589
- shellExec(`sudo restorecon -Rv /var/lib/containerd`);
590
- shellExec(`sudo restorecon -Rv /var/lib/kubelet`);
591
-
592
- logger.info(' -> Stopping kubelet, docker, and podman services...');
593
- shellExec('sudo systemctl stop kubelet');
594
- shellExec('sudo systemctl stop docker');
595
- shellExec('sudo systemctl stop podman');
596
- // Lazy-unmount all kubelet pod mounts to avoid 'Device or resource busy' on rm.
597
- shellExec(
598
- `sudo sh -c 'findmnt --raw --noheadings -o TARGET | grep /var/lib/kubelet | sort -r | xargs -r umount -l' 2>/dev/null || true`,
599
- );
877
+ /**
878
+ * @method _lazyUmountKubeletMounts
879
+ * @description Lazy-unmounts every mount under /var/lib/kubelet so a
880
+ * subsequent `rm -rf` does not hit 'Device or resource busy'. Best-effort.
881
+ * @private
882
+ */
883
+ _lazyUmountKubeletMounts() {
884
+ shellExec(
885
+ `sudo sh -c 'findmnt --raw --noheadings -o TARGET | grep /var/lib/kubelet | sort -r | xargs -r umount -l'`,
886
+ { silentOnError: true },
887
+ );
888
+ },
600
889
 
601
- // Phase 3: Execute official uninstallation commands (type-specific)
602
- const clusterType = options.clusterType || 'kind';
603
- logger.info(
604
- `Phase 3/7: Executing official reset/uninstallation commands for cluster type: '${clusterType}'...`,
605
- );
606
- if (clusterType === 'kubeadm') {
607
- // Kill control plane processes that hold ports (6443, 10257, 10259, 2379, 2380)
608
- // so the next `kubeadm init` does not fail with [ERROR Port-xxxx].
609
- logger.info(' -> Stopping and killing control plane containers and processes...');
610
- shellExec('sudo crictl rm -a -f 2>/dev/null || true');
611
- shellExec('sudo crictl rmp -a -f 2>/dev/null || true');
612
- shellExec('sudo systemctl stop etcd 2>/dev/null || true');
613
- for (const port of [6443, 10259, 10257, 2379, 2380])
614
- shellExec(`sudo fuser -k ${port}/tcp 2>/dev/null || true`);
615
- logger.info(' -> Executing kubeadm reset...');
616
- shellExec('sudo kubeadm reset --force');
617
- } else if (clusterType === 'k3s') {
618
- logger.info(' -> Executing K3s uninstallation script if it exists...');
619
- shellExec('sudo /usr/local/bin/k3s-uninstall.sh');
620
- } else {
621
- // Default: kind
622
- logger.info(' -> Deleting Kind clusters...');
623
- shellExec('kind get clusters | xargs -r -t -n1 kind delete cluster');
624
- }
890
+ // Per-type reset methods. Each only touches what its own runtime owns.
625
891
 
626
- // Phase 4: File system cleanup
627
- logger.info('Phase 4/7: Cleaning up remaining file system artifacts...');
628
- // Remove any leftover configurations and data.
629
- shellExec('sudo rm -rf /etc/kubernetes/*');
630
- shellExec('sudo rm -rf /etc/cni/net.d/*');
631
- // Second-pass lazy umount before rm to clear any remaining busy mounts.
632
- shellExec(
633
- `sudo sh -c 'findmnt --raw --noheadings -o TARGET | grep /var/lib/kubelet | sort -r | xargs -r umount -l' 2>/dev/null || true`,
634
- );
635
- shellExec('sudo rm -rf /var/lib/kubelet/*');
636
- shellExec('sudo rm -rf /var/lib/etcd');
637
- shellExec('sudo rm -rf /var/lib/cni/*');
638
- shellExec('sudo rm -rf /var/lib/docker/*');
639
- shellExec('sudo rm -rf /var/lib/containerd/*');
640
- shellExec('sudo rm -rf /var/lib/containers/storage/*');
641
- // Clean up the current user's kubeconfig.
642
- shellExec('rm -rf $HOME/.kube');
643
-
644
- // Phase 5: Host network cleanup
645
- logger.info('Phase 5/7: Cleaning up host network configurations...');
646
- // Remove iptables rules and CNI network interfaces.
647
- shellExec('sudo iptables -F');
648
- shellExec('sudo iptables -t nat -F');
649
- shellExec('sudo ip link del cni0 2>/dev/null || true');
650
- shellExec('sudo ip link del flannel.1 2>/dev/null || true');
651
- shellExec('sudo ip link del vxlan.calico 2>/dev/null || true');
652
- shellExec('sudo ip link del tunl0 2>/dev/null || true');
653
-
654
- logger.info('Phase 6/7: Clean up images');
655
- shellExec('sudo podman rmi --all --force 2>/dev/null || true');
656
- shellExec('sudo crictl rmi --prune 2>/dev/null || true');
657
-
658
- // Phase 6: Reload daemon and finalize
659
- logger.info('Phase 7/7: Reloading the system daemon and finalizing...');
660
- // shellExec('sudo systemctl daemon-reload');
661
- Underpost.cluster.config();
662
- logger.info('Safe and complete reset finished. The system is ready for a new cluster initialization.');
663
- } catch (error) {
664
- logger.error(`Error during reset: ${error.message}`);
665
- console.error(error);
892
+ /**
893
+ * @method safeResetKind
894
+ * @description Kind (Kubernetes in Docker) reset — Docker-scoped only.
895
+ * Does not touch host kubelet / containerd / iptables / SELinux.
896
+ * @param {object} [options]
897
+ * @param {string} [options.underpostRoot='.']
898
+ * @param {boolean} [options.removeVolumeHostPaths=false]
899
+ * @memberof UnderpostCluster
900
+ */
901
+ async safeResetKind(options = { underpostRoot: '.', removeVolumeHostPaths: false }) {
902
+ logger.info('=== KIND SAFE RESET (development) ===');
903
+
904
+ logger.info('Phase 1/5: Cleaning Kind node-local MongoDB hostPath directories...');
905
+ Underpost.cluster.cleanKindMongoHostPaths({ basePath: '/data/mongodb', replicaCount: 3 });
906
+
907
+ logger.info('Phase 2/5: PersistentVolume hostPath cleanup...');
908
+ if (options.removeVolumeHostPaths) Underpost.cluster._cleanHostPathPvs();
909
+ else logger.info(' -> Skipping (pass --remove-volume-host-paths to enable).');
910
+
911
+ logger.info('Phase 3/5: Deleting all Kind clusters...');
912
+ shellExec(`clusters=$(kind get clusters)
913
+ if [ -n "$clusters" ]; then
914
+ for c in $clusters; do
915
+ echo "Deleting cluster: $c"
916
+ kind delete cluster --name "$c"
917
+ done
918
+ fi`);
919
+
920
+ logger.info('Phase 4/5: Cleaning kubeconfig and Kind Docker networks...');
921
+ shellExec(`rm -rf "$HOME/.kube"`);
922
+ Underpost.cluster.recoverKindDockerNetworks();
923
+
924
+ logger.info('Phase 5/5: Re-applying host configuration (Docker, containerd, sysctl).');
925
+ Underpost.cluster.config();
926
+
927
+ logger.info('=== KIND SAFE RESET COMPLETE ===');
928
+ },
929
+
930
+ /**
931
+ * @method safeResetKubeadm
932
+ * @description Kubeadm reset on the host: stop kubelet + runtime, kill
933
+ * control-plane ports (6443 / 2379 / 2380 / 10257 / 10259), run
934
+ * `kubeadm reset --force`, wipe kubeadm-managed FS + network state.
935
+ * Does not touch K3s or Kind state.
936
+ * @param {object} [options]
937
+ * @param {string} [options.underpostRoot='.']
938
+ * @param {boolean} [options.removeVolumeHostPaths=false]
939
+ * @memberof UnderpostCluster
940
+ */
941
+ async safeResetKubeadm(options = { underpostRoot: '.', removeVolumeHostPaths: false }) {
942
+ logger.info('=== KUBEADM SAFE RESET ===');
943
+
944
+ logger.info('Phase 0/7: Truncating large /var/log files...');
945
+ Underpost.cluster._truncateLargeLogs();
946
+
947
+ logger.info('Phase 1/7: PersistentVolume hostPath cleanup...');
948
+ if (options.removeVolumeHostPaths) Underpost.cluster._cleanHostPathPvs();
949
+ else logger.info(' -> Skipping (pass --remove-volume-host-paths to enable).');
950
+
951
+ logger.info('Phase 2/7: SELinux permissive + restore contexts (when present)...');
952
+ shellExec(`if command -v setenforce >/dev/null 2>&1; then sudo setenforce 0; fi`);
953
+ shellExec(
954
+ `if [ -f /etc/selinux/config ]; then sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config; fi`,
955
+ );
956
+ shellExec(
957
+ `if command -v restorecon >/dev/null 2>&1 && [ -d /var/lib/kubelet ]; then sudo restorecon -Rv /var/lib/kubelet; fi`,
958
+ );
959
+ shellExec(
960
+ `if command -v restorecon >/dev/null 2>&1 && [ -d /var/lib/containerd ]; then sudo restorecon -Rv /var/lib/containerd; fi`,
961
+ );
962
+
963
+ logger.info('Phase 3/7: Stopping host kubelet and container runtimes (kubeadm-scope only)...');
964
+ shellExec(`if systemctl is-active --quiet kubelet; then sudo systemctl stop kubelet; fi`);
965
+ shellExec(`if systemctl is-active --quiet docker; then sudo systemctl stop docker; fi`);
966
+ shellExec(`if systemctl is-active --quiet crio; then sudo systemctl stop crio; fi`);
967
+ Underpost.cluster._lazyUmountKubeletMounts();
968
+
969
+ logger.info('Phase 4/7: Killing control-plane processes and running kubeadm reset...');
970
+ shellExec(`if command -v crictl >/dev/null 2>&1; then sudo crictl rm -a -f; fi`, { silentOnError: true });
971
+ // Remove CNI config before stopping sandboxes so Calico's CNI delete hook is
972
+ // not invoked (the API server is already down and the hook would fail).
973
+ shellExec(`sudo rm -rf /etc/cni/net.d/*`);
974
+ shellExec(`if command -v crictl >/dev/null 2>&1; then sudo crictl rmp -a -f; fi`, { silentOnError: true });
975
+ shellExec(`if systemctl is-active --quiet etcd; then sudo systemctl stop etcd; fi`);
976
+ for (const port of [6443, 10259, 10257, 2379, 2380]) {
977
+ shellExec(`if sudo fuser ${port}/tcp >/dev/null 2>&1; then sudo fuser -k ${port}/tcp; fi`);
666
978
  }
979
+ shellExec(`if command -v kubeadm >/dev/null 2>&1; then sudo kubeadm reset --force; fi`);
980
+
981
+ logger.info('Phase 5/7: Filesystem cleanup (kubeadm-managed paths only)...');
982
+ shellExec(`sudo rm -rf /etc/kubernetes/*`);
983
+ Underpost.cluster._lazyUmountKubeletMounts();
984
+ shellExec(`sudo rm -rf /var/lib/kubelet/*`);
985
+ shellExec(`sudo rm -rf /var/lib/etcd`);
986
+ shellExec(`sudo rm -rf /var/lib/cni/*`);
987
+ shellExec(`sudo rm -rf /var/lib/containerd/*`);
988
+ shellExec(`rm -rf "$HOME/.kube"`);
989
+
990
+ logger.info('Phase 6/7: Network cleanup (Calico interfaces + host iptables)...');
991
+ shellExec(`if ip link show cni0 >/dev/null 2>&1; then sudo ip link del cni0; fi`);
992
+ shellExec(`if ip link show vxlan.calico >/dev/null 2>&1; then sudo ip link del vxlan.calico; fi`);
993
+ shellExec(`if ip link show tunl0 >/dev/null 2>&1; then sudo ip link del tunl0; fi`);
994
+ shellExec(`sudo iptables -F`);
995
+ shellExec(`sudo iptables -t nat -F`);
996
+ shellExec(`if command -v crictl >/dev/null 2>&1; then sudo crictl rmi --prune; fi`);
997
+
998
+ logger.info('Phase 7/7: Re-applying host configuration (Docker, containerd, sysctl).');
999
+ Underpost.cluster.config();
1000
+
1001
+ logger.info('=== KUBEADM SAFE RESET COMPLETE ===');
1002
+ },
1003
+
1004
+ /**
1005
+ * @method safeResetK3s
1006
+ * @description Centralized K3s teardown. Runs the same way on a physical
1007
+ * host (`node bin cluster --dev --reset --k3s`) or inside an LXD VM via
1008
+ * `lxc exec` (driven by `_resetK3sInVm` in src/cli/lxd.js).
1009
+ * @param {object} [options]
1010
+ * @param {string} [options.underpostRoot='.']
1011
+ * @param {'drain'|'full'} [options.resetMode='full'] - `drain` stops
1012
+ * services + runs `k3s-killall.sh` (K3s persists, returns on next boot).
1013
+ * `full` also runs `k3s-uninstall.sh` and cleans residual state.
1014
+ * @memberof UnderpostCluster
1015
+ */
1016
+ async safeResetK3s(options = { underpostRoot: '.', resetMode: 'full' }) {
1017
+ const resetMode = options.resetMode === 'drain' ? 'drain' : 'full';
1018
+ logger.info(`=== K3s SAFE RESET (resetMode=${resetMode}) ===`);
1019
+
1020
+ logger.info('Phase 1/5: Stopping K3s systemd units...');
1021
+ shellExec(`if systemctl list-unit-files | grep -q '^k3s\\.service'; then sudo systemctl stop k3s; fi`);
1022
+ shellExec(
1023
+ `if systemctl list-unit-files | grep -q '^k3s-agent\\.service'; then sudo systemctl stop k3s-agent; fi`,
1024
+ );
1025
+
1026
+ logger.info('Phase 2/5: Running k3s-killall.sh (unmount pod overlays, tear down CNI)...');
1027
+ shellExec(`if [ -x /usr/local/bin/k3s-killall.sh ]; then sudo /usr/local/bin/k3s-killall.sh; fi`);
1028
+
1029
+ if (resetMode === 'drain') {
1030
+ logger.info('=== K3s DRAIN COMPLETE (K3s remains installed; will start on next boot) ===');
1031
+ return;
1032
+ }
1033
+
1034
+ logger.info('Phase 3/5: Running k3s-uninstall.sh...');
1035
+ shellExec(`if [ -x /usr/local/bin/k3s-uninstall.sh ]; then sudo /usr/local/bin/k3s-uninstall.sh; fi`);
1036
+ shellExec(`if [ -x /usr/local/bin/k3s-agent-uninstall.sh ]; then sudo /usr/local/bin/k3s-agent-uninstall.sh; fi`);
1037
+
1038
+ logger.info('Phase 4/5: Removing residual K3s state...');
1039
+ shellExec(`rm -rf "$HOME/.kube"`);
1040
+ shellExec(`if [ -d /etc/rancher/k3s ]; then sudo rm -rf /etc/rancher/k3s; fi`);
1041
+ shellExec(`if ip link show flannel.1 >/dev/null 2>&1; then sudo ip link del flannel.1; fi`);
1042
+ shellExec(`if ip link show cni0 >/dev/null 2>&1; then sudo ip link del cni0; fi`);
1043
+
1044
+ logger.info('Phase 5/5: Re-applying minimal K3s host config.');
1045
+ Underpost.cluster.configMinimalK3s();
1046
+
1047
+ logger.info('=== K3s SAFE RESET COMPLETE (full) ===');
667
1048
  },
668
1049
 
669
1050
  /**
@@ -785,8 +1166,8 @@ EOF`);
785
1166
 
786
1167
  // Remove CRI-O
787
1168
  console.log('Removing CRI-O...');
788
- shellExec(`sudo systemctl stop crio 2>/dev/null || true`);
789
- shellExec(`sudo systemctl disable crio 2>/dev/null || true`);
1169
+ shellExec('sudo systemctl stop crio', { silentOnError: true });
1170
+ shellExec('sudo systemctl disable crio', { silentOnError: true });
790
1171
  shellExec(`sudo dnf -y remove cri-o`);
791
1172
  shellExec(`sudo rm -f /etc/yum.repos.d/cri-o.repo`);
792
1173
  shellExec(`sudo rm -f /etc/crictl.yaml`);
@@ -827,6 +1208,93 @@ EOF`);
827
1208
 
828
1209
  console.log('Uninstall process completed.');
829
1210
  },
1211
+
1212
+ /**
1213
+ * @method cleanKindMongoHostPaths
1214
+ * @description Best-effort cleanup of MongoDB hostPath directories inside Kind node containers.
1215
+ * This prevents stale replica/auth state when hostPath data lives in node-local container filesystems.
1216
+ * @param {object} [options]
1217
+ * @param {string} [options.basePath='/data/mongodb'] - Node-internal base path for MongoDB data.
1218
+ * @param {number} [options.replicaCount=3] - Number of replica ordinal directories (v0..vN-1).
1219
+ * @memberof UnderpostCluster
1220
+ */
1221
+ cleanKindMongoHostPaths(options = { basePath: '/data/mongodb', replicaCount: 3 }) {
1222
+ const basePath = options.basePath || '/data/mongodb';
1223
+ const replicaCount = Math.max(Number(options.replicaCount) || 3, 1);
1224
+ const nodesRaw = shellExec('kind get nodes', {
1225
+ stdout: true,
1226
+ silent: true,
1227
+ silentOnError: true,
1228
+ });
1229
+ const nodes = nodesRaw
1230
+ .split('\n')
1231
+ .map((node) => node.trim())
1232
+ .filter((node) => !!node);
1233
+
1234
+ if (nodes.length === 0) {
1235
+ logger.info('No Kind nodes detected for node-local MongoDB hostPath cleanup.');
1236
+ return;
1237
+ }
1238
+
1239
+ for (const node of nodes) {
1240
+ logger.info(
1241
+ `Cleaning Kind node-local MongoDB paths '${basePath}/v0..v${replicaCount - 1}' on node '${node}'...`,
1242
+ );
1243
+ const prepareReplicaDirsCmd = Array.from(
1244
+ { length: replicaCount },
1245
+ (_, index) => `mkdir -p ${basePath}/v${index}; rm -rf ${basePath}/v${index}/*;`,
1246
+ ).join(' ');
1247
+ const verifyReplicaDirsCmd = Array.from(
1248
+ { length: replicaCount },
1249
+ (_, index) => `test -d ${basePath}/v${index};`,
1250
+ ).join(' ');
1251
+ shellExec(`sudo docker exec ${node} sh -lc 'mkdir -p ${basePath}; ${prepareReplicaDirsCmd}'`, {
1252
+ silentOnError: true,
1253
+ });
1254
+ shellExec(`sudo docker exec ${node} sh -lc '${verifyReplicaDirsCmd}'`);
1255
+ }
1256
+ },
1257
+
1258
+ /**
1259
+ * @method recoverKindDockerNetworks
1260
+ * @description Best-effort cleanup of stale Kind Docker resources when Docker bridge
1261
+ * address pools are exhausted and new networks cannot be allocated.
1262
+ * @memberof UnderpostCluster
1263
+ */
1264
+ recoverKindDockerNetworks() {
1265
+ logger.warn('Attempting Docker network recovery for Kind (address pool exhaustion detected)...');
1266
+ shellExec(`sudo docker ps -aq --filter label=io.x-k8s.kind.cluster | xargs -r sudo docker rm -f`, {
1267
+ silentOnError: true,
1268
+ });
1269
+ shellExec(`sudo docker network ls -q --filter label=io.x-k8s.kind.cluster | xargs -r sudo docker network rm`, {
1270
+ silentOnError: true,
1271
+ });
1272
+ shellExec(`sudo docker network rm kind`, { silentOnError: true });
1273
+ shellExec(`sudo docker network prune -f`, { silentOnError: true });
1274
+ },
1275
+
1276
+ /**
1277
+ * @method ensureDockerDefaultAddressPools
1278
+ * @description Writes a sane Docker default-address-pools config to reduce
1279
+ * Kind network allocation failures on hosts with exhausted predefined pools.
1280
+ * @memberof UnderpostCluster
1281
+ */
1282
+ ensureDockerDefaultAddressPools() {
1283
+ logger.warn('Applying Docker default-address-pools workaround for Kind network creation...');
1284
+ shellExec(`cat <<'EOF' | sudo tee /etc/docker/daemon.json
1285
+ {
1286
+ "default-address-pools": [
1287
+ {"base": "172.17.0.0/16", "size": 24},
1288
+ {"base": "172.18.0.0/16", "size": 24},
1289
+ {"base": "172.19.0.0/16", "size": 24},
1290
+ {"base": "172.20.0.0/14", "size": 24},
1291
+ {"base": "172.24.0.0/14", "size": 24}
1292
+ ]
1293
+ }
1294
+ EOF`);
1295
+ shellExec('sudo systemctl restart docker');
1296
+ shellExec('sudo docker network prune -f', { silentOnError: true });
1297
+ },
830
1298
  };
831
1299
  }
832
1300
  export default UnderpostCluster;