Skip to content

Commit

Permalink
Merge pull request #4 from dealroadshow/cron-job
Browse files Browse the repository at this point in the history
cron job maker
  • Loading branch information
petr-buchyn authored Oct 14, 2020
2 parents f01e27e + 3c9e4b2 commit d02bf9b
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 33 deletions.
37 changes: 37 additions & 0 deletions src/Core/CronJob/AbstractCronJob.php
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
{
}
}
19 changes: 19 additions & 0 deletions src/Core/CronJob/CronJobInterface.php
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;
}
51 changes: 51 additions & 0 deletions src/Core/Job/JobSpecProcessor.php
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);
}
}
}
78 changes: 78 additions & 0 deletions src/ResourceMaker/CronJobMaker.php
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;
}
}
40 changes: 7 additions & 33 deletions src/ResourceMaker/JobMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
Expand Down

0 comments on commit d02bf9b

Please sign in to comment.