Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Kinds/K8sResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use RenokiCo\PhpK8s\KubernetesCluster;
use RenokiCo\PhpK8s\Traits\Resource\HasAnnotations;
use RenokiCo\PhpK8s\Traits\Resource\HasAttributes;
use RenokiCo\PhpK8s\Traits\Resource\HasCreationTimestamp;
use RenokiCo\PhpK8s\Traits\Resource\HasEvents;
use RenokiCo\PhpK8s\Traits\Resource\HasKind;
use RenokiCo\PhpK8s\Traits\Resource\HasLabels;
Expand All @@ -29,6 +30,7 @@ class K8sResource implements Arrayable, Jsonable
use HasLabels;
use HasName;
use HasNamespace;
use HasCreationTimestamp;
use HasVersion;
use RunsClusterOperations;

Expand Down
25 changes: 25 additions & 0 deletions src/Traits/Resource/HasCreationTimestamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace RenokiCo\PhpK8s\Traits\Resource;

trait HasCreationTimestamp
{
use HasAttributes;

public function getCreationTimestamp(): \DateTimeImmutable
{
$timestamp = $this->getAttribute('metadata.creationTimestamp');

if ($timestamp === null) {
throw new \InvalidArgumentException('Creation timestamp is missing.');
}

$dateTime = \DateTimeImmutable::createFromFormat(\DateTimeInterface::ATOM, $timestamp);

if ($dateTime === false) {
throw new \RuntimeException('Invalid creation timestamp format.');
}

return $dateTime;
}
}
4 changes: 4 additions & 0 deletions tests/ClusterRoleBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ public function runCreationTests()
$this->assertEquals(['tier' => 'backend'], $crb->getLabels());
$this->assertEquals([$subject], $crb->getSubjects());
$this->assertEquals(['apiGroup' => 'rbac.authorization.k8s.io', 'kind' => 'ClusterRole', 'name' => 'admin-cr-for-binding'], $crb->getRole());
$this->assertTrue(
$crb->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/ClusterRoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public function runCreationTests()
$this->assertEquals('admin-cr', $cr->getName());
$this->assertEquals(['tier' => 'backend'], $cr->getLabels());
$this->assertEquals([$rule], $cr->getRules());
$this->assertTrue(
$cr->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/ConfigMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public function runCreationTests()
$this->assertEquals(['tier' => 'backend'], $cm->getLabels());
$this->assertEquals(['key2' => 'val2'], $cm->getData());
$this->assertEquals('val2', $cm->getData('key2'));
$this->assertTrue(
$cm->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/CronJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public function runCreationTests()
$this->assertEquals(['tier' => 'useless'], $cronjob->getLabels());
$this->assertEquals(['perl/annotation' => 'no'], $cronjob->getAnnotations());
$this->assertEquals('Never', $pod->getRestartPolicy());
$this->assertTrue(
$cronjob->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

$this->assertInstanceOf(K8sJob::class, $cronjob->getJobTemplate());
$this->assertInstanceOf(CronExpression::class, $cronjob->getSchedule());
Expand Down
4 changes: 4 additions & 0 deletions tests/DaemonSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public function runCreationTests()
$this->assertEquals(['tier' => 'backend'], $ds->getLabels());
$this->assertEquals(0, $ds->getMinReadySeconds());
$this->assertEquals($pod->getName(), $ds->getTemplate()->getName());
$this->assertTrue(
$ds->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

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

Expand Down
4 changes: 4 additions & 0 deletions tests/DeploymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public function runCreationTests()
$this->assertEquals(1, $dep->getReplicas());
$this->assertEquals(0, $dep->getMinReadySeconds());
$this->assertEquals($pod->getName(), $dep->getTemplate()->getName());
$this->assertTrue(
$dep->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

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

Expand Down
4 changes: 4 additions & 0 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function runCreationTests()

$this->assertInstanceOf(K8sEvent::class, $matchedEvent);
$this->assertTrue($matchedEvent->is($event));
$this->assertTrue(
$event->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/HorizontalPodAutoscalerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ public function runCreationTests()
$this->assertEquals([$cpuMetric->toArray()], $hpa->getMetrics());
$this->assertEquals(1, $hpa->getMinReplicas());
$this->assertEquals(10, $hpa->getMaxReplicas());
$this->assertTrue(
$hpa->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

while (! $dep->allPodsAreRunning()) {
dump("Waiting for pods of {$dep->getName()} to be up and running...");
Expand Down
4 changes: 4 additions & 0 deletions tests/IngressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public function runCreationTests()
$this->assertEquals(['nginx/ann' => 'yes'], $ing->getAnnotations());
$this->assertEquals(self::$tls, $ing->getTls());
$this->assertEquals(self::$rules, $ing->getRules());
$this->assertTrue(
$ing->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public function runCreationTests()
$this->assertEquals('batch/v1', $job->getApiVersion());
$this->assertEquals('pi', $job->getName());
$this->assertEquals(['tier' => 'compute'], $job->getLabels());
$this->assertTrue(
$job->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

$annotations = $job->getAnnotations();
foreach (['perl/annotation' => 'yes'] as $key => $value) {
Expand Down
4 changes: 4 additions & 0 deletions tests/NamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public function runCreationTests()
'kubernetes.io/metadata.name' => 'production',
'tier' => 'backend',
], $ns->getLabels());
$this->assertTrue(
$ns->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

$ns->refresh();

Expand Down
4 changes: 4 additions & 0 deletions tests/PersistentVolumeClaimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function runCreationTests()
$this->assertEquals('1Gi', $pvc->getCapacity());
$this->assertEquals(['ReadWriteOnce'], $pvc->getAccessModes());
$this->assertEquals('standard', $pvc->getStorageClass());
$this->assertTrue(
$pvc->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

if ($standard->getVolumeBindingMode() == 'Immediate') {
while (! $pvc->isBound()) {
Expand Down
4 changes: 4 additions & 0 deletions tests/PersistentVolumeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function runCreationTests()
$this->assertEquals(['ReadWriteOnce'], $pv->getAccessModes());
$this->assertEquals(['debug'], $pv->getMountOptions());
$this->assertEquals('sc1', $pv->getStorageClass());
$this->assertTrue(
$pv->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

while (! $pv->isAvailable()) {
dump("Waiting for PV {$pv->getName()} to be available...");
Expand Down
4 changes: 4 additions & 0 deletions tests/PodDisruptionBudgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public function runCreationTests()
$this->assertEquals(['mysql/annotation' => 'yes'], $pdb->getAnnotations());
$this->assertEquals('25%', $pdb->getMaxUnavailable());
$this->assertEquals(null, $pdb->getMinAvailable());
$this->assertTrue(
$pdb->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

while (! $dep->allPodsAreRunning()) {
dump("Waiting for pods of {$dep->getName()} to be up and running...");
Expand Down
5 changes: 5 additions & 0 deletions tests/PodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ public function runCreationTests()

$ipSlug = str_replace('.', '-', $pod->getPodIps()[0]['ip'] ?? '');
$this->assertEquals("{$ipSlug}.{$pod->getNamespace()}.pod.cluster.local", $pod->getClusterDns());

$this->assertTrue(
$pod->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/RoleBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ public function runCreationTests()
$this->assertEquals(['tier' => 'backend'], $rb->getLabels());
$this->assertEquals([$subject], $rb->getSubjects());
$this->assertEquals(['apiGroup' => 'rbac.authorization.k8s.io', 'kind' => 'Role', 'name' => 'admin'], $rb->getRole());
$this->assertTrue(
$rb->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public function runCreationTests()
$this->assertEquals('admin', $role->getName());
$this->assertEquals(['tier' => 'backend'], $role->getLabels());
$this->assertEquals([$rule], $role->getRules());
$this->assertTrue(
$role->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/SecretTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public function runCreationTests()
$this->assertEquals(['tier' => 'backend'], $secret->getLabels());
$this->assertEquals(['postgres' => base64_encode('postgres')], $secret->getData(false));
$this->assertEquals(['postgres' => 'postgres'], $secret->getData(true));
$this->assertTrue(
$secret->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/ServiceAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function runCreationTests()
$this->assertEquals(['tier' => 'backend'], $sa->getLabels());
$this->assertEquals([['name' => $secret->getName()]], $sa->getSecrets());
$this->assertEquals([['name' => 'postgres']], $sa->getImagePullSecrets());
$this->assertTrue(
$sa->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function runCreationTests()
'protocol' => 'TCP', 'port' => 80, 'targetPort' => 80,
]], $svc->getPorts());
$this->assertEquals("{$svc->getName()}.{$svc->getNamespace()}.svc.cluster.local", $svc->getClusterDns());
$this->assertTrue(
$svc->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/StatefulSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ public function runCreationTests()
$this->assertEquals($svc->getName(), $sts->getService());
$this->assertEquals($pod->getName(), $sts->getTemplate()->getName());
$this->assertEquals($pvc->getName(), $sts->getVolumeClaims()[0]->getName());
$this->assertTrue(
$sts->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

$this->assertInstanceOf(K8sPod::class, $sts->getTemplate());
$this->assertInstanceOf(K8sPersistentVolumeClaim::class, $sts->getVolumeClaims()[0]);
Expand Down
4 changes: 4 additions & 0 deletions tests/StorageClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function runCreationTests()
$this->assertEquals('csi.aws.amazon.com', $sc->getProvisioner());
$this->assertEquals(['type' => 'io1', 'iopsPerGB' => 10], $sc->getParameters());
$this->assertEquals(['debug'], $sc->getMountOptions());
$this->assertTrue(
$sc->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);
}

public function runGetAllTests()
Expand Down
4 changes: 4 additions & 0 deletions tests/ValidatingWebhookConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public function runCreationTests()
$this->assertEquals(['tier' => 'webhook'], $validatingWebhookConfiguration->getLabels());
$this->assertArrayHasKey('webhook/annotation', $validatingWebhookConfiguration->getAnnotations());
$this->assertEquals(1, count($validatingWebhookConfiguration->getWebhooks()));
$this->assertTrue(
$validatingWebhookConfiguration->getCreationTimestamp()->getTimestamp() > (time() - 60),
'Creation timestamp is not within the last minute.'
);

foreach ($validatingWebhookConfiguration->getWebhooks() as $vw) {
$this->assertEquals($webhook->getName(), $vw->getName());
Expand Down
Loading