diff --git a/src/Contracts/Dnsable.php b/src/Contracts/Dnsable.php new file mode 100644 index 00000000..84fc8589 --- /dev/null +++ b/src/Contracts/Dnsable.php @@ -0,0 +1,13 @@ +getPodIps()[0]['ip'] ?? null); + + return $ipSlug ? "{$ipSlug}.{$this->getNamespace()}.pod.cluster.local" : null; + } + /** * Set the Pod containers. * diff --git a/src/Kinds/K8sService.php b/src/Kinds/K8sService.php index d6d09854..0397e5d2 100644 --- a/src/Kinds/K8sService.php +++ b/src/Kinds/K8sService.php @@ -2,6 +2,7 @@ namespace RenokiCo\PhpK8s\Kinds; +use RenokiCo\PhpK8s\Contracts\Dnsable; use RenokiCo\PhpK8s\Contracts\InteractsWithK8sCluster; use RenokiCo\PhpK8s\Contracts\Watchable; use RenokiCo\PhpK8s\Traits\HasAnnotations; @@ -9,7 +10,7 @@ use RenokiCo\PhpK8s\Traits\HasSelector; use RenokiCo\PhpK8s\Traits\HasSpec; -class K8sService extends K8sResource implements InteractsWithK8sCluster, Watchable +class K8sService extends K8sResource implements Dnsable, InteractsWithK8sCluster, Watchable { use HasAnnotations; use HasSelector; @@ -30,6 +31,16 @@ class K8sService extends K8sResource implements InteractsWithK8sCluster, Watchab */ protected static $namespaceable = true; + /** + * Get the DNS name within the cluster. + * + * @return string|null + */ + public function getClusterDns() + { + return "{$this->getName()}.{$this->getNamespace()}.svc.cluster.local"; + } + /** * Set the ports spec attribute. * diff --git a/tests/PodTest.php b/tests/PodTest.php index 0ba4fe36..6ffdfee5 100644 --- a/tests/PodTest.php +++ b/tests/PodTest.php @@ -156,6 +156,9 @@ public function runCreationTests() $this->assertTrue(is_string($pod->getHostIp())); $this->assertCount(1, $pod->getPodIps()); $this->assertEquals('BestEffort', $pod->getQos()); + + $ipSlug = Str::slug($pod->getPodIps()[0]['ip']); + $this->assertEquals("{$ipSlug}.{$pod->getNamespace()}.pod.cluster.local", $pod->getClusterDns()); } public function runGetAllTests() diff --git a/tests/ServiceTest.php b/tests/ServiceTest.php index a03a55de..1447a5b9 100644 --- a/tests/ServiceTest.php +++ b/tests/ServiceTest.php @@ -80,6 +80,7 @@ public function runCreationTests() $this->assertEquals([[ 'protocol' => 'TCP', 'port' => 80, 'targetPort' => 80, ]], $svc->getPorts()); + $this->assertEquals("{$svc->getName()}.{$svc->getNamespace()}.svc.cluster.local", $svc->getClusterDns()); } public function runGetAllTests()