Skip to content

Commit 0ba9261

Browse files
authored
Merge pull request #21 from appwrite-labs/codex/container-restart-policy
[codex] Add container restart policy accessors
2 parents 8ae0072 + 71dc88e commit 0ba9261

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

src/Instances/Container.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace RenokiCo\PhpK8s\Instances;
44

5+
use RenokiCo\PhpK8s\Enums\RestartPolicy;
6+
57
class Container extends Instance
68
{
79
/**
@@ -94,6 +96,32 @@ public function getWorkingDir(): ?string
9496
return $this->getAttribute('workingDir');
9597
}
9698

99+
/**
100+
* Set the restart policy for the container.
101+
*/
102+
public function setRestartPolicy(RestartPolicy|string $policy): static
103+
{
104+
if ($policy instanceof RestartPolicy) {
105+
$policy = $policy->value;
106+
}
107+
108+
return $this->setAttribute('restartPolicy', $policy);
109+
}
110+
111+
/**
112+
* Get the restart policy for the container.
113+
*/
114+
public function getRestartPolicy(): ?RestartPolicy
115+
{
116+
$policy = $this->getAttribute('restartPolicy');
117+
118+
if ($policy === null) {
119+
return null;
120+
}
121+
122+
return RestartPolicy::tryFrom($policy);
123+
}
124+
97125
/**
98126
* Add a new port to the container list.
99127
*

tests/ContainerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace RenokiCo\PhpK8s\Test;
44

5+
use RenokiCo\PhpK8s\Enums\RestartPolicy;
56
use RenokiCo\PhpK8s\Instances\MountedVolume;
67
use RenokiCo\PhpK8s\Instances\Probe;
78
use RenokiCo\PhpK8s\K8s;
@@ -10,6 +11,8 @@ class ContainerTest extends TestCase
1011
{
1112
public function test_container_build()
1213
{
14+
$this->assertNull(K8s::container()->getRestartPolicy());
15+
1316
$container = K8s::container();
1417

1518
$volume = K8s::volume()->awsEbs('vol-1234', 'ext3');
@@ -21,6 +24,7 @@ public function test_container_build()
2124
->addConfigMapRefs(['SECRET_TWO' => ['cm_ref_name', 'cm_ref_key']])
2225
->addFieldRefs(['NODE_NAME' => ['spec.nodeName']])
2326
->setArgs(['--test'])
27+
->setRestartPolicy(RestartPolicy::ALWAYS)
2428
->addPort(80, 'TCP', 'http')
2529
->addPort(443, 'TCP', 'https')
2630
->setMountedVolumes([$volume->mountTo('/some/path')]);
@@ -98,10 +102,15 @@ public function test_container_build()
98102
$this->assertStringEndsWith('nginx:1.23', $container->getImage());
99103
$this->assertEquals([], $container->getEnv([]));
100104
$this->assertEquals(['--test'], $container->getArgs());
105+
$this->assertEquals(RestartPolicy::ALWAYS, $container->getRestartPolicy());
101106
$this->assertEquals([
102107
['name' => 'http', 'protocol' => 'TCP', 'containerPort' => 80],
103108
['name' => 'https', 'protocol' => 'TCP', 'containerPort' => 443],
104109
], $container->getPorts());
110+
111+
$stringPolicy = K8s::container()->setRestartPolicy('Never');
112+
$this->assertEquals(RestartPolicy::NEVER, $stringPolicy->getRestartPolicy());
113+
105114
$this->assertEquals('1Gi', $container->getMinMemory());
106115
$this->assertEquals('2Gi', $container->getMaxMemory());
107116
$this->assertEquals('500m', $container->getMinCpu());

0 commit comments

Comments
 (0)