Skip to content

Commit 9354443

Browse files
committed
pass more identifiers to provider and apply labels
1 parent 132e47d commit 9354443

File tree

7 files changed

+121
-26
lines changed

7 files changed

+121
-26
lines changed

apps/kubernetes-provider/src/index.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
TaskOperationsIndexOptions,
88
TaskOperationsRestoreOptions,
99
} from "@trigger.dev/core-apps";
10-
import { Machine, PostStartCauses, PreStopCauses } from "@trigger.dev/core/v3";
10+
import { Machine, PostStartCauses, PreStopCauses, EnvironmentType } from "@trigger.dev/core/v3";
1111
import { randomUUID } from "crypto";
1212

1313
const RUNTIME_ENV = process.env.KUBERNETES_PORT ? "kubernetes" : "local";
@@ -56,6 +56,12 @@ class KubernetesTaskOperations implements TaskOperations {
5656
metadata: {
5757
labels: {
5858
app: "task-index",
59+
"app.kubernetes.io/part-of": "trigger-worker",
60+
"app.kubernetes.io/component": "index",
61+
env: opts.envId,
62+
envtype: this.#envTypeToLabelValue(opts.envType),
63+
org: opts.orgId,
64+
project: opts.projectId,
5965
},
6066
},
6167
spec: {
@@ -162,6 +168,13 @@ class KubernetesTaskOperations implements TaskOperations {
162168
namespace: this.#namespace.metadata.name,
163169
labels: {
164170
app: "task-run",
171+
"app.kubernetes.io/part-of": "trigger-worker",
172+
"app.kubernetes.io/component": "create",
173+
env: opts.envId,
174+
envtype: this.#envTypeToLabelValue(opts.envType),
175+
org: opts.orgId,
176+
project: opts.projectId,
177+
run: opts.runId,
165178
},
166179
},
167180
spec: {
@@ -276,6 +289,14 @@ class KubernetesTaskOperations implements TaskOperations {
276289
namespace: this.#namespace.metadata.name,
277290
labels: {
278291
app: "task-run",
292+
"app.kubernetes.io/part-of": "trigger-worker",
293+
"app.kubernetes.io/component": "restore",
294+
env: opts.envId,
295+
envtype: this.#envTypeToLabelValue(opts.envType),
296+
org: opts.orgId,
297+
project: opts.projectId,
298+
run: opts.runId,
299+
checkpoint: opts.checkpointId,
279300
},
280301
},
281302
spec: {
@@ -372,6 +393,19 @@ class KubernetesTaskOperations implements TaskOperations {
372393
await this.#getPod(opts.runId, this.#namespace);
373394
}
374395

396+
#envTypeToLabelValue(type: EnvironmentType) {
397+
switch (type) {
398+
case "PRODUCTION":
399+
return "prod";
400+
case "STAGING":
401+
return "stg";
402+
case "DEVELOPMENT":
403+
return "dev";
404+
case "PREVIEW":
405+
return "preview";
406+
}
407+
}
408+
375409
#getResourcesFromMachineConfig(config: Machine) {
376410
return {
377411
cpu: `${config.cpu}`,

apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@ export class SharedQueueConsumer {
449449
const { machineConfig } = taskRunAttempt.backgroundWorkerTask;
450450
const machine = Machine.safeParse(machineConfig ?? {});
451451

452-
453452
if (!machine.success) {
454453
logger.error("Failed to parse machine config", {
455454
queueMessage: message.data,
@@ -489,12 +488,16 @@ export class SharedQueueConsumer {
489488
backgroundWorkerId: deployment.worker.friendlyId,
490489
data: {
491490
type: "SCHEDULE_ATTEMPT",
492-
id: taskRunAttempt.id,
493491
image: deployment.imageReference,
494-
envId: environment.id,
495-
runId: taskRunAttempt.taskRunId,
496492
version: deployment.version,
497493
machine: machine.data,
494+
// identifiers
495+
id: taskRunAttempt.id,
496+
envId: environment.id,
497+
envType: environment.type,
498+
orgId: environment.organizationId,
499+
projectId: environment.projectId,
500+
runId: taskRunAttempt.taskRunId,
498501
},
499502
});
500503
}

apps/webapp/app/v3/services/indexDeployment.server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ export class IndexDeploymentService extends BaseService {
4949
version: "v1",
5050
shortCode: deployment.shortCode,
5151
imageTag: deployment.imageReference,
52-
envId: deployment.environmentId,
5352
apiKey: deployment.environment.apiKey,
5453
apiUrl: env.APP_ORIGIN,
54+
// identifiers
55+
envId: deployment.environmentId,
56+
envType: deployment.environment.type,
57+
projectId: deployment.projectId,
58+
orgId: deployment.environment.organizationId,
5559
});
5660

5761
logger.debug("Index ACK received", { responses });

apps/webapp/app/v3/services/restoreCheckpoint.server.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class RestoreCheckpointService extends BaseService {
3838
},
3939
},
4040
},
41+
runtimeEnvironment: true,
4142
},
4243
},
4344
},
@@ -85,13 +86,18 @@ export class RestoreCheckpointService extends BaseService {
8586

8687
socketIo.providerNamespace.emit("RESTORE", {
8788
version: "v1",
88-
checkpointId: checkpoint.id,
89-
runId: checkpoint.runId,
9089
type: checkpoint.type,
9190
location: checkpoint.location,
9291
reason: checkpoint.reason ?? undefined,
9392
imageRef: checkpoint.imageRef,
9493
machine: machine.data,
94+
// identifiers
95+
checkpointId: checkpoint.id,
96+
envId: checkpoint.runtimeEnvironment.id,
97+
envType: checkpoint.runtimeEnvironment.type,
98+
orgId: checkpoint.runtimeEnvironment.organizationId,
99+
projectId: checkpoint.runtimeEnvironment.projectId,
100+
runId: checkpoint.runId,
95101
});
96102

97103
return checkpoint;

packages/core-apps/src/provider.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createServer } from "node:http";
22
import {
33
ClientToSharedQueueMessages,
44
clientWebsocketMessages,
5+
EnvironmentType,
56
Machine,
67
PlatformToProviderMessages,
78
ProviderToPlatformMessages,
@@ -25,24 +26,39 @@ const logger = new SimpleLogger(`[${MACHINE_NAME}]`);
2526
export interface TaskOperationsIndexOptions {
2627
shortCode: string;
2728
imageRef: string;
28-
envId: string;
2929
apiKey: string;
3030
apiUrl: string;
31+
// identifiers
32+
envId: string;
33+
envType: EnvironmentType;
34+
orgId: string;
35+
projectId: string;
3136
}
3237

3338
export interface TaskOperationsCreateOptions {
34-
runId: string;
3539
image: string;
3640
machine: Machine;
37-
envId: string;
3841
version: string;
42+
// identifiers
43+
envId: string;
44+
envType: EnvironmentType;
45+
orgId: string;
46+
projectId: string;
47+
runId: string;
48+
attemptId: string;
3949
}
4050

4151
export interface TaskOperationsRestoreOptions {
42-
runId: string;
4352
imageRef: string;
4453
checkpointRef: string;
4554
machine: Machine;
55+
// identifiers
56+
envId: string;
57+
envType: EnvironmentType;
58+
orgId: string;
59+
projectId: string;
60+
runId: string;
61+
checkpointId: string;
4662
}
4763

4864
export interface TaskOperations {
@@ -103,11 +119,16 @@ export class ProviderShell implements Provider {
103119
if (message.data.type === "SCHEDULE_ATTEMPT") {
104120
try {
105121
this.tasks.create({
106-
envId: message.data.envId,
107-
runId: message.data.runId,
108122
image: message.data.image,
109123
machine: message.data.machine,
110-
version: message.version,
124+
version: message.data.version,
125+
// identifiers
126+
envId: message.data.envId,
127+
envType: message.data.envType,
128+
orgId: message.data.orgId,
129+
projectId: message.data.projectId,
130+
runId: message.data.runId,
131+
attemptId: message.data.id,
111132
});
112133
} catch (error) {
113134
logger.error("create failed", error);
@@ -168,9 +189,13 @@ export class ProviderShell implements Provider {
168189
await this.tasks.index({
169190
shortCode: message.shortCode,
170191
imageRef: message.imageTag,
171-
envId: message.envId,
172192
apiKey: message.apiKey,
173193
apiUrl: message.apiUrl,
194+
// identifiers
195+
envId: message.envId,
196+
envType: message.envType,
197+
orgId: message.orgId,
198+
projectId: message.projectId,
174199
});
175200
} catch (error) {
176201
logger.error("index failed", error);
@@ -209,10 +234,16 @@ export class ProviderShell implements Provider {
209234

210235
try {
211236
await this.tasks.restore({
212-
runId: message.runId,
213237
checkpointRef: message.location,
214238
machine: message.machine,
215239
imageRef: message.imageRef,
240+
// identifiers
241+
envId: message.envId,
242+
envType: message.envType,
243+
orgId: message.orgId,
244+
projectId: message.projectId,
245+
runId: message.runId,
246+
checkpointId: message.checkpointId,
216247
});
217248
} catch (error) {
218249
logger.error("restore failed", error);

packages/core/src/v3/schemas/messages.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { z } from "zod";
22
import { TaskRunExecution, TaskRunExecutionResult } from "./common";
33

4+
export const EnvironmentType = z.enum(["PRODUCTION", "STAGING", "DEVELOPMENT", "PREVIEW"])
5+
export type EnvironmentType = z.infer<typeof EnvironmentType>;
6+
47
export const MachineCpu = z
58
.union([z.literal(0.25), z.literal(0.5), z.literal(1), z.literal(2), z.literal(4)])
69
.default(0.5);
@@ -59,12 +62,16 @@ export const BackgroundWorkerServerMessages = z.discriminatedUnion("type", [
5962
}),
6063
z.object({
6164
type: z.literal("SCHEDULE_ATTEMPT"),
62-
id: z.string(),
6365
image: z.string(),
64-
envId: z.string(),
65-
runId: z.string(),
6666
version: z.string(),
6767
machine: Machine,
68+
// identifiers
69+
id: z.string(), // attempt
70+
envId: z.string(),
71+
envType: EnvironmentType,
72+
orgId: z.string(),
73+
projectId: z.string(),
74+
runId: z.string(),
6875
}),
6976
]);
7077

packages/core/src/v3/schemas/schemas.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import {
77
ProdTaskRunExecution,
88
ProdTaskRunExecutionPayload,
99
RetryOptions,
10-
Machine
10+
Machine,
11+
EnvironmentType,
1112
} from "./messages";
1213
import { TaskResource } from "./resources";
1314

1415
export const PostStartCauses = z.enum(["index", "create", "restore"]);
15-
export type PostStartCauses = z.infer<typeof PostStartCauses>
16+
export type PostStartCauses = z.infer<typeof PostStartCauses>;
1617

1718
export const PreStopCauses = z.enum(["terminate"]);
18-
export type PreStopCauses = z.infer<typeof PreStopCauses>
19+
export type PreStopCauses = z.infer<typeof PreStopCauses>;
1920

2021
const RegexSchema = z.custom<RegExp>((val) => {
2122
try {
@@ -85,9 +86,13 @@ export const PlatformToProviderMessages = {
8586
version: z.literal("v1").default("v1"),
8687
imageTag: z.string(),
8788
shortCode: z.string(),
88-
envId: z.string(),
8989
apiKey: z.string(),
9090
apiUrl: z.string(),
91+
// identifiers
92+
envId: z.string(),
93+
envType: EnvironmentType,
94+
orgId: z.string(),
95+
projectId: z.string(),
9196
}),
9297
callback: z.discriminatedUnion("success", [
9398
z.object({
@@ -107,13 +112,18 @@ export const PlatformToProviderMessages = {
107112
RESTORE: {
108113
message: z.object({
109114
version: z.literal("v1").default("v1"),
110-
checkpointId: z.string(),
111-
runId: z.string(),
112115
type: z.enum(["DOCKER", "KUBERNETES"]),
113116
location: z.string(),
114117
reason: z.string().optional(),
115118
imageRef: z.string(),
116119
machine: Machine,
120+
// identifiers
121+
checkpointId: z.string(),
122+
envId: z.string(),
123+
envType: EnvironmentType,
124+
orgId: z.string(),
125+
projectId: z.string(),
126+
runId: z.string(),
117127
}),
118128
},
119129
DELETE: {

0 commit comments

Comments
 (0)