Skip to content

Commit

Permalink
zod validation
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyblasczyk committed Nov 4, 2024
1 parent 890acd3 commit c5fbf3a
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions integrations/kubernetes-job-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Job } from "@ctrlplane/node-sdk";
import { CronJob } from "cron";
import handlebars from "handlebars";
import yaml from "js-yaml";
import { z } from "zod";

import { logger } from "@ctrlplane/logger";

Expand Down Expand Up @@ -73,6 +74,10 @@ const deployManifest = async (
}
};

const jobAgentConfigSchema = z.object({
manifest: z.string(),
});

const spinUpNewJobs = async () => {
try {
const jobs = await agent.next();
Expand All @@ -84,10 +89,19 @@ const spinUpNewJobs = async () => {
logger.info(`Running job ${jobDetails.id}`);
logger.debug(`Job details:`, { job: jobDetails });

const manifest = renderManifest(
jobDetails.jobAgentConfig.manifest as string,
jobDetails,
const parseResult = jobAgentConfigSchema.safeParse(
jobDetails.jobAgentConfig,
);
if (!parseResult.success) {
await job.update({
status: "failed",
message:
"Invalid job agent configuration: " + parseResult.error.message,
});
return;
}

const manifest = renderManifest(parseResult.data.manifest, jobDetails);
const namespace = manifest?.metadata?.namespace ?? env.KUBE_NAMESPACE;

await job.acknowledge();
Expand Down

0 comments on commit c5fbf3a

Please sign in to comment.