diff --git a/src/Kinds/K8sResource.php b/src/Kinds/K8sResource.php index 63c2fc4..a87e8f3 100644 --- a/src/Kinds/K8sResource.php +++ b/src/Kinds/K8sResource.php @@ -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; @@ -29,6 +30,7 @@ class K8sResource implements Arrayable, Jsonable use HasLabels; use HasName; use HasNamespace; + use HasCreationTimestamp; use HasVersion; use RunsClusterOperations; diff --git a/src/Traits/Resource/HasCreationTimestamp.php b/src/Traits/Resource/HasCreationTimestamp.php new file mode 100644 index 0000000..2d9fc98 --- /dev/null +++ b/src/Traits/Resource/HasCreationTimestamp.php @@ -0,0 +1,25 @@ +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; + } +} diff --git a/tests/ClusterRoleBindingTest.php b/tests/ClusterRoleBindingTest.php index 34af21f..cab0e68 100644 --- a/tests/ClusterRoleBindingTest.php +++ b/tests/ClusterRoleBindingTest.php @@ -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() diff --git a/tests/ClusterRoleTest.php b/tests/ClusterRoleTest.php index d322fcf..485b871 100644 --- a/tests/ClusterRoleTest.php +++ b/tests/ClusterRoleTest.php @@ -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() diff --git a/tests/ConfigMapTest.php b/tests/ConfigMapTest.php index 5f80fb7..37b5755 100644 --- a/tests/ConfigMapTest.php +++ b/tests/ConfigMapTest.php @@ -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() diff --git a/tests/CronJobTest.php b/tests/CronJobTest.php index 8961c42..403cc46 100644 --- a/tests/CronJobTest.php +++ b/tests/CronJobTest.php @@ -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()); diff --git a/tests/DaemonSetTest.php b/tests/DaemonSetTest.php index 4f99af6..4522f91 100644 --- a/tests/DaemonSetTest.php +++ b/tests/DaemonSetTest.php @@ -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()); diff --git a/tests/DeploymentTest.php b/tests/DeploymentTest.php index 9fd09a9..e4ebc6f 100644 --- a/tests/DeploymentTest.php +++ b/tests/DeploymentTest.php @@ -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()); diff --git a/tests/EventTest.php b/tests/EventTest.php index 0df2baf..4d604d9 100644 --- a/tests/EventTest.php +++ b/tests/EventTest.php @@ -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() diff --git a/tests/HorizontalPodAutoscalerTest.php b/tests/HorizontalPodAutoscalerTest.php index 44301ed..018a44f 100644 --- a/tests/HorizontalPodAutoscalerTest.php +++ b/tests/HorizontalPodAutoscalerTest.php @@ -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..."); diff --git a/tests/IngressTest.php b/tests/IngressTest.php index 2ce0e4d..02eba78 100644 --- a/tests/IngressTest.php +++ b/tests/IngressTest.php @@ -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() diff --git a/tests/JobTest.php b/tests/JobTest.php index 72e7664..363cea0 100644 --- a/tests/JobTest.php +++ b/tests/JobTest.php @@ -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) { diff --git a/tests/NamespaceTest.php b/tests/NamespaceTest.php index 1d6ae95..da73640 100644 --- a/tests/NamespaceTest.php +++ b/tests/NamespaceTest.php @@ -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(); diff --git a/tests/PersistentVolumeClaimTest.php b/tests/PersistentVolumeClaimTest.php index 2ba30c9..7502da2 100644 --- a/tests/PersistentVolumeClaimTest.php +++ b/tests/PersistentVolumeClaimTest.php @@ -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()) { diff --git a/tests/PersistentVolumeTest.php b/tests/PersistentVolumeTest.php index 626e828..9f92a6e 100644 --- a/tests/PersistentVolumeTest.php +++ b/tests/PersistentVolumeTest.php @@ -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..."); diff --git a/tests/PodDisruptionBudgetTest.php b/tests/PodDisruptionBudgetTest.php index a6dcc67..424fefd 100644 --- a/tests/PodDisruptionBudgetTest.php +++ b/tests/PodDisruptionBudgetTest.php @@ -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..."); diff --git a/tests/PodTest.php b/tests/PodTest.php index 6cbd737..8c10931 100644 --- a/tests/PodTest.php +++ b/tests/PodTest.php @@ -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() diff --git a/tests/RoleBindingTest.php b/tests/RoleBindingTest.php index f938800..9ad8798 100644 --- a/tests/RoleBindingTest.php +++ b/tests/RoleBindingTest.php @@ -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() diff --git a/tests/RoleTest.php b/tests/RoleTest.php index 5c328ea..1affbae 100644 --- a/tests/RoleTest.php +++ b/tests/RoleTest.php @@ -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() diff --git a/tests/SecretTest.php b/tests/SecretTest.php index 20e644c..1f434a8 100644 --- a/tests/SecretTest.php +++ b/tests/SecretTest.php @@ -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() diff --git a/tests/ServiceAccountTest.php b/tests/ServiceAccountTest.php index 9d946ee..4f9830e 100644 --- a/tests/ServiceAccountTest.php +++ b/tests/ServiceAccountTest.php @@ -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() diff --git a/tests/ServiceTest.php b/tests/ServiceTest.php index 1447a5b..c0ca7a4 100644 --- a/tests/ServiceTest.php +++ b/tests/ServiceTest.php @@ -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() diff --git a/tests/StatefulSetTest.php b/tests/StatefulSetTest.php index ea0e213..ff5376d 100644 --- a/tests/StatefulSetTest.php +++ b/tests/StatefulSetTest.php @@ -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]); diff --git a/tests/StorageClassTest.php b/tests/StorageClassTest.php index ecd96bd..37f5066 100644 --- a/tests/StorageClassTest.php +++ b/tests/StorageClassTest.php @@ -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() diff --git a/tests/ValidatingWebhookConfigurationTest.php b/tests/ValidatingWebhookConfigurationTest.php index 0f2f6b7..c4993b3 100644 --- a/tests/ValidatingWebhookConfigurationTest.php +++ b/tests/ValidatingWebhookConfigurationTest.php @@ -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());