From e342d129713ff8ed1524d588b7d0004a94f22dac Mon Sep 17 00:00:00 2001 From: Pasechnik Bogdan Date: Wed, 14 Oct 2020 13:27:42 +0300 Subject: [PATCH 1/4] cron job maker --- src/Core/CronJob/AbstractCronJob.php | 37 ++++++++++ src/Core/CronJob/CronJobInterface.php | 26 ++++++++ src/Core/Deployment/AbstractDeployment.php | 6 ++ src/Core/Job/JobSpecProcessor.php | 52 +++++++++++++++ src/ResourceMaker/CronJobMaker.php | 78 ++++++++++++++++++++++ src/ResourceMaker/JobMaker.php | 40 ++--------- 6 files changed, 206 insertions(+), 33 deletions(-) create mode 100644 src/Core/CronJob/AbstractCronJob.php create mode 100644 src/Core/CronJob/CronJobInterface.php create mode 100644 src/Core/Job/JobSpecProcessor.php create mode 100644 src/ResourceMaker/CronJobMaker.php 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); + } + } +} \ No newline at end of file diff --git a/src/ResourceMaker/CronJobMaker.php b/src/ResourceMaker/CronJobMaker.php new file mode 100644 index 0000000..7768ae0 --- /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; + } +} \ No newline at end of file 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; From 23ef2cb1fc665b26d8b5398e19297c82d034d838 Mon Sep 17 00:00:00 2001 From: Pasechnik Bogdan Date: Wed, 14 Oct 2020 13:57:31 +0300 Subject: [PATCH 2/4] job maker --- src/Core/Deployment/AbstractDeployment.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Core/Deployment/AbstractDeployment.php b/src/Core/Deployment/AbstractDeployment.php index 42cedb5..266912e 100644 --- a/src/Core/Deployment/AbstractDeployment.php +++ b/src/Core/Deployment/AbstractDeployment.php @@ -3,7 +3,6 @@ namespace Dealroadshow\K8S\Framework\Core\Deployment; use Dealroadshow\K8S\API\Apps\Deployment; -use Dealroadshow\K8S\API\Batch\CronJob; use Dealroadshow\K8S\Data\Collection\StringMap; use Dealroadshow\K8S\Data\PodSpec; use Dealroadshow\K8S\Framework\Core\MetadataConfigurator; @@ -66,9 +65,4 @@ public function progressDeadlineSeconds(): ?int public function configureDeployment(Deployment $deployment): void { } - - public function configureCronJob(CronJob $cronJob): void - { - - } } From ebf9c030de37ab54ca20fa271cd135d09d516e35 Mon Sep 17 00:00:00 2001 From: Pasechnik Bogdan Date: Wed, 14 Oct 2020 14:04:26 +0300 Subject: [PATCH 3/4] job maker --- src/Core/Job/JobSpecProcessor.php | 3 +-- src/ResourceMaker/CronJobMaker.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Core/Job/JobSpecProcessor.php b/src/Core/Job/JobSpecProcessor.php index 617433f..0008479 100644 --- a/src/Core/Job/JobSpecProcessor.php +++ b/src/Core/Job/JobSpecProcessor.php @@ -4,7 +4,6 @@ use Dealroadshow\K8S\Data\JobSpec; use Dealroadshow\K8S\Framework\App\AppInterface; -use Dealroadshow\K8S\Framework\Core\LabelSelector\LabelSelectorConfigurator; use Dealroadshow\K8S\Framework\Core\Pod\PodTemplateSpecProcessor; class JobSpecProcessor @@ -49,4 +48,4 @@ public function process(JobInterface $manifest, JobSpec $spec, AppInterface $app $spec->setParallelism($parallelism); } } -} \ No newline at end of file +} diff --git a/src/ResourceMaker/CronJobMaker.php b/src/ResourceMaker/CronJobMaker.php index 7768ae0..96a6702 100644 --- a/src/ResourceMaker/CronJobMaker.php +++ b/src/ResourceMaker/CronJobMaker.php @@ -75,4 +75,4 @@ protected function supportsClass(): string { return CronJobInterface::class; } -} \ No newline at end of file +} From 3c9e4b27af49d1e165593eaf00851ff97bfe4519 Mon Sep 17 00:00:00 2001 From: Pasechnik Bogdan Date: Wed, 14 Oct 2020 14:06:48 +0300 Subject: [PATCH 4/4] job maker --- src/Core/CronJob/CronJobInterface.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/Core/CronJob/CronJobInterface.php b/src/Core/CronJob/CronJobInterface.php index 246e8e5..6af9869 100644 --- a/src/Core/CronJob/CronJobInterface.php +++ b/src/Core/CronJob/CronJobInterface.php @@ -9,18 +9,11 @@ interface CronJobInterface extends ManifestInterface { public function concurrencyPolicy(): ?string; - public function failedJobsHistoryLimit(): ?int; - public function job(): JobInterface; - public function schedule(): string; - public function startingDeadlineSeconds(): ?int; - public function successfulJobsHistoryLimit(): ?int; - public function suspend(): ?bool; - public function configureCronJob(CronJob $cronJob): void; }