@controlplane/cli
Version:
Control Plane Corporation CLI
92 lines • 3.88 kB
JavaScript
"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");
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;
// Validate cron job
this.validate();
// Process labels as tags
if ((_a = this.cronJob.spec.jobTemplate.spec.template.metadata) === null || _a === void 0 ? void 0 : _a.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