Skip to content

Commit 4640160

Browse files
authored
Merge pull request #93 from renoki-co/feature/custom-caller-for-pod-selectors
[2.x] Custom Pods label selector from controllers
2 parents 7d95af9 + 8c3dabf commit 4640160

File tree

13 files changed

+234
-4
lines changed

13 files changed

+234
-4
lines changed

docs/kinds/DaemonSet.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- [Example](#example)
33
- [Pod Template Retrieval](#pod-template-retrieval)
44
- [Getting Pods](#getting-pods)
5+
- [Custom Pod Labels](#custom-pod-labels)
56
- [Scaling](#scaling)
67
- [Daemon Set Status](#daemon-set-status)
78

@@ -55,6 +56,18 @@ $podName = $template['name'];
5556

5657
## Getting Pods
5758

59+
To get the pods, the Pod template must have the `daemonset-name` label set. This way, the `labelSelector` API parameter is issued and you may retrieve the associated pods:
60+
61+
```yaml
62+
metadata:
63+
name: [here it goes the daemonset name]
64+
spec:
65+
template:
66+
metadata:
67+
labels:
68+
daemonset-name: [here it goes the daemonset name]
69+
```
70+
5871
You can retrieve the pods as resources controlled by the Daemon Set by issuing `->getPods()`:
5972

6073
```php
@@ -63,6 +76,23 @@ foreach ($ds->getPods() as $pod) {
6376
}
6477
```
6578

79+
### Custom Pod Labels
80+
81+
If you cannot declare the `daemonset-name` label or simply want to use something else, you may call `selectPods` from the resource:
82+
83+
```php
84+
use RenokiCo\PhpK8s\Kinds\K8sDaemonSet;
85+
86+
K8sDaemonSet::selectPods(function (K8sDaemonSet $ds) {
87+
// $ds is the current DaemonSet
88+
89+
return [
90+
'some-label' => 'some-label-value',
91+
'some-other-label' => "{$ds->getName()}-custom-name",
92+
];
93+
});
94+
```
95+
6696
## Scaling
6797

6898
The Scaling API is available via a `K8sScale` resource:

docs/kinds/Deployment.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- [Example](#example)
33
- [Pod Template Retrieval](#pod-template-retrieval)
44
- [Getting Pods](#getting-pods)
5+
- [Custom Pod Labels](#custom-pod-labels)
56
- [Scaling](#scaling)
67
- [Deployment Status](#deployment-status)
78

@@ -55,6 +56,18 @@ $podName = $template['name'];
5556

5657
## Getting Pods
5758

59+
To get the pods, the Pod template must have the `deployment-name` label set. This way, the `labelSelector` API parameter is issued and you may retrieve the associated pods:
60+
61+
```yaml
62+
metadata:
63+
name: [here it goes the deployment name]
64+
spec:
65+
template:
66+
metadata:
67+
labels:
68+
deployment-name: [here it goes the deployment name]
69+
```
70+
5871
You can retrieve the pods as resources controlled by the Deployment by issuing `->getPods()`:
5972

6073
```php
@@ -63,6 +76,23 @@ foreach ($de->getPods() as $pod) {
6376
}
6477
```
6578

79+
### Custom Pod Labels
80+
81+
If you cannot declare the `deployment-name` label or simply want to use something else, you may call `selectPods` from the resource:
82+
83+
```php
84+
use RenokiCo\PhpK8s\Kinds\K8sDeployment;
85+
86+
K8sDeployment::selectPods(function (K8sDeployment $dep) {
87+
// $dep is the current Deployment
88+
89+
return [
90+
'some-label' => 'some-label-value',
91+
'some-other-label' => "{$dep->getName()}-custom-name",
92+
];
93+
});
94+
```
95+
6696
## Scaling
6797

6898
The Scaling API is available via a `K8sScale` resource:

docs/kinds/Job.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- [Example](#example)
33
- [Pod Template Retrieval](#pod-template-retrieval)
44
- [Getting Pods](#getting-pods)
5+
- [Custom Pod Labels](#custom-pod-labels)
56
- [Job's Restart Policy](#jobs-restart-policy)
67
- [Job Status](#job-status)
78

@@ -53,6 +54,18 @@ $podName = $template['name'];
5354

5455
## Getting Pods
5556

57+
To get the pods, the Pod template must have the `job-name` label set. This way, the `labelSelector` API parameter is issued and you may retrieve the associated pods:
58+
59+
```yaml
60+
metadata:
61+
name: [here it goes the job name]
62+
spec:
63+
template:
64+
metadata:
65+
labels:
66+
job-name: [here it goes the job name]
67+
```
68+
5669
You can retrieve the pods as resources controlled by the Job by issuing `->getPods()`:
5770

5871
```php
@@ -61,6 +74,23 @@ foreach ($job->getPods() as $pod) {
6174
}
6275
```
6376

77+
### Custom Pod Labels
78+
79+
If you cannot declare the `job-name` label or simply want to use something else, you may call `selectPods` from the resource:
80+
81+
```php
82+
use RenokiCo\PhpK8s\Kinds\K8sJob;
83+
84+
K8sJob::selectPods(function (K8sJob $job) {
85+
// $job is the current Job
86+
87+
return [
88+
'some-label' => 'some-label-value',
89+
'some-other-label' => "{$job->getName()}-custom-name",
90+
];
91+
});
92+
```
93+
6494
## Job's Restart Policy
6595

6696
You might want to use `OnFailure` or `Never` as restart policies. These can be applied to the pod before passing it

docs/kinds/StatefulSet.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- [Example](#example)
33
- [Pod Template Retrieval](#pod-template-retrieval)
44
- [Getting Pods](#getting-pods)
5+
- [Custom Pod Labels](#custom-pod-labels)
56
- [Scaling](#scaling)
67
- [StatefulSet Status](#statefulset-status)
78

@@ -68,6 +69,18 @@ $podName = $template['name'];
6869

6970
## Getting Pods
7071

72+
To get the pods, the Pod template must have the `statefulset-name` label set. This way, the `labelSelector` API parameter is issued and you may retrieve the associated pods:
73+
74+
```yaml
75+
metadata:
76+
name: [here it goes the statefulset name]
77+
spec:
78+
template:
79+
metadata:
80+
labels:
81+
statefulset-name: [here it goes the statefulset name]
82+
```
83+
7184
You can retrieve the pods as resources controlled by the Stateful Set by issuing `->getPods()`:
7285

7386
```php
@@ -76,6 +89,23 @@ foreach ($sts->getPods() as $pod) {
7689
}
7790
```
7891

92+
### Custom Pod Labels
93+
94+
If you cannot declare the `statefulset-name` label or simply want to use something else, you may call `selectPods` from the resource:
95+
96+
```php
97+
use RenokiCo\PhpK8s\Kinds\K8sStatefulSet;
98+
99+
K8sStatefulSet::selectPods(function (K8sStatefulSet $sts) {
100+
// $sts is the current StatefulSet
101+
102+
return [
103+
'some-label' => 'some-label-value',
104+
'some-other-label' => "{$sts->getName()}-custom-name",
105+
];
106+
});
107+
```
108+
79109
## Scaling
80110

81111
The Scaling API is available via a `K8sScale` resource:

src/Kinds/K8sDaemonSet.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
class K8sDaemonSet extends K8sResource implements InteractsWithK8sCluster, Podable, Watchable
1717
{
1818
use HasMinimumSurge;
19-
use HasPods;
19+
use HasPods {
20+
podsSelector as protected customPodsSelector;
21+
}
2022
use HasSelector;
2123
use HasSpec;
2224
use HasStatus;
@@ -67,6 +69,10 @@ public function setUpdateStrategy(string $strategy, int $maxUnavailable = 1)
6769
*/
6870
public function podsSelector(): array
6971
{
72+
if ($podsSelector = $this->customPodsSelector()) {
73+
return $podsSelector;
74+
}
75+
7076
return [
7177
'daemonset-name' => $this->getName(),
7278
];

src/Kinds/K8sDeployment.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ class K8sDeployment extends K8sResource implements
2424
{
2525
use CanScale;
2626
use HasMinimumSurge;
27-
use HasPods;
27+
use HasPods {
28+
podsSelector as protected customPodsSelector;
29+
}
2830
use HasReplicas;
2931
use HasSelector;
3032
use HasSpec;
@@ -78,6 +80,10 @@ public function setUpdateStrategy(string $strategy, $maxUnavailable = '25%', $ma
7880
*/
7981
public function podsSelector(): array
8082
{
83+
if ($podsSelector = $this->customPodsSelector()) {
84+
return $podsSelector;
85+
}
86+
8187
return [
8288
'deployment-name' => $this->getName(),
8389
];

src/Kinds/K8sJob.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class K8sJob extends K8sResource implements
1818
Podable,
1919
Watchable
2020
{
21-
use HasPods;
21+
use HasPods {
22+
podsSelector as protected customPodsSelector;
23+
}
2224
use HasSelector;
2325
use HasSpec;
2426
use HasStatus;
@@ -64,6 +66,10 @@ public function setTTL(int $ttl = 100)
6466
*/
6567
public function podsSelector(): array
6668
{
69+
if ($podsSelector = $this->customPodsSelector()) {
70+
return $podsSelector;
71+
}
72+
6773
return [
6874
'job-name' => $this->getName(),
6975
];

src/Kinds/K8sStatefulSet.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class K8sStatefulSet extends K8sResource implements
2222
Watchable
2323
{
2424
use CanScale;
25-
use HasPods;
25+
use HasPods {
26+
podsSelector as protected customPodsSelector;
27+
}
2628
use HasReplicas;
2729
use HasSelector;
2830
use HasSpec;
@@ -147,6 +149,10 @@ public function getVolumeClaims(bool $asInstance = true)
147149
*/
148150
public function podsSelector(): array
149151
{
152+
if ($podsSelector = $this->customPodsSelector()) {
153+
return $podsSelector;
154+
}
155+
150156
return [
151157
'statefulset-name' => $this->getName(),
152158
];

src/Traits/HasPods.php

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

33
namespace RenokiCo\PhpK8s\Traits;
44

5+
use Closure;
6+
57
trait HasPods
68
{
9+
/**
10+
* Custom closure to set a dynamic pod selector.
11+
*
12+
* @var Closure|null
13+
*/
14+
protected static $podSelctorCallback;
15+
16+
/**
17+
* Get the selector for the pods that are owned by this resource.
18+
*
19+
* @return array
20+
*/
21+
public function podsSelector(): array
22+
{
23+
if ($callback = static::$podSelctorCallback) {
24+
return $callback($this);
25+
}
26+
27+
return [
28+
//
29+
];
30+
}
31+
32+
/**
33+
* Reset the pods selector callback.
34+
*
35+
* @return void
36+
*/
37+
public static function resetPodsSelector(): void
38+
{
39+
static::$podSelctorCallback = null;
40+
}
41+
42+
/**
43+
* Dynamically select the pods based on selectors.
44+
*
45+
* @param Closure $callback
46+
* @return void
47+
*/
48+
public static function selectPods(Closure $callback): void
49+
{
50+
static::$podSelctorCallback = $callback;
51+
}
52+
753
/**
854
* Get the pods owned by this resource.
955
*

tests/DaemonSetTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,18 @@ public function runCreationTests()
120120
sleep(1);
121121
}
122122

123+
K8sDaemonSet::selectPods(function ($ds) {
124+
$this->assertInstanceOf(K8sDaemonSet::class, $ds);
125+
126+
return ['tier' => 'backend'];
127+
});
128+
123129
$pods = $ds->getPods();
130+
$this->assertTrue($pods->count() > 0);
131+
132+
K8sDaemonSet::resetPodsSelector();
124133

134+
$pods = $ds->getPods();
125135
$this->assertTrue($pods->count() > 0);
126136

127137
foreach ($pods as $pod) {

0 commit comments

Comments
 (0)