Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/K8s.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RenokiCo\PhpK8s;

use Closure;
use Illuminate\Support\Traits\Macroable;

class K8s
Expand Down Expand Up @@ -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);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/YamlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
6 changes: 6 additions & 0 deletions tests/yaml/configmap_with_placeholder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: settings
data:
key: "{value}"