UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

97 lines 4.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.K8sCronJobHandler = void 0; const helper_1 = require("../../util/helper"); const pod_template_1 = require("../processors/pod-template"); const constants_1 = require("../../config/constants"); class K8sCronJobHandler { constructor(filePath, cronJob, mapper) { this.filePath = filePath; this.cronJob = cronJob; this.mapper = mapper; this.workload = { kind: 'workload', name: cronJob.metadata.name, spec: { type: 'cron', containers: [], defaultOptions: { capacityAI: false, }, job: { schedule: '* * * * *', concurrencyPolicy: 'Forbid', historyLimit: 5, restartPolicy: 'Never', }, }, }; } /*** Public Methods ***/ handle() { var _a, _b, _c, _d; // Validate cron job this.validate(); // CronJobs always convert to workloads of type 'cron' if ((0, helper_1.getCplnAnnotation)(constants_1.K8S_CPLN_ANNOTATION_WORKLOAD_TYPE, this.cronJob, (_c = (_b = (_a = this.cronJob.spec.jobTemplate) === null || _a === void 0 ? void 0 : _a.spec) === null || _b === void 0 ? void 0 : _b.template) === null || _c === void 0 ? void 0 : _c.metadata)) { this.mapper.warnings.push(`Annotation '${constants_1.K8S_CPLN_ANNOTATION_WORKLOAD_TYPE}' on CronJob '${this.cronJob.metadata.name}' was ignored because CronJobs always convert to workloads of type 'cron'.`); } // Process labels as tags if ((_d = this.cronJob.spec.jobTemplate.spec.template.metadata) === null || _d === void 0 ? void 0 : _d.labels) { this.workload.tags = this.cronJob.spec.jobTemplate.spec.template.metadata.labels; } if (!this.workload.tags || Object.keys(this.workload.tags).length == 0) { this.workload.tags = this.cronJob.metadata.labels; } // Handle suspend if (this.cronJob.spec.suspend) { this.workload.spec.defaultOptions.suspend = true; } // Process template property const templateProcessor = new pod_template_1.K8sPodTemplateProcessor(this.filePath, 'spec.template', this.cronJob, this.workload, this.cronJob.spec.jobTemplate.spec.template, this.mapper); templateProcessor.process(); // Process job if (this.cronJob.spec.schedule) { this.workload.spec.job.schedule = this.cronJob.spec.schedule; } if (this.cronJob.spec.concurrencyPolicy) { this.workload.spec.job.concurrencyPolicy = this.cronJob.spec.concurrencyPolicy; } if (this.cronJob.spec.successfulJobsHistoryLimit) { this.workload.spec.job.historyLimit = this.cronJob.spec.successfulJobsHistoryLimit; } return this.workload; } /*** Private Methods ***/ // Validators // validate() { if (!this.cronJob.spec) { this.ensurePropertyPresence('spec'); } if (!this.cronJob.spec.jobTemplate) { this.ensurePropertyPresence('spec.jobTemplate'); } if (!this.cronJob.spec.jobTemplate.spec) { this.ensurePropertyPresence('spec.jobTemplate.spec'); } if (!this.cronJob.spec.jobTemplate.spec.template) { this.ensurePropertyPresence('spec.jobTemplate.spec.template'); } if (this.cronJob.spec.concurrencyPolicy && this.cronJob.spec.concurrencyPolicy.toLowerCase() == 'allow') { this.raiseCustomK8sError(`'spec.concurrencyPolicy' cannot be set to 'Allow' for workload of type cron. It should be either 'Forbid' or 'Replace'`); } if (this.cronJob.spec.successfulJobsHistoryLimit && (this.cronJob.spec.successfulJobsHistoryLimit == 0 || this.cronJob.spec.successfulJobsHistoryLimit > 10)) { this.raiseCustomK8sError(`'spec.successfulJobsHistoryLimit' must be greater than or equal to 1 or less than and equal 10`); } } // Validation Helpers // ensurePropertyPresence(property) { (0, helper_1.ensurePropertyPresence)(property, this.filePath, this.cronJob); } raiseCustomK8sError(message) { (0, helper_1.raiseCustomK8sError)(message, this.filePath, this.cronJob); } } exports.K8sCronJobHandler = K8sCronJobHandler; //# sourceMappingURL=cron-job.js.map