@yarkivaev/scada 2.27.0 → 2.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/package.json +1 -1
- package/src/application/deploy/collectionEvents.js +49 -0
- package/src/application/deploy/collectionPlacement.js +42 -0
- package/src/application/deploy/compileLocalConsumers.js +76 -0
- package/src/application/deploy/compileNamedControlCatalog.js +23 -0
- package/src/application/deploy/expand.js +3 -1
- package/src/application/deploy/mergeDocuments.js +10 -5
- package/src/application/deploy/namedCollection.js +157 -0
- package/src/application/deploy/namedProcessing.js +86 -0
- package/src/application/deploy/normalizeControlPlane.js +80 -0
- package/src/application/deploy/projectCollectionRoutes.js +101 -0
- package/src/application/deploy/projectControlPlane.js +4 -2
- package/src/application/deploy/readDeploymentDocument.js +30 -2
- package/src/application/plantServer.js +1 -0
- package/src/application/runtime/collectorLaunch.js +19 -3
- package/src/application/siteServer.js +1 -0
- package/src/infrastructure/catalog/tagCatalog.js +4 -3
- package/src/infrastructure/deploy/centralTelemetry.js +10 -1
- package/src/infrastructure/deploy/collectorChartPlan.js +1 -1
- package/src/infrastructure/deploy/configuredBundleCapabilities.js +1 -1
- package/src/infrastructure/deploy/configuredControlPlane.js +3 -1
- package/src/infrastructure/deploy/index.js +6 -0
- package/src/infrastructure/deploy/stagedSitePlan.js +3 -1
- package/src/infrastructure/opcua/projectSources.js +3 -3
package/README.md
CHANGED
|
@@ -42,6 +42,14 @@ Module entries are local JS files relative to the declaring runtime document. Th
|
|
|
42
42
|
|
|
43
43
|
The release workflow publishes Node.js and JVM images and records their digests alongside the release revision. The Node image includes `scada-install-app URL SHA256 DESTINATION`, which verifies and extracts an application bundle for read-only mounting. The JVM image loads an optional profile JAR with the class named by `SCADA_RUNTIME_PROFILE`. The application selects compatible image and bundle digests in the deployment.
|
|
44
44
|
|
|
45
|
+
## Collection deployment DSL
|
|
46
|
+
|
|
47
|
+
A deployment document can declare equipment endpoints in `sources`, logical inputs in `streams`, and assignment policy in `controlPlane.readers`. Each reader refers to one stream through its key or to a list under `streams`. Its ordered `eligible` nodes and requested `copies` guide the controller. A source address can be a `$secret` reference that the executing adapter resolves. Push sources declare their API and event type through a stream.
|
|
48
|
+
|
|
49
|
+
`delivery` can group streams and processing outputs with the same destination under a `streams` list and a `to` target. It derives senders from reader or workload placement. A `$ref: localState` target uses the machine's home from `controlPlane.localState.placements`. Archive destinations may receive data without participating in reads.
|
|
50
|
+
|
|
51
|
+
`readDeploymentDocument` loads `controlPlane.fragments` and machine type processing profiles relative to the root document. It rejects duplicate control-plane keys across fragments. `resolveCollection` resolves inputs and delivery references. `compileNamedControlCatalog` combines placement with application adapter specifications. `bindControlInstallation` joins topology with installation settings and agent options from server profiles. The default node scheduling capacity is 100.
|
|
52
|
+
|
|
45
53
|
## Package boundary
|
|
46
54
|
|
|
47
55
|
SCADA owns the shared collection and processing engines, storage and APIs. Plant applications supply the deployment DSL, device mappings, business rules, routes and HMI. See the [package boundary](docs/architecture/package-boundary.md).
|
package/package.json
CHANGED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { localOwner } from './collectionPlacement.js';
|
|
2
|
+
|
|
3
|
+
function eventRoute({ id, type, node, machine, api }, route, owners) {
|
|
4
|
+
const home = route.localState && localOwner(owners, machine);
|
|
5
|
+
const to = home && !route.to.includes(home) ? [...route.to, home] : route.to;
|
|
6
|
+
const viaApi = route.viaApi ?? api;
|
|
7
|
+
return { id, type, node, machine, to, ...(viaApi && { viaApi }) };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function pushRoutes(stream, route, machines, owners, servers) {
|
|
11
|
+
const { api } = stream.source;
|
|
12
|
+
const assigned = stream.path ? [stream.path] : [...machines.keys()];
|
|
13
|
+
return assigned.flatMap((machine) => {
|
|
14
|
+
const node = localOwner(owners, machine) ?? machines.get(machine);
|
|
15
|
+
if (!node || (api && !servers.get(node)?.api?.some(({ id }) => { return id === api; }))) { return []; }
|
|
16
|
+
return [eventRoute({ id: stream.output ?? stream.id, type: stream.type, node, machine, api }, route, owners)];
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function readerEvents(readers, deliverable, delivery, owners) {
|
|
21
|
+
return readers.filter(({ output }) => { return output !== undefined; }).flatMap((reader) => {
|
|
22
|
+
const { output, eligible, streams } = reader;
|
|
23
|
+
const type = deliverable.get(output)?.type;
|
|
24
|
+
if (!type) { throw new TypeError(`Unknown event output ${output} for reader ${reader.id}`); }
|
|
25
|
+
if (!Object.hasOwn(delivery, output)) { return []; }
|
|
26
|
+
return streams.flatMap(({ path }) => {
|
|
27
|
+
return eligible.map((node) => { return eventRoute({ id: output, type, node, machine: path }, delivery[output], owners); });
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function eventRoutes({ document, streams, outputs, delivery, owners, workloads }) {
|
|
33
|
+
const machines = new Map(Object.entries(owners));
|
|
34
|
+
for (const { assignments } of Object.values(workloads)) {
|
|
35
|
+
for (const { machines: selected, nodes: [primary] } of assignments) {
|
|
36
|
+
selected.forEach(({ path }) => { if (!machines.has(path)) { machines.set(path, primary); } });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const servers = new Map(document.servers?.map((server) => { return [server.id, server]; }));
|
|
40
|
+
const processing = Object.entries(outputs).filter(([id]) => { return Object.hasOwn(delivery, id); }).flatMap(([id, { type, producers }]) => {
|
|
41
|
+
return producers.flatMap(({ machine, nodes }) => {
|
|
42
|
+
return nodes.map((node) => { return eventRoute({ id, type, node, machine }, delivery[id], owners); });
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
const pushed = [...streams.values()].filter(({ source, output, id }) => {
|
|
46
|
+
return source.mode === 'push' && Object.hasOwn(delivery, output ?? id);
|
|
47
|
+
}).flatMap((stream) => { return pushRoutes(stream, delivery[stream.output ?? stream.id], machines, owners, servers); });
|
|
48
|
+
return [...processing, ...pushed];
|
|
49
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { treePaths } from './expandNamespace.js';
|
|
2
|
+
|
|
3
|
+
function machinePath(paths, machine) {
|
|
4
|
+
const matches = paths.filter((path) => { return path === machine || path.split('/').at(-1) === machine; });
|
|
5
|
+
if (matches.length !== 1) { throw new TypeError(`Invalid local state machine ${machine}`); }
|
|
6
|
+
return matches[0];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function placementEntries(placements) {
|
|
10
|
+
if (!placements || typeof placements !== 'object' || Array.isArray(placements)) {
|
|
11
|
+
throw new TypeError('Local state placements must be a map');
|
|
12
|
+
}
|
|
13
|
+
return Object.entries(placements);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function placementMap(document, servers) {
|
|
17
|
+
const placements = document.controlPlane?.localState?.placements;
|
|
18
|
+
if (placements === undefined) { return document.objectOwners ?? {}; }
|
|
19
|
+
const paths = treePaths(document.namespace);
|
|
20
|
+
const owners = new Map(Object.entries(document.objectOwners ?? {}));
|
|
21
|
+
for (const [node, machines] of placementEntries(placements)) {
|
|
22
|
+
if (!servers.has(node) || !Array.isArray(machines) || machines.length === 0) {
|
|
23
|
+
throw new TypeError(`Invalid local state placement on ${node}`);
|
|
24
|
+
}
|
|
25
|
+
for (const machine of machines) {
|
|
26
|
+
const path = machinePath(paths, machine);
|
|
27
|
+
if (owners.has(path)) { throw new TypeError(`Duplicate local state machine ${machine}`); }
|
|
28
|
+
owners.set(path, node);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return Object.fromEntries(owners);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function localOwner(owners, path) {
|
|
35
|
+
if (path === undefined) { return undefined; }
|
|
36
|
+
return Object.entries(owners).filter(([prefix]) => { return path === prefix || path.startsWith(`${prefix}/`); })
|
|
37
|
+
.sort(([left], [right]) => { return right.length - left.length; })[0]?.[1];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function localStateReference(value) {
|
|
41
|
+
return value?.$ref === 'localState' && Object.keys(value).length === 1;
|
|
42
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { collectorConsumerTasks } from './collectorSourcePlan.js';
|
|
2
|
+
import deploymentTemplate from '../../infrastructure/deploy/deploymentTemplate.js';
|
|
3
|
+
import { resolveCollection } from './namedCollection.js';
|
|
4
|
+
|
|
5
|
+
const batchOptions = { size: 50, interval: 2, threshold: 5, timeout: 60 };
|
|
6
|
+
const postgres = { $secret: 'POSTGRES_URL' };
|
|
7
|
+
const amqp = { $secret: 'AMQP_URL' };
|
|
8
|
+
|
|
9
|
+
function localReceivers(node, { routes, topics, devices, clientId, localState, preferences }) {
|
|
10
|
+
const { operations, telemetry, stomp } = preferences;
|
|
11
|
+
function localEvents(event) { return routes[event]?.localState || routes[event]?.to.includes(node); }
|
|
12
|
+
return [
|
|
13
|
+
topics.length && { id: 'mqtt', spec: { adapter: 'mqtt-pg-metrics', mqtt: { $secret: 'MQTT_URL' },
|
|
14
|
+
postgres, devices, topic: topics.join(','), firstWins: true, options: { ...batchOptions, clientId } } },
|
|
15
|
+
localEvents('operations') && operations?.enabled !== false && { id: 'operations', spec: { adapter: 'operation-sync', amqp,
|
|
16
|
+
postgres, queue: operations?.queue ?? 'scada.operations.from-sensors', options: batchOptions } },
|
|
17
|
+
['segments', 'alerts', 'user_decisions'].some(localEvents) && { id: 'stomp', spec: { adapter: 'stomp-ingest',
|
|
18
|
+
stomp: { $secret: 'STOMP_URL' }, postgres, config: { ...batchOptions, ...localState,
|
|
19
|
+
prefetch: 128, repairHistoryOnStart: false, deadLetter: false, ...stomp } } },
|
|
20
|
+
topics.length && telemetry?.enabled !== false && { id: 'amqp-metrics', spec: { adapter: 'amqp-pg-metrics', amqp,
|
|
21
|
+
postgres, devices, queue: telemetry?.queue ?? 'scada.telemetry.ingest', firstWins: true, options: batchOptions } }
|
|
22
|
+
].filter(Boolean);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Build the ingress assignments for state owned by one node using collection and delivery. */
|
|
26
|
+
export function namedLocalConsumers(document, node, { generation = 1, wire, preferences = {} }) {
|
|
27
|
+
const { objectOwners, delivery, readers } = resolveCollection(document);
|
|
28
|
+
const paths = Object.keys(objectOwners).filter((path) => { return objectOwners[path] === node; });
|
|
29
|
+
if (!paths.length || wire === undefined) { return []; }
|
|
30
|
+
const targets = readers.flatMap(({ streams }) => { return streams; }).filter(({ id, path }) => {
|
|
31
|
+
return delivery[id]?.to.includes(node) && paths.some((home) => { return path === home || path.startsWith(`${home}/`); });
|
|
32
|
+
});
|
|
33
|
+
const configuration = wire(paths, targets);
|
|
34
|
+
return collectorConsumerTasks(localReceivers(node, { ...configuration, routes: delivery, preferences,
|
|
35
|
+
localState: document.controlPlane.history?.sink?.segmentPolicy }), node, {}, generation);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function ownedPath(document, prefix, cluster) {
|
|
39
|
+
const paths = Object.entries(document.objectOwners ?? {}).filter(([path, owner]) => {
|
|
40
|
+
return owner === cluster && path.startsWith(prefix) && path.length > prefix.length;
|
|
41
|
+
});
|
|
42
|
+
if (paths.length > 1) { throw new TypeError(`Local consumer site ${cluster} owns multiple objects under ${prefix}`); }
|
|
43
|
+
return paths[0]?.[0];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function withBatchDefaults(declarations, defaults) {
|
|
47
|
+
const ids = new Set();
|
|
48
|
+
return declarations.map(({ id, when, batchTarget, spec }) => {
|
|
49
|
+
if (typeof id !== 'string' || !id || ids.has(id) || typeof spec?.adapter !== 'string' || !spec.adapter) {
|
|
50
|
+
throw new TypeError('Invalid or duplicate consumer declaration');
|
|
51
|
+
}
|
|
52
|
+
ids.add(id);
|
|
53
|
+
const configuration = batchTarget === undefined ? spec : {
|
|
54
|
+
...spec, [batchTarget]: { ...defaults, ...spec[batchTarget] }
|
|
55
|
+
};
|
|
56
|
+
return { id, when, spec: configuration };
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Resolve local task templates against the object and profile of their declared owner. */
|
|
61
|
+
export default function compileLocalConsumers(document, server, app, { generation = 1, bindings, validate } = {}) {
|
|
62
|
+
const policy = document.controlPlane?.localConsumers;
|
|
63
|
+
if (policy === undefined) { return []; }
|
|
64
|
+
const { objectOwnerPrefix, consumers, batchDefaults = {} } = policy;
|
|
65
|
+
if (typeof objectOwnerPrefix !== 'string' || !objectOwnerPrefix.endsWith('/') || !Array.isArray(consumers)) {
|
|
66
|
+
throw new TypeError('Local consumers require an object owner prefix and declarations');
|
|
67
|
+
}
|
|
68
|
+
const cluster = server.id;
|
|
69
|
+
const path = ownedPath(document, objectOwnerPrefix, cluster);
|
|
70
|
+
if (!path) { return []; }
|
|
71
|
+
const declarations = withBatchDefaults(consumers, batchDefaults);
|
|
72
|
+
const context = { path, machine: path.slice(objectOwnerPrefix.length), cluster, server, app, document };
|
|
73
|
+
const values = { ...context, ...bindings?.(context), ...deploymentTemplate(policy.bindings ?? {}, context) };
|
|
74
|
+
validate?.(values);
|
|
75
|
+
return collectorConsumerTasks(declarations, cluster, values, generation);
|
|
76
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { versionedControlCatalog } from './controlPlan.js';
|
|
2
|
+
import { resolveCollection } from './namedCollection.js';
|
|
3
|
+
|
|
4
|
+
/** Compile assignment specs from readers and pin consumers to their local state. */
|
|
5
|
+
export default function compileNamedControlCatalog(document, config, { readerTasks, localTasks = () => { return []; } }) {
|
|
6
|
+
const { readers } = resolveCollection(document);
|
|
7
|
+
const physical = readers.flatMap((reader) => {
|
|
8
|
+
const { eligible, copies, cost } = reader;
|
|
9
|
+
return readerTasks(reader).map(({ taskId, spec, specVersion, pinnedNode }) => {
|
|
10
|
+
if (pinnedNode !== undefined && (copies !== 1 || eligible.length !== 1 || pinnedNode !== eligible[0])) {
|
|
11
|
+
throw new TypeError(`Reader ${reader.id} pins a task outside its placement policy`);
|
|
12
|
+
}
|
|
13
|
+
return { taskId, spec, specVersion, copies, cost, eligibleNodes: eligible,
|
|
14
|
+
...(pinnedNode === undefined ? { preference: eligible } : { pinnedNode }) };
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
const local = config.nodes.flatMap((node) => {
|
|
18
|
+
return localTasks(node).map(({ taskId, spec, specVersion }) => {
|
|
19
|
+
return { taskId, spec, specVersion, copies: 1, cost: 1, eligibleNodes: [node.id], pinnedNode: node.id };
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
return versionedControlCatalog({ version: config.version, controlEpoch: config.controlEpoch, nodes: config.nodes, tasks: [...physical, ...local] });
|
|
23
|
+
}
|
|
@@ -2,6 +2,7 @@ import { expandTree, treePaths } from './expandNamespace.js';
|
|
|
2
2
|
import { expandProfiles } from './expandProfiles.js';
|
|
3
3
|
import { expandServers } from './expandFlows.js';
|
|
4
4
|
import resolveDeploymentRouting from './deploymentRouting.js';
|
|
5
|
+
import projectCollectionRoutes from './projectCollectionRoutes.js';
|
|
5
6
|
import frozen from './frozen.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -36,7 +37,7 @@ export default function expand(document) {
|
|
|
36
37
|
if (!document || typeof document !== 'object' || Array.isArray(document)) {
|
|
37
38
|
throw new Error('system document is empty');
|
|
38
39
|
}
|
|
39
|
-
const input = resolveDeploymentRouting(document);
|
|
40
|
+
const input = projectCollectionRoutes(resolveDeploymentRouting(document));
|
|
40
41
|
const namespace = expandTree(input.namespace, input.templates || {});
|
|
41
42
|
const paths = treePaths(namespace);
|
|
42
43
|
const profiles = expandProfiles(input.profiles || {});
|
|
@@ -44,6 +45,7 @@ export default function expand(document) {
|
|
|
44
45
|
const servers = expandServers(input.servers || [], paths, input.mappings || {});
|
|
45
46
|
return frozen({
|
|
46
47
|
...(input.hmi !== undefined && { hmi: input.hmi }),
|
|
48
|
+
...(input.eventDelivery !== undefined && { eventDelivery: input.eventDelivery }),
|
|
47
49
|
mappings: input.mappings || {},
|
|
48
50
|
namespace,
|
|
49
51
|
profiles,
|
|
@@ -68,6 +68,13 @@ function mergeNodes(left, right) {
|
|
|
68
68
|
return Array.from(value.values());
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
function mergeDelivery(left, right) {
|
|
72
|
+
if (!Array.isArray(left) && !Array.isArray(right)) { return mergeMaps(left, right); }
|
|
73
|
+
const groups = [left, right].filter((value) => { return value !== undefined; });
|
|
74
|
+
if (!groups.every(Array.isArray)) { throw new TypeError('Cannot mix delivery lists and maps'); }
|
|
75
|
+
return groups.flat();
|
|
76
|
+
}
|
|
77
|
+
|
|
71
78
|
/**
|
|
72
79
|
* Overlays one system document on another.
|
|
73
80
|
*
|
|
@@ -88,12 +95,10 @@ export default function mergeDocuments(base, extra) {
|
|
|
88
95
|
if (base.mappings || extra.mappings) {
|
|
89
96
|
document.mappings = mergeMaps(base.mappings, extra.mappings);
|
|
90
97
|
}
|
|
91
|
-
|
|
92
|
-
document
|
|
93
|
-
}
|
|
94
|
-
if (base.routes || extra.routes) {
|
|
95
|
-
document.routes = mergeMaps(base.routes, extra.routes);
|
|
98
|
+
for (const key of ['sources', 'streams', 'objectOwners', 'externalPublications', 'routes', 'machineTypes']) {
|
|
99
|
+
if (base[key] || extra[key]) { document[key] = mergeMaps(base[key], extra[key]); }
|
|
96
100
|
}
|
|
101
|
+
if (base.delivery || extra.delivery) { document.delivery = mergeDelivery(base.delivery, extra.delivery); }
|
|
97
102
|
if (base.profiles || extra.profiles) {
|
|
98
103
|
document.profiles = mergeMaps(base.profiles, extra.profiles);
|
|
99
104
|
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { sourceAddress } from './deploymentRouting.js';
|
|
2
|
+
import { processingOutputs, resolveWorkloads } from './namedProcessing.js';
|
|
3
|
+
import { eventRoutes, readerEvents } from './collectionEvents.js';
|
|
4
|
+
import { localOwner, localStateReference, placementMap } from './collectionPlacement.js';
|
|
5
|
+
|
|
6
|
+
function asMap(value, label) {
|
|
7
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) { throw new TypeError(`${label} must be a map`); }
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function ids(value, label, known) {
|
|
12
|
+
const list = typeof value === 'string' ? [value] : value;
|
|
13
|
+
if (!Array.isArray(list) || list.length === 0 || new Set(list).size !== list.length
|
|
14
|
+
|| list.some((id) => { return typeof id !== 'string' || !known.has(id); })) {
|
|
15
|
+
throw new TypeError(`Invalid ${label}`);
|
|
16
|
+
}
|
|
17
|
+
return list;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function pullStream(id, declaration, sources, mappings) {
|
|
21
|
+
const { source, path, query, mapping } = declaration;
|
|
22
|
+
const input = sources[source];
|
|
23
|
+
const { address } = input;
|
|
24
|
+
if (typeof path !== 'string' || path.split('/').some((part) => { return !part; })) {
|
|
25
|
+
throw new TypeError(`Invalid path for stream ${id}`);
|
|
26
|
+
}
|
|
27
|
+
if (typeof mapping === 'string' && !Object.hasOwn(mappings, mapping)) {
|
|
28
|
+
throw new TypeError(`Unknown mapping ${mapping} for stream ${id}`);
|
|
29
|
+
}
|
|
30
|
+
const resolvedMapping = typeof mapping === 'string' ? mappings[mapping] : mapping;
|
|
31
|
+
const secret = address && typeof address === 'object' && Object.keys(address).length === 1
|
|
32
|
+
&& typeof address.$secret === 'string' && address.$secret;
|
|
33
|
+
if (secret && query !== undefined) { throw new TypeError(`Source ${source} cannot add query parameters to a secret address`); }
|
|
34
|
+
return { ...declaration, id, source: input, address, from: secret ? address : sourceAddress(sources, source, query), path, name: path, resolvedMapping };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function resolveStream(id, declaration, sources, mappings) {
|
|
38
|
+
const { source, type, query } = asMap(declaration, `Stream ${id}`);
|
|
39
|
+
if (typeof source !== 'string' || !Object.hasOwn(sources ?? {}, source)) { throw new TypeError(`Unknown source ${source}`); }
|
|
40
|
+
const input = asMap(sources[source], `Source ${source}`);
|
|
41
|
+
if (input.mode !== 'push') { return pullStream(id, declaration, sources, mappings); }
|
|
42
|
+
if (typeof type !== 'string' || !type || query !== undefined) { throw new TypeError(`Invalid push stream ${id}`); }
|
|
43
|
+
return { ...declaration, id, source: input };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function readerNodes(id, eligible, reading, controlNodes, owners) {
|
|
47
|
+
const list = Array.isArray(eligible) ? eligible : [eligible];
|
|
48
|
+
return ids(list.map((node) => {
|
|
49
|
+
if (!localStateReference(node)) { return node; }
|
|
50
|
+
const homes = new Set(reading.map(({ path }) => { return localOwner(owners, path); }));
|
|
51
|
+
if (homes.size !== 1 || homes.has(undefined)) { throw new TypeError(`Reader ${id} requires one local state placement`); }
|
|
52
|
+
return [...homes][0];
|
|
53
|
+
}), `eligible nodes for reader ${id}`, controlNodes);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function resolveReaders(declarations, streams, controlNodes, owners) {
|
|
57
|
+
const readers = [];
|
|
58
|
+
const assigned = new Set();
|
|
59
|
+
for (const [id, declaration] of Object.entries(asMap(declarations, 'Control readers'))) {
|
|
60
|
+
const { streams: included = [id], eligible, copies = 1, cost = 1, adapter, output } = asMap(declaration, `Reader ${id}`);
|
|
61
|
+
const reading = ids(included, `streams for reader ${id}`, streams).map((stream) => {
|
|
62
|
+
if (assigned.has(stream)) { throw new TypeError(`Stream ${stream} has more than one reader`); }
|
|
63
|
+
assigned.add(stream);
|
|
64
|
+
return streams.get(stream);
|
|
65
|
+
});
|
|
66
|
+
const nodes = readerNodes(id, eligible, reading, controlNodes, owners);
|
|
67
|
+
if (!Number.isSafeInteger(copies) || copies < 1 || copies > nodes.length || !Number.isSafeInteger(cost) || cost < 1) {
|
|
68
|
+
throw new TypeError(`Invalid copies or cost for reader ${id}`);
|
|
69
|
+
}
|
|
70
|
+
readers.push({ id, eligible: nodes, copies, cost, streams: reading,
|
|
71
|
+
...(adapter !== undefined && { adapter }), ...(output !== undefined && { output }) });
|
|
72
|
+
}
|
|
73
|
+
for (const id of streams.keys()) {
|
|
74
|
+
if (!assigned.has(id)) { throw new TypeError(`Stream ${id} has no reader`); }
|
|
75
|
+
}
|
|
76
|
+
return readers;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function routeEntries(delivery, known) {
|
|
80
|
+
if (!Array.isArray(delivery)) { return Object.entries(asMap(delivery, 'Delivery')); }
|
|
81
|
+
return delivery.flatMap((route) => {
|
|
82
|
+
return ids(asMap(route, 'Delivery group').streams, 'streams in delivery group', known).map((stream) => {
|
|
83
|
+
return [stream, route];
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function routeDestinations(id, route, stream, servers, owners) {
|
|
89
|
+
const { to, viaApi } = asMap(route, `Delivery ${id}`);
|
|
90
|
+
if (Object.hasOwn(route, 'from')) { throw new TypeError(`Delivery ${id} cannot declare from`); }
|
|
91
|
+
if (viaApi !== undefined && (typeof viaApi !== 'string' || !viaApi)) { throw new TypeError(`Invalid delivery API for ${id}`); }
|
|
92
|
+
const targets = Array.isArray(to) ? to : [to];
|
|
93
|
+
const local = targets.some(localStateReference);
|
|
94
|
+
const dynamic = local && stream.path === undefined;
|
|
95
|
+
const home = localOwner(owners, stream.path);
|
|
96
|
+
const resolved = targets.filter((target) => { return !localStateReference(target); });
|
|
97
|
+
if (local && home !== undefined && !resolved.includes(home)) { resolved.push(home); }
|
|
98
|
+
if (!dynamic || resolved.length > 0) { ids(resolved, `destinations for stream ${id}`, servers); }
|
|
99
|
+
return { to: resolved, viaApi, ...(dynamic && { localState: true }) };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function routes(delivery, streams, servers, owners) {
|
|
103
|
+
const destinations = new Map();
|
|
104
|
+
for (const [id, route] of routeEntries(delivery, streams)) {
|
|
105
|
+
const stream = streams.get(id);
|
|
106
|
+
if (!stream) { throw new TypeError(`Unknown delivery stream ${id}`); }
|
|
107
|
+
if (destinations.has(id)) { throw new TypeError(`Stream ${id} has more than one delivery declaration`); }
|
|
108
|
+
destinations.set(id, routeDestinations(id, route, stream, servers, owners));
|
|
109
|
+
}
|
|
110
|
+
return Object.fromEntries(destinations);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function outputStreams(streams, outputs) {
|
|
114
|
+
const deliverable = new Map(streams);
|
|
115
|
+
for (const [id, output] of Object.entries(outputs)) {
|
|
116
|
+
if (deliverable.has(id)) { throw new TypeError(`Processing output conflicts with stream ${id}`); }
|
|
117
|
+
deliverable.set(id, output);
|
|
118
|
+
}
|
|
119
|
+
const aliases = [...streams.values()].filter(({ output }) => { return output !== undefined; });
|
|
120
|
+
for (const stream of aliases) {
|
|
121
|
+
const prior = deliverable.get(stream.output);
|
|
122
|
+
if (typeof stream.output !== 'string' || !stream.output || (prior && prior.type !== stream.type)) {
|
|
123
|
+
throw new TypeError(`Invalid output alias for stream ${stream.id}`);
|
|
124
|
+
}
|
|
125
|
+
if (!prior) { deliverable.set(stream.output, { type: stream.type }); }
|
|
126
|
+
}
|
|
127
|
+
return deliverable;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function knownControlNodes(nodes, serverIds) {
|
|
131
|
+
const members = new Set(nodes.map(({ id }) => { return id; }));
|
|
132
|
+
if (members.size !== nodes.length) { throw new TypeError('Invalid control membership'); }
|
|
133
|
+
if ([...members].some((id) => { return !serverIds.has(id); })) { throw new TypeError('Unknown control node'); }
|
|
134
|
+
return members;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Resolve named collection inputs without assigning readers to machines. */
|
|
138
|
+
export function resolveCollection(document) {
|
|
139
|
+
const { controlPlane, sources, streams, mappings = {}, servers = [], delivery = {}, externalPublications = {} } = document;
|
|
140
|
+
const serverIds = new Set(servers.map(({ id }) => { return id; }));
|
|
141
|
+
const controlNodes = knownControlNodes(controlPlane?.nodes ?? [], serverIds);
|
|
142
|
+
const resolved = new Map(Object.entries(asMap(streams, 'Streams')).map(([id, stream]) => {
|
|
143
|
+
return [id, resolveStream(id, stream, sources, mappings)];
|
|
144
|
+
}));
|
|
145
|
+
const physical = new Map([...resolved].filter(([, stream]) => { return stream.source.mode !== 'push'; }));
|
|
146
|
+
const objectOwners = placementMap(document, serverIds);
|
|
147
|
+
const workloads = resolveWorkloads(document);
|
|
148
|
+
const outputs = processingOutputs(document, workloads);
|
|
149
|
+
const deliverable = outputStreams(resolved, outputs);
|
|
150
|
+
const destinations = routes(delivery, deliverable, serverIds, objectOwners);
|
|
151
|
+
const readers = resolveReaders(controlPlane?.readers, physical, controlNodes, objectOwners);
|
|
152
|
+
return {
|
|
153
|
+
readers, delivery: destinations, objectOwners, externalPublications, outputs,
|
|
154
|
+
events: [...eventRoutes({ document, streams: resolved, outputs, delivery: destinations, owners: objectOwners, workloads }),
|
|
155
|
+
...readerEvents(readers, deliverable, destinations, objectOwners)]
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
function machineType(template, types) {
|
|
2
|
+
return Object.entries(types).find(([, type]) => { return type.signalTemplates?.includes(template); })?.[0];
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function machinesAt(nodes, types, parent = '') {
|
|
6
|
+
return (nodes ?? []).flatMap(({ id, template, nodes: children }) => {
|
|
7
|
+
const path = parent ? `${parent}/${id}` : id;
|
|
8
|
+
const type = template && machineType(template, types);
|
|
9
|
+
return [...(type ? [{ id, path, type }] : []), ...machinesAt(children, types, path)];
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Load profiles by the machine type that uses them, sharing profile files when possible. */
|
|
14
|
+
export function readProcessingProfiles(document, load, parse) {
|
|
15
|
+
const files = new Map();
|
|
16
|
+
const profiles = Object.entries(document.machineTypes ?? {}).flatMap(([type, { processingProfile }]) => {
|
|
17
|
+
if (processingProfile === undefined) { return []; }
|
|
18
|
+
if (typeof processingProfile !== 'string' || !processingProfile.trim()) {
|
|
19
|
+
throw new TypeError(`Invalid processing profile for ${type}`);
|
|
20
|
+
}
|
|
21
|
+
if (!files.has(processingProfile)) { files.set(processingProfile, parse(load(processingProfile), load)); }
|
|
22
|
+
return [[type, files.get(processingProfile)]];
|
|
23
|
+
});
|
|
24
|
+
return profiles.length ? { ...document, processingProfiles: Object.fromEntries(profiles) } : document;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function chooseMachines(all, id, selector) {
|
|
28
|
+
const matches = selector?.type
|
|
29
|
+
? all.filter(({ type }) => { return type === selector.type; })
|
|
30
|
+
: all.filter(({ id: short, path }) => { return id === short || id === path; });
|
|
31
|
+
if (!matches.length || (!selector?.type && matches.length !== 1)) {
|
|
32
|
+
throw new TypeError(`Invalid workload machine assignment ${id}`);
|
|
33
|
+
}
|
|
34
|
+
return matches;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function assignmentEntries(all, workload, serverIds) {
|
|
38
|
+
const nodes = workload.placement?.nodes;
|
|
39
|
+
if (!Array.isArray(nodes) || !nodes.length || new Set(nodes).size !== nodes.length || nodes.some((node) => { return !serverIds.has(node); })) {
|
|
40
|
+
throw new TypeError('Invalid workload placement');
|
|
41
|
+
}
|
|
42
|
+
return Object.entries(workload.assignments ?? {}).map(([id, assignment]) => {
|
|
43
|
+
const { preferred, standby } = assignment.placement ?? {};
|
|
44
|
+
const eligible = [preferred, standby].filter((node) => { return node !== undefined; });
|
|
45
|
+
if (!eligible.length || new Set(eligible).size !== eligible.length || eligible.some((node) => { return !nodes.includes(node); })) {
|
|
46
|
+
throw new TypeError(`Invalid workload placement for ${id}`);
|
|
47
|
+
}
|
|
48
|
+
return { id, nodes: eligible, machines: chooseMachines(all, id, assignment.machines) };
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Bind logical processing work to machine types without tying runtime ownership to its prerequisites. */
|
|
53
|
+
export function resolveWorkloads(document) {
|
|
54
|
+
const all = machinesAt(document.namespace, document.machineTypes ?? {});
|
|
55
|
+
const serverIds = new Set((document.servers ?? []).map(({ id }) => { return id; }));
|
|
56
|
+
return Object.fromEntries(Object.entries(document.controlPlane?.workloads ?? {}).map(([id, workload]) => {
|
|
57
|
+
const { run } = workload;
|
|
58
|
+
if (typeof run !== 'string' || !run) { throw new TypeError(`Workload ${id} requires a runtime`); }
|
|
59
|
+
const assignments = assignmentEntries(all, workload, serverIds);
|
|
60
|
+
return [id, { run, nodes: workload.placement.nodes, assignments }];
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function addOutputs(outputs, declarations, producer) {
|
|
65
|
+
for (const [id, declaration] of Object.entries(declarations ?? {})) {
|
|
66
|
+
const prior = outputs.get(id);
|
|
67
|
+
if (typeof declaration?.type !== 'string' || !declaration.type || (prior && declaration.type !== prior.type)) {
|
|
68
|
+
throw new TypeError(`Invalid or conflicting processing output ${id}`);
|
|
69
|
+
}
|
|
70
|
+
if (!prior) { outputs.set(id, { type: declaration.type, producers: [] }); }
|
|
71
|
+
outputs.get(id).producers.push(producer);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Map output streams to their processing workloads and eligible publishers. */
|
|
76
|
+
export function processingOutputs(document, workloads = resolveWorkloads(document)) {
|
|
77
|
+
const outputs = new Map();
|
|
78
|
+
for (const [workload, { assignments }] of Object.entries(workloads)) {
|
|
79
|
+
for (const { machines, nodes } of assignments) {
|
|
80
|
+
for (const { path: machine, type } of machines) {
|
|
81
|
+
addOutputs(outputs, document.processingProfiles?.[type]?.outputs, { workload, machine, nodes });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return Object.fromEntries(outputs);
|
|
86
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import deploymentTemplate from '../../infrastructure/deploy/deploymentTemplate.js';
|
|
2
|
+
import { profileDefinitionFor } from './releaseNodes.js';
|
|
3
|
+
import { expandProfiles } from './expandProfiles.js';
|
|
4
|
+
|
|
5
|
+
function resources(cpu, memory, maxCpu, maxMemory) {
|
|
6
|
+
return { requests: { cpu, memory }, limits: { cpu: maxCpu, memory: maxMemory } };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const controlDefaults = {
|
|
10
|
+
authUsernameKey: 'username', authPasswordKey: 'password', replacementDelayMs: 30000, clockSkewMs: 5000,
|
|
11
|
+
initialClusterState: 'new', autoCompactionRetention: '1h', storage: '4Gi', storageClass: 'local-path',
|
|
12
|
+
queueMaxBytes: 16777216, queueMaxRecords: 8192,
|
|
13
|
+
agentResources: resources('100m', '128Mi', '1', '640Mi'),
|
|
14
|
+
memberResources: resources('100m', '96Mi', '500m', '256Mi'),
|
|
15
|
+
controllerResources: resources('25m', '32Mi', '250m', '128Mi'),
|
|
16
|
+
historyResources: resources('50m', '96Mi', '500m', '384Mi')
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function nodeSettings(node, deployment) {
|
|
20
|
+
const profile = node.agentProfile;
|
|
21
|
+
const profiles = deployment.agentProfiles ?? {};
|
|
22
|
+
if (profile !== undefined && !Object.hasOwn(profiles, profile)) {
|
|
23
|
+
throw new TypeError(`Unknown control agent profile ${profile}`);
|
|
24
|
+
}
|
|
25
|
+
const clientTlsSecret = node.clientTlsSecret ?? (deployment.nodeTlsSecretTemplate
|
|
26
|
+
&& deploymentTemplate(deployment.nodeTlsSecretTemplate, { node: node.id }));
|
|
27
|
+
return {
|
|
28
|
+
...node,
|
|
29
|
+
capacity: node.capacity ?? 100,
|
|
30
|
+
...(profile !== undefined && { agent: { ...profiles[profile], ...node.agent } }),
|
|
31
|
+
...(clientTlsSecret && { clientTlsSecret })
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Supply runtime defaults for a named control-plane declaration. */
|
|
36
|
+
export default function normalizeControlPlane(raw) {
|
|
37
|
+
if (raw?.readers === undefined) { return raw; }
|
|
38
|
+
const { deployment = {}, nodes = [], ...control } = raw;
|
|
39
|
+
const { images = {} } = deployment;
|
|
40
|
+
return {
|
|
41
|
+
version: 1, controlEpoch: 'initial', writerEpoch: 1, defaultCopies: 1,
|
|
42
|
+
etcdImage: images.etcd,
|
|
43
|
+
controllerImage: images.control,
|
|
44
|
+
agentImage: images.control,
|
|
45
|
+
supervisorImage: images.supervisor,
|
|
46
|
+
receiverImage: images.historyReceiver,
|
|
47
|
+
classifyApply: raw.classification !== undefined,
|
|
48
|
+
historyEnabled: raw.history !== undefined,
|
|
49
|
+
...control,
|
|
50
|
+
deployment,
|
|
51
|
+
nodes: nodes.map((node) => { return nodeSettings(node, deployment); })
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function serverSettings(document, node) {
|
|
56
|
+
const server = document.servers?.find(({ id }) => { return id === node.id; });
|
|
57
|
+
const profile = server?.profile && profileDefinitionFor(document, server);
|
|
58
|
+
const agent = profile?.apps?.scada?.collector?.controlAgent;
|
|
59
|
+
return { ...node, ...(agent && { agent: { ...agent, ...node.agent } }) };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Bind a named topology to its installation's Kubernetes, credentials and release settings. */
|
|
63
|
+
export function bindControlInstallation(document, installation, images = {}) {
|
|
64
|
+
const plane = document.controlPlane;
|
|
65
|
+
const { etcd = {}, secrets = {}, ...release } = installation;
|
|
66
|
+
const deployment = {
|
|
67
|
+
nodeTlsSecretTemplate: secrets.nodeTls, catalogSecret: secrets.catalog, deliverySecret: secrets.delivery,
|
|
68
|
+
...plane.deployment, images: { ...plane.deployment?.images, ...images }
|
|
69
|
+
};
|
|
70
|
+
const input = { ...document, profiles: expandProfiles(document.profiles ?? {}) };
|
|
71
|
+
return normalizeControlPlane({
|
|
72
|
+
...controlDefaults, ...release, ...etcd,
|
|
73
|
+
controllerAuthSecret: secrets.controllerAuth, controllerTlsSecret: secrets.controllerTls,
|
|
74
|
+
controllerEnabled: true, classifyApply: Object.values(plane.workloads ?? {}).some(({ assignments }) => {
|
|
75
|
+
return Object.keys(assignments ?? {}).length > 0;
|
|
76
|
+
}),
|
|
77
|
+
...plane, deployment,
|
|
78
|
+
nodes: (plane.nodes ?? []).map((node) => { return serverSettings(input, node); })
|
|
79
|
+
});
|
|
80
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { resolveCollection } from './namedCollection.js';
|
|
2
|
+
import { fits, zipped } from './expandPattern.js';
|
|
3
|
+
import { profileDefinitionFor } from './releaseNodes.js';
|
|
4
|
+
|
|
5
|
+
function addPublications(servers, declarations) {
|
|
6
|
+
for (const [node, publications] of Object.entries(declarations)) {
|
|
7
|
+
const server = servers.get(node);
|
|
8
|
+
if (!server) { throw new TypeError(`Unknown publication node ${node}`); }
|
|
9
|
+
for (const [id, publishes] of Object.entries(publications)) {
|
|
10
|
+
if (!server.api?.some((api) => { return api.id === id; })) { throw new TypeError(`Unknown publication API ${id} on ${node}`); }
|
|
11
|
+
if (!Array.isArray(publishes)) { throw new TypeError(`Invalid publications on ${node} API ${id}`); }
|
|
12
|
+
server.api = server.api.map((api) => { return api.id === id ? { ...api, publishes: [...(api.publishes ?? []), ...publishes] } : api; });
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function addRoute(servers, origin, stream, route, brokerApi) {
|
|
18
|
+
if (!route) { return; }
|
|
19
|
+
const api = route.viaApi ?? origin.api?.find((entry) => { return entry.id === brokerApi; })?.id;
|
|
20
|
+
for (const id of route.to.filter((target) => { return target !== origin.id; })) {
|
|
21
|
+
const target = servers.get(id);
|
|
22
|
+
target.fan_in.push({ from: origin.id, name: stream.path, ...(api && { api }) });
|
|
23
|
+
origin.fan_out.push({ name: stream.path, to: id });
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function publicationApi(publications, path, viaApi) {
|
|
28
|
+
return Object.entries(publications).find(([name, entries]) => {
|
|
29
|
+
return (!viaApi || viaApi === name) && entries.some(({ name: pattern }) => {
|
|
30
|
+
return zipped(pattern).some((part) => { return fits(part, path); });
|
|
31
|
+
});
|
|
32
|
+
})?.[0];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function addExternalRoutes(servers, primary, stream, route, declarations) {
|
|
36
|
+
if (!route) { return; }
|
|
37
|
+
for (const [id, publications] of Object.entries(declarations)) {
|
|
38
|
+
if (id !== primary) {
|
|
39
|
+
const api = publicationApi(publications, stream.path, route.viaApi);
|
|
40
|
+
if (api) { addRoute(servers, servers.get(id), stream, { ...route, viaApi: api }); }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function deliveryApi(plane, objectOwners, origin, path) {
|
|
46
|
+
const { addressApi = 'plant', brokerApi = 'bus' } = plane.deployment ?? {};
|
|
47
|
+
const owns = Object.entries(objectOwners).some(([object, owner]) => {
|
|
48
|
+
return owner === origin.id && (path === object || path.startsWith(`${object}/`));
|
|
49
|
+
});
|
|
50
|
+
return owns && origin.api?.some(({ id }) => { return id === addressApi; }) ? addressApi : brokerApi;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function brokerServices(servers, document) {
|
|
54
|
+
const plane = document.controlPlane;
|
|
55
|
+
const { addressApi = 'plant', brokerApi = 'bus' } = plane.deployment ?? {};
|
|
56
|
+
for (const { id } of plane.nodes) {
|
|
57
|
+
const server = servers.get(id);
|
|
58
|
+
const peer = server.profile && profileDefinitionFor(document, server).modules?.message_bus?.peerService;
|
|
59
|
+
const port = peer?.nodePort ?? plane.brokerService?.nodePort;
|
|
60
|
+
const address = server.api?.find(({ id: api }) => { return api === addressApi; });
|
|
61
|
+
if (port && address && !server.api.some(({ id: api }) => { return api === brokerApi; })) {
|
|
62
|
+
const { hostname } = new URL(address.url);
|
|
63
|
+
server.api = [...server.api, { id: brokerApi, url: `amqp://${hostname}:${port}` }];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function routeServers(declarations, objectOwners) {
|
|
69
|
+
const servers = new Map(declarations.map((server) => {
|
|
70
|
+
if (Object.hasOwn(server, 'fan_in') || Object.hasOwn(server, 'fan_out')) {
|
|
71
|
+
throw new TypeError(`Server ${server.id} cannot declare flows alongside control readers`);
|
|
72
|
+
}
|
|
73
|
+
return [server.id, { ...server, fan_in: [], fan_out: [], objects: [...(server.objects ?? [])] }];
|
|
74
|
+
}));
|
|
75
|
+
for (const [path, id] of Object.entries(objectOwners)) {
|
|
76
|
+
const server = servers.get(id);
|
|
77
|
+
if (!server) { throw new TypeError(`Unknown owner ${id} for ${path}`); }
|
|
78
|
+
if (!server.objects.includes(path)) { server.objects.push(path); }
|
|
79
|
+
}
|
|
80
|
+
return servers;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Derive the static transport view used by runtime and broker plan consumers. */
|
|
84
|
+
export default function projectCollectionRoutes(document) {
|
|
85
|
+
if (document.controlPlane?.readers === undefined) { return document; }
|
|
86
|
+
const { readers, delivery, objectOwners, externalPublications, events } = resolveCollection(document);
|
|
87
|
+
const servers = routeServers(document.servers, objectOwners);
|
|
88
|
+
brokerServices(servers, document);
|
|
89
|
+
addPublications(servers, externalPublications);
|
|
90
|
+
for (const { eligible: [primary], streams } of readers) {
|
|
91
|
+
const origin = servers.get(primary);
|
|
92
|
+
for (const stream of streams.filter(({ from }) => { return typeof from === 'string'; })) {
|
|
93
|
+
const { from, path: name, mapping, sampling } = stream;
|
|
94
|
+
origin.fan_in.push({ from, name, ...(mapping !== undefined && { mapping }), ...(sampling !== undefined && { sampling }) });
|
|
95
|
+
const route = delivery[stream.id];
|
|
96
|
+
addRoute(servers, origin, stream, route, deliveryApi(document.controlPlane, objectOwners, origin, stream.path));
|
|
97
|
+
addExternalRoutes(servers, primary, stream, route, externalPublications);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return { ...document, servers: [...servers.values()], eventDelivery: events };
|
|
101
|
+
}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { collectorMemoryDeployment, etcdMembership } from './controlPlan.js';
|
|
2
2
|
import controlPlaneConfiguration from './controlPlaneConfiguration.js';
|
|
3
3
|
import { controlSecretKey } from './compileControlCatalog.js';
|
|
4
|
+
import projectCollectionRoutes from './projectCollectionRoutes.js';
|
|
5
|
+
import normalizeControlPlane from './normalizeControlPlane.js';
|
|
4
6
|
|
|
5
7
|
function hostDevices(servers, node) {
|
|
6
8
|
const streams = servers.find(({ id }) => { return id === node; })?.fan_in ?? [];
|
|
@@ -33,8 +35,8 @@ function chartValues(config, server, servers, policy, ports) {
|
|
|
33
35
|
|
|
34
36
|
/** Project one control release from a plant document and supplied policy and runtime bindings. */
|
|
35
37
|
export default function projectControlPlane(cluster, document, policy, ports) {
|
|
36
|
-
const { servers } = document;
|
|
37
|
-
const config = controlPlaneConfiguration(document[policy.section], servers, policy);
|
|
38
|
+
const { servers } = projectCollectionRoutes(document);
|
|
39
|
+
const config = controlPlaneConfiguration(normalizeControlPlane(document[policy.section]), servers, policy);
|
|
38
40
|
const server = config.nodes.find(({ id }) => { return id === cluster; });
|
|
39
41
|
if (!server) { throw new TypeError(`Unknown edge ${cluster}`); }
|
|
40
42
|
const catalog = ports.catalog(document, config);
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import readDocumentFile, { selectDeploymentPhase } from './readDocumentFile.js';
|
|
2
|
+
import readDocument from './readDocument.js';
|
|
2
3
|
import resolveDeploymentRouting from './deploymentRouting.js';
|
|
4
|
+
import { readProcessingProfiles } from './namedProcessing.js';
|
|
3
5
|
|
|
4
6
|
function fileSelection(file) {
|
|
5
7
|
if (file instanceof URL) {
|
|
@@ -9,10 +11,36 @@ function fileSelection(file) {
|
|
|
9
11
|
return fragment < 0 ? { path: file } : { path: file.slice(0, fragment), phase: file.slice(fragment + 1) };
|
|
10
12
|
}
|
|
11
13
|
|
|
14
|
+
function controlFragments(document, load, parse) {
|
|
15
|
+
const { fragments, ...controlPlane } = document.controlPlane ?? {};
|
|
16
|
+
if (fragments === undefined) { return document; }
|
|
17
|
+
if (!Array.isArray(fragments) || fragments.some((name) => { return typeof name !== 'string' || !name.trim(); })) {
|
|
18
|
+
throw new TypeError('Control plane fragments must be paths');
|
|
19
|
+
}
|
|
20
|
+
for (const name of fragments) {
|
|
21
|
+
const fragment = parse(load(name), load);
|
|
22
|
+
const section = fragment?.controlPlane;
|
|
23
|
+
if (Object.keys(fragment ?? {}).length !== 1 || !section || typeof section !== 'object' || Array.isArray(section)) {
|
|
24
|
+
throw new TypeError(`Control plane fragment ${name} must declare controlPlane`);
|
|
25
|
+
}
|
|
26
|
+
for (const [key, value] of Object.entries(section)) {
|
|
27
|
+
if (key === 'fragments' || Object.hasOwn(controlPlane, key)) {
|
|
28
|
+
throw new TypeError(`Duplicate control plane key ${key} in ${name}`);
|
|
29
|
+
}
|
|
30
|
+
Object.defineProperty(controlPlane, key, { value, enumerable: true, configurable: true, writable: true });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return { ...document, controlPlane };
|
|
34
|
+
}
|
|
35
|
+
|
|
12
36
|
/** A deployment document can declare its phase policy and be opened as path#phase. */
|
|
13
|
-
export default function readDeploymentDocument(file, parse) {
|
|
37
|
+
export default function readDeploymentDocument(file, parse = readDocument) {
|
|
14
38
|
const { path, phase } = fileSelection(file);
|
|
15
|
-
const
|
|
39
|
+
const loaded = readDocumentFile(path, (text, load) => {
|
|
40
|
+
const document = controlFragments(parse(text, load), load, parse);
|
|
41
|
+
return readProcessingProfiles(document, load, parse);
|
|
42
|
+
});
|
|
43
|
+
const { deploymentPhase: policy, ...document } = resolveDeploymentRouting(loaded);
|
|
16
44
|
if (policy === undefined) {
|
|
17
45
|
if (phase !== undefined) { throw new TypeError('Deployment phase policy is missing'); }
|
|
18
46
|
return document;
|
|
@@ -110,6 +110,7 @@ export default async function plantServer(config) {
|
|
|
110
110
|
heartbeat: config.heartbeat || 1000,
|
|
111
111
|
requestTimeoutMs,
|
|
112
112
|
extraRoutes: extra,
|
|
113
|
+
tagCatalog: config.tagCatalog,
|
|
113
114
|
timelineOperator: config.timelineOperator,
|
|
114
115
|
operationDecisions: config.operationDecisions,
|
|
115
116
|
owners: config.owners,
|
|
@@ -21,16 +21,32 @@ export async function collectorLaunch(options, registry, manifest, { run = runCo
|
|
|
21
21
|
return null;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
function checkAssignmentSource(source, options, argv, env) {
|
|
25
|
+
if (source === undefined) { return; }
|
|
26
|
+
if (source !== 'controlPlane') { throw new TypeError('Unknown collector assignment source'); }
|
|
27
|
+
if (options) {
|
|
28
|
+
if (!options.tasks.length) { throw new TypeError('Control-managed collectors only support explicit DSL selection for named tasks'); }
|
|
29
|
+
if (!options.render && env.COLLECTOR_CONTROL_ENDPOINTS) { throw new TypeError('Cannot combine a named task with a controller'); }
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (argv.length || env.COLLECTOR_MANIFEST || env.COLLECTOR_MANIFEST_PATH) {
|
|
33
|
+
throw new TypeError('Control-managed collectors cannot start an independent assignment manifest');
|
|
34
|
+
}
|
|
35
|
+
if (!env.COLLECTOR_CONTROL_ENDPOINTS) { throw new TypeError('Control-managed collectors require control endpoints'); }
|
|
36
|
+
}
|
|
37
|
+
|
|
24
38
|
/** Run the collector command with the application's registry and manifest bindings. */
|
|
25
39
|
export async function launchCollectorCommand(argv, {
|
|
26
|
-
registry, manifest, delivery, env = process.env, run = runCollector,
|
|
40
|
+
registry, manifest, delivery, assignmentSource, env = process.env, run = runCollector,
|
|
27
41
|
requireOnce = false,
|
|
28
42
|
write = (body) => { process.stdout.write(body); }
|
|
29
43
|
}) {
|
|
44
|
+
const options = argv.some((arg) => { return arg.startsWith('--'); }) ? collectorOptions(argv) : undefined;
|
|
45
|
+
checkAssignmentSource(assignmentSource, options, argv, env);
|
|
30
46
|
controlDeliveryEnvironment(env, delivery);
|
|
31
47
|
const adapters = registry();
|
|
32
|
-
if (
|
|
33
|
-
const result = await collectorLaunch(
|
|
48
|
+
if (options) {
|
|
49
|
+
const result = await collectorLaunch(options, adapters, manifest, { run, env, requireOnce });
|
|
34
50
|
if (result) { write(`${JSON.stringify(result, null, 2)}\n`); }
|
|
35
51
|
} else {
|
|
36
52
|
if (requireOnce) { throw new Error('collector jobs require explicit DSL selection'); }
|
|
@@ -167,6 +167,7 @@ export default async function siteServer(config) {
|
|
|
167
167
|
port: config.port || parseInt(env.PORT || '3000', 10),
|
|
168
168
|
basePath,
|
|
169
169
|
translations: config.translations,
|
|
170
|
+
tagCatalog: config.tagCatalog,
|
|
170
171
|
requirePool: config.requirePool,
|
|
171
172
|
stomp: stompFromEnv(env),
|
|
172
173
|
plantFactory: plantFactoryWithOperations(config.plantFactory, ops, sink),
|
|
@@ -3,19 +3,20 @@ import { readFileSync } from 'node:fs';
|
|
|
3
3
|
/**
|
|
4
4
|
* Loads hierarchical tag catalog entries from a configured JSON file.
|
|
5
5
|
*
|
|
6
|
-
* Uses
|
|
6
|
+
* Uses the declared catalog when TAG_CATALOG_PATH is unset.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* const catalog = tagCatalog({ TAG_CATALOG_PATH: '/etc/scada/tag-catalog.json' });
|
|
10
10
|
* catalog.labels();
|
|
11
11
|
*
|
|
12
12
|
* @param {object} [env] - environment map, defaults to process.env
|
|
13
|
+
* @param {Array<object>} [fallback] - declared catalog rows
|
|
13
14
|
* @returns {object} catalog with entries(), labels(), children(parent)
|
|
14
15
|
*/
|
|
15
|
-
export default function tagCatalog(env) {
|
|
16
|
+
export default function tagCatalog(env, fallback = []) {
|
|
16
17
|
const source = env || process.env;
|
|
17
18
|
const path = source.TAG_CATALOG_PATH;
|
|
18
|
-
const raw = path ? JSON.parse(readFileSync(path, 'utf8')) :
|
|
19
|
+
const raw = path ? JSON.parse(readFileSync(path, 'utf8')) : fallback.slice();
|
|
19
20
|
if (!Array.isArray(raw)) {
|
|
20
21
|
throw new Error('tag catalog must be a JSON array');
|
|
21
22
|
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { expandProfiles } from '../../application/deploy/expandProfiles.js';
|
|
2
|
+
import { profileFor } from '../../application/deploy/releaseNodes.js';
|
|
1
3
|
import readDeploymentDocument from '../../application/deploy/readDeploymentDocument.js';
|
|
2
4
|
import centralTelemetryPolicy, { centralTelemetryVersion, normalizeCentralPolicy } from '../ingest/telemetry/centralPolicy.js';
|
|
3
5
|
|
|
@@ -5,7 +7,14 @@ const policyRuntime = { centralTelemetryPolicy, centralTelemetryVersion };
|
|
|
5
7
|
const metricsAdapters = new Set(['amqp-metrics', 'mqtt-metrics', 'amqp-pg-metrics', 'mqtt-pg-metrics', 'silent-streams']);
|
|
6
8
|
|
|
7
9
|
export function centralTelemetryValues(document) {
|
|
8
|
-
return normalizeCentralPolicy(document.centralTelemetry);
|
|
10
|
+
if (Object.hasOwn(document, 'centralTelemetry')) { return normalizeCentralPolicy(document.centralTelemetry); }
|
|
11
|
+
const profiles = expandProfiles(document.profiles ?? {});
|
|
12
|
+
const stores = (document.servers ?? []).filter(({ profile }) => { return profile?.id && profiles[profile.id]; }).map((server) => {
|
|
13
|
+
return profileFor({ ...document, profiles }, server).modules?.telemetry_store;
|
|
14
|
+
}).filter((store) => { return store?.policy && store.enabled !== false; });
|
|
15
|
+
const store = stores[0] ?? profiles.archive?.modules?.telemetry_store;
|
|
16
|
+
if (stores.length > 1) { throw new TypeError('Central telemetry policy requires a single archive store'); }
|
|
17
|
+
return normalizeCentralPolicy(store?.policy ? { enabled: store.enabled, ...store.policy } : undefined);
|
|
9
18
|
}
|
|
10
19
|
|
|
11
20
|
export function centralTelemetryChartEnv(document, file, previous = []) {
|
|
@@ -16,7 +16,7 @@ function compileCollectors(context, policy, ports) {
|
|
|
16
16
|
const machines = scada.machines.filter(({ from }) => {
|
|
17
17
|
return !policy.excludedSources.some((prefix) => { return from.startsWith(prefix); });
|
|
18
18
|
});
|
|
19
|
-
const sources = ports.processes(machines, app);
|
|
19
|
+
const sources = ports.processes(machines, app, context);
|
|
20
20
|
const initial = sources[0];
|
|
21
21
|
const defaults = initial ? deploymentTemplate(policy.environment, target) : {};
|
|
22
22
|
const environment = Object.fromEntries(Object.entries(defaults).map(([key, value]) => {
|
|
@@ -46,7 +46,7 @@ export default function configuredBundleCapabilities(policy, runtime) {
|
|
|
46
46
|
try {
|
|
47
47
|
installBundle(artifact, volume, execute, kube);
|
|
48
48
|
const candidates = bundleCandidates(policy.applicationBundle, runtime, artifact, volume);
|
|
49
|
-
const
|
|
49
|
+
const protocol = deploymentTemplate(policy.applicationBundle.protocol ?? policy.selection.protocol, plan);
|
|
50
50
|
verifyRuntimeCapabilities({ candidates, required: [protocol], match: policy.match,
|
|
51
51
|
subject: policy.subject, mismatch: policy.mismatch }, execute);
|
|
52
52
|
} finally {
|
|
@@ -100,7 +100,9 @@ export default function configuredControlPlane(policy, ports) {
|
|
|
100
100
|
return function plan(cluster, document = null) {
|
|
101
101
|
const source = document || ports.read();
|
|
102
102
|
return projectControlPlane(cluster, source, policy, {
|
|
103
|
-
catalog(input, config) {
|
|
103
|
+
catalog(input, config) {
|
|
104
|
+
return (ports.catalog ?? compileControlCatalog)(input, config, readers);
|
|
105
|
+
},
|
|
104
106
|
classification(input, config, catalog) { return associatedTasks(ports.classification(input, config), catalog); },
|
|
105
107
|
replicas(config, input) {
|
|
106
108
|
const central = serverFor(input, policy.central);
|
|
@@ -29,6 +29,12 @@ export { default as preflightKubeStorage } from './preflightKubeStorage.js';
|
|
|
29
29
|
export { default as applyWhenChanged } from './applyWhenChanged.js';
|
|
30
30
|
export { default as prepareKubePrerequisites } from './prepareKubePrerequisites.js';
|
|
31
31
|
export { default as readDeploymentDocument } from '../../application/deploy/readDeploymentDocument.js';
|
|
32
|
+
export { resolveCollection } from '../../application/deploy/namedCollection.js';
|
|
33
|
+
export { resolveWorkloads } from '../../application/deploy/namedProcessing.js';
|
|
34
|
+
export { default as compileNamedControlCatalog } from '../../application/deploy/compileNamedControlCatalog.js';
|
|
35
|
+
export { default as compileLocalConsumers, namedLocalConsumers } from '../../application/deploy/compileLocalConsumers.js';
|
|
36
|
+
export { default as normalizeControlPlane, bindControlInstallation } from '../../application/deploy/normalizeControlPlane.js';
|
|
37
|
+
export { profileDefinitionFor } from '../../application/deploy/releaseNodes.js';
|
|
32
38
|
export { sourceAddress } from '../../application/deploy/deploymentRouting.js';
|
|
33
39
|
export { appendReplicaTopology, replicaShovels, brokerReplicaPlan, appendBrokerReplicas } from './brokerReplicaTopology.js';
|
|
34
40
|
export { default as compileDeploymentHooks } from './compileDeploymentHooks.js';
|
|
@@ -67,7 +67,9 @@ function collector(state, policy, ports) {
|
|
|
67
67
|
const process = deploymentTemplate(policy.collectorProcess, { app, collectorEnv, collectorResources: resources });
|
|
68
68
|
return { values: { processes: [ports.controlCollector(document, cluster, process)] },
|
|
69
69
|
...(controlCatalog && { controlCatalog, cutoverTasks: controlCatalog.tasks.filter((task) => {
|
|
70
|
-
|
|
70
|
+
if (task.pinnedNode) { return false; }
|
|
71
|
+
return document.controlPlane?.readers ? task.preference?.[0] === cluster
|
|
72
|
+
: assignments.some(({ taskId }) => { return taskId === task.taskId; });
|
|
71
73
|
}) }) };
|
|
72
74
|
}
|
|
73
75
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { validateConnections } from './config.js';
|
|
2
2
|
|
|
3
3
|
/** Selects sampling policy by stream path while the fan-in remains the source inventory. */
|
|
4
|
-
function timing(name, config) {
|
|
5
|
-
const match = (config.sampling || []).find((item) => {
|
|
4
|
+
function timing({ name, sampling }, config) {
|
|
5
|
+
const match = sampling ?? (config.sampling || []).find((item) => {
|
|
6
6
|
const parts = item.name.split('/');
|
|
7
7
|
const path = name.split('/');
|
|
8
8
|
return parts.length === path.length && parts.every((part, index) => {
|
|
@@ -18,7 +18,7 @@ function timing(name, config) {
|
|
|
18
18
|
export default function projectOpcuaSources(rows, config, deviceFor) {
|
|
19
19
|
const connections = new Map();
|
|
20
20
|
rows.forEach((row) => {
|
|
21
|
-
const intervals = timing(row
|
|
21
|
+
const intervals = timing(row, config);
|
|
22
22
|
const connection = connections.get(row.from) || { url: row.from, ...intervals, channels: [] };
|
|
23
23
|
if (connection.publishMs !== intervals.publishMs || connection.heartbeatMs !== intervals.heartbeatMs || connection.healthRequired !== intervals.healthRequired) {
|
|
24
24
|
throw new Error(`incompatible OPC UA sampling policies at ${row.from}`);
|