diff --git a/src/Core/Container/Env/EnvConfigurator.php b/src/Core/Container/Env/EnvConfigurator.php index 10d501b..3f55a91 100644 --- a/src/Core/Container/Env/EnvConfigurator.php +++ b/src/Core/Container/Env/EnvConfigurator.php @@ -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 { @@ -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); @@ -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(); @@ -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); @@ -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); @@ -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); @@ -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(); @@ -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();