Skip to content

Commit

Permalink
Merge pull request #3 from dealroadshow/v0.4.0-rc0
Browse files Browse the repository at this point in the history
Services, Ingresses, refactoring
  • Loading branch information
petr-buchyn authored Oct 13, 2020
2 parents 607cd29 + 3b437d4 commit f01e27e
Show file tree
Hide file tree
Showing 17 changed files with 578 additions and 16 deletions.
24 changes: 24 additions & 0 deletions src/Core/Ingress/AbstractIngress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Ingress;

use Dealroadshow\K8S\API\Extensions\Ingress;
use Dealroadshow\K8S\Data\IngressBackend;
use Dealroadshow\K8S\Framework\Core\Ingress\Configurator\IngressBackendFactory;
use Dealroadshow\K8S\Framework\Core\MetadataConfigurator;

abstract class AbstractIngress implements IngressInterface
{
public function backend(IngressBackendFactory $factory): ?IngressBackend
{
return null;
}

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

public function configureIngress(Ingress $ingress): void
{
}
}
61 changes: 61 additions & 0 deletions src/Core/Ingress/Configurator/IngressBackendFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Ingress\Configurator;

use Dealroadshow\K8S\Data\IngressBackend;
use Dealroadshow\K8S\Framework\App\AppInterface;
use Dealroadshow\K8S\ValueObject\IntOrString;

class IngressBackendFactory
{
private AppInterface $app;

public function __construct(AppInterface $app)
{
$this->app = $app;
}

/**
* @param string $serviceName
* @param int|string $servicePort
*
* @return IngressBackend
*/
public function fromServiceNameAndPort(string $serviceName, $servicePort): IngressBackend
{
$servicePort = $this->getPort($servicePort);
$backend = new IngressBackend($serviceName, $servicePort);
$backend
->setServiceName($serviceName)
->setServicePort($servicePort);

return $backend;
}

/**
* @param string $serviceClass
* @param int|string $servicePort
*
* @return IngressBackend
*/
public function fromServiceClassAndPort(string $serviceClass, $servicePort): IngressBackend
{
$serviceName = $this->app->namesHelper()->byServiceClass($serviceClass);

return $this->fromServiceNameAndPort($serviceName, $servicePort);
}

private function getPort($servicePort): IntOrString
{
// TODO change this to int|string typehint when PHP 8.0 is released
if (is_int($servicePort)) {
$servicePort = IntOrString::fromInt($servicePort);
} elseif (is_string($servicePort)) {
$servicePort = IntOrString::fromString($servicePort);
} else {
throw new \TypeError('$servicePort must be an int or a string');
}

return $servicePort;
}
}
34 changes: 34 additions & 0 deletions src/Core/Ingress/Configurator/IngressRulesConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Ingress\Configurator;

use Dealroadshow\K8S\Data\Collection\IngressRuleList;
use Dealroadshow\K8S\Data\HTTPIngressPath;
use Dealroadshow\K8S\Data\IngressBackend;
use Dealroadshow\K8S\Data\IngressRule;
use Dealroadshow\K8S\Framework\App\AppInterface;

class IngressRulesConfigurator
{
private AppInterface $app;
private IngressRuleList $rules;

public function __construct(AppInterface $app, IngressRuleList $rules)
{
$this->app = $app;
$this->rules = $rules;
}

public function addHttpRule(string $path, IngressBackend $backend, string $host = null): void
{
$ingressPath = new HTTPIngressPath($backend);
$ingressPath->setPath($path);

$rule = new IngressRule();
if (null !== $host) {
$rule->setHost($host);
}
$rule->http()->paths()->add($ingressPath);
$this->rules->add($rule);
}
}
16 changes: 16 additions & 0 deletions src/Core/Ingress/IngressInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Ingress;

use Dealroadshow\K8S\API\Extensions\Ingress;
use Dealroadshow\K8S\Data\IngressBackend;
use Dealroadshow\K8S\Framework\Core\Ingress\Configurator\IngressBackendFactory;
use Dealroadshow\K8S\Framework\Core\Ingress\Configurator\IngressRulesConfigurator;
use Dealroadshow\K8S\Framework\Core\ManifestInterface;

interface IngressInterface extends ManifestInterface
{
public function backend(IngressBackendFactory $factory): ?IngressBackend;
public function rules(IngressRulesConfigurator $rules, IngressBackendFactory $factory): void;
public function configureIngress(Ingress $ingress): void;
}
32 changes: 32 additions & 0 deletions src/Core/Service/AbstractService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Service;

use Dealroadshow\K8S\API\Service;
use Dealroadshow\K8S\Data\Collection\StringMap;
use Dealroadshow\K8S\Framework\Core\MetadataConfigurator;
use Dealroadshow\K8S\Framework\Core\Service\Configurator\ServicePortsConfigurator;
use Dealroadshow\K8S\Framework\Core\Service\Configurator\ServiceTypeConfigurator;

abstract class AbstractService implements ServiceInterface
{
public function configureMeta(MetadataConfigurator $meta): void
{
}

public function ports(ServicePortsConfigurator $ports): void
{
}

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

public function type(ServiceTypeConfigurator $type): void
{
}

public function configureService(Service $service): void
{
}
}
37 changes: 37 additions & 0 deletions src/Core/Service/Configurator/ServicePortConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Service\Configurator;

use Dealroadshow\K8S\Data\ServicePort;
use Dealroadshow\K8S\Framework\Core\Service\IPProtocol;

class ServicePortConfigurator
{
private ServicePort $port;

public function __construct(ServicePort $port)
{
$this->port = $port;
}

public function setProtocol(IPProtocol $protocol): self
{
$this->port->setProtocol($protocol->toString());

return $this;
}

public function setName(string $name): self
{
$this->port->setName($name);

return $this;
}

public function setNodePort(int $nodePort): self
{
$this->port->setNodePort($nodePort);

return $this;
}
}
41 changes: 41 additions & 0 deletions src/Core/Service/Configurator/ServicePortsConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Service\Configurator;

use Dealroadshow\K8S\Data\Collection\ServicePortList;
use Dealroadshow\K8S\Data\ServicePort;
use Dealroadshow\K8S\ValueObject\IntOrString;

class ServicePortsConfigurator
{
private ServicePortList $ports;

public function __construct(ServicePortList $ports)
{
$this->ports = $ports;
}

/**
* @param int $servicePort
* @param int|string $targetPort
*
* @return ServicePortConfigurator
*/
public function add(int $servicePort, $targetPort)
{
// TODO change this to int|string typehint when PHP 8.0 is released
if (is_int($targetPort)) {
$targetPort = IntOrString::fromInt($targetPort);
} elseif (is_string($targetPort)) {
$targetPort = IntOrString::fromString($targetPort);
} else {
throw new \TypeError('$targetPort must be an int or a string');
}

$port = new ServicePort($servicePort);
$port->setTargetPort($targetPort);
$this->ports->add($port);

return new ServicePortConfigurator($port);
}
}
56 changes: 56 additions & 0 deletions src/Core/Service/Configurator/ServiceTypeConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Service\Configurator;

use Dealroadshow\K8S\API\Service;

class ServiceTypeConfigurator
{
const TYPE_CLUSTER_IP = 'ClusterIP';
const TYPE_NODE_PORT = 'NodePort';
const TYPE_LOAD_BALANCER = 'LoadBalancer';
const TYPE_EXTERNAL_NAME = 'ExternalName';

private Service $service;

public function __construct(Service $service)
{
$this->service = $service;
}

public function clusterIP(): void
{
$this->setServiceType(self::TYPE_CLUSTER_IP);
}

public function nodePort(): void
{
$this->setServiceType(self::TYPE_NODE_PORT);
}

public function loadBalancer(): void
{
$this->setServiceType(self::TYPE_LOAD_BALANCER);
}

public function externalName(string $externalName): void
{
$this->setServiceType(self::TYPE_EXTERNAL_NAME);
$this->service->spec()->setExternalName($externalName);
}

private function setServiceType(string $type): void
{
$existingType = $this->service->spec()->getType();
if (null !== $existingType && $existingType !== $type) {
throw new \LogicException(
sprintf(
'Cannot set service type to "%s", it is already set to "%s"',
$type,
$existingType
)
);
}
$this->service->spec()->setType($type);
}
}
37 changes: 37 additions & 0 deletions src/Core/Service/IPProtocol.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Service;

final class IPProtocol
{
const TCP = 'TCP';
const UDP = 'UDP';
const SCTP = 'SCTP';

private string $value;

private function __construct(string $value)
{
$this->value = $value;
}

public function toString(): string
{
return $this->value;
}

public static function tcp(): self
{
return new self(self::TCP);
}

public static function udp(): self
{
return new self(self::UDP);
}

public static function sctp(): self
{
return new self(self::SCTP);
}
}
17 changes: 17 additions & 0 deletions src/Core/Service/ServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Dealroadshow\K8S\Framework\Core\Service;

use Dealroadshow\K8S\API\Service;
use Dealroadshow\K8S\Data\Collection\StringMap;
use Dealroadshow\K8S\Framework\Core\ManifestInterface;
use Dealroadshow\K8S\Framework\Core\Service\Configurator\ServicePortsConfigurator;
use Dealroadshow\K8S\Framework\Core\Service\Configurator\ServiceTypeConfigurator;

interface ServiceInterface extends ManifestInterface
{
public function ports(ServicePortsConfigurator $ports): void;
public function selector(StringMap $selector): void;
public function type(ServiceTypeConfigurator $type): void;
public function configureService(Service $service): void;
}
10 changes: 10 additions & 0 deletions src/Helper/Names/DefaultNamesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dealroadshow\K8S\Framework\Core\ConfigMap\ConfigMapInterface;
use Dealroadshow\K8S\Framework\Core\ManifestInterface;
use Dealroadshow\K8S\Framework\Core\Secret\SecretInterface;
use Dealroadshow\K8S\Framework\Core\Service\ServiceInterface;
use Dealroadshow\K8S\Framework\Helper\HelperTrait;

class DefaultNamesHelper implements NamesHelperInterface
Expand Down Expand Up @@ -60,6 +61,15 @@ public function bySecretClass(string $configMapClass): string
);
}

public function byServiceClass(string $serviceClass): string
{
return $this->byExpectedClassName(
$serviceClass,
ServiceInterface::class,
__METHOD__
);
}

private function byExpectedClassName(string $actualClass, string $expectedClass, string $methodName): string
{
if (!class_exists($actualClass)) {
Expand Down
1 change: 1 addition & 0 deletions src/Helper/Names/NamesHelperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public function byManifestClass(string $manifestClass): string;

public function byConfigMapClass(string $configMapClass): string;
public function bySecretClass(string $configMapClass): string;
public function byServiceClass(string $serviceClass): string;
}
Loading

0 comments on commit f01e27e

Please sign in to comment.