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

Feature/add host aliases to pod #393

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
"vimeo/psalm": "^4.20"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
}
},
"minimum-stability": "dev"
}
15 changes: 15 additions & 0 deletions src/Instances/HostAlias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace RenokiCo\PhpK8s\Instances;

class HostAlias extends Instance
{
public function setHostAlias(string $ip, string ...$hostnames): self

Check warning on line 7 in src/Instances/HostAlias.php

View check run for this annotation

Codecov / codecov/patch

src/Instances/HostAlias.php#L7

Added line #L7 was not covered by tests
{
foreach ($hostnames as $hostname) {
$this->addToAttribute('hostnames', $hostname);

Check warning on line 10 in src/Instances/HostAlias.php

View check run for this annotation

Codecov / codecov/patch

src/Instances/HostAlias.php#L9-L10

Added lines #L9 - L10 were not covered by tests
}

return $this->setAttribute('ip', $ip);

Check warning on line 13 in src/Instances/HostAlias.php

View check run for this annotation

Codecov / codecov/patch

src/Instances/HostAlias.php#L13

Added line #L13 was not covered by tests
}
}
37 changes: 37 additions & 0 deletions src/Kinds/K8sPod.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use RenokiCo\PhpK8s\Contracts\Watchable;
use RenokiCo\PhpK8s\Instances\Affinity;
use RenokiCo\PhpK8s\Instances\Container;
use RenokiCo\PhpK8s\Instances\HostAlias;
use RenokiCo\PhpK8s\Instances\Volume;
use RenokiCo\PhpK8s\K8s;
use RenokiCo\PhpK8s\Traits\Resource\HasSpec;
Expand Down Expand Up @@ -483,4 +484,40 @@
{
return $this->getPhase() === 'Succeeded';
}

/**
* Set the host aliases.
*
* @param array $hostAliases
* @return $this
*/
public function setHostAliases(array $hostAliases)

Check warning on line 494 in src/Kinds/K8sPod.php

View check run for this annotation

Codecov / codecov/patch

src/Kinds/K8sPod.php#L494

Added line #L494 was not covered by tests
{
foreach ($hostAliases as &$hostAlias) {
if ($hostAlias instanceof HostAlias) {
$hostAlias = $hostAlias->toArray();

Check warning on line 498 in src/Kinds/K8sPod.php

View check run for this annotation

Codecov / codecov/patch

src/Kinds/K8sPod.php#L496-L498

Added lines #L496 - L498 were not covered by tests
}
}

return $this->setSpec('hostAliases', $hostAliases);

Check warning on line 502 in src/Kinds/K8sPod.php

View check run for this annotation

Codecov / codecov/patch

src/Kinds/K8sPod.php#L502

Added line #L502 was not covered by tests
}

/**
* Get the volumes.
*
* @param bool $asInstance
* @return array
*/
public function getHostAliases(bool $asInstance = true)

Check warning on line 511 in src/Kinds/K8sPod.php

View check run for this annotation

Codecov / codecov/patch

src/Kinds/K8sPod.php#L511

Added line #L511 was not covered by tests
{
$hostAliases = $this->getSpec('hostAliases', []);

Check warning on line 513 in src/Kinds/K8sPod.php

View check run for this annotation

Codecov / codecov/patch

src/Kinds/K8sPod.php#L513

Added line #L513 was not covered by tests

if ($asInstance) {
foreach ($hostAliases as &$hostAlias) {
$hostAlias = new HostAlias($hostAlias);

Check warning on line 517 in src/Kinds/K8sPod.php

View check run for this annotation

Codecov / codecov/patch

src/Kinds/K8sPod.php#L515-L517

Added lines #L515 - L517 were not covered by tests
}
}

return $hostAliases;

Check warning on line 521 in src/Kinds/K8sPod.php

View check run for this annotation

Codecov / codecov/patch

src/Kinds/K8sPod.php#L521

Added line #L521 was not covered by tests
}
}
12 changes: 12 additions & 0 deletions src/Traits/InitializesInstances.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use RenokiCo\PhpK8s\Instances\Affinity;
use RenokiCo\PhpK8s\Instances\Container;
use RenokiCo\PhpK8s\Instances\Expression;
use RenokiCo\PhpK8s\Instances\HostAlias;
use RenokiCo\PhpK8s\Instances\Probe;
use RenokiCo\PhpK8s\Instances\ResourceMetric;
use RenokiCo\PhpK8s\Instances\ResourceObject;
Expand Down Expand Up @@ -124,4 +125,15 @@
{
return new Webhook($attributes);
}

/**
* Create a new webhook instance.
*
* @param array $attributes
* @return \RenokiCo\PhpK8s\Instances\HostAlias
*/
public static function hostAlias(array $attributes = [])

Check warning on line 135 in src/Traits/InitializesInstances.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/InitializesInstances.php#L135

Added line #L135 was not covered by tests
{
return new HostAlias($attributes);

Check warning on line 137 in src/Traits/InitializesInstances.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/InitializesInstances.php#L137

Added line #L137 was not covered by tests
}
}
71 changes: 71 additions & 0 deletions tests/HostAlias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace RenokiCo\PhpK8s\Test;

use RenokiCo\PhpK8s\K8s;

class HostAlias extends TestCase
{
public function test_single_host_alias()
{
$hostAlias = K8s::hostAlias()
->setHostAlias(
'127.0.0.1',
'my-fancy-host',
);

$mysql = K8s::container()
->setName('mysql')
->setImage('mysql', '5.7');

$pod = K8s::pod()
->setName('mysql')
->setContainers([$mysql])
->setHostAliases([$hostAlias]);

$this->assertEquals([
'ip' => '127.0.0.1',
'hostnames' => ['my-fancy-host'],
], $hostAlias->toArray());

$this->assertEquals($pod->getHostAliases()[0]->toArray(), $hostAlias->toArray());
}

public function test_multiple_host_aliases()
{
$hostAlias1 = K8s::hostAlias()
->setHostAlias(
'127.0.0.1',
'my-fancy-host',
);

$hostAlias2 = K8s::hostAlias()
->setHostAlias(
'127.0.0.2',
'my-fancy-host-2',
'my-fancy-host-3',
);

$mysql = K8s::container()
->setName('mysql')
->setImage('mysql', '5.7');

$pod = K8s::pod()
->setName('mysql')
->setContainers([$mysql])
->setHostAliases([$hostAlias1, $hostAlias2]);

$this->assertEquals([
'ip' => '127.0.0.1',
'hostnames' => ['my-fancy-host'],
], $hostAlias1->toArray());

$this->assertEquals([
'ip' => '127.0.0.2',
'hostnames' => ['my-fancy-host-2', 'my-fancy-host-3'],
], $hostAlias2->toArray());

$this->assertEquals($pod->getHostAliases()[0]->toArray(), $hostAlias1->toArray());
$this->assertEquals($pod->getHostAliases()[1]->toArray(), $hostAlias2->toArray());
}
}
Loading