Skip to content

Commit 7dfb3f0

Browse files
authored
Merge pull request #105 from renoki-co/feature/set-or-update-metadata
[feature] setOrUpdateAnnotaitons and setOrUpdateLabels
2 parents 75ac6ac + 1918c05 commit 7dfb3f0

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
lines changed

docs/kinds/Resource.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
- [Labels](#labels)
1212
- [`setLabels(array $labels)`](#setlabelsarray-labels)
1313
- [`getLabels()`](#getlabels)
14+
- [`getLabel($name, $default = null)`](#getlabelname-default--null)
15+
- [`setOrUpdateLabels(array $labels)`](#setorupdatelabelsarray-labels)
1416
- [Annotations](#annotations)
1517
- [`setAnnotations(array $annotations)`](#setannotationsarray-annotations)
1618
- [`getAnnotations()`](#getannotations)
19+
- [`getAnnotation($name, $default = null)`](#getannotationname-default--null)
20+
- [`setOrUpdateAnnotations(array $annotations)`](#setorupdateannotationsarray-annotations)
1721
- [API](#api)
1822
- [`getApiVersion()`](#getapiversion)
1923
- [`getKind()`](#getkind)
@@ -106,6 +110,26 @@ Get the labels of a resource.
106110
$service->getLabels();
107111
```
108112

113+
### `getLabel($name, $default = null)`
114+
115+
Get the label value by key. Defaults to null.
116+
117+
```php
118+
$service->getLabel('tier'); // "backend"
119+
$service->getLabel('not-a-label'); // null
120+
```
121+
122+
### `setOrUpdateLabels(array $labels)`
123+
124+
Set or update the labels. The method used will be merge.
125+
126+
```php
127+
$service->setOrUpdateLabels([
128+
'tier' => 'backend'
129+
'some-other-label' => 'test',
130+
]);
131+
```
132+
109133
## Annotations
110134

111135
### `setAnnotations(array $annotations)`
@@ -124,6 +148,26 @@ Get the annotations of a resource.
124148
$service->getAnnotations();
125149
```
126150

151+
### `getAnnotation($name, $default = null)`
152+
153+
Get the annotation value by key. Defaults to null.
154+
155+
```php
156+
$service->getAnnotation('kubernetes.io/some-annotation'); // "yes"
157+
$service->getAnnotation('kubernetes.io/non-existing-annotation'); // null
158+
```
159+
160+
### `setOrUpdateAnnotations(array $annotations)`
161+
162+
Set or update the annotations. The method used will be merge.
163+
164+
```php
165+
$service->setOrUpdateAnnotations([
166+
'kubernetes.io/some-annotation' => 'yes'
167+
'kubernetes.io/some-otherannotation' => 'yes',
168+
]);
169+
```
170+
127171
## API
128172

129173
### `getApiVersion()`

src/Traits/HasAnnotations.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,17 @@ public function getAnnotation(string $name, $default = null)
3636
{
3737
return $this->getAnnotations()[$name] ?? $default;
3838
}
39+
40+
/**
41+
* Set or update the given annotations.
42+
*
43+
* @param array $annotations
44+
* @return $this
45+
*/
46+
public function setOrUpdateAnnotations(array $annotations = [])
47+
{
48+
return $this->setAnnotations(
49+
array_merge($this->getAnnotations(), $annotations)
50+
);
51+
}
3952
}

src/Traits/HasLabels.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,17 @@ public function getLabel(string $name, $default = null)
3636
{
3737
return $this->getLabels()[$name] ?? $default;
3838
}
39+
40+
/**
41+
* Set or update the given labels.
42+
*
43+
* @param array $labels
44+
* @return $this
45+
*/
46+
public function setOrUpdateLabels(array $labels = [])
47+
{
48+
return $this->setLabels(
49+
array_merge($this->getLabels(), $labels)
50+
);
51+
}
3952
}

tests/PodTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,18 @@ public function test_pod_build()
2929

3030
$pod = $this->cluster->pod()
3131
->setName('mysql')
32-
->setLabels(['tier' => 'backend'])
33-
->setAnnotations(['mysql/annotation' => 'yes'])
32+
->setOrUpdateLabels(['tier' => 'test'])
33+
->setOrUpdateLabels(['tier' => 'backend', 'type' => 'test'])
34+
->setOrUpdateAnnotations(['mysql/annotation' => 'no'])
35+
->setOrUpdateAnnotations(['mysql/annotation' => 'yes', 'mongodb/annotation' => 'no'])
3436
->addPulledSecrets(['secret1', 'secret2'])
3537
->setInitContainers([$busybox])
3638
->setContainers([$mysql]);
3739

3840
$this->assertEquals('v1', $pod->getApiVersion());
3941
$this->assertEquals('mysql', $pod->getName());
40-
$this->assertEquals(['tier' => 'backend'], $pod->getLabels());
41-
$this->assertEquals(['mysql/annotation' => 'yes'], $pod->getAnnotations());
42+
$this->assertEquals(['tier' => 'backend', 'type' => 'test'], $pod->getLabels());
43+
$this->assertEquals(['mysql/annotation' => 'yes', 'mongodb/annotation' => 'no'], $pod->getAnnotations());
4244
$this->assertEquals([['name' => 'secret1'], ['name' => 'secret2']], $pod->getPulledSecrets());
4345
$this->assertEquals([$busybox->toArray()], $pod->getInitContainers(false));
4446
$this->assertEquals([$mysql->toArray()], $pod->getContainers(false));
@@ -47,6 +49,7 @@ public function test_pod_build()
4749
$this->assertNull($pod->getLabel('inexistentLabel'));
4850

4951
$this->assertEquals('yes', $pod->getAnnotation('mysql/annotation'));
52+
$this->assertEquals('no', $pod->getAnnotation('mongodb/annotation'));
5053
$this->assertNull($pod->getAnnotation('inexistentAnnot'));
5154

5255
foreach ($pod->getInitContainers() as $container) {

0 commit comments

Comments
 (0)