Skip to content

Commit ace92d8

Browse files
authored
Merge pull request #32 from renoki-co/feature/get-pods
[feature] Get pods
2 parents b0b8cbb + a88a2c9 commit ace92d8

13 files changed

+153
-13
lines changed

docs/kinds/Deployment.md

+10
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,13 @@ $pod = $dep->getTemplate(false);
100100

101101
$podName = $template['name'];
102102
```
103+
104+
### Deployment's Pods
105+
106+
You can retrieve the pods as resources controlled by the Deployment by issuing `->getPods()`:
107+
108+
```php
109+
foreach ($dep->getPods() as $pod) {
110+
// $pod->logs()
111+
}
112+
```

docs/kinds/Job.md

+10
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,13 @@ $pod = $job->getTemplate(false);
125125

126126
$podName = $template['name'];
127127
```
128+
129+
### Job's Pods
130+
131+
You can retrieve the pods as resources controlled by the Job by issuing `->getPods()`:
132+
133+
```php
134+
foreach ($job->getPods() as $pod) {
135+
// $pod->logs()
136+
}
137+
```

docs/kinds/StatefulSet.md

+10
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,13 @@ $pod = $sts->getTemplate(false);
116116

117117
$podName = $template['name'];
118118
```
119+
120+
### StatefulSet's Pods
121+
122+
You can retrieve the pods as resources controlled by the Statefulset by issuing `->getPods()`:
123+
124+
```php
125+
foreach ($sts->getPods() as $pod) {
126+
// $pod->logs()
127+
}
128+
```

src/Contracts/Podable.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace RenokiCo\PhpK8s\Contracts;
4+
5+
interface Podable
6+
{
7+
/**
8+
* Get the selector for the pods that are owned by this resource.
9+
*
10+
* @return array
11+
*/
12+
public function podsSelector(): array;
13+
}

src/Kinds/K8sDeployment.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
namespace RenokiCo\PhpK8s\Kinds;
44

55
use RenokiCo\PhpK8s\Contracts\InteractsWithK8sCluster;
6+
use RenokiCo\PhpK8s\Contracts\Podable;
67
use RenokiCo\PhpK8s\Contracts\Watchable;
78
use RenokiCo\PhpK8s\Traits\HasAnnotations;
89
use RenokiCo\PhpK8s\Traits\HasLabels;
10+
use RenokiCo\PhpK8s\Traits\HasPods;
911
use RenokiCo\PhpK8s\Traits\HasSelector;
1012
use RenokiCo\PhpK8s\Traits\HasSpec;
1113
use RenokiCo\PhpK8s\Traits\HasTemplate;
1214

13-
class K8sDeployment extends K8sResource implements InteractsWithK8sCluster, Watchable
15+
class K8sDeployment extends K8sResource implements InteractsWithK8sCluster, Podable, Watchable
1416
{
15-
use HasAnnotations, HasLabels, HasSelector, HasSpec, HasTemplate;
17+
use HasAnnotations, HasLabels, HasPods, HasSelector, HasSpec, HasTemplate;
1618

1719
/**
1820
* The resource Kind parameter.
@@ -95,4 +97,16 @@ public function resourceWatchPath(): string
9597
{
9698
return "/apis/{$this->getApiVersion()}/watch/namespaces/{$this->getNamespace()}/deployments/{$this->getIdentifier()}";
9799
}
100+
101+
/**
102+
* Get the selector for the pods that are owned by this resource.
103+
*
104+
* @return array
105+
*/
106+
public function podsSelector(): array
107+
{
108+
return [
109+
'deployment-name' => $this->getName(),
110+
];
111+
}
98112
}

src/Kinds/K8sJob.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
namespace RenokiCo\PhpK8s\Kinds;
44

55
use RenokiCo\PhpK8s\Contracts\InteractsWithK8sCluster;
6+
use RenokiCo\PhpK8s\Contracts\Podable;
67
use RenokiCo\PhpK8s\Contracts\Watchable;
78
use RenokiCo\PhpK8s\Traits\HasAnnotations;
89
use RenokiCo\PhpK8s\Traits\HasLabels;
10+
use RenokiCo\PhpK8s\Traits\HasPods;
911
use RenokiCo\PhpK8s\Traits\HasSelector;
1012
use RenokiCo\PhpK8s\Traits\HasSpec;
1113
use RenokiCo\PhpK8s\Traits\HasTemplate;
1214

13-
class K8sJob extends K8sResource implements InteractsWithK8sCluster, Watchable
15+
class K8sJob extends K8sResource implements InteractsWithK8sCluster, Podable, Watchable
1416
{
15-
use HasAnnotations, HasLabels, HasSelector, HasSpec, HasTemplate;
17+
use HasAnnotations, HasLabels, HasPods, HasSelector, HasSpec, HasTemplate;
1618

1719
/**
1820
* The resource Kind parameter.
@@ -85,4 +87,16 @@ public function resourceWatchPath(): string
8587
{
8688
return "/apis/{$this->getApiVersion()}/watch/namespaces/{$this->getNamespace()}/jobs/{$this->getIdentifier()}";
8789
}
90+
91+
/**
92+
* Get the selector for the pods that are owned by this resource.
93+
*
94+
* @return array
95+
*/
96+
public function podsSelector(): array
97+
{
98+
return [
99+
'job-name' => $this->getName(),
100+
];
101+
}
88102
}

src/Kinds/K8sResource.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,12 @@ public function whereName(string $name)
270270
* Get a resource by name.
271271
*
272272
* @param string $name
273+
* @param array $query
273274
* @return \RenokiCo\PhpK8s\Kinds\K8sResource
274275
*/
275-
public function getByName(string $name)
276+
public function getByName(string $name, array $query = ['pretty' => 1])
276277
{
277-
return $this->whereName($name)->get();
278+
return $this->whereName($name)->get($query);
278279
}
279280

280281
/**

src/Kinds/K8sStatefulSet.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
namespace RenokiCo\PhpK8s\Kinds;
44

55
use RenokiCo\PhpK8s\Contracts\InteractsWithK8sCluster;
6+
use RenokiCo\PhpK8s\Contracts\Podable;
67
use RenokiCo\PhpK8s\Contracts\Watchable;
78
use RenokiCo\PhpK8s\Traits\HasAnnotations;
89
use RenokiCo\PhpK8s\Traits\HasLabels;
10+
use RenokiCo\PhpK8s\Traits\HasPods;
911
use RenokiCo\PhpK8s\Traits\HasSelector;
1012
use RenokiCo\PhpK8s\Traits\HasSpec;
1113
use RenokiCo\PhpK8s\Traits\HasTemplate;
1214

13-
class K8sStatefulSet extends K8sResource implements InteractsWithK8sCluster, Watchable
15+
class K8sStatefulSet extends K8sResource implements InteractsWithK8sCluster, Podable, Watchable
1416
{
15-
use HasAnnotations, HasLabels, HasSelector, HasSpec, HasTemplate;
17+
use HasAnnotations, HasLabels, HasPods, HasSelector, HasSpec, HasTemplate;
1618

1719
/**
1820
* The resource Kind parameter.
@@ -156,4 +158,16 @@ public function resourceWatchPath(): string
156158
{
157159
return "/apis/{$this->getApiVersion()}/watch/namespaces/{$this->getNamespace()}/statefulsets/{$this->getIdentifier()}";
158160
}
161+
162+
/**
163+
* Get the selector for the pods that are owned by this resource.
164+
*
165+
* @return array
166+
*/
167+
public function podsSelector(): array
168+
{
169+
return [
170+
'statefulset-name' => $this->getName(),
171+
];
172+
}
159173
}

src/KubernetesCluster.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ public function __call($method, $parameters)
8686
if (method_exists(K8s::class, $resource)) {
8787
return $this->{$resource}()
8888
->whereNamespace($parameters[1] ?? K8sResource::$defaultNamespace)
89-
->getByName($parameters[0]);
89+
->getByName($parameters[0], $parameters[3] ?? ['pretty' => 1]);
9090
}
9191
}
9292

93-
// Proxy the ->getAll[Resources]($namespace = 'default')
93+
// Proxy the ->getAll[Resources]($namespace = 'default', $query = [...])
9494
// For example, ->getAllServices('staging')
9595
if (preg_match('/getAll(.+)/', $method, $matches)) {
9696
[$method, $resourcePlural] = $matches;
@@ -100,7 +100,7 @@ public function __call($method, $parameters)
100100
if (method_exists(K8s::class, $resource)) {
101101
return $this->{$resource}()
102102
->whereNamespace($parameters[1] ?? K8sResource::$defaultNamespace)
103-
->all();
103+
->all($parameters[2] ?? ['pretty' => 1]);
104104
}
105105
}
106106

src/Traits/HasPods.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace RenokiCo\PhpK8s\Traits;
4+
5+
trait HasPods
6+
{
7+
/**
8+
* Get the pods owned by this resource.
9+
*
10+
* @param array $query
11+
* @return \RenokiCo\PhpK8s\ResourceList
12+
*/
13+
public function getPods(array $query = ['pretty' => 1])
14+
{
15+
$labelSelector = http_build_query(
16+
$this->podsSelector()
17+
);
18+
19+
return $this->cluster
20+
->pod()
21+
->setNamespace($this->getNamespace())
22+
->all(array_merge(['labelSelector' => $labelSelector], $query));
23+
}
24+
}

tests/DeploymentTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function runCreationTests()
9292

9393
$pod = $this->cluster->pod()
9494
->setName('mysql')
95-
->setLabels(['tier' => 'backend'])
95+
->setLabels(['tier' => 'backend', 'deployment-name' => 'mysql'])
9696
->setAnnotations(['mysql/annotation' => 'yes'])
9797
->setContainers([$mysql]);
9898

@@ -123,6 +123,16 @@ public function runCreationTests()
123123

124124
$this->assertInstanceOf(K8sPod::class, $dep->getTemplate());
125125

126+
sleep(10);
127+
128+
$pods = $dep->getPods();
129+
130+
$this->assertTrue($pods->count() > 0);
131+
132+
foreach ($pods as $pod) {
133+
$this->assertInstanceOf(K8sPod::class, $pod);
134+
}
135+
126136
// Wait for the pod to create entirely.
127137
sleep(60);
128138
}

tests/JobTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ public function runCreationTests()
113113

114114
$this->assertInstanceOf(K8sPod::class, $job->getTemplate());
115115

116+
sleep(10);
117+
118+
$pods = $job->getPods();
119+
120+
$this->assertTrue($pods->count() > 0);
121+
122+
foreach ($pods as $pod) {
123+
$this->assertInstanceOf(K8sPod::class, $pod);
124+
}
125+
116126
// Wait for the pod to create entirely.
117127
sleep(20);
118128
}

tests/StatefulSetTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function runCreationTests()
125125

126126
$pod = $this->cluster->pod()
127127
->setName('mysql')
128-
->setLabels(['tier' => 'backend'])
128+
->setLabels(['tier' => 'backend', 'statefulset-name' => 'mysql'])
129129
->setAnnotations(['mysql/annotation' => 'yes'])
130130
->setContainers([$mysql]);
131131

@@ -174,6 +174,16 @@ public function runCreationTests()
174174
$this->assertInstanceOf(K8sPod::class, $sts->getTemplate());
175175
$this->assertInstanceOf(K8sPersistentVolumeClaim::class, $sts->getVolumeClaims()[0]);
176176

177+
sleep(10);
178+
179+
$pods = $sts->getPods();
180+
181+
$this->assertTrue($pods->count() > 0);
182+
183+
foreach ($pods as $pod) {
184+
$this->assertInstanceOf(K8sPod::class, $pod);
185+
}
186+
177187
// Wait for the pod to create entirely.
178188
sleep(60);
179189
}

0 commit comments

Comments
 (0)