diff --git a/src/Core/CronJob/AbstractCronJob.php b/src/Core/CronJob/AbstractCronJob.php new file mode 100644 index 0000000..7aeedf2 --- /dev/null +++ b/src/Core/CronJob/AbstractCronJob.php @@ -0,0 +1,37 @@ +podSpecProcessor = $podSpecProcessor; + } + + public function process(JobInterface $manifest, JobSpec $spec, AppInterface $app): void + { + $this->podSpecProcessor->process($manifest, $spec->template(), $app); + + foreach ($spec->selector()->matchLabels()->all() as $name => $value) { + $spec->template()->metadata()->labels()->add($name, $value); + } + + $backoffLimit = $manifest->backoffLimit(); + $activeDeadlineSeconds = $manifest->activeDeadlineSeconds(); + $ttlSecondsAfterFinished = $manifest->ttlSecondsAfterFinished(); + $completions = $manifest->completions(); + $manualSelector = $manifest->manualSelector(); + $parallelism = $manifest->parallelism(); + if (null !== $backoffLimit) { + $spec->setBackoffLimit($backoffLimit); + } + if (null !== $activeDeadlineSeconds) { + $spec->setActiveDeadlineSeconds($activeDeadlineSeconds); + } + if (null !== $ttlSecondsAfterFinished) { + $spec->setTtlSecondsAfterFinished($ttlSecondsAfterFinished); + } + if (null !== $completions) { + $spec->setCompletions($completions); + } + if (null !== $manualSelector) { + $spec->setManualSelector($manualSelector); + } + if (null !== $parallelism) { + $spec->setParallelism($parallelism); + } + } +} diff --git a/src/ResourceMaker/CronJobMaker.php b/src/ResourceMaker/CronJobMaker.php new file mode 100644 index 0000000..96a6702 --- /dev/null +++ b/src/ResourceMaker/CronJobMaker.php @@ -0,0 +1,78 @@ +jobSpecProcessor = $jobSpecProcessor; + } + + /** + * @param ManifestInterface|CronJobInterface $manifest + * @param AppInterface $app + * + * @return CronJob + */ + protected function makeResource(ManifestInterface $manifest, AppInterface $app): CronJob + { + $spec = new CronJobSpec($manifest->schedule()); + $cronJob = new CronJob($spec); + + $app->metadataHelper()->configureMeta($manifest, $cronJob); + + $this->configureJobTemplate($spec->jobTemplate(), $manifest->job(), $app); + + $concurrencyPolicy = $manifest->concurrencyPolicy(); + $failedJobsHistoryLimit = $manifest->failedJobsHistoryLimit(); + $startingDeadlineSeconds = $manifest->startingDeadlineSeconds(); + $successfulJobsHistoryLimit = $manifest->successfulJobsHistoryLimit(); + $suspend = $manifest->suspend(); + if (null !== $concurrencyPolicy) { + $spec->setConcurrencyPolicy($concurrencyPolicy); + } + if (null !== $failedJobsHistoryLimit) { + $spec->setFailedJobsHistoryLimit($failedJobsHistoryLimit); + } + if (null !== $startingDeadlineSeconds) { + $spec->setStartingDeadlineSeconds($startingDeadlineSeconds); + } + if (null !== $successfulJobsHistoryLimit) { + $spec->setSuccessfulJobsHistoryLimit($successfulJobsHistoryLimit); + } + if (null !== $suspend) { + $spec->setSuspend($suspend); + } + $manifest->configureCronJob($cronJob); + + return $cronJob; + } + + private function configureJobTemplate(JobTemplateSpec $templateSpec, JobInterface $manifest, AppInterface $app) + { + $jobSpec = $templateSpec->spec(); + $manifest->labelSelector(new LabelSelectorConfigurator($jobSpec->selector())); + $this->jobSpecProcessor->process($manifest, $jobSpec, $app); + foreach ($jobSpec->selector()->matchLabels()->all() as $name => $value) { + $templateSpec->metadata()->labels()->add($name, $value); + } + } + + protected function supportsClass(): string + { + return CronJobInterface::class; + } +} diff --git a/src/ResourceMaker/JobMaker.php b/src/ResourceMaker/JobMaker.php index b0828d1..810b6be 100644 --- a/src/ResourceMaker/JobMaker.php +++ b/src/ResourceMaker/JobMaker.php @@ -5,17 +5,18 @@ use Dealroadshow\K8S\API\Batch\Job; use Dealroadshow\K8S\Framework\App\AppInterface; use Dealroadshow\K8S\Framework\Core\Job\JobInterface; +use Dealroadshow\K8S\Framework\Core\Job\JobSpecProcessor; use Dealroadshow\K8S\Framework\Core\LabelSelector\LabelSelectorConfigurator; use Dealroadshow\K8S\Framework\Core\ManifestInterface; use Dealroadshow\K8S\Framework\Core\Pod\PodTemplateSpecProcessor; class JobMaker extends AbstractResourceMaker { - private PodTemplateSpecProcessor $specProcessor; + private JobSpecProcessor $jobSpecProcessor; - public function __construct(PodTemplateSpecProcessor $specProcessor) + public function __construct(JobSpecProcessor $jobSpecProcessor) { - $this->specProcessor = $specProcessor; + $this->jobSpecProcessor = $jobSpecProcessor; } /** @@ -27,43 +28,16 @@ public function __construct(PodTemplateSpecProcessor $specProcessor) protected function makeResource(ManifestInterface $manifest, AppInterface $app): Job { $job = new Job(); + $spec = $job->spec(); - $labelSelector = new LabelSelectorConfigurator($job->spec()->selector()); - $manifest->labelSelector($labelSelector); + $manifest->labelSelector(new LabelSelectorConfigurator($spec->selector())); $app->metadataHelper()->configureMeta($manifest, $job); - $this->specProcessor->process($manifest, $job->spec()->template(), $app); - - $spec = $job->spec(); + $this->jobSpecProcessor->process($manifest, $spec, $app); foreach ($spec->selector()->matchLabels()->all() as $name => $value) { $job->metadata()->labels()->add($name, $value); - $job->spec()->template()->metadata()->labels()->add($name, $value); } - $backoffLimit = $manifest->backoffLimit(); - $activeDeadlineSeconds = $manifest->activeDeadlineSeconds(); - $ttlSecondsAfterFinished = $manifest->ttlSecondsAfterFinished(); - $completions = $manifest->completions(); - $manualSelector = $manifest->manualSelector(); - $parallelism = $manifest->parallelism(); - if (null !== $backoffLimit) { - $spec->setBackoffLimit($backoffLimit); - } - if (null !== $activeDeadlineSeconds) { - $spec->setActiveDeadlineSeconds($activeDeadlineSeconds); - } - if (null !== $ttlSecondsAfterFinished) { - $spec->setTtlSecondsAfterFinished($ttlSecondsAfterFinished); - } - if (null !== $completions) { - $spec->setCompletions($completions); - } - if (null !== $manualSelector) { - $spec->setManualSelector($manualSelector); - } - if (null !== $parallelism) { - $spec->setParallelism($parallelism); - } $manifest->configureJob($job); return $job;