Skip to content

Commit 4eb1235

Browse files
authored
Merge pull request #73 from renoki-co/feature/handle-yaml-before-parse
[2.x] Handle YAML file content before parsing
2 parents a8b3226 + f9e5542 commit 4eb1235

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/K8s.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace RenokiCo\PhpK8s;
44

5+
use Closure;
56
use Illuminate\Support\Traits\Macroable;
67

78
class K8s
@@ -396,11 +397,18 @@ public static function fromYaml($cluster, string $yaml)
396397
*
397398
* @param \RenokiCo\PhpK8s\Kinds\KubernetesCluster|null $cluster
398399
* @param string $path
400+
* @param Closure|null $callback
399401
* @return \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource]
400402
*/
401-
public static function fromYamlFile($cluster, string $path)
403+
public static function fromYamlFile($cluster, string $path, Closure $callback = null)
402404
{
403-
return static::fromYaml($cluster, file_get_contents($path));
405+
$content = file_get_contents($path);
406+
407+
if ($callback) {
408+
$content = $callback($content);
409+
}
410+
411+
return static::fromYaml($cluster, $content);
404412
}
405413

406414
/**

tests/YamlTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@ public function test_yaml_import_multiple_kinds_in_same_file()
1919
$this->assertEquals(['postgres' => base64_encode('postgres')], $secret->getData(false));
2020
$this->assertEquals(['postgres' => 'postgres'], $secret->getData(true));
2121
}
22+
23+
public function test_yaml_import_with_handler()
24+
{
25+
$cm = $this->cluster->fromYamlFile(__DIR__.'/yaml/configmap_with_placeholder.yaml', function ($content) {
26+
return str_replace('{value}', 'assigned_value', $content);
27+
});
28+
29+
$this->assertEquals('v1', $cm->getApiVersion());
30+
$this->assertEquals('settings', $cm->getName());
31+
$this->assertEquals(['key' => 'assigned_value'], $cm->getData());
32+
}
2233
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: settings
5+
data:
6+
key: "{value}"

0 commit comments

Comments
 (0)