-
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.
- Loading branch information
1 parent
607cd29
commit 3b437d4
Showing
17 changed files
with
578 additions
and
16 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,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 | ||
{ | ||
} | ||
} |
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,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
34
src/Core/Ingress/Configurator/IngressRulesConfigurator.php
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,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); | ||
} | ||
} |
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,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; | ||
} |
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,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 | ||
{ | ||
} | ||
} |
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\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
41
src/Core/Service/Configurator/ServicePortsConfigurator.php
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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\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); | ||
} | ||
} |
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,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; | ||
} |
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
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
Oops, something went wrong.