hammerkit 2.0.0 → 2.1.0-next.2
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/build.schema.json +87 -10
- package/dist/docker/item-network.js +6 -5
- package/dist/docker/remove-orphaned-containers.js +5 -4
- package/dist/executer/check-readiness.js +8 -4
- package/dist/executer/docker-healthcheck.js +73 -0
- package/dist/executer/docker-service.js +21 -15
- package/dist/kubernetes/ensure-kubernetes-deployment-exists.js +62 -14
- package/dist/kubernetes/kept-claims.js +12 -9
- package/dist/network/healthcheck-request.js +38 -0
- package/dist/optimizer/work-task-cache-description.js +2 -1
- package/dist/planner/service-definition.js +2 -1
- package/dist/planner/utils/append-work-service.js +12 -4
- package/dist/planner/work-healthcheck.js +23 -2
- package/dist/planner/work-service.js +2 -2
- package/dist/remote/remote-status.js +1 -1
- package/dist/schema/build-file-service-container-healthcheck.js +25 -7
- package/dist/server/borrowed-quota.js +216 -0
- package/dist/server/capacity.js +18 -6
- package/dist/server/kubernetes-backend.js +114 -28
- package/dist/server/runs.js +4 -6
- package/dist/server/server-config.js +59 -2
- package/package.json +1 -1
|
@@ -24,9 +24,9 @@ function getServiceCommand(service, options = {}) {
|
|
|
24
24
|
return { entrypoint: null, cmd: [service.cmd.parsed.command, ...args] };
|
|
25
25
|
}
|
|
26
26
|
exports.getServiceCommand = getServiceCommand;
|
|
27
|
-
// The healthcheck as an exec command, through the service's `shell` if it has one.
|
|
27
|
+
// The `cmd` of the healthcheck as an exec command, through the service's `shell` if it has one.
|
|
28
28
|
function getHealthcheckCommand(service) {
|
|
29
|
-
if (!service.healthcheck) {
|
|
29
|
+
if (!service.healthcheck || !('cmd' in service.healthcheck)) {
|
|
30
30
|
return null;
|
|
31
31
|
}
|
|
32
32
|
const cmd = service.healthcheck.cmd;
|
|
@@ -95,7 +95,7 @@ function describeCapacity(capacity, short = false) {
|
|
|
95
95
|
return lines;
|
|
96
96
|
}
|
|
97
97
|
for (const group of capacity.groups) {
|
|
98
|
-
lines.push(` ${(0, server_client_1.printable)(group.name)} ${group.kind === 'service-run' ? 'service run' : 'caller'}${group.owner ? ` of ${(0, server_client_1.printable)(group.owner.sub)}` : ''}${group.run ? ` ${(0, server_client_1.printable)(group.run)}` : ''} ${(0, capacity_format_1.plural)(group.pods ?? 0, 'pod')} requested ${resources(group.requested)}${group.quota ? ` of ${resources(group.quota)}` : ''}${group.used ? ` in use ${resources(group.used)}` : ''}`);
|
|
98
|
+
lines.push(` ${(0, server_client_1.printable)(group.name)} ${group.kind === 'service-run' ? 'service run' : 'caller'}${group.owner ? ` of ${(0, server_client_1.printable)(group.owner.sub)}` : ''}${group.run ? ` ${(0, server_client_1.printable)(group.run)}` : ''} ${(0, capacity_format_1.plural)(group.pods ?? 0, 'pod')} requested ${resources(group.requested)}${group.quota ? ` of ${resources(group.quota)}` : ''}${group.share ? ` (borrowed, share ${resources(group.share)})` : ''}${group.used ? ` in use ${resources(group.used)}` : ''}`);
|
|
99
99
|
}
|
|
100
100
|
for (const problem of capacity.problems) {
|
|
101
101
|
lines.push(colors_1.default.yellow(` not shown: ${(0, server_client_1.printable)(problem)}`));
|
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildFileServiceContainerHealthcheck = void 0;
|
|
3
|
+
exports.buildFileServiceContainerHealthcheck = exports.buildFileServiceTcpHealthcheck = exports.buildFileServiceHttpHealthcheck = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const duration_schema_1 = require("./duration-schema");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
// How long the service may take to pass its check once its container runs
|
|
7
|
+
// (default 20s, HAMMERKIT_HEALTHCHECK_TIMEOUT changes the default).
|
|
8
|
+
const timeout = duration_schema_1.durationSchema.optional();
|
|
9
|
+
const port = (0, zod_1.number)().int().min(1).max(65535);
|
|
10
|
+
// A request to a port of the service, from its own network: no tool of the image runs it.
|
|
11
|
+
exports.buildFileServiceHttpHealthcheck = (0, zod_1.object)({
|
|
12
|
+
port,
|
|
13
|
+
path: (0, zod_1.string)().startsWith('/', 'path must start with /').optional(),
|
|
14
|
+
scheme: (0, zod_1.enum)(['http', 'https']).optional(),
|
|
15
|
+
// the status codes that count as healthy, any 2xx or 3xx without
|
|
16
|
+
status: (0, zod_1.array)((0, zod_1.number)().int().min(100).max(599)).min(1).optional(),
|
|
17
|
+
})
|
|
18
|
+
.strict()
|
|
19
|
+
.describe('GET <scheme>://<service>:<port><path>, healthy on one of status (default any 2xx or 3xx)');
|
|
20
|
+
// a connection to a port of the service
|
|
21
|
+
exports.buildFileServiceTcpHealthcheck = (0, zod_1.object)({ port })
|
|
22
|
+
.strict()
|
|
23
|
+
.describe('healthy once <port> takes a connection');
|
|
24
|
+
// Exactly one of `cmd`, `http` or `tcp`.
|
|
25
|
+
exports.buildFileServiceContainerHealthcheck = (0, zod_1.union)([
|
|
26
|
+
(0, zod_1.object)({ cmd: (0, zod_1.string)(), timeout }).strict(),
|
|
27
|
+
(0, zod_1.object)({ http: exports.buildFileServiceHttpHealthcheck, timeout }).strict(),
|
|
28
|
+
(0, zod_1.object)({ tcp: exports.buildFileServiceTcpHealthcheck, timeout }).strict(),
|
|
29
|
+
]).describe('exactly one of cmd, http or tcp, and how long it may take to pass');
|
|
12
30
|
//# sourceMappingURL=build-file-service-container-healthcheck.js.map
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BorrowedQuotas = exports.readPoolRoom = exports.isPoolNode = exports.getBorrowedQuota = exports.getBorrowedResource = exports.BORROWED_RESOURCES = void 0;
|
|
4
|
+
const log_1 = require("../log");
|
|
5
|
+
const apply_1 = require("../kubernetes/apply");
|
|
6
|
+
const units_1 = require("../utils/units");
|
|
7
|
+
/**
|
|
8
|
+
* A caller's quota above its share (`backend.quota`), up to `backend.maxQuota`: the room of
|
|
9
|
+
* the nodes that no other caller is promised. While a run of a caller executes, the caller is
|
|
10
|
+
* promised its share; the room of the nodes of the pools beyond the shares of the callers with
|
|
11
|
+
* a run executing and the quotas of the service runs that are up is split evenly among those
|
|
12
|
+
* callers, each up to its `maxQuota`. A caller alone on the cluster gets all of it. The server
|
|
13
|
+
* works it out again when a run of a caller or a service run starts or ends, from nothing but
|
|
14
|
+
* which of them execute and what the nodes hold, so the same runs give the same quotas. It only
|
|
15
|
+
* lowers a quota and never evicts: the pods a caller runs above its new quota run on, and its
|
|
16
|
+
* next ones wait for room under the quota as they do under a full one.
|
|
17
|
+
*/
|
|
18
|
+
// The keys of a quota a caller may borrow, by the resource of the nodes whose room they take.
|
|
19
|
+
// `cpu` and `memory` are `requests.cpu` and `requests.memory`, as Kubernetes reads them.
|
|
20
|
+
exports.BORROWED_RESOURCES = {
|
|
21
|
+
cpu: { requests: ['requests.cpu', 'cpu'], limits: ['limits.cpu'] },
|
|
22
|
+
memory: { requests: ['requests.memory', 'memory'], limits: ['limits.memory'] },
|
|
23
|
+
};
|
|
24
|
+
function getBorrowedResource(key) {
|
|
25
|
+
return Object.keys(exports.BORROWED_RESOURCES).find((resource) => {
|
|
26
|
+
const keys = [...exports.BORROWED_RESOURCES[resource].requests, ...exports.BORROWED_RESOURCES[resource].limits];
|
|
27
|
+
return keys.includes(key);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
exports.getBorrowedResource = getBorrowedResource;
|
|
31
|
+
// The key of a quota that holds what the pods of a namespace request of `resource`
|
|
32
|
+
function requestsKey(quota, resource) {
|
|
33
|
+
return exports.BORROWED_RESOURCES[resource].requests.find((key) => quota[key] !== undefined);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The quota of each caller with a run executing: its share, and of each resource of
|
|
37
|
+
* `maxQuota` the part of the room `callers` share evenly beyond what is promised, up to
|
|
38
|
+
* `maxQuota`. Every key of a resource rises by the same part of the way from its share to its
|
|
39
|
+
* `maxQuota`, its limits as its requests. Without the room of the nodes, the share.
|
|
40
|
+
*/
|
|
41
|
+
function getBorrowedQuota(options) {
|
|
42
|
+
const { quota, maxQuota } = options;
|
|
43
|
+
const borrowed = { ...quota };
|
|
44
|
+
const callers = Math.max(1, options.callers);
|
|
45
|
+
for (const resource of Object.keys(exports.BORROWED_RESOURCES)) {
|
|
46
|
+
const key = requestsKey(maxQuota, resource);
|
|
47
|
+
const room = options.room?.[resource];
|
|
48
|
+
if (!key || room === undefined) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const share = (0, units_1.parseQuantity)(quota[key]) ?? 0;
|
|
52
|
+
const max = (0, units_1.parseQuantity)(maxQuota[key]) ?? share;
|
|
53
|
+
if (max <= share) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const promised = callers * share +
|
|
57
|
+
options.serviceRuns.reduce((sum, up) => {
|
|
58
|
+
const of = requestsKey(up, resource);
|
|
59
|
+
return sum + (of ? (0, units_1.parseQuantity)(up[of]) ?? 0 : 0);
|
|
60
|
+
}, 0);
|
|
61
|
+
const part = Math.min(1, Math.max(0, (room - promised) / callers / (max - share)));
|
|
62
|
+
if (part === 0) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
for (const of of [...exports.BORROWED_RESOURCES[resource].requests, ...exports.BORROWED_RESOURCES[resource].limits]) {
|
|
66
|
+
if (maxQuota[of] === undefined) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (part === 1) {
|
|
70
|
+
borrowed[of] = maxQuota[of];
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const from = (0, units_1.parseQuantity)(quota[of]) ?? 0;
|
|
74
|
+
const to = (0, units_1.parseQuantity)(maxQuota[of]) ?? from;
|
|
75
|
+
borrowed[of] = formatQuantity(resource, from + part * (to - from));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return borrowed;
|
|
79
|
+
}
|
|
80
|
+
exports.getBorrowedQuota = getBorrowedQuota;
|
|
81
|
+
// Down to a millicore and a MiB, so that a quota changes only with what it is worked out from
|
|
82
|
+
function formatQuantity(resource, amount) {
|
|
83
|
+
return resource === 'cpu' ? `${Math.floor(amount * 1000)}m` : `${Math.floor(amount / 2 ** 20)}Mi`;
|
|
84
|
+
}
|
|
85
|
+
// Whether a node is one of a pool's, where the pods of tasks and services go
|
|
86
|
+
function isPoolNode(node, pools) {
|
|
87
|
+
if (node.spec?.unschedulable) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
const labels = node.metadata?.labels ?? {};
|
|
91
|
+
return pools.some((pool) => labels['kubernetes.io/os'] === pool.platform.os &&
|
|
92
|
+
labels['kubernetes.io/arch'] === pool.platform.arch &&
|
|
93
|
+
Object.entries(pool.nodeSelector).every(([label, value]) => labels[label] === value));
|
|
94
|
+
}
|
|
95
|
+
exports.isPoolNode = isPoolNode;
|
|
96
|
+
// What the schedulable nodes of the pools hold together
|
|
97
|
+
async function readPoolRoom(instance, pools) {
|
|
98
|
+
const nodes = await instance.coreApi.listNode();
|
|
99
|
+
const room = { cpu: 0, memory: 0 };
|
|
100
|
+
for (const node of nodes.items.filter((candidate) => isPoolNode(candidate, pools))) {
|
|
101
|
+
room.cpu = (room.cpu ?? 0) + ((0, units_1.parseQuantity)(node.status?.allocatable?.cpu) ?? 0);
|
|
102
|
+
room.memory = (room.memory ?? 0) + ((0, units_1.parseQuantity)(node.status?.allocatable?.memory) ?? 0);
|
|
103
|
+
}
|
|
104
|
+
return room;
|
|
105
|
+
}
|
|
106
|
+
exports.readPoolRoom = readPoolRoom;
|
|
107
|
+
/**
|
|
108
|
+
* The quotas of the callers' namespaces while `backend.maxQuota` lets them borrow: which
|
|
109
|
+
* callers have a run executing and which service runs are up, and the quota last written to
|
|
110
|
+
* each namespace. Changes are worked out and written one at a time, in the order they came.
|
|
111
|
+
*/
|
|
112
|
+
class BorrowedQuotas {
|
|
113
|
+
constructor(options) {
|
|
114
|
+
this.options = options;
|
|
115
|
+
// per caller namespace, its runs that execute
|
|
116
|
+
this.runs = new Map();
|
|
117
|
+
// per namespace of a service run that is up, its quota
|
|
118
|
+
this.serviceRuns = new Map();
|
|
119
|
+
// per caller namespace, the quota last written to it
|
|
120
|
+
this.written = new Map();
|
|
121
|
+
this.last = Promise.resolve();
|
|
122
|
+
}
|
|
123
|
+
// A run of the caller of `namespace` executes from now, until `leave`.
|
|
124
|
+
enter(namespace, run) {
|
|
125
|
+
const runs = this.runs.get(namespace) ?? new Set();
|
|
126
|
+
runs.add(run);
|
|
127
|
+
this.runs.set(namespace, runs);
|
|
128
|
+
}
|
|
129
|
+
// The run ended: the quotas are worked out again, the caller's back to its share once its
|
|
130
|
+
// last run ended.
|
|
131
|
+
leave(namespace, run) {
|
|
132
|
+
const runs = this.runs.get(namespace);
|
|
133
|
+
if (!runs?.delete(run)) {
|
|
134
|
+
return this.last;
|
|
135
|
+
}
|
|
136
|
+
if (runs.size === 0) {
|
|
137
|
+
this.runs.delete(namespace);
|
|
138
|
+
}
|
|
139
|
+
return this.apply();
|
|
140
|
+
}
|
|
141
|
+
// A service run is up, with the quota of its namespace, until `serviceRunEnded`.
|
|
142
|
+
serviceRun(namespace, quota) {
|
|
143
|
+
this.serviceRuns.set(namespace, quota ?? {});
|
|
144
|
+
return this.apply();
|
|
145
|
+
}
|
|
146
|
+
serviceRunEnded(namespace) {
|
|
147
|
+
if (!this.serviceRuns.delete(namespace)) {
|
|
148
|
+
return this.last;
|
|
149
|
+
}
|
|
150
|
+
return this.apply();
|
|
151
|
+
}
|
|
152
|
+
// The quotas as they are now, written where they changed; the quota of `own` (the namespace
|
|
153
|
+
// of a run that starts, which it is written into before its tasks run) also when it did
|
|
154
|
+
// not, since the server may have made the namespace since. Writing to `own` fails the call,
|
|
155
|
+
// writing to another caller's namespace is logged.
|
|
156
|
+
apply(own) {
|
|
157
|
+
const next = this.last.then(() => this.write(own));
|
|
158
|
+
this.last = next.catch(() => undefined);
|
|
159
|
+
return next;
|
|
160
|
+
}
|
|
161
|
+
async write(own) {
|
|
162
|
+
const room = await this.options.room().then((room) => {
|
|
163
|
+
this.roomProblem = undefined;
|
|
164
|
+
return room;
|
|
165
|
+
}, (e) => {
|
|
166
|
+
const problem = (0, log_1.getErrorMessage)(e);
|
|
167
|
+
if (problem !== this.roomProblem) {
|
|
168
|
+
this.roomProblem = problem;
|
|
169
|
+
this.options.log?.(`callers borrow no quota, the nodes could not be read: ${problem}`);
|
|
170
|
+
}
|
|
171
|
+
return undefined;
|
|
172
|
+
});
|
|
173
|
+
const callers = [...this.runs.keys()];
|
|
174
|
+
const borrowed = getBorrowedQuota({
|
|
175
|
+
quota: this.options.quota,
|
|
176
|
+
maxQuota: this.options.maxQuota,
|
|
177
|
+
callers: callers.length,
|
|
178
|
+
serviceRuns: [...this.serviceRuns.values()],
|
|
179
|
+
room,
|
|
180
|
+
});
|
|
181
|
+
// those that give back first, then those that borrow
|
|
182
|
+
const quotas = [
|
|
183
|
+
...[...this.written.keys()]
|
|
184
|
+
.filter((namespace) => !this.runs.has(namespace))
|
|
185
|
+
.map((namespace) => [namespace, this.options.quota]),
|
|
186
|
+
...callers.map((namespace) => [namespace, borrowed]),
|
|
187
|
+
];
|
|
188
|
+
for (const [namespace, quota] of quotas) {
|
|
189
|
+
const text = JSON.stringify(quota);
|
|
190
|
+
if (namespace !== own && this.written.get(namespace) === text) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
try {
|
|
194
|
+
await this.options.write(namespace, quota);
|
|
195
|
+
this.written.set(namespace, text);
|
|
196
|
+
}
|
|
197
|
+
catch (e) {
|
|
198
|
+
if (namespace === own) {
|
|
199
|
+
throw e;
|
|
200
|
+
}
|
|
201
|
+
this.written.delete(namespace);
|
|
202
|
+
if ((0, apply_1.statusCodeOf)(e) !== 404) {
|
|
203
|
+
this.options.log?.(`the quota of ${namespace} could not be changed: ${(0, log_1.getErrorMessage)(e)}`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// a caller back at its share needs nothing written until it borrows again
|
|
208
|
+
for (const namespace of [...this.written.keys()]) {
|
|
209
|
+
if (!this.runs.has(namespace)) {
|
|
210
|
+
this.written.delete(namespace);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
exports.BorrowedQuotas = BorrowedQuotas;
|
|
216
|
+
//# sourceMappingURL=borrowed-quota.js.map
|
package/dist/server/capacity.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatMemory = exports.formatCpu = exports.HostCapacity = exports.KubernetesCapacity = exports.cachedCapacity = exports.parseQuantity = void 0;
|
|
3
|
+
exports.formatMemory = exports.formatCpu = exports.HostCapacity = exports.KubernetesCapacity = exports.getQuotaResources = exports.cachedCapacity = exports.parseQuantity = void 0;
|
|
4
4
|
const os_1 = require("os");
|
|
5
5
|
const log_1 = require("../log");
|
|
6
6
|
const apply_1 = require("../kubernetes/apply");
|
|
@@ -71,6 +71,16 @@ function briefly(e, what) {
|
|
|
71
71
|
}
|
|
72
72
|
return what === 'nodes' ? 'nodes not readable' : 'metrics not readable';
|
|
73
73
|
}
|
|
74
|
+
// The CPU and memory pods may request under a quota, if it limits them
|
|
75
|
+
function getQuotaResources(hard) {
|
|
76
|
+
const cpu = (0, units_1.parseQuantity)(hard['requests.cpu'] ?? hard.cpu);
|
|
77
|
+
const memory = (0, units_1.parseQuantity)(hard['requests.memory'] ?? hard.memory);
|
|
78
|
+
if (cpu === undefined && memory === undefined) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
return { ...(cpu !== undefined ? { cpu } : {}), ...(memory !== undefined ? { memory } : {}) };
|
|
82
|
+
}
|
|
83
|
+
exports.getQuotaResources = getQuotaResources;
|
|
74
84
|
const ENDED = ['Succeeded', 'Failed'];
|
|
75
85
|
// On Kubernetes: per namespace of the server, its pods' requests, its quota and its usage.
|
|
76
86
|
class KubernetesCapacity {
|
|
@@ -162,11 +172,13 @@ class KubernetesCapacity {
|
|
|
162
172
|
name: this.options.quotaName,
|
|
163
173
|
namespace,
|
|
164
174
|
});
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
175
|
+
const resources = getQuotaResources(quota.spec?.hard ?? {});
|
|
176
|
+
if (resources) {
|
|
177
|
+
group.quota = resources;
|
|
178
|
+
const share = this.options.share;
|
|
179
|
+
if (kind === 'caller' && share && (share.cpu !== resources.cpu || share.memory !== resources.memory)) {
|
|
180
|
+
group.share = share;
|
|
181
|
+
}
|
|
170
182
|
}
|
|
171
183
|
}
|
|
172
184
|
catch (e) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.awaitRunObjectsGone = exports.removeRunObjects = exports.CallerNamespaces = exports.getVolumeScope = exports.getPrivilegedNamespaceProblem = exports.getCapabilitiesProblem = exports.getPodSecurityLabels = exports.KubernetesExecutor = exports.getRunRoleBinding = exports.removeServiceRunNamespace = exports.getServerIngressPolicy = exports.getNamespacePolicies = exports.getClusterAccess = exports.isCallerNamespace = exports.SERVICE_RUN_LABEL = exports.getServiceRunNamespace = exports.getCallerNamespace = void 0;
|
|
3
|
+
exports.awaitRunObjectsGone = exports.removeRunObjects = exports.CallerNamespaces = exports.getNamespaceQuota = exports.getVolumeScope = exports.getKeptClaimTasks = exports.getPrivilegedNamespaceProblem = exports.getCapabilitiesProblem = exports.getPodSecurityLabels = exports.KubernetesExecutor = exports.getRunRoleBinding = exports.removeServiceRunNamespace = exports.getServerIngressPolicy = exports.getNamespacePolicies = exports.getClusterAccess = exports.isCallerNamespace = exports.SERVICE_RUN_LABEL = exports.getServiceRunNamespace = exports.getCallerNamespace = void 0;
|
|
4
4
|
const crypto_1 = require("crypto");
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const os_1 = require("os");
|
|
@@ -9,6 +9,7 @@ const net_1 = require("net");
|
|
|
9
9
|
const apply_1 = require("../kubernetes/apply");
|
|
10
10
|
const job_placement_1 = require("../kubernetes/job-placement");
|
|
11
11
|
const capacity_1 = require("./capacity");
|
|
12
|
+
const borrowed_quota_1 = require("./borrowed-quota");
|
|
12
13
|
const kubernetes_instance_1 = require("../kubernetes/kubernetes-instance");
|
|
13
14
|
const log_1 = require("../log");
|
|
14
15
|
const network_policy_1 = require("../kubernetes/network-policy");
|
|
@@ -259,9 +260,9 @@ exports.getRunRoleBinding = getRunRoleBinding;
|
|
|
259
260
|
* of a caller execute side by side in their namespace, up to `runs.limits.running`:
|
|
260
261
|
* the objects of a run are named and found by its run label (getItemKey), so two runs
|
|
261
262
|
* of the same task have their own. What two runs share are the claims the server keeps
|
|
262
|
-
* (`backend.keepVolumes`) of a scope (getVolumeScope):
|
|
263
|
-
* a time (
|
|
264
|
-
* kept claims, which go once they expired.
|
|
263
|
+
* (`backend.keepVolumes`) of a scope (getVolumeScope), one per task: two runs of one scope
|
|
264
|
+
* with a task in common go one at a time (excludes). When a run ends, everything made for it
|
|
265
|
+
* is removed, but for the kept claims, which go once they expired.
|
|
265
266
|
*/
|
|
266
267
|
class KubernetesExecutor {
|
|
267
268
|
constructor(config, processEnvs, options) {
|
|
@@ -269,7 +270,8 @@ class KubernetesExecutor {
|
|
|
269
270
|
// per caller namespace, what changes objects of the whole namespace: its setup, the
|
|
270
271
|
// removal of what a stopped server left, the sweeps of kept claims. The last one waiting.
|
|
271
272
|
this.queues = new Map();
|
|
272
|
-
// per caller namespace, the runs that execute in it: by run label,
|
|
273
|
+
// per caller namespace, the runs that execute in it and mount kept claims: by run label,
|
|
274
|
+
// their volume scope
|
|
273
275
|
this.active = new Map();
|
|
274
276
|
// per caller namespace, when its kept claims are next looked at
|
|
275
277
|
this.sweeps = new Map();
|
|
@@ -302,13 +304,27 @@ class KubernetesExecutor {
|
|
|
302
304
|
this.keep = config.backend.keepVolumes === false ? 0 : (0, units_1.parseDuration)(config.backend.keepVolumes);
|
|
303
305
|
this.keepServiceRuns = config.runs.up.keepVolumes;
|
|
304
306
|
this.log = options.log;
|
|
307
|
+
const { quota, maxQuota, pools } = config.backend;
|
|
308
|
+
if (quota && maxQuota) {
|
|
309
|
+
this.quotas = new borrowed_quota_1.BorrowedQuotas({
|
|
310
|
+
quota,
|
|
311
|
+
maxQuota,
|
|
312
|
+
room: () => (0, borrowed_quota_1.readPoolRoom)(this.instance, pools),
|
|
313
|
+
write: async (namespace, hard) => {
|
|
314
|
+
await (0, apply_1.replace)(this.instance, getNamespaceQuota(namespace, hard));
|
|
315
|
+
},
|
|
316
|
+
log: options.log,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
305
319
|
this.host = new executor_1.HostExecutor({ ...options, placement: this.placement() });
|
|
306
320
|
}
|
|
307
321
|
// what the namespaces of the server's callers and service runs request and use
|
|
308
322
|
capacity() {
|
|
309
323
|
const prefix = this.config.backend.namespacePrefix;
|
|
324
|
+
const { quota, maxQuota } = this.config.backend;
|
|
310
325
|
return new capacity_1.KubernetesCapacity(this.instance, {
|
|
311
326
|
quotaName: POLICY_NAME,
|
|
327
|
+
...(quota && maxQuota ? { share: (0, capacity_1.getQuotaResources)(quota) } : {}),
|
|
312
328
|
namespaces: async () => (await this.instance.coreApi.listNamespace({ labelSelector: `${MANAGED_LABEL}=true` })).items
|
|
313
329
|
.filter((namespace) => isCallerNamespace(prefix, namespace.metadata?.name ?? ''))
|
|
314
330
|
.map((namespace) => ({
|
|
@@ -343,7 +359,11 @@ class KubernetesExecutor {
|
|
|
343
359
|
runs = new Map();
|
|
344
360
|
this.active.set(namespace, runs);
|
|
345
361
|
}
|
|
346
|
-
|
|
362
|
+
if (this.holds(run)) {
|
|
363
|
+
runs.set(label, getVolumeScope(run));
|
|
364
|
+
}
|
|
365
|
+
// the caller is promised its share while the run executes
|
|
366
|
+
this.quotas?.enter(namespace, run.id);
|
|
347
367
|
try {
|
|
348
368
|
await this.host.execute(run, context);
|
|
349
369
|
}
|
|
@@ -352,6 +372,9 @@ class KubernetesExecutor {
|
|
|
352
372
|
if (runs.size === 0 && this.active.get(namespace) === runs) {
|
|
353
373
|
this.active.delete(namespace);
|
|
354
374
|
}
|
|
375
|
+
await this.quotas
|
|
376
|
+
?.leave(namespace, run.id)
|
|
377
|
+
.catch((e) => this.log?.(`the quotas could not be worked out again: ${(0, log_1.getErrorMessage)(e)}`));
|
|
355
378
|
}
|
|
356
379
|
}
|
|
357
380
|
// A service run moves to another commit in its namespace: unchanged services stay, the
|
|
@@ -417,17 +440,41 @@ class KubernetesExecutor {
|
|
|
417
440
|
this.serviceRunsSwept = false;
|
|
418
441
|
}
|
|
419
442
|
}
|
|
420
|
-
// The runs of a scope share
|
|
421
|
-
// what a task finds in
|
|
422
|
-
//
|
|
423
|
-
|
|
443
|
+
// The runs of a scope share the kept claim of each task, which one of them may change at a
|
|
444
|
+
// time: what a task finds in it is what the last run left there. Two runs of a scope go
|
|
445
|
+
// side by side when they have no task in common, and a run that mounts no kept claim holds
|
|
446
|
+
// nothing. Without kept claims, runs share no object and nothing is held.
|
|
447
|
+
excludes(run, other) {
|
|
448
|
+
const held = this.holds(run);
|
|
449
|
+
const theirs = this.holds(other);
|
|
450
|
+
if (!held || !theirs || held.scope !== theirs.scope) {
|
|
451
|
+
return false;
|
|
452
|
+
}
|
|
453
|
+
if (!held.tasks || !theirs.tasks) {
|
|
454
|
+
return true;
|
|
455
|
+
}
|
|
456
|
+
const tasks = new Set(theirs.tasks);
|
|
457
|
+
return held.tasks.some((task) => tasks.has(task));
|
|
458
|
+
}
|
|
459
|
+
// The kept claims a run mounts: its scope in the caller's namespace and the tasks of the
|
|
460
|
+
// claims (getKeptClaimTasks), all of the scope's for a run without a plan. Nothing for a
|
|
461
|
+
// run that mounts none.
|
|
462
|
+
holds(run) {
|
|
424
463
|
if (this.keep === 0 || run.up) {
|
|
425
464
|
return undefined;
|
|
426
465
|
}
|
|
427
|
-
|
|
466
|
+
const tasks = getKeptClaimTasks(run);
|
|
467
|
+
if (tasks?.length === 0) {
|
|
468
|
+
return undefined;
|
|
469
|
+
}
|
|
470
|
+
return {
|
|
471
|
+
scope: `${getCallerNamespace(this.config.backend.namespacePrefix, run.owner)}/${getVolumeScope(run)}`,
|
|
472
|
+
tasks,
|
|
473
|
+
};
|
|
428
474
|
}
|
|
429
|
-
// The scopes of the runs that execute in the namespace
|
|
430
|
-
// are theirs to mount and stay. A server that keeps none
|
|
475
|
+
// The scopes of the runs that execute in the namespace and mount kept claims, but
|
|
476
|
+
// `except`'s: their kept claims are theirs to mount and stay. A server that keeps none
|
|
477
|
+
// mounts none.
|
|
431
478
|
scopesInUse(namespace, except) {
|
|
432
479
|
if (this.keep === 0) {
|
|
433
480
|
return new Set();
|
|
@@ -474,6 +521,8 @@ class KubernetesExecutor {
|
|
|
474
521
|
quota: this.config.runs.up.quota,
|
|
475
522
|
});
|
|
476
523
|
});
|
|
524
|
+
// its pods take room the callers can't borrow
|
|
525
|
+
await this.quotas?.serviceRun(namespace, this.config.runs.up.quota ?? this.config.backend.quota);
|
|
477
526
|
context.emit({ type: 'log', task: 'hammerkit', stream: 'stdout', text: `runs in the namespace ${namespace}` });
|
|
478
527
|
return { kubernetes: this.environmentOf(run, namespace), env: this.access.env };
|
|
479
528
|
}
|
|
@@ -504,6 +553,9 @@ class KubernetesExecutor {
|
|
|
504
553
|
}
|
|
505
554
|
});
|
|
506
555
|
this.sweepLeftClaims();
|
|
556
|
+
// the quota of the caller, and of the callers it borrows from or lends to, before its
|
|
557
|
+
// tasks run
|
|
558
|
+
await this.quotas?.apply(namespace);
|
|
507
559
|
context.emit({ type: 'log', task: 'hammerkit', stream: 'stdout', text: `runs in the namespace ${namespace}` });
|
|
508
560
|
return {
|
|
509
561
|
kubernetes: this.environmentOf(run, namespace, work, capabilitiesProblem),
|
|
@@ -540,6 +592,9 @@ class KubernetesExecutor {
|
|
|
540
592
|
}
|
|
541
593
|
else {
|
|
542
594
|
this.serviceRuns.delete(namespace);
|
|
595
|
+
await this.quotas
|
|
596
|
+
?.serviceRunEnded(namespace)
|
|
597
|
+
.catch((e) => this.log?.(`the quotas could not be worked out again: ${(0, log_1.getErrorMessage)(e)}`));
|
|
543
598
|
}
|
|
544
599
|
}
|
|
545
600
|
return;
|
|
@@ -736,6 +791,22 @@ function getPrivilegedNamespaceProblem(namespace, labels, backend) {
|
|
|
736
791
|
`${namespace} ${ENFORCE_LABEL}=baseline ${AUDIT_LABEL}- --overwrite`);
|
|
737
792
|
}
|
|
738
793
|
exports.getPrivilegedNamespaceProblem = getPrivilegedNamespaceProblem;
|
|
794
|
+
// The tasks of a run whose kept claims it mounts, by name: each container task of its plan,
|
|
795
|
+
// which mounts its own claim, while the tasks that depend on it and the services that mount its
|
|
796
|
+
// outputs mount it too (they have it in their plan). Another task has none: an aggregate, or a
|
|
797
|
+
// local task on a server that allows them. A kept claim is named after its task
|
|
798
|
+
// (getClaimName), so two runs with no task in common mount no claim in common, whatever
|
|
799
|
+
// their tasks are. Null for a run without a plan (submitted by a version that recorded none).
|
|
800
|
+
function getKeptClaimTasks(run) {
|
|
801
|
+
if (!run.plan) {
|
|
802
|
+
return null;
|
|
803
|
+
}
|
|
804
|
+
return Object.entries(run.plan)
|
|
805
|
+
.filter(([, task]) => task.kind === 'task' && task.runtime === 'docker')
|
|
806
|
+
.map(([name]) => name)
|
|
807
|
+
.sort();
|
|
808
|
+
}
|
|
809
|
+
exports.getKeptClaimTasks = getKeptClaimTasks;
|
|
739
810
|
// What may mount a claim a run kept: the runs of the same branch or tag of a repository,
|
|
740
811
|
// which share its cache, so that the runs of two branches go side by side and the runs of one find what
|
|
741
812
|
// the last one left; on a sandbox the runs of that sandbox only, whose claim is on the same
|
|
@@ -755,6 +826,19 @@ function getVolumeScope(run) {
|
|
|
755
826
|
return (0, executor_1.cacheNamespace)(run);
|
|
756
827
|
}
|
|
757
828
|
exports.getVolumeScope = getVolumeScope;
|
|
829
|
+
function getPolicyMetadata(namespace) {
|
|
830
|
+
return {
|
|
831
|
+
name: POLICY_NAME,
|
|
832
|
+
namespace,
|
|
833
|
+
annotations: { 'hammerkit.dev/version': (0, version_1.getVersion)() },
|
|
834
|
+
labels: { [MANAGED_LABEL]: 'true' },
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
// The ResourceQuota of a namespace of the server
|
|
838
|
+
function getNamespaceQuota(namespace, hard) {
|
|
839
|
+
return { apiVersion: 'v1', kind: 'ResourceQuota', metadata: getPolicyMetadata(namespace), spec: { hard } };
|
|
840
|
+
}
|
|
841
|
+
exports.getNamespaceQuota = getNamespaceQuota;
|
|
758
842
|
/**
|
|
759
843
|
* The namespaces of callers on the cluster: named by the caller (getCallerNamespace),
|
|
760
844
|
* made on first use, with the operator's quota.
|
|
@@ -842,18 +926,9 @@ class CallerNamespaces {
|
|
|
842
926
|
]) {
|
|
843
927
|
await (0, apply_1.replace)(this.instance, policy);
|
|
844
928
|
}
|
|
845
|
-
const metadata =
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
annotations: { 'hammerkit.dev/version': (0, version_1.getVersion)() },
|
|
849
|
-
labels: { [MANAGED_LABEL]: 'true' },
|
|
850
|
-
};
|
|
851
|
-
const quota = {
|
|
852
|
-
apiVersion: 'v1',
|
|
853
|
-
kind: 'ResourceQuota',
|
|
854
|
-
metadata,
|
|
855
|
-
spec: { hard: serviceRun?.quota ?? backend.quota },
|
|
856
|
-
};
|
|
929
|
+
const metadata = getPolicyMetadata(namespace);
|
|
930
|
+
const hard = serviceRun?.quota ?? backend.quota;
|
|
931
|
+
const quota = getNamespaceQuota(namespace, hard ?? {});
|
|
857
932
|
const defaults = backend.defaultResources;
|
|
858
933
|
const limits = {
|
|
859
934
|
apiVersion: 'v1',
|
|
@@ -869,10 +944,21 @@ class CallerNamespaces {
|
|
|
869
944
|
],
|
|
870
945
|
},
|
|
871
946
|
};
|
|
872
|
-
|
|
873
|
-
|
|
947
|
+
// a caller who may borrow has the quota the server works out for them (BorrowedQuotas),
|
|
948
|
+
// written once the namespace is there: here it is made with the share, never replaced
|
|
949
|
+
const borrows = !!backend.maxQuota && !serviceRun;
|
|
950
|
+
if (borrows) {
|
|
951
|
+
await this.instance.objectApi.create(quota).catch((e) => {
|
|
952
|
+
if ((0, apply_1.statusCodeOf)(e) !== 409) {
|
|
953
|
+
throw e;
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
const objects = [
|
|
958
|
+
...(borrows ? [] : [[quota, !!hard]]),
|
|
874
959
|
[limits, !!defaults],
|
|
875
|
-
]
|
|
960
|
+
];
|
|
961
|
+
for (const [object, wanted] of objects) {
|
|
876
962
|
if (wanted) {
|
|
877
963
|
await (0, apply_1.replace)(this.instance, object);
|
|
878
964
|
}
|
package/dist/server/runs.js
CHANGED
|
@@ -817,15 +817,13 @@ class RunRegistry {
|
|
|
817
817
|
return [...this.entries.values()].find((entry) => entry.run.state === 'running' && onSandbox(entry.run, source.id))
|
|
818
818
|
?.run.id;
|
|
819
819
|
}
|
|
820
|
-
// The run that executes and holds what the run would (RunExecutor.
|
|
820
|
+
// The run that executes and holds what the run would (RunExecutor.excludes)
|
|
821
821
|
held(run) {
|
|
822
|
-
const
|
|
823
|
-
|
|
824
|
-
if (key === undefined) {
|
|
822
|
+
const executor = this.options.executor;
|
|
823
|
+
if (!executor.excludes) {
|
|
825
824
|
return undefined;
|
|
826
825
|
}
|
|
827
|
-
return [...this.entries.values()].find((entry) => entry.run.state === 'running' &&
|
|
828
|
-
?.run.id;
|
|
826
|
+
return [...this.entries.values()].find((entry) => entry.run.state === 'running' && entry.run.id !== run.id && executor.excludes?.(run, entry.run))?.run.id;
|
|
829
827
|
}
|
|
830
828
|
// What keeps a queued run from starting, checked as dispatch checks it: what holds it
|
|
831
829
|
// even when a place frees up is named first.
|