Skip to content

Commit

Permalink
Merge pull request #126 from renoki-co/3.x/templated-yaml
Browse files Browse the repository at this point in the history
[3.x] Template YAML file to resource
  • Loading branch information
rennokki authored Sep 5, 2021
2 parents 79ddcc1 + da0f587 commit 7d9dfc2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/K8s.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
3 changes: 3 additions & 0 deletions src/KubernetesCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
14 changes: 14 additions & 0 deletions tests/YamlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 7d9dfc2

Please sign in to comment.