-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from dealroadshow/cron-job
cron job maker
- Loading branch information
Showing
5 changed files
with
192 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Dealroadshow\K8S\Framework\Core\CronJob; | ||
|
||
use Dealroadshow\K8S\API\Batch\CronJob; | ||
|
||
abstract class AbstractCronJob implements CronJobInterface | ||
{ | ||
public function concurrencyPolicy(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
public function failedJobsHistoryLimit(): ?int | ||
{ | ||
return null; | ||
} | ||
|
||
public function startingDeadlineSeconds(): ?int | ||
{ | ||
return null; | ||
} | ||
|
||
public function successfulJobsHistoryLimit(): ?int | ||
{ | ||
return null; | ||
} | ||
|
||
public function suspend(): ?bool | ||
{ | ||
return null; | ||
} | ||
|
||
public function configureCronJob(CronJob $cronJob): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Dealroadshow\K8S\Framework\Core\CronJob; | ||
|
||
use Dealroadshow\K8S\API\Batch\CronJob; | ||
use Dealroadshow\K8S\Framework\Core\Job\JobInterface; | ||
use Dealroadshow\K8S\Framework\Core\ManifestInterface; | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Dealroadshow\K8S\Framework\Core\Job; | ||
|
||
use Dealroadshow\K8S\Data\JobSpec; | ||
use Dealroadshow\K8S\Framework\App\AppInterface; | ||
use Dealroadshow\K8S\Framework\Core\Pod\PodTemplateSpecProcessor; | ||
|
||
class JobSpecProcessor | ||
{ | ||
private PodTemplateSpecProcessor $podSpecProcessor; | ||
|
||
public function __construct(PodTemplateSpecProcessor $podSpecProcessor) | ||
{ | ||
$this->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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Dealroadshow\K8S\Framework\ResourceMaker; | ||
|
||
use Dealroadshow\K8S\API\Batch\CronJob; | ||
use Dealroadshow\K8S\Data\CronJobSpec; | ||
use Dealroadshow\K8S\Data\JobTemplateSpec; | ||
use Dealroadshow\K8S\Framework\App\AppInterface; | ||
use Dealroadshow\K8S\Framework\Core\CronJob\CronJobInterface; | ||
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; | ||
|
||
class CronJobMaker extends AbstractResourceMaker | ||
{ | ||
private JobSpecProcessor $jobSpecProcessor; | ||
|
||
public function __construct(JobSpecProcessor $jobSpecProcessor) | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters