@toa.io/operations 1.0.0-alpha.30 → 1.0.0-alpha.301

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 (75) hide show
  1. package/CHANGELOG.md +142 -0
  2. package/image/runtime.Dockerfile +14 -0
  3. package/package.json +10 -10
  4. package/readme.md +201 -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 +138 -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.yaml +1 -1
  24. package/src/deployment/chart/templates/compositions.yaml +99 -4
  25. package/src/deployment/chart/templates/mono.yaml +185 -0
  26. package/src/deployment/chart/templates/services.yaml +85 -7
  27. package/src/deployment/chart/values.yaml +23 -1
  28. package/src/deployment/composition.js +6 -6
  29. package/src/deployment/deployment.js +147 -34
  30. package/src/deployment/drain.js +70 -0
  31. package/src/deployment/drain.test.js +64 -0
  32. package/src/deployment/factory.js +169 -37
  33. package/src/deployment/images/bundle.js +88 -0
  34. package/src/deployment/images/composition.Dockerfile +8 -16
  35. package/src/deployment/images/composition.js +9 -56
  36. package/src/deployment/images/dependencies.Dockerfile +23 -0
  37. package/src/deployment/images/dependencies.js +171 -0
  38. package/src/deployment/images/dependencies.test.js +268 -0
  39. package/src/deployment/images/factory.js +31 -15
  40. package/src/deployment/images/format.js +66 -0
  41. package/src/deployment/images/format.test.js +149 -0
  42. package/src/deployment/images/image.fixtures.js +17 -19
  43. package/src/deployment/images/image.js +77 -38
  44. package/src/deployment/images/image.test.js +11 -5
  45. package/src/deployment/images/index.js +1 -5
  46. package/src/deployment/images/mono.Dockerfile +11 -0
  47. package/src/deployment/images/mono.js +15 -0
  48. package/src/deployment/images/publish.js +91 -0
  49. package/src/deployment/images/runtime-base.test.js +97 -0
  50. package/src/deployment/images/service.Dockerfile +16 -4
  51. package/src/deployment/images/service.js +63 -14
  52. package/src/deployment/index.js +1 -5
  53. package/src/deployment/operator.js +22 -16
  54. package/src/deployment/operator.test.js +57 -7
  55. package/src/deployment/registry.js +210 -46
  56. package/src/deployment/registry.test.js +539 -0
  57. package/src/deployment/service.js +4 -6
  58. package/src/deployment/workspace.js +13 -8
  59. package/src/index.js +3 -2
  60. package/src/process.js +51 -13
  61. package/src/process.test.js +33 -0
  62. package/types/_deployment/composition.d.ts +1 -3
  63. package/types/_deployment/dependency.d.ts +13 -7
  64. package/types/_deployment/deployment.d.ts +7 -8
  65. package/types/_deployment/factory.d.ts +2 -4
  66. package/types/_deployment/images/factory.d.ts +6 -8
  67. package/types/_deployment/images/image.d.ts +16 -1
  68. package/types/_deployment/images/registry.d.ts +8 -11
  69. package/types/_deployment/operator.d.ts +10 -9
  70. package/types/_deployment/registry.d.ts +7 -4
  71. package/types/_deployment/service.d.ts +4 -3
  72. package/types/dependency.ts +79 -0
  73. package/types/index.ts +1 -0
  74. package/types/dependency.d.ts +0 -39
  75. package/types/index.d.ts +0 -1
@@ -1,8 +1,11 @@
1
- 'use strict'
1
+ import * as desc from './.describe/index.js'
2
+ import { addVariables } from './.describe/variables.js'
3
+ import { addMounts } from './.describe/mounts.js'
4
+ import { resources } from './.describe/resources.js'
5
+ import { events } from './.describe/events.js'
6
+ import { fold } from './.describe/fold.js'
2
7
 
3
- const get = require('./.describe')
4
-
5
- const describe = (context, compositions, dependency) => {
8
+ export const describe = (context, compositions, dependency, image) => {
6
9
  const { services } = dependency
7
10
 
8
11
  dependency.variables.global ??= []
@@ -11,24 +14,149 @@ const describe = (context, compositions, dependency) => {
11
14
  {
12
15
  name: 'TOA_CONTEXT',
13
16
  value: context.name
14
- }, {
17
+ },
18
+ {
15
19
  name: 'TOA_ENV',
16
20
  value: context.environment
17
21
  }
18
22
  )
19
23
 
20
- const components = get.components(compositions)
24
+ const atomicity = context.atomicity
25
+
26
+ if (atomicity?.redis !== undefined) {
27
+ const addresses = Array.isArray(atomicity.redis) ? atomicity.redis : [atomicity.redis]
28
+
29
+ // a lock is taken on `floor(n / 2) + 1` of them, so an even number tolerates no more
30
+ // losses than the odd number below it, and two tolerate fewer than one does
31
+ if (addresses.length % 2 === 0)
32
+ throw new Error(
33
+ `'atomicity.redis' takes an odd number of addresses, ` +
34
+ `${addresses.length} given`
35
+ )
36
+
37
+ dependency.variables.global.push({
38
+ name: 'TOA_ATOMICITY_REDIS',
39
+ value: addresses.join(' ')
40
+ })
41
+ }
42
+
43
+ if (atomicity?.interval !== undefined)
44
+ dependency.variables.global.push({
45
+ name: 'TOA_ATOMICITY_INTERVAL',
46
+ value: String(atomicity.interval)
47
+ })
48
+
49
+ const outbox = context.outbox
50
+
51
+ if (outbox?.interval !== undefined)
52
+ dependency.variables.global.push({
53
+ name: 'TOA_OUTBOX_INTERVAL',
54
+ value: String(outbox.interval)
55
+ })
56
+
57
+ if (outbox?.batch !== undefined)
58
+ dependency.variables.global.push({
59
+ name: 'TOA_OUTBOX_BATCH',
60
+ value: String(outbox.batch)
61
+ })
62
+
63
+ if (outbox?.retention !== undefined)
64
+ dependency.variables.global.push({
65
+ name: 'TOA_OUTBOX_RETENTION',
66
+ value: String(outbox.retention)
67
+ })
68
+
69
+ if (context.inbox?.retention !== undefined)
70
+ dependency.variables.global.push({
71
+ name: 'TOA_INBOX_RETENTION',
72
+ value: String(context.inbox.retention)
73
+ })
74
+
75
+ if (context.addressed?.timeout !== undefined)
76
+ dependency.variables.global.push({
77
+ name: 'TOA_ADDRESSED_TIMEOUT',
78
+ value: String(context.addressed.timeout)
79
+ })
80
+
81
+ events(context, dependency)
82
+
21
83
  const credentials = context.registry?.credentials
22
84
 
23
- get.compositions(compositions, dependency.variables, context.environment)
24
- get.services(services, dependency.variables)
85
+ if (image !== undefined) {
86
+ const mono = unit(context, dependency)
87
+
88
+ mono.image = image.reference
89
+
90
+ const values = {
91
+ compositions: [],
92
+ components: mono.components,
93
+ services: [],
94
+ credentials,
95
+ mono
96
+ }
97
+
98
+ resources(context, values)
99
+
100
+ return values
101
+ }
102
+
103
+ const components = desc.components(compositions)
104
+
105
+ desc.compositions(compositions, dependency)
106
+ desc.services(services, dependency.variables, dependency.probe, context.ingress)
25
107
 
26
- return {
108
+ const values = {
27
109
  compositions,
28
110
  components,
29
111
  services,
30
112
  credentials
31
113
  }
114
+
115
+ resources(context, values)
116
+
117
+ return values
32
118
  }
33
119
 
34
- exports.describe = describe
120
+ function unit(context, dependency) {
121
+ // an evicted component is deployed by other means, so mono runs it nowhere
122
+ const components = (context.components ?? [])
123
+ .filter((component) => component.evicted !== true)
124
+ .map((component) => component.locator.label)
125
+
126
+ const variables = dependency.variables ?? {}
127
+ const mounts = dependency.mounts ?? {}
128
+
129
+ const mono = {
130
+ replicas: context.mono?.replicas,
131
+ resources: context.mono?.resources,
132
+ components,
133
+ variables: []
134
+ }
135
+
136
+ if (context.ingress !== undefined) mono.ingress = Object.assign({}, context.ingress)
137
+
138
+ addVariables(mono, variables, Object.keys(variables))
139
+ addMounts(mono, dependency.mounts, Object.keys(mounts))
140
+
141
+ fold(mono, dependency.services ?? [], dependency)
142
+
143
+ // mono is the one workload that fronts every service it runs under a single name,
144
+ // so it inherits how they are reached as well as what they run
145
+ for (const service of dependency.services ?? []) {
146
+ // one Service fronts every backend, so its annotations are the union
147
+ if (service.annotations !== undefined)
148
+ mono.annotations = Object.assign(mono.annotations ?? {}, service.annotations)
149
+
150
+ if (service.ingress !== undefined) {
151
+ const { path, hosts, ...ingress } = service.ingress
152
+
153
+ mono.ingress = Object.assign(mono.ingress ?? {}, ingress)
154
+
155
+ // one Ingress serves every path, so its hosts are the union, not the last word
156
+ if (hosts !== undefined)
157
+ mono.ingress.hosts = [...new Set([...(mono.ingress.hosts ?? []), ...hosts])]
158
+ }
159
+ }
160
+
161
+ return mono
162
+ }
@@ -1,9 +1,3 @@
1
- 'use strict'
2
-
3
- const { declare } = require('./declare')
4
- const { describe } = require('./describe')
5
- const { merge } = require('./merge')
6
-
7
- exports.declare = declare
8
- exports.describe = describe
9
- exports.merge = merge
1
+ export { declare } from './declare.js'
2
+ export { describe } from './describe.js'
3
+ export { merge } from './merge.js'
@@ -1,6 +1,4 @@
1
- 'use strict'
2
-
3
- const merge = (dependencies) => {
1
+ export const merge = (dependencies) => {
4
2
  /** @type {toa.deployment.dependency.Reference[]} */
5
3
  const references = []
6
4
 
@@ -13,14 +11,78 @@ const merge = (dependencies) => {
13
11
  /** @type {toa.deployment.dependency.Variables} */
14
12
  const variables = {}
15
13
 
14
+ /** event labels consumed by something other than a component's own receivers */
15
+ const events = []
16
+
17
+ const mounts = {}
18
+
19
+ /** @type {toa.deployment.dependency.Probe | false | undefined} */
20
+ let probe
21
+
16
22
  for (const dependency of dependencies) {
17
23
  if (dependency.references !== undefined) references.push(...dependency.references)
18
24
  if (dependency.services !== undefined) services.push(...dependency.services)
19
25
  if (dependency.proxies !== undefined) proxies.push(...dependency.proxies)
20
26
  if (dependency.variables !== undefined) append(variables, dependency.variables)
27
+ if (dependency.events !== undefined) events.push(...dependency.events)
28
+ if (dependency.mounts !== undefined) append(mounts, dependency.mounts)
29
+ if (dependency.probe !== undefined) probe = dependency.probe
21
30
  }
22
31
 
23
- return { references, services, proxies, variables }
32
+ reserve(services, probe)
33
+
34
+ return { references, services, proxies, variables, mounts, events, probe }
35
+ }
36
+
37
+ /**
38
+ * A workload that hosts services puts them in one process — `mono`, a composition that
39
+ * declares them, a local run — so within one a port may be claimed once and only once.
40
+ * A service deploying on its own is a pod of its own, and shares a port with nobody.
41
+ */
42
+ const reserve = (services, probe) => {
43
+ /** @type {Map<string, Map<number, string>>} */
44
+ const workloads = new Map()
45
+
46
+ for (const service of services)
47
+ // a service several compositions run binds its ports in each of their pods
48
+ for (const workload of service.workload ?? [service.name]) {
49
+ let claimed = workloads.get(workload)
50
+
51
+ if (claimed === undefined) {
52
+ claimed = new Map()
53
+
54
+ if (probe !== undefined && probe !== false)
55
+ claimed.set(probe.port, 'the readiness probe')
56
+
57
+ workloads.set(workload, claimed)
58
+ }
59
+
60
+ for (const [port, claimant] of ports(service)) {
61
+ const conflicting = claimed.get(port)
62
+
63
+ if (conflicting !== undefined)
64
+ throw new Error(
65
+ `Port ${port} is claimed by both ${conflicting} and ${claimant}` +
66
+ (service.workload === undefined ? '' : ` in '${workload}'`)
67
+ )
68
+
69
+ claimed.set(port, claimant)
70
+ }
71
+ }
72
+ }
73
+
74
+ function* ports(service) {
75
+ // every service reaching here is named the way it is deployed, `<group>-<name>`
76
+ const name = `'${service.name}'`
77
+
78
+ if (service.port !== undefined) yield [service.port, name]
79
+
80
+ if (
81
+ service.probe !== undefined &&
82
+ service.probe !== false &&
83
+ service.probe.port !== service.port
84
+ )
85
+ yield [service.probe.port, `the readiness probe of ${name}`]
24
86
  }
25
87
 
26
88
  const append = (merged, variables) => {
@@ -30,5 +92,3 @@ const append = (merged, variables) => {
30
92
  merged[component].push(...vars)
31
93
  }
32
94
  }
33
-
34
- exports.merge = merge
@@ -0,0 +1,133 @@
1
+ import { describe, it } from 'node:test'
2
+ import assert from 'node:assert/strict'
3
+
4
+ import { merge } from './merge.js'
5
+
6
+ // a service is named the way it is deployed by the time it reaches `merge`
7
+ const service = (name, extra = {}) => ({
8
+ group: 'group',
9
+ name: `group-${name}`,
10
+ version: '0',
11
+ ...extra
12
+ })
13
+
14
+ it('should merge services of all dependencies', () => {
15
+ const merged = merge([
16
+ { services: [service('one', { port: 8000 })] },
17
+ { services: [service('two', { port: 8001 })] }
18
+ ])
19
+
20
+ assert.strictEqual(merged.services.length, 2)
21
+ })
22
+
23
+ /*
24
+ * A workload that hosts services puts them in one process, so a port is claimed once
25
+ * within one. Services in separate pods share nothing.
26
+ */
27
+ describe('port reservation', () => {
28
+ const hosted = (name, extra = {}) => service(name, { workload: ['mono'], ...extra })
29
+
30
+ it('should reject two services of one workload claiming one port', () => {
31
+ const dependencies = [
32
+ { services: [hosted('one', { port: 8000 })] },
33
+ { services: [hosted('two', { port: 8000 })] }
34
+ ]
35
+
36
+ assert.throws(
37
+ () => merge(dependencies),
38
+ (error) =>
39
+ /Port 8000 is claimed by both 'group-one' and 'group-two' in 'mono'/.test(
40
+ error.message
41
+ )
42
+ )
43
+ })
44
+
45
+ it('should allow two workloads to claim one port', () => {
46
+ const dependencies = [
47
+ { services: [service('one', { port: 8000, workload: ['edge'] })] },
48
+ { services: [service('two', { port: 8000, workload: ['inner'] })] }
49
+ ]
50
+
51
+ assert.doesNotThrow(() => merge(dependencies))
52
+ })
53
+
54
+ it('should reserve the ports of a service in every workload running it', () => {
55
+ const dependencies = [
56
+ { services: [service('one', { port: 8000, workload: ['edge', 'inner'] })] },
57
+ { services: [service('two', { port: 8000, workload: ['inner'] })] }
58
+ ]
59
+
60
+ assert.throws(
61
+ () => merge(dependencies),
62
+ (error) =>
63
+ /Port 8000 is claimed by both 'group-one' and 'group-two' in 'inner'/.test(
64
+ error.message
65
+ )
66
+ )
67
+ })
68
+
69
+ it('should allow two services deployed on their own to claim one port', () => {
70
+ const dependencies = [
71
+ { services: [service('one', { port: 8000 })] },
72
+ { services: [service('two', { port: 8000 })] }
73
+ ]
74
+
75
+ assert.doesNotThrow(() => merge(dependencies))
76
+ })
77
+
78
+ it('should reject a service claiming the port of the readiness probe', () => {
79
+ const dependencies = [
80
+ { probe: { path: '/.ready', port: 8001 } },
81
+ { services: [service('one', { port: 8001 })] }
82
+ ]
83
+
84
+ assert.throws(
85
+ () => merge(dependencies),
86
+ (error) =>
87
+ /Port 8001 is claimed by both the readiness probe and 'group-one'/.test(
88
+ error.message
89
+ )
90
+ )
91
+ })
92
+
93
+ it('should reject a probe claiming the port of another service of the workload', () => {
94
+ const dependencies = [
95
+ { services: [hosted('one', { port: 8000 })] },
96
+ {
97
+ services: [hosted('two', { port: 8002, probe: { path: '/.ready', port: 8000 } })]
98
+ }
99
+ ]
100
+
101
+ assert.throws(
102
+ () => merge(dependencies),
103
+ (error) =>
104
+ /Port 8000 is claimed by both 'group-one' and the readiness probe of 'group-two' in 'mono'/.test(
105
+ error.message
106
+ )
107
+ )
108
+ })
109
+
110
+ it('should allow a service to probe its own port', () => {
111
+ const dependencies = [
112
+ {
113
+ services: [service('one', { port: 8000, probe: { path: '/.ready', port: 8000 } })]
114
+ }
115
+ ]
116
+
117
+ assert.doesNotThrow(() => merge(dependencies))
118
+ })
119
+
120
+ it('should ignore services without a port', () => {
121
+ const dependencies = [{ services: [service('one'), service('two')] }]
122
+
123
+ assert.doesNotThrow(() => merge(dependencies))
124
+ })
125
+
126
+ it('should ignore a disabled probe', () => {
127
+ const dependencies = [
128
+ { probe: false, services: [service('one', { port: 8000, probe: false })] }
129
+ ]
130
+
131
+ assert.doesNotThrow(() => merge(dependencies))
132
+ })
133
+ })
@@ -6,7 +6,7 @@ metadata:
6
6
  spec:
7
7
  type: ClusterIP
8
8
  selector:
9
- {{ . }}: "1"
9
+ toa/component-{{ . }}: "1"
10
10
  ports:
11
11
  - name: http-binding
12
12
  protocol: TCP
@@ -5,29 +5,124 @@ metadata:
5
5
  name: composition-{{ required "composition name is required" .name }}
6
6
  spec:
7
7
  replicas: {{ .replicas | default 2 }}
8
+ # A backstop for stuck rollouts only. Must stay above the `helm upgrade --timeout`
9
+ # the deploy runs with, so a slow but progressing rollout is not marked failed.
10
+ progressDeadlineSeconds: 900
11
+ strategy:
12
+ type: RollingUpdate
13
+ rollingUpdate:
14
+ # a replacement must pass readiness before a running replica is taken out, and every
15
+ # replacement starts at once: the rollout takes one start-up, not one per replica
16
+ maxUnavailable: 0
17
+ maxSurge: 100%
8
18
  selector:
9
19
  matchLabels:
10
- toa.io/composition: {{ .name }}
20
+ toa/composition: {{ .name }}
11
21
  template:
12
22
  metadata:
13
23
  labels:
14
- toa.io/composition: {{ .name }}
24
+ toa/composition: {{ .name }}
15
25
  {{- range .components }}
16
- {{ . }}: "1"
26
+ toa/component-{{ . }}: "1"
27
+ {{- end }}
28
+ {{- range .hosted }}
29
+ toa/service-{{ . }}: "1"
17
30
  {{- end }}
18
31
  spec:
19
32
  containers:
20
33
  - name: {{ .name }}
21
34
  image: {{ .image }}
22
- {{- if .variables }}
35
+ {{- if .resources }}
36
+ resources:
37
+ {{- if or .resources.cpu .resources.memory }}
38
+ requests:
39
+ {{- if .resources.cpu }}
40
+ cpu: {{ index .resources.cpu 0 }}
41
+ {{- end }}
42
+ {{- if .resources.memory }}
43
+ memory: {{ index .resources.memory 0 }}
44
+ {{- end }}
45
+ limits:
46
+ {{- if .resources.cpu }}
47
+ cpu: {{ index .resources.cpu 1 }}
48
+ {{- end }}
49
+ {{- if .resources.memory }}
50
+ memory: {{ index .resources.memory 1 }}
51
+ {{- end }}
52
+ {{- end }}
53
+ {{- end }}
54
+ {{- if or .variables .services }}
23
55
  env:
56
+ {{- if .services }}
57
+ - name: TOA_SERVICES
58
+ value: {{ join " " .services | quote }}
59
+ {{- end }}
24
60
  {{- range .variables }}
25
61
  {{- include "env.var" . | indent 12 }}
26
62
  {{- end }}
27
63
  {{- end }}
64
+ {{- if or .backends .probe }}
65
+ ports:
66
+ {{- range .backends }}
67
+ - containerPort: {{ .port }}
68
+ {{- end }}
69
+ {{- if .probe }}
70
+ - containerPort: {{ .probe.port }}
71
+ {{- end }}
72
+ {{- end }}
73
+ {{- if .probe }}
74
+ startupProbe:
75
+ httpGet:
76
+ path: {{ .probe.path }}
77
+ port: {{ .probe.port }}
78
+ {{- if .probe.delay }}
79
+ initialDelaySeconds: {{ .probe.delay }}
80
+ {{- end }}
81
+ periodSeconds: 2
82
+ timeoutSeconds: 3
83
+ failureThreshold: 150
84
+ readinessProbe:
85
+ httpGet:
86
+ path: {{ .probe.path }}
87
+ port: {{ .probe.port }}
88
+ # a replica is ready when it says so, not at the next tick of a slow clock
89
+ periodSeconds: 2
90
+ timeoutSeconds: 3
91
+ failureThreshold: 5
92
+ {{- end }}
93
+ {{- if .mounts }}
94
+ volumeMounts:
95
+ {{- range .mounts }}
96
+ - name: {{ .name }}
97
+ mountPath: {{ .path }}
98
+ {{- end }}
99
+ {{- end }}
100
+ lifecycle:
101
+ preStop:
102
+ exec:
103
+ command: [sleep, "5"]
104
+ terminationGracePeriodSeconds: 45
28
105
  {{- if $.Values.credentials }}
29
106
  imagePullSecrets:
30
107
  - name: {{ $.Values.credentials }}
31
108
  {{- end }}
109
+ {{- if .mounts }}
110
+ volumes:
111
+ {{- range .mounts }}
112
+ - name: {{ .name }}
113
+ persistentVolumeClaim:
114
+ claimName: {{ .claim }}
115
+ {{- end }}
116
+ {{- end }}
117
+ topologySpreadConstraints:
118
+ - maxSkew: 1
119
+ topologyKey: kubernetes.io/hostname
120
+ whenUnsatisfiable: ScheduleAnyway
121
+ labelSelector:
122
+ matchLabels:
123
+ toa/composition: {{ .name }}
124
+ # only this revision's pods: a rollout spreads the new ones, whatever the old ones occupy
125
+ matchLabelKeys:
126
+ - pod-template-hash
32
127
  ---
33
128
  {{- end }}