From da0f587fee08ec246b7962dbe2a34a963d052c11 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sun, 5 Sep 2021 19:04:09 +0300 Subject: [PATCH] Added a templating yaml function --- src/K8s.php | 22 ++++++++++++++++++++++ src/KubernetesCluster.php | 3 +++ tests/YamlTest.php | 14 ++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/K8s.php b/src/K8s.php index a59a139d..5ca2d9d6 100644 --- a/src/K8s.php +++ b/src/K8s.php @@ -59,6 +59,28 @@ public static function fromYamlFile($cluster, string $path, Closure $callback = return static::fromYaml($cluster, $content); } + /** + * Load Kind configuration fron a YAML file, making sure to + * replace all variables in curly braces with the values from + * the given array. + * + * @param \RenokiCo\PhpK8s\Kinds\KubernetesCluster|null $cluster + * @param string $path + * @param array $replace + * @param \Closure|null $callback + * @return \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] + */ + public static function fromTemplatedYamlFile($cluster, string $path, array $replace, Closure $callback = null) + { + return static::fromYamlFile($cluster, $path, function ($content) use ($replace, $callback) { + foreach ($replace as $search => $replacement) { + $content = str_replace("{{$search}}", $replacement, $content); + } + + return $callback ? $callback($content) : $content; + }); + } + /** * Proxy the K8s call to cluster object. * diff --git a/src/KubernetesCluster.php b/src/KubernetesCluster.php index 8d1fa1fb..2ea1b644 100644 --- a/src/KubernetesCluster.php +++ b/src/KubernetesCluster.php @@ -98,6 +98,9 @@ * @method \RenokiCo\PhpK8s\Kinds\K8sPodDisruptionBudget getPodDisruptionBudgetByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1]) * @method \RenokiCo\PhpK8s\ResourcesList getAllPodDisruptionBudgetsFromAllNamespaces(array $query = ['pretty' => 1]) * @method \RenokiCo\PhpK8s\ResourcesList getAllPodDisruptionBudgets(string $namespace = 'default', array $query = ['pretty' => 1]) + * @method \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] fromYaml(string $yaml) + * @method \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] fromYamlFile(string $path, \Closure $callback = null) + * @method \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] fromTemplatedYamlFile(string $path, array $replace, \Closure $callback = null) * * @see \RenokiCo\PhpK8s\K8s */ diff --git a/tests/YamlTest.php b/tests/YamlTest.php index 784ec33d..252d27eb 100644 --- a/tests/YamlTest.php +++ b/tests/YamlTest.php @@ -30,4 +30,18 @@ public function test_yaml_import_with_handler() $this->assertEquals('settings', $cm->getName()); $this->assertEquals(['key' => 'assigned_value'], $cm->getData()); } + + public function test_yaml_template() + { + $replacements = [ + 'value' => 'assigned_value_at_template', + 'value2' => 'not_assigned', + ]; + + $cm = $this->cluster->fromTemplatedYamlFile(__DIR__.'/yaml/configmap_with_placeholder.yaml', $replacements); + + $this->assertEquals('v1', $cm->getApiVersion()); + $this->assertEquals('settings', $cm->getName()); + $this->assertEquals(['key' => 'assigned_value_at_template'], $cm->getData()); + } }