Skip to content

Commit

Permalink
Merge pull request #2 from dealroadshow/job_maker
Browse files Browse the repository at this point in the history
job maker
  • Loading branch information
petr-buchyn authored Oct 13, 2020
2 parents 6c34023 + f952abd commit 607cd29
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/Core/Job/AbstractJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Job;

use Dealroadshow\K8S\API\Batch\Job;
use Dealroadshow\K8S\Data\Collection\StringMap;
use Dealroadshow\K8S\Data\PodSpec;
use Dealroadshow\K8S\Framework\Core\LabelSelector\LabelSelectorConfigurator;
use Dealroadshow\K8S\Framework\Core\MetadataConfigurator;
use Dealroadshow\K8S\Framework\Core\Pod\Affinity\AffinityConfigurator;
use Dealroadshow\K8S\Framework\Core\Pod\Containers\PodContainers;
use Dealroadshow\K8S\Framework\Core\Pod\ImagePullSecrets\ImagePullSecretsConfigurator;
use Dealroadshow\K8S\Framework\Core\Pod\Policy\RestartPolicy;
use Dealroadshow\K8S\Framework\Core\Pod\Volume\VolumesConfigurator;

abstract class AbstractJob implements JobInterface
{
public function labelSelector(LabelSelectorConfigurator $selector): void
{
}

public function backoffLimit(): ?int
{
return null;
}

public function activeDeadlineSeconds(): ?int
{
return null;
}

public function ttlSecondsAfterFinished(): ?int
{
return null;
}

public function completions(): ?int
{
return null;
}

public function manualSelector(): ?bool
{
return null;
}

public function parallelism(): ?int
{
return null;
}

public function configureMeta(MetadataConfigurator $meta): void
{
}

public function affinity(AffinityConfigurator $affinity): void
{
}

public function initContainers(PodContainers $containers): void
{
}

public function imagePullSecrets(ImagePullSecretsConfigurator $secrets): void
{
}

public function nodeSelector(StringMap $nodeSelector): void
{
}

public function volumes(VolumesConfigurator $volumes): void
{
}

public function restartPolicy(): ?RestartPolicy
{
return null;
}

public function configurePodSpec(PodSpec $spec): void
{
}

public function configureJob(Job $job): void
{
}
}
20 changes: 20 additions & 0 deletions src/Core/Job/JobInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Job;

use Dealroadshow\K8S\API\Batch\Job;
use Dealroadshow\K8S\Framework\Core\LabelSelector\LabelSelectorConfigurator;
use Dealroadshow\K8S\Framework\Core\ManifestInterface;
use Dealroadshow\K8S\Framework\Core\Pod\PodTemplateSpecInterface;

interface JobInterface extends PodTemplateSpecInterface, ManifestInterface
{
public function labelSelector(LabelSelectorConfigurator $selector): void;
public function backoffLimit(): ?int;
public function activeDeadlineSeconds(): ?int;
public function ttlSecondsAfterFinished(): ?int;
public function completions(): ?int;
public function manualSelector(): ?bool;
public function parallelism(): ?int;
public function configureJob(Job $job): void;
}
76 changes: 76 additions & 0 deletions src/ResourceMaker/JobMaker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Dealroadshow\K8S\Framework\ResourceMaker;

use Dealroadshow\K8S\API\Batch\Job;
use Dealroadshow\K8S\Framework\App\AppInterface;
use Dealroadshow\K8S\Framework\Core\Job\JobInterface;
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;

public function __construct(PodTemplateSpecProcessor $specProcessor)
{
$this->specProcessor = $specProcessor;
}

/**
* @param ManifestInterface|JobInterface $manifest
* @param AppInterface $app
*
* @return Job
*/
protected function makeResource(ManifestInterface $manifest, AppInterface $app): Job
{
$job = new Job();

$labelSelector = new LabelSelectorConfigurator($job->spec()->selector());
$manifest->labelSelector($labelSelector);

$app->metadataHelper()->configureMeta($manifest, $job);
$this->specProcessor->process($manifest, $job->spec()->template(), $app);

$spec = $job->spec();
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;
}

protected function supportsClass(): string
{
return JobInterface::class;
}
}

0 comments on commit 607cd29

Please sign in to comment.