@toa.io/operations 1.0.0-alpha.34 → 1.0.0-alpha.340

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 (78) hide show
  1. package/CHANGELOG.md +449 -0
  2. package/image/runtime.Dockerfile +20 -0
  3. package/package.json +10 -10
  4. package/readme.md +224 -0
  5. package/src/deployment/.deployment/.describe/components.js +1 -5
  6. package/src/deployment/.deployment/.describe/compositions.js +26 -7
  7. package/src/deployment/.deployment/.describe/dependencies.js +1 -6
  8. package/src/deployment/.deployment/.describe/events.js +63 -0
  9. package/src/deployment/.deployment/.describe/fold.js +42 -0
  10. package/src/deployment/.deployment/.describe/fold.test.js +108 -0
  11. package/src/deployment/.deployment/.describe/index.js +4 -13
  12. package/src/deployment/.deployment/.describe/mounts.js +17 -0
  13. package/src/deployment/.deployment/.describe/resources.js +129 -0
  14. package/src/deployment/.deployment/.describe/resources.test.js +129 -0
  15. package/src/deployment/.deployment/.describe/services.js +32 -6
  16. package/src/deployment/.deployment/.describe/services.test.js +78 -0
  17. package/src/deployment/.deployment/.describe/variables.js +5 -9
  18. package/src/deployment/.deployment/declare.js +1 -5
  19. package/src/deployment/.deployment/describe.js +168 -10
  20. package/src/deployment/.deployment/index.js +3 -9
  21. package/src/deployment/.deployment/merge.js +66 -6
  22. package/src/deployment/.deployment/merge.test.js +133 -0
  23. package/src/deployment/chart/templates/components.configmap.yaml +13 -0
  24. package/src/deployment/chart/templates/components.yaml +4 -3
  25. package/src/deployment/chart/templates/compositions.yaml +109 -4
  26. package/src/deployment/chart/templates/mono.yaml +199 -0
  27. package/src/deployment/chart/templates/services.yaml +97 -7
  28. package/src/deployment/chart/values.yaml +23 -1
  29. package/src/deployment/composition.js +8 -6
  30. package/src/deployment/deployment.js +175 -35
  31. package/src/deployment/drain.js +70 -0
  32. package/src/deployment/drain.test.js +64 -0
  33. package/src/deployment/factory.js +178 -37
  34. package/src/deployment/images/bundle.js +93 -0
  35. package/src/deployment/images/composition.Dockerfile +9 -17
  36. package/src/deployment/images/composition.js +9 -56
  37. package/src/deployment/images/dependencies.Dockerfile +23 -0
  38. package/src/deployment/images/dependencies.js +171 -0
  39. package/src/deployment/images/dependencies.test.js +271 -0
  40. package/src/deployment/images/factory.js +31 -15
  41. package/src/deployment/images/format.js +66 -0
  42. package/src/deployment/images/format.test.js +150 -0
  43. package/src/deployment/images/image.fixtures.js +17 -19
  44. package/src/deployment/images/image.js +79 -38
  45. package/src/deployment/images/image.test.js +11 -5
  46. package/src/deployment/images/index.js +1 -5
  47. package/src/deployment/images/mono.Dockerfile +11 -0
  48. package/src/deployment/images/mono.js +15 -0
  49. package/src/deployment/images/publish.js +91 -0
  50. package/src/deployment/images/runtime-base.test.js +97 -0
  51. package/src/deployment/images/service.Dockerfile +17 -5
  52. package/src/deployment/images/service.js +63 -14
  53. package/src/deployment/index.js +1 -5
  54. package/src/deployment/operator.js +25 -16
  55. package/src/deployment/operator.test.js +98 -7
  56. package/src/deployment/registry.js +210 -46
  57. package/src/deployment/registry.test.js +539 -0
  58. package/src/deployment/secrets.js +92 -0
  59. package/src/deployment/secrets.test.js +180 -0
  60. package/src/deployment/service.js +4 -6
  61. package/src/deployment/workspace.js +13 -8
  62. package/src/index.js +3 -2
  63. package/src/process.js +51 -13
  64. package/src/process.test.js +33 -0
  65. package/types/_deployment/composition.d.ts +2 -3
  66. package/types/_deployment/dependency.d.ts +15 -7
  67. package/types/_deployment/deployment.d.ts +10 -8
  68. package/types/_deployment/factory.d.ts +2 -4
  69. package/types/_deployment/images/factory.d.ts +6 -8
  70. package/types/_deployment/images/image.d.ts +16 -1
  71. package/types/_deployment/images/registry.d.ts +8 -11
  72. package/types/_deployment/operator.d.ts +10 -9
  73. package/types/_deployment/registry.d.ts +7 -4
  74. package/types/_deployment/service.d.ts +4 -3
  75. package/types/dependency.ts +79 -0
  76. package/types/index.ts +1 -0
  77. package/types/dependency.d.ts +0 -39
  78. package/types/index.d.ts +0 -1
package/readme.md CHANGED
@@ -1,9 +1,188 @@
1
1
  # Toa Operations
2
2
 
3
+ ## Installing
4
+
5
+ `toa deploy`, `toa build`, `toa push`, `toa env`, `toa export` and `toa conceal` need this package
6
+ beside the CLI. The runtime does not depend on it, so an application that deploys lists it with the
7
+ runtime:
8
+
9
+ ```shell
10
+ $ npm i -D @toa.io/runtime @toa.io/operations
11
+ ```
12
+
13
+ A machine that only deploys needs no runtime and no extension:
14
+
15
+ ```shell
16
+ $ npm i @toa.io/cli @toa.io/operations
17
+ $ npx toa deploy production -p application
18
+ ```
19
+
20
+ What the extensions declare is read from `@toa.io/definitions`, which the CLI brings; what they run
21
+ is not needed to render a chart. `registry.services: build` is the exception: it builds an
22
+ extension's service image from the installed package, so it needs the runtime installed beside
23
+ this one. `published` does not.
24
+
25
+ ## Compositions
26
+
27
+ A composition is deployed as one pod. Beside its components it may run extension services,
28
+ which are otherwise each deployed on their own:
29
+
30
+ ```yaml
31
+ # context.toa.yaml
32
+
33
+ compositions:
34
+ - name: edge
35
+ components:
36
+ - todos.tasks
37
+ services:
38
+ - exposition
39
+ ```
40
+
41
+ See [compositions](../documentation/compositions.md).
42
+
43
+ ## Evicted
44
+
45
+ What Toa does not deploy for a context, whatever a composition lists. It stays part of the
46
+ context, and is deployed by other means:
47
+
48
+ ```yaml
49
+ # context.toa.yaml
50
+
51
+ evicted:
52
+ components:
53
+ - todos.stats
54
+ services:
55
+ - exposition
56
+ ```
57
+
58
+ Nothing an evicted component alone required is deployed either — its storage, the extensions it
59
+ declared. `evicted@production` states it for one environment.
60
+
61
+ See [compositions](../documentation/compositions.md#evicted).
62
+
63
+ ## Secrets
64
+
65
+ A value that must not be in the chart is rendered as a reference to a Kubernetes secret, and the
66
+ secret is the application's to deploy. A deploy reads the namespace it deploys into before it builds
67
+ anything, and refuses where a key a workload would read is not there:
68
+
69
+ ```
70
+ $ toa deploy production
71
+ Secrets are not deployed: toa-mongodb.default/username, toa-mongodb.default/password
72
+ ```
73
+
74
+ Required is what `toa export secrets` lists, less the keys it marks `(optional)`, plus the
75
+ `registry.credentials` secret where a context names one. `toa deploy --dry` reads no cluster, and
76
+ neither does anything else that reads a context: `toa env`, `toa export`, `toa build` and `toa push`.
77
+
78
+ See [variables and secrets](../documentation/deployment.md#variables-and-secrets).
79
+
3
80
  ## Context
4
81
 
5
82
  ### Container Registry
6
83
 
84
+ Deploy images default to `FROM ghcr.io/toa-io/runtime:<runtime.version>`
85
+ (published with each Toa release). The base image already has `@toa.io/runtime`
86
+ installed; composition/service Dockerfiles only install component dependencies.
87
+
88
+ To use a custom base image, set `registry.build.image` (or `composition.image`).
89
+ That image must provide the `toa` CLI, or install it via `registry.build.run`. The binary is
90
+ `@toa.io/cli`'s, which the runtime depends on, so a local install puts it on the `PATH`:
91
+
92
+ ```yaml
93
+ # context.toa.yaml
94
+
95
+ registry:
96
+ build:
97
+ image: node:24.22.1-alpine3.24
98
+ run: |
99
+ npm i --prefix /toa @toa.io/runtime --omit=dev
100
+ ln -s /toa/node_modules/.bin/toa /usr/local/bin/toa
101
+ ```
102
+
103
+ `/toa` is where it goes: what an extension needs for what a component declares is installed
104
+ there, beside the extension that reads the declaration.
105
+
106
+ #### Composition Images
107
+
108
+ A composition is two images in one repository. `composition-<name>:deps-<hash>` is what its
109
+ components depend on, installed on the base image: it is tagged by everything the install
110
+ reads — the runtime version, the base image, the build options, each component's
111
+ `package.json` (and `package-lock.json`, where there is one), and what the extensions install
112
+ for what the components declare — and is built only when one of those changes. `composition-<name>:<hash>` is the sources laid over it in a single linked
113
+ layer, so a deploy that changes code alone builds and pushes that layer, and neither
114
+ downloads nor uploads the dependencies again. The same holds for `mono`.
115
+
116
+ The sources are copied as files, so a component that links a directory in — sources it shares with
117
+ another component — carries what the link points to, and a link the build cannot read fails the
118
+ build. What is linked is source alone: dependencies are installed per component, from its own
119
+ `package.json`.
120
+
121
+ Beside each component's sources the layer carries `manifest.toa.json`, the manifest as this
122
+ build normalised it. A composition reads it instead of normalising again, so it loads no
123
+ bridge to read what a module declares; a workspace has no such file and is read as it always
124
+ was. A component an extension ships has none either — its package is published long before any
125
+ image is built — and is read from the digest `@toa.io/definitions` carries, which a deploy has
126
+ always read. So nothing that runs reads a module's source, and `oxc-parser` is installed only
127
+ where a manifest is normalised: a workspace and a deploy, both through `@toa.io/operations`. What a build cannot express there — a prototype in a directory of the application's own
128
+ rather than in a package — fails the build, where before it failed the container.
129
+
130
+ A pushed image builds on the `toa` container builder, whose registry exporter is what lays
131
+ an image over a base it never pulled; `toa build` loads on the daemon's own.
132
+
133
+ A dependency named by a moving git ref is installed when the dependencies image is built and
134
+ not again until its manifest changes, so pin such a dependency to a commit and bump it there.
135
+
136
+ An extension's heavy dependency is not installed with the extension: `@toa.io/extensions.storages`
137
+ declares the AWS and Cloudinary SDKs as optional peers, so the base image carries neither. What a
138
+ component declares is what a deploy installs — the storages named in its `manifest.toa.yaml`,
139
+ resolved through the context's annotation to their providers' packages, installed into `/toa`
140
+ beside the extension that reads them. A composition whose components declare no `s3` storage
141
+ carries no AWS SDK, whatever the context declares for another composition. A workspace installs
142
+ the same set with `toa npm`.
143
+
144
+ #### Extension Service Images
145
+
146
+ `registry.services` says where an extension service's image comes from.
147
+
148
+ `build`, the default, builds one per service into `registry.base`. Its tag carries the
149
+ runtime version, so every Toa release rebuilds them all.
150
+
151
+ `published` deploys the image the extension ships and builds nothing:
152
+
153
+ ```yaml
154
+ # context.toa.yaml
155
+
156
+ registry:
157
+ services: published
158
+ ```
159
+
160
+ The images are tagged with the runtime version:
161
+
162
+ ```
163
+ ghcr.io/toa-io/extension-exposition-gateway:1.0.0-alpha.285
164
+ ghcr.io/toa-io/extension-introspection-explorer:1.0.0-alpha.285
165
+ ghcr.io/toa-io/extension-configuration-values:1.0.0-alpha.285
166
+ ```
167
+
168
+ The cluster pulls them from `ghcr.io` rather than from `registry.base`. They are public and
169
+ `registry.credentials` does not apply; a cluster that cannot reach `ghcr.io` needs `build`.
170
+
171
+ The field is environment-scoped like the rest:
172
+
173
+ ```yaml
174
+ # context.toa.yaml
175
+
176
+ registry:
177
+ base@local: localhost:5000
178
+ base@production: registry.digitalocean.com/acme
179
+ services: published
180
+ services@local: build
181
+ ```
182
+
183
+ An extension that states no image is built whatever this says, as is a service a
184
+ composition runs in its own pod.
185
+
7
186
  #### Build Options
8
187
 
9
188
  ```yaml
@@ -40,3 +219,48 @@ a secret containing required credentials can be specified using `registry.creden
40
219
  registry:
41
220
  credentials: docker-credentials-secret-name
42
221
  ```
222
+
223
+ The secret itself is not Toa's to create, and a deploy that does not find it in the cluster is
224
+ [refused](#secrets).
225
+
226
+ #### Toa images
227
+
228
+ Each release publishes `ghcr.io/toa-io/runtime:<version>` and, beside it, `:alpha` or `:latest`.
229
+ The extension services share that version:
230
+
231
+ ```
232
+ ghcr.io/toa-io/extension-exposition-gateway:1.0.0-alpha.285
233
+ ghcr.io/toa-io/extension-introspection-explorer:1.0.0-alpha.285
234
+ ghcr.io/toa-io/extension-configuration-values:1.0.0-alpha.285
235
+ ```
236
+
237
+ Untagged manifests are deleted. A prerelease older than 90 days, and not among the last 30, is
238
+ deleted with the four service images of that version. `:alpha`, `:latest`, and a version without
239
+ a pre-release suffix stay.
240
+
241
+ An application still on a deleted prerelease cannot pull `registry.services: published` and cannot
242
+ rebuild `FROM` that runtime. Images already in `registry.base` keep running. Stay inside the
243
+ window, or pin a graduated release.
244
+
245
+ #### Userspace images
246
+
247
+ `toa deploy production` writes two tags on the same composition image (`mono` and a service
248
+ `registry.services: build` built are the same):
249
+
250
+ - `<name>:<hash>` — what the chart pins. A hash of the runtime and the sources. `toa push` writes
251
+ this tag and nothing else.
252
+ - `<name>:production` — moved onto that image. The next production deploy moves it again.
253
+
254
+ `deps-<hash>` is not tagged with the environment. Do not name an environment eight hex digits, or
255
+ `deps-` plus eight: a retention job would treat it as a content tag.
256
+
257
+ A job the application runs deletes every `<hash>` and `deps-<hash>` older than 7 days, except the
258
+ last 5, except the image `:production` (or `:staging`, or `:latest`) currently names. Then:
259
+
260
+ ```shell
261
+ $ doctl registries garbage-collection start --include-untagged-manifests <registry>
262
+ ```
263
+
264
+ Rollback is only those 7 days, those last 5, and whatever the environment tag still names. Turn
265
+ the job on after the first deploy that writes that tag. The first garbage collection returns the
266
+ space the old tags held, and blocks pushes while it runs.
@@ -1,10 +1,8 @@
1
- 'use strict'
2
-
3
1
  /**
4
2
  * @param {toa.deployment.Composition[]} compositions
5
3
  * @returns {string[]}
6
4
  */
7
- const components = (compositions) => {
5
+ export const components = (compositions) => {
8
6
  /** @type {Set<string>} */
9
7
  const components = new Set()
10
8
 
@@ -16,5 +14,3 @@ const components = (compositions) => {
16
14
 
17
15
  return Array.from(components)
18
16
  }
19
-
20
- exports.components = components
@@ -1,11 +1,30 @@
1
- 'use strict'
1
+ import { addVariables } from './variables.js'
2
+ import { addMounts } from './mounts.js'
3
+ import { fold } from './fold.js'
2
4
 
3
- const { addVariables } = require('./variables')
4
-
5
- function compositions (compositions, variables) {
5
+ export function compositions(compositions, dependency) {
6
6
  for (const composition of compositions) {
7
- addVariables(composition, variables)
7
+ const claimed = (dependency.services ?? []).filter(
8
+ (service) => service.workload?.includes(composition.name) === true
9
+ )
10
+
11
+ // a service brings components of its own — the identity components inside the gateway —
12
+ // and their variables are keyed by their own labels, not by the composition's
13
+ const keys = composition.components.concat(
14
+ ...claimed.map((service) => service.components ?? [])
15
+ )
16
+
17
+ addVariables(composition, dependency.variables, keys)
18
+ addMounts(composition, dependency.mounts, keys)
19
+
20
+ if (claimed.length > 0) {
21
+ // the label each hosted service's own Service selects on
22
+ composition.hosted = claimed.map((service) => service.name)
23
+
24
+ fold(composition, claimed, dependency)
25
+ }
26
+
27
+ if (dependency.probe !== undefined && dependency.probe !== false)
28
+ composition.probe ??= dependency.probe
8
29
  }
9
30
  }
10
-
11
- exports.compositions = compositions
@@ -1,10 +1,8 @@
1
- 'use strict'
2
-
3
1
  /**
4
2
  * @param {toa.deployment.dependency.Reference[]} references
5
3
  * @returns {*}
6
4
  */
7
- const dependencies = (references) => {
5
+ export const dependencies = (references) => {
8
6
  return references?.reduce((map, reference) => {
9
7
  const { name, alias, values } = reference
10
8
 
@@ -13,6 +11,3 @@ const dependencies = (references) => {
13
11
  return map
14
12
  }, {})
15
13
  }
16
-
17
-
18
- exports.dependencies = dependencies
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Tells each component which of its events something consumes. An event nobody consumes gets
3
+ * no emitter, no exchange and no outbox row, and a component none of whose events are consumed
4
+ * gets no outbox at all.
5
+ *
6
+ * A component that declares events always gets the variable, empty when nothing is consumed —
7
+ * an absent variable means every event, which is what a run without a deployment gets.
8
+ *
9
+ * @param {toa.norm.Context} context
10
+ * @param {toa.deployment.Dependency} dependency
11
+ */
12
+ export function events(context, dependency) {
13
+ const components = deployed(context)
14
+ const consumed = collect(components, context.events, dependency.events)
15
+
16
+ for (const { locator, events } of components) {
17
+ if (events === undefined) continue
18
+
19
+ const labels = Object.keys(events).filter((label) =>
20
+ consumed.has(locator.id + '.' + label)
21
+ )
22
+
23
+ dependency.variables[locator.label] ??= []
24
+
25
+ dependency.variables[locator.label].push({
26
+ name: VARIABLE + locator.uppercase,
27
+ value: labels.join(' ')
28
+ })
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Every component this context deploys, its own and those its extensions bring. `components`
34
+ * holds only the former; the latter reach a deployment as instances of what they depend on,
35
+ * which is also how they are given the rest of their variables.
36
+ */
37
+ function deployed(context) {
38
+ const components = new Map()
39
+
40
+ for (const instances of Object.values(context.dependencies ?? {}))
41
+ for (const { component } of instances) components.set(component.locator.id, component)
42
+
43
+ return [...components.values()]
44
+ }
45
+
46
+ /**
47
+ * Everything that consumes an event of this context: the receivers of its own components, what
48
+ * a dependency declares it consumes, and what the context says is consumed outside it.
49
+ */
50
+ function collect(components, declared, contributed) {
51
+ const consumed = new Set(declared ?? [])
52
+
53
+ for (const label of contributed ?? []) consumed.add(label)
54
+
55
+ for (const { receivers } of components)
56
+ for (const [label, receiver] of Object.entries(receivers ?? {}))
57
+ // a receiver with a source consumes from a foreign broker, not from this context
58
+ if (receiver.source === undefined) consumed.add(label)
59
+
60
+ return consumed
61
+ }
62
+
63
+ const VARIABLE = 'TOA_EVENTS_'
@@ -0,0 +1,42 @@
1
+ /**
2
+ * What a workload takes on by running services in its own pod: their variables, the ports
3
+ * they bind, and a readiness probe answered by one of them rather than by the workload.
4
+ *
5
+ * What it does *not* take on is how they are reached. A composition-hosted service keeps
6
+ * its own Service and Ingress — only the pods behind them change — so the ingress union
7
+ * belongs to `mono`, the one workload that fronts every service under a single name.
8
+ *
9
+ * @param {object} workload
10
+ * @param {toa.deployment.dependency.Service[]} services
11
+ * @param {toa.deployment.Dependency} dependency
12
+ */
13
+ export function fold(workload, services, dependency) {
14
+ workload.variables ??= []
15
+
16
+ for (const service of services) {
17
+ if (service.variables !== undefined)
18
+ for (const variable of service.variables)
19
+ if (!workload.variables.some((item) => item.name === variable.name))
20
+ workload.variables.push(variable)
21
+
22
+ // every declared port is bound by the single process, none is primary
23
+ if (service.port !== undefined)
24
+ (workload.backends ??= []).push({
25
+ port: service.port,
26
+ path: service.ingress?.path ?? '/'
27
+ })
28
+
29
+ if (service.probe !== undefined && service.probe !== false)
30
+ workload.probe = service.probe
31
+ }
32
+
33
+ if (
34
+ workload.probe === undefined &&
35
+ dependency.probe !== undefined &&
36
+ dependency.probe !== false
37
+ )
38
+ workload.probe = dependency.probe
39
+
40
+ // the more specific prefix must come first, whatever the controller's tie-break
41
+ workload.backends?.sort((a, b) => b.path.length - a.path.length)
42
+ }
@@ -0,0 +1,108 @@
1
+ import { describe, it } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+
4
+ import { fold } from './fold.js'
5
+
6
+ const service = (name, extra = {}) => ({ group: 'group', name, version: '0', ...extra })
7
+
8
+ it('should collect the ports of every service', () => {
9
+ const workload = {}
10
+
11
+ fold(workload, [service('one', { port: 8000 }), service('two', { port: 8002 })], {})
12
+
13
+ assert.deepStrictEqual(workload.backends, [
14
+ { port: 8000, path: '/' },
15
+ { port: 8002, path: '/' }
16
+ ])
17
+ })
18
+
19
+ it('should take the path a service declares', () => {
20
+ const workload = {}
21
+
22
+ fold(workload, [service('one', { port: 8002, ingress: { path: '/explorer' } })], {})
23
+
24
+ assert.deepStrictEqual(workload.backends, [{ port: 8002, path: '/explorer' }])
25
+ })
26
+
27
+ it('should put the more specific prefix first', () => {
28
+ const workload = {}
29
+
30
+ fold(
31
+ workload,
32
+ [
33
+ service('one', { port: 8000, ingress: { path: '/' } }),
34
+ service('two', { port: 8002, ingress: { path: '/explorer' } })
35
+ ],
36
+ {}
37
+ )
38
+
39
+ assert.deepStrictEqual(
40
+ workload.backends.map((backend) => backend.path),
41
+ ['/explorer', '/']
42
+ )
43
+ })
44
+
45
+ it('should leave a service without a port out of the backends', () => {
46
+ const workload = {}
47
+
48
+ fold(workload, [service('one')], {})
49
+
50
+ assert.strictEqual(workload.backends, undefined)
51
+ })
52
+
53
+ describe('variables', () => {
54
+ it('should take those of every service', () => {
55
+ const workload = {}
56
+
57
+ fold(
58
+ workload,
59
+ [
60
+ service('one', { variables: [{ name: 'A', value: '1' }] }),
61
+ service('two', { variables: [{ name: 'B', value: '2' }] })
62
+ ],
63
+ {}
64
+ )
65
+
66
+ assert.deepStrictEqual(workload.variables, [
67
+ { name: 'A', value: '1' },
68
+ { name: 'B', value: '2' }
69
+ ])
70
+ })
71
+
72
+ it('should keep the first of a name', () => {
73
+ const workload = { variables: [{ name: 'A', value: 'own' }] }
74
+
75
+ fold(workload, [service('one', { variables: [{ name: 'A', value: 'theirs' }] })], {})
76
+
77
+ assert.deepStrictEqual(workload.variables, [{ name: 'A', value: 'own' }])
78
+ })
79
+ })
80
+
81
+ describe('probe', () => {
82
+ const probe = { path: '/.ready', port: 8004 }
83
+ const fallback = { path: '/.ready', port: 8001 }
84
+
85
+ it('should take the probe of a service over the default', () => {
86
+ const workload = {}
87
+
88
+ fold(workload, [service('one', { port: 8000, probe })], { probe: fallback })
89
+
90
+ assert.deepStrictEqual(workload.probe, probe)
91
+ })
92
+
93
+ it('should fall back to the default', () => {
94
+ const workload = {}
95
+
96
+ fold(workload, [service('one', { port: 8000 })], { probe: fallback })
97
+
98
+ assert.deepStrictEqual(workload.probe, fallback)
99
+ })
100
+
101
+ it('should ignore a disabled probe', () => {
102
+ const workload = {}
103
+
104
+ fold(workload, [service('one', { port: 8000, probe: false })], { probe: false })
105
+
106
+ assert.strictEqual(workload.probe, undefined)
107
+ })
108
+ })
@@ -1,13 +1,4 @@
1
- 'use strict'
2
-
3
- const { dependencies } = require('./dependencies')
4
- const { components } = require('./components')
5
- const { compositions } = require('./compositions')
6
- const { variables } = require('./variables')
7
- const { services } = require('./services')
8
-
9
- exports.dependencies = dependencies
10
- exports.components = components
11
- exports.compositions = compositions
12
- exports.variables = variables
13
- exports.services = services
1
+ export { dependencies } from './dependencies.js'
2
+ export { components } from './components.js'
3
+ export { compositions } from './compositions.js'
4
+ export { services } from './services.js'
@@ -0,0 +1,17 @@
1
+ export function addMounts(composition, mounts, keys = composition.components) {
2
+ if (mounts === undefined) return
3
+
4
+ const used = new Set()
5
+
6
+ for (const [key, mount] of Object.entries(mounts)) {
7
+ if (key !== 'global' && !keys?.includes(key)) continue
8
+
9
+ for (const { name, path, claim } of mount) {
10
+ if (used.has(name)) continue
11
+
12
+ composition.mounts ??= []
13
+ composition.mounts.push({ name, path, claim })
14
+ used.add(name)
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,129 @@
1
+ /**
2
+ * Every deployment states what it may take. One that states nothing is `BestEffort`: the
3
+ * first evicted under memory pressure and the last given CPU under contention — which on
4
+ * a busy node is slow enough for its own startup probe to kill it, in a loop, while the
5
+ * chart reports nothing wrong. That is not a thing to find out in production.
6
+ *
7
+ * A deployment takes its own declaration, or the context's `resources` where it has none.
8
+ * Deploying without any is a decision rather than an omission, and it is spelled
9
+ * `resources: null` — at either place.
10
+ */
11
+ export function resources(context, values) {
12
+ for (const unit of units(values, evicted(context))) {
13
+ // `null` is an answer and `undefined` is not one, so the fallback reads only the latter
14
+ if (unit.deployment.resources === undefined)
15
+ unit.deployment.resources = context.resources
16
+
17
+ if (unit.deployment.resources === undefined)
18
+ throw new Error(
19
+ `${unit.subject} declares no resources. ` +
20
+ "Declare them on it or as the context's 'resources', " +
21
+ "or 'resources: null' to deploy it without any."
22
+ )
23
+
24
+ heap(unit.deployment)
25
+ }
26
+ }
27
+
28
+ /**
29
+ * The memory limit sizes the heap. Node reads the machine's memory rather than the
30
+ * container's, so without this the heap grows past the limit and the pod is killed instead
31
+ * of collected. What is left of the limit is the process itself: its code, its buffers and
32
+ * its threads. A deployment that states `NODE_OPTIONS` of its own keeps them.
33
+ */
34
+ function heap(deployment) {
35
+ const limit = deployment.resources?.memory?.[1]
36
+
37
+ if (limit === undefined) return
38
+
39
+ deployment.variables ??= []
40
+
41
+ if (deployment.variables.some((variable) => variable.name === NODE_OPTIONS)) return
42
+
43
+ const megabytes = Math.floor((quantity(limit) * HEAP_SHARE) / 2 ** 20)
44
+
45
+ deployment.variables.push({
46
+ name: NODE_OPTIONS,
47
+ value: `--max-old-space-size=${megabytes}`
48
+ })
49
+ }
50
+
51
+ /**
52
+ * A Kubernetes quantity, in bytes.
53
+ *
54
+ * @param {string | number} value
55
+ * @returns {number}
56
+ */
57
+ export function quantity(value) {
58
+ const match = String(value).match(/^(\d+(?:\.\d+)?)([KMGTPE]i?|[kmun])?$/)
59
+
60
+ if (match === null) throw new Error(`'${value}' is not a quantity`)
61
+
62
+ const [, number, suffix = ''] = match
63
+ const scale = SUFFIXES[suffix]
64
+
65
+ if (scale === undefined) throw new Error(`'${value}' is not a memory quantity`)
66
+
67
+ return Number(number) * scale
68
+ }
69
+
70
+ const NODE_OPTIONS = 'NODE_OPTIONS'
71
+ const HEAP_SHARE = 0.75
72
+
73
+ const SUFFIXES = {
74
+ '': 1,
75
+ K: 1e3,
76
+ M: 1e6,
77
+ G: 1e9,
78
+ T: 1e12,
79
+ P: 1e15,
80
+ E: 1e18,
81
+ Ki: 2 ** 10,
82
+ Mi: 2 ** 20,
83
+ Gi: 2 ** 30,
84
+ Ti: 2 ** 40,
85
+ Pi: 2 ** 50,
86
+ Ei: 2 ** 60
87
+ }
88
+
89
+ /**
90
+ * The labels of the components the context evicts, whether or not the eviction was applied:
91
+ * `toa env -c` does not apply it, since a local run of an evicted component still needs its
92
+ * variables.
93
+ *
94
+ * @returns {Set<string>}
95
+ */
96
+ function evicted(context) {
97
+ const ids = new Set(context.evicted?.components ?? [])
98
+
99
+ return new Set(
100
+ (context.components ?? [])
101
+ .filter(({ locator }) => ids.has(locator.id))
102
+ .map(({ locator }) => locator.label)
103
+ )
104
+ }
105
+
106
+ function* units(values, evicted) {
107
+ if (values.mono !== undefined)
108
+ yield { deployment: values.mono, subject: 'The mono deployment' }
109
+
110
+ for (const composition of values.compositions ?? []) {
111
+ // a composition of what the context evicts is here only for a local run of it, and Toa
112
+ // deploys none of it, so nothing asks what it may take
113
+ if (
114
+ composition.components?.length > 0 &&
115
+ composition.components.every((label) => evicted.has(label))
116
+ )
117
+ continue
118
+
119
+ yield { deployment: composition, subject: `Composition '${composition.name}'` }
120
+ }
121
+
122
+ for (const service of values.services ?? []) {
123
+ // a service a composition runs has no deployment of its own to size; the composition
124
+ // it runs in states what the pod may take
125
+ if (service.workload !== undefined) continue
126
+
127
+ yield { deployment: service, subject: `Service '${service.name}'` }
128
+ }
129
+ }