Skip to content

Commit 9e7d0c8

Browse files
committed
Expose resource creation timestamp
1 parent 688aeb9 commit 9e7d0c8

26 files changed

+124
-0
lines changed

src/Kinds/K8sResource.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use RenokiCo\PhpK8s\KubernetesCluster;
1313
use RenokiCo\PhpK8s\Traits\Resource\HasAnnotations;
1414
use RenokiCo\PhpK8s\Traits\Resource\HasAttributes;
15+
use RenokiCo\PhpK8s\Traits\Resource\HasCreationTimestamp;
1516
use RenokiCo\PhpK8s\Traits\Resource\HasEvents;
1617
use RenokiCo\PhpK8s\Traits\Resource\HasKind;
1718
use RenokiCo\PhpK8s\Traits\Resource\HasLabels;
@@ -29,6 +30,7 @@ class K8sResource implements Arrayable, Jsonable
2930
use HasLabels;
3031
use HasName;
3132
use HasNamespace;
33+
use HasCreationTimestamp;
3234
use HasVersion;
3335
use RunsClusterOperations;
3436

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace RenokiCo\PhpK8s\Traits\Resource;
4+
5+
trait HasCreationTimestamp
6+
{
7+
use HasAttributes;
8+
9+
public function getCreationTimestamp(): \DateTimeImmutable
10+
{
11+
$timestamp = $this->getAttribute('metadata.creationTimestamp');
12+
13+
if ($timestamp === null) {
14+
throw new \InvalidArgumentException('Creation timestamp is missing.');
15+
}
16+
17+
$dateTime = \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $timestamp);
18+
19+
if ($dateTime === false) {
20+
throw new \RuntimeException('Invalid creation timestamp format.');
21+
}
22+
23+
return $dateTime;
24+
}
25+
}

tests/ClusterRoleBindingTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ public function runCreationTests()
121121
$this->assertEquals(['tier' => 'backend'], $crb->getLabels());
122122
$this->assertEquals([$subject], $crb->getSubjects());
123123
$this->assertEquals(['apiGroup' => 'rbac.authorization.k8s.io', 'kind' => 'ClusterRole', 'name' => 'admin-cr-for-binding'], $crb->getRole());
124+
$this->assertTrue(
125+
$crb->getCreationTimestamp()->getTimestamp() > (time() - 60),
126+
'Creation timestamp is not within the last minute.'
127+
);
124128
}
125129

126130
public function runGetAllTests()

tests/ClusterRoleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public function runCreationTests()
8282
$this->assertEquals('admin-cr', $cr->getName());
8383
$this->assertEquals(['tier' => 'backend'], $cr->getLabels());
8484
$this->assertEquals([$rule], $cr->getRules());
85+
$this->assertTrue(
86+
$cr->getCreationTimestamp()->getTimestamp() > (time() - 60),
87+
'Creation timestamp is not within the last minute.'
88+
);
8589
}
8690

8791
public function runGetAllTests()

tests/ConfigMapTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public function runCreationTests()
9090
$this->assertEquals(['tier' => 'backend'], $cm->getLabels());
9191
$this->assertEquals(['key2' => 'val2'], $cm->getData());
9292
$this->assertEquals('val2', $cm->getData('key2'));
93+
$this->assertTrue(
94+
$cm->getCreationTimestamp()->getTimestamp() > (time() - 60),
95+
'Creation timestamp is not within the last minute.'
96+
);
9397
}
9498

9599
public function runGetAllTests()

tests/CronJobTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public function runCreationTests()
127127
$this->assertEquals(['tier' => 'useless'], $cronjob->getLabels());
128128
$this->assertEquals(['perl/annotation' => 'no'], $cronjob->getAnnotations());
129129
$this->assertEquals('Never', $pod->getRestartPolicy());
130+
$this->assertTrue(
131+
$cronjob->getCreationTimestamp()->getTimestamp() > (time() - 60),
132+
'Creation timestamp is not within the last minute.'
133+
);
130134

131135
$this->assertInstanceOf(K8sJob::class, $cronjob->getJobTemplate());
132136
$this->assertInstanceOf(CronExpression::class, $cronjob->getSchedule());

tests/DaemonSetTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public function runCreationTests()
112112
$this->assertEquals(['tier' => 'backend'], $ds->getLabels());
113113
$this->assertEquals(0, $ds->getMinReadySeconds());
114114
$this->assertEquals($pod->getName(), $ds->getTemplate()->getName());
115+
$this->assertTrue(
116+
$ds->getCreationTimestamp()->getTimestamp() > (time() - 60),
117+
'Creation timestamp is not within the last minute.'
118+
);
115119

116120
$this->assertInstanceOf(K8sPod::class, $ds->getTemplate());
117121

tests/DeploymentTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ public function runCreationTests()
122122
$this->assertEquals(1, $dep->getReplicas());
123123
$this->assertEquals(0, $dep->getMinReadySeconds());
124124
$this->assertEquals($pod->getName(), $dep->getTemplate()->getName());
125+
$this->assertTrue(
126+
$dep->getCreationTimestamp()->getTimestamp() > (time() - 60),
127+
'Creation timestamp is not within the last minute.'
128+
);
125129

126130
$this->assertInstanceOf(K8sPod::class, $dep->getTemplate());
127131

tests/EventTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public function runCreationTests()
6969

7070
$this->assertInstanceOf(K8sEvent::class, $matchedEvent);
7171
$this->assertTrue($matchedEvent->is($event));
72+
$this->assertTrue(
73+
$event->getCreationTimestamp()->getTimestamp() > (time() - 60),
74+
'Creation timestamp is not within the last minute.'
75+
);
7276
}
7377

7478
public function runGetAllTests()

tests/HorizontalPodAutoscalerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ public function runCreationTests()
147147
$this->assertEquals([$cpuMetric->toArray()], $hpa->getMetrics());
148148
$this->assertEquals(1, $hpa->getMinReplicas());
149149
$this->assertEquals(10, $hpa->getMaxReplicas());
150+
$this->assertTrue(
151+
$hpa->getCreationTimestamp()->getTimestamp() > (time() - 60),
152+
'Creation timestamp is not within the last minute.'
153+
);
150154

151155
while (! $dep->allPodsAreRunning()) {
152156
dump("Waiting for pods of {$dep->getName()} to be up and running...");

0 commit comments

Comments
 (0)