Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Internal Cluster DNS names #76

Merged
merged 2 commits into from
Mar 22, 2021
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
13 changes: 13 additions & 0 deletions src/Contracts/Dnsable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RenokiCo\PhpK8s\Contracts;

interface Dnsable
{
/**
* Get the DNS name within the cluster.
*
* @return string|null
*/
public function getClusterDns();
}
16 changes: 15 additions & 1 deletion src/Kinds/K8sPod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace RenokiCo\PhpK8s\Kinds;

use Illuminate\Support\Str;
use RenokiCo\PhpK8s\Contracts\Dnsable;
use RenokiCo\PhpK8s\Contracts\InteractsWithK8sCluster;
use RenokiCo\PhpK8s\Contracts\Loggable;
use RenokiCo\PhpK8s\Contracts\Watchable;
Expand All @@ -16,7 +18,7 @@
use RenokiCo\PhpK8s\Traits\HasStatusConditions;
use RenokiCo\PhpK8s\Traits\HasStatusPhase;

class K8sPod extends K8sResource implements InteractsWithK8sCluster, Watchable, Loggable
class K8sPod extends K8sResource implements Dnsable, InteractsWithK8sCluster, Watchable, Loggable
{
use HasAnnotations;
use HasLabels;
Expand All @@ -39,6 +41,18 @@ class K8sPod extends K8sResource implements InteractsWithK8sCluster, Watchable,
*/
protected static $namespaceable = true;

/**
* Get the DNS name within the cluster.
*
* @return string|null
*/
public function getClusterDns()
{
$ipSlug = Str::slug($this->getPodIps()[0]['ip'] ?? null);

return $ipSlug ? "{$ipSlug}.{$this->getNamespace()}.pod.cluster.local" : null;
}

/**
* Set the Pod containers.
*
Expand Down
13 changes: 12 additions & 1 deletion src/Kinds/K8sService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace RenokiCo\PhpK8s\Kinds;

use RenokiCo\PhpK8s\Contracts\Dnsable;
use RenokiCo\PhpK8s\Contracts\InteractsWithK8sCluster;
use RenokiCo\PhpK8s\Contracts\Watchable;
use RenokiCo\PhpK8s\Traits\HasAnnotations;
use RenokiCo\PhpK8s\Traits\HasLabels;
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;
Expand All @@ -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.
*
Expand Down
3 changes: 3 additions & 0 deletions tests/PodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tests/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down