Skip to content

Commit

Permalink
EnvConfigurator::addFrom() method
Browse files Browse the repository at this point in the history
  • Loading branch information
petr-buchyn committed Dec 13, 2020
1 parent d2c6aaa commit 42e39b3
Showing 1 changed file with 41 additions and 7 deletions.
48 changes: 41 additions & 7 deletions src/Core/Container/Env/EnvConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use Dealroadshow\K8S\Data\EnvVar;
use Dealroadshow\K8S\Data\SecretKeySelector;
use Dealroadshow\K8S\Framework\App\AppInterface;
use Dealroadshow\K8S\Framework\Core\ConfigMap\ConfigMapInterface;
use Dealroadshow\K8S\Framework\Core\Container\Resources\ContainerResourcesField;
use Dealroadshow\K8S\Framework\Core\Pod\PodField;
use Dealroadshow\K8S\Framework\Core\Secret\SecretInterface;

class EnvConfigurator
{
Expand All @@ -26,7 +28,39 @@ public function __construct(EnvVarList $vars, EnvFromSourceList $sources, AppInt
$this->app = $app;
}

public function addConfigMap(string $configMapClass, bool $mustExist = true, string $varNamesPrefix = null): self
public function addFrom(string $className, bool $mustExist, string $varNamesPrefix = null): static
{
try {
$class = new \ReflectionClass($className);
} catch (\ReflectionException) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist', $className));
}

if ($class->implementsInterface(ConfigMapInterface::class)) {
return $this->addConfigMap($className, $mustExist, $varNamesPrefix);
} elseif ($class->implementsInterface(SecretInterface::class)) {
if (null !== $varNamesPrefix) {
throw new \LogicException(
sprintf(
'$varNamesPrefix must be specified only with config map classes, but specified with secret class "%s"',
$className
)
);
}

$this->addSecret($className, $mustExist);
}

throw new \InvalidArgumentException(
sprintf(
'$className must be a name of class, that implements either "%s" or "%s"',
ConfigMapInterface::class,
SecretInterface::class
)
);
}

public function addConfigMap(string $configMapClass, bool $mustExist = true, string $varNamesPrefix = null): static
{
$cmName = $this->app->namesHelper()->byConfigMapClass($configMapClass);

Expand All @@ -47,7 +81,7 @@ public function addConfigMap(string $configMapClass, bool $mustExist = true, str
return $this;
}

public function addSecret(string $secretClass, bool $mustExist = true): self
public function addSecret(string $secretClass, bool $mustExist = true): static
{
$secretName = $this->app->namesHelper()->bySecretClass($secretClass);
$envFromSource = new EnvFromSource();
Expand All @@ -60,7 +94,7 @@ public function addSecret(string $secretClass, bool $mustExist = true): self
return $this;
}

public function var(string $name, string $value): self
public function var(string $name, string $value): static
{
$var = new EnvVar($name);
$var->setValue($value);
Expand All @@ -70,7 +104,7 @@ public function var(string $name, string $value): self
return $this;
}

public function varFromConfigMap(string $varName, string $configMapClass, string $configMapKey, bool $optional = false): self
public function varFromConfigMap(string $varName, string $configMapClass, string $configMapKey, bool $optional = false): static
{
$cmName = $this->app->namesHelper()->byConfigMapClass($configMapClass);
$keySelector = new ConfigMapKeySelector($configMapKey);
Expand All @@ -85,7 +119,7 @@ public function varFromConfigMap(string $varName, string $configMapClass, string
return $this;
}

public function varFromSecret(string $varName, string $secretClass, string $secretKey, bool $optional = false): self
public function varFromSecret(string $varName, string $secretClass, string $secretKey, bool $optional = false): static
{
$secretName = $this->app->namesHelper()->bySecretClass($secretClass);
$keySelector = new SecretKeySelector($secretKey);
Expand All @@ -100,7 +134,7 @@ public function varFromSecret(string $varName, string $secretClass, string $secr
return $this;
}

public function varFromPod(string $varName, PodField $podField): self
public function varFromPod(string $varName, PodField $podField): static
{
$fieldSelector = $podField->selector();

Expand All @@ -111,7 +145,7 @@ public function varFromPod(string $varName, PodField $podField): self
return $this;
}

public function varFromContainerResources(string $varName, ContainerResourcesField $field): self
public function varFromContainerResources(string $varName, ContainerResourcesField $field): static
{
$fieldSelector = $field->selector();

Expand Down

0 comments on commit 42e39b3

Please sign in to comment.