diff --git a/src/K8s.php b/src/K8s.php index 6b7e4299..dde6ebc5 100644 --- a/src/K8s.php +++ b/src/K8s.php @@ -2,6 +2,7 @@ namespace RenokiCo\PhpK8s; +use Closure; use Illuminate\Support\Traits\Macroable; class K8s @@ -396,11 +397,18 @@ public static function fromYaml($cluster, string $yaml) * * @param \RenokiCo\PhpK8s\Kinds\KubernetesCluster|null $cluster * @param string $path + * @param Closure|null $callback * @return \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] */ - public static function fromYamlFile($cluster, string $path) + public static function fromYamlFile($cluster, string $path, Closure $callback = null) { - return static::fromYaml($cluster, file_get_contents($path)); + $content = file_get_contents($path); + + if ($callback) { + $content = $callback($content); + } + + return static::fromYaml($cluster, $content); } /** diff --git a/tests/YamlTest.php b/tests/YamlTest.php index 85d96582..784ec33d 100644 --- a/tests/YamlTest.php +++ b/tests/YamlTest.php @@ -19,4 +19,15 @@ public function test_yaml_import_multiple_kinds_in_same_file() $this->assertEquals(['postgres' => base64_encode('postgres')], $secret->getData(false)); $this->assertEquals(['postgres' => 'postgres'], $secret->getData(true)); } + + public function test_yaml_import_with_handler() + { + $cm = $this->cluster->fromYamlFile(__DIR__.'/yaml/configmap_with_placeholder.yaml', function ($content) { + return str_replace('{value}', 'assigned_value', $content); + }); + + $this->assertEquals('v1', $cm->getApiVersion()); + $this->assertEquals('settings', $cm->getName()); + $this->assertEquals(['key' => 'assigned_value'], $cm->getData()); + } } diff --git a/tests/yaml/configmap_with_placeholder.yaml b/tests/yaml/configmap_with_placeholder.yaml new file mode 100644 index 00000000..b331eee0 --- /dev/null +++ b/tests/yaml/configmap_with_placeholder.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: settings +data: + key: "{value}"