From d2c04da338c67f9a0d175ac02372efdeb648deaf Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Mon, 8 Mar 2021 21:57:59 +0200 Subject: [PATCH 1/2] Optional YAML handling before file importing --- src/K8s.php | 11 +++++++++-- tests/YamlTest.php | 11 +++++++++++ tests/yaml/configmap_with_placeholder.yaml | 6 ++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/yaml/configmap_with_placeholder.yaml diff --git a/src/K8s.php b/src/K8s.php index 6b7e4299..5c8891b1 100644 --- a/src/K8s.php +++ b/src/K8s.php @@ -396,11 +396,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}" From f9e5542ba98faec2e527f1006044294cea9bb315 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Mon, 8 Mar 2021 22:00:29 +0200 Subject: [PATCH 2/2] fix test --- src/K8s.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/K8s.php b/src/K8s.php index 5c8891b1..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