generated from renoki-co/laravel-package-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathK8s.php
158 lines (137 loc) · 4.47 KB
/
K8s.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace RenokiCo\PhpK8s;
use Closure;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use RenokiCo\PhpK8s\Kinds\K8sResource;
use RenokiCo\PhpK8s\Traits\InitializesInstances;
use RenokiCo\PhpK8s\Traits\InitializesResources;
class K8s
{
use InitializesInstances;
use InitializesResources;
use Macroable {
__call as macroCall;
__callStatic as macroCallStatic;
}
/**
* Load Kind configuration from an YAML text.
*
* @param \RenokiCo\PhpK8s\KubernetesCluster|null $cluster
* @param string $yaml
* @return \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource]
*/
public static function fromYaml($cluster, string $yaml)
{
$instances = collect(yaml_parse($yaml, -1))->reduce(function ($classes, $yaml) use ($cluster) {
$kind = $yaml['kind'];
$apiVersion = $yaml['apiVersion'];
unset($yaml['apiVersion'], $yaml['kind']);
if (static::hasMacro($macro = K8sResource::getUniqueCrdMacro($kind, $apiVersion))) {
$classes[] = static::{$macro}($cluster, $yaml);
}
if (method_exists(static::class, $kind)) {
$classes[] = static::{$kind}($cluster, $yaml);
}
return $classes;
}, []);
return count($instances) === 1
? $instances[0]
: $instances;
}
/**
* Load Kind configuration from an YAML file.
*
* @param \RenokiCo\PhpK8s\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, Closure $callback = null)
{
$content = file_get_contents($path);
if ($callback) {
$content = $callback($content);
}
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\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;
});
}
/**
* Register a CRD inside the package.
*
* @param string $class
* @param string|null $name
* @return void
*/
public static function registerCrd(string $class, string $name = null): void
{
static::macro(
Str::camel($name ?: substr($class, strrpos($class, '\\') + 1)),
function ($cluster = null, array $attributes = []) use ($class) {
return new $class($cluster, $attributes);
}
);
static::macro(
$class::getUniqueCrdMacro(),
function ($cluster = null, array $attributes = []) use ($class) {
return new $class($cluster, $attributes);
}
);
}
/**
* Flush the macros.
*
* @return void
*/
public static function flushMacros(): void
{
static::$macros = [];
}
/**
* Proxy the K8s call to cluster object.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}
return $this->cluster->{$method}(...$parameters);
}
/**
* Proxy the K8s static call to cluster object.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
if (static::hasMacro($method)) {
return static::macroCallStatic($method, $parameters);
}
return new static;
}
}