Skip to content

Commit b7f25bd

Browse files
committed
Added container methods
1 parent 73caae1 commit b7f25bd

File tree

5 files changed

+458
-0
lines changed

5 files changed

+458
-0
lines changed

docs/instances/Container.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Containers
2+
3+
## Example
4+
5+
### Creating a container
6+
7+
```php
8+
$container = K8s::container()
9+
->setName('mysql')
10+
->setImage('mysql', '5.7')
11+
->setPorts([
12+
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
13+
])
14+
->addPort(3307, 'TCP', 'mysql-alt')
15+
->setCommand(['mysqld'])
16+
->setArgs(['--test'])
17+
->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']);
18+
```
19+
20+
### Setting probes
21+
22+
You might want to set probes for your containers:
23+
24+
Command probes:
25+
26+
```php
27+
$probe = K8s::probe()->command(['sh', 'test.sh']);
28+
```
29+
30+
HTTP probes:
31+
32+
```php
33+
$probe = K8s::probe()->http('/health', 80, ['X-CSRF-TOKEN' => 'some-token'])
34+
```
35+
36+
TCP probes:
37+
38+
```php
39+
$probe = K8s::probe()->tcp(3306);
40+
```
41+
42+
### Attaching probes
43+
44+
You might attach the probes to the container:
45+
46+
```php
47+
$container->setLivenessProbe(
48+
K8s::probe()->command(['sh', 'test.sh'])
49+
->initialDelaySeconds(10)
50+
->periodSeconds(60)
51+
->timeout(10)
52+
->failureThreshold(3)
53+
->successThrehshold(2)
54+
);
55+
56+
$container->setStartupProbe(
57+
K8s::probe()->http('/health', 80, ['X-CSRF-TOKEN' => 'some-token'])
58+
->initialDelaySeconds(10)
59+
->periodSeconds(60)
60+
->timeout(10)
61+
->failureThreshold(3)
62+
->successThrehshold(2)
63+
);
64+
65+
$container->setReadinessProbe(
66+
K8s::probe()->tcp(3306, '10.0.0.0')
67+
->initialDelaySeconds(10)
68+
->periodSeconds(60)
69+
->timeout(10)
70+
->failureThreshold(3)
71+
->successThrehshold(2)
72+
);
73+
```
74+
75+
### Setting resources
76+
77+
```php
78+
$container->minMemory(512, 'Mi')->maxMemory(2, 'Gi');
79+
80+
$container->minCpu('500m')->maxCpu(1);
81+
```
82+
83+
### Setting custom fields
84+
85+
If you wish to set custom fields for containers or probes, you can use the `setAttribute('...', $value)` directly onto the instance.
86+
87+
Please check the [Resource](../kinds/Resource) documentation for methods that does not exist in the current instance.

src/Instances/Container.php

+171
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,177 @@ public function addPort(int $containerPort, string $protocol = 'TCP', string $na
5353
return $this->setAttribute('ports', $ports);
5454
}
5555

56+
/**
57+
* Requests minimum memory for the container.
58+
*
59+
* @param int $size
60+
* @param string $measure
61+
* @return $this
62+
*/
63+
public function minMemory(int $size, string $measure = 'Gi')
64+
{
65+
return $this->setAttribute('resources.requests.memory', $size.$measure);
66+
}
67+
68+
/**
69+
* Get the minimum memory amount.
70+
*
71+
* @return string|null
72+
*/
73+
public function getMinMemory()
74+
{
75+
return $this->getAttribute('resources.requests.memory', null);
76+
}
77+
78+
/**
79+
* Requests minimum CPU for the container.
80+
*
81+
* @param string $size
82+
* @return $this
83+
*/
84+
public function minCpu(string $size)
85+
{
86+
return $this->setAttribute('resources.requests.cpu', $size);
87+
}
88+
89+
/**
90+
* Get the minimum CPU amount.
91+
*
92+
* @return string|null
93+
*/
94+
public function getMinCpu()
95+
{
96+
return $this->getAttribute('resources.requests.cpu', null);
97+
}
98+
99+
/**
100+
* Sets max memory for the container.
101+
*
102+
* @param int $size
103+
* @param string $measure
104+
* @return $this
105+
*/
106+
public function maxMemory(int $size, string $measure = 'Gi')
107+
{
108+
return $this->setAttribute('resources.limits.memory', $size.$measure);
109+
}
110+
111+
/**
112+
* Get the max memory amount.
113+
*
114+
* @return string|null
115+
*/
116+
public function getMaxMemory()
117+
{
118+
return $this->getAttribute('resources.limits.memory', null);
119+
}
120+
121+
/**
122+
* Sets max CPU for the container.
123+
*
124+
* @param string $size
125+
* @return $this
126+
*/
127+
public function maxCpu(string $size)
128+
{
129+
return $this->setAttribute('resources.limits.cpu', $size);
130+
}
131+
132+
/**
133+
* Get the max CPU amount.
134+
*
135+
* @return string|null
136+
*/
137+
public function getMaxCpu()
138+
{
139+
return $this->getAttribute('resources.limits.cpu', null);
140+
}
141+
142+
143+
/**
144+
* Set the readiness probe for the container.
145+
*
146+
* @param \RenokiCo\PhpK8s\Instances\Probe $probe
147+
* @return $this
148+
*/
149+
public function setReadinessProbe(Probe $probe)
150+
{
151+
return $this->setAttribute('readinessProbe', $probe->toArray());
152+
}
153+
154+
/**
155+
* Get the readiness probe.
156+
*
157+
* @param bool $asInstance
158+
* @return null|array|\RenokiCo\PhpK8s\Instances\Probe
159+
*/
160+
public function getReadinessProbe(bool $asInstance = true)
161+
{
162+
$probe = $this->getAttribute('readinessProbe', null);
163+
164+
if (! $probe) {
165+
return;
166+
}
167+
168+
return $asInstance ? new Probe($probe) : $probe;
169+
}
170+
171+
/**
172+
* Set the liveness probe for the container.
173+
*
174+
* @param \RenokiCo\PhpK8s\Instances\Probe $probe
175+
* @return $this
176+
*/
177+
public function setLivenessProbe(Probe $probe)
178+
{
179+
return $this->setAttribute('livenessProbe', $probe->toArray());
180+
}
181+
182+
/**
183+
* Get the liveness probe.
184+
*
185+
* @param bool $asInstance
186+
* @return null|array|\RenokiCo\PhpK8s\Instances\Probe
187+
*/
188+
public function getLivenessProbe(bool $asInstance = true)
189+
{
190+
$probe = $this->getAttribute('livenessProbe', null);
191+
192+
if (! $probe) {
193+
return;
194+
}
195+
196+
return $asInstance ? new Probe($probe) : $probe;
197+
}
198+
199+
/**
200+
* Set the startup probe for the container.
201+
*
202+
* @param \RenokiCo\PhpK8s\Instances\Probe $probe
203+
* @return $this
204+
*/
205+
public function setStartupProbe(Probe $probe)
206+
{
207+
return $this->setAttribute('startupProbe', $probe->toArray());
208+
}
209+
210+
/**
211+
* Get the startup probe.
212+
*
213+
* @param bool $asInstance
214+
* @return null|array|\RenokiCo\PhpK8s\Instances\Probe
215+
*/
216+
public function getStartupProbe(bool $asInstance = true)
217+
{
218+
$probe = $this->getAttribute('startupProbe', null);
219+
220+
if (! $probe) {
221+
return;
222+
}
223+
224+
return $asInstance ? new Probe($probe) : $probe;
225+
}
226+
56227
/**
57228
* Check if the container is ready.
58229
*

0 commit comments

Comments
 (0)