diff --git a/composer.json b/composer.json index 845befa3..8e1247cc 100644 --- a/composer.json +++ b/composer.json @@ -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" } diff --git a/src/Instances/HostAlias.php b/src/Instances/HostAlias.php new file mode 100644 index 00000000..5b259ffc --- /dev/null +++ b/src/Instances/HostAlias.php @@ -0,0 +1,15 @@ +addToAttribute('hostnames', $hostname); + } + + return $this->setAttribute('ip', $ip); + } +} diff --git a/src/Kinds/K8sPod.php b/src/Kinds/K8sPod.php index 0507c570..85ae0274 100644 --- a/src/Kinds/K8sPod.php +++ b/src/Kinds/K8sPod.php @@ -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; @@ -483,4 +484,40 @@ public function isSuccessful(): bool { return $this->getPhase() === 'Succeeded'; } + + /** + * Set the host aliases. + * + * @param array $hostAliases + * @return $this + */ + public function setHostAliases(array $hostAliases) + { + foreach ($hostAliases as &$hostAlias) { + if ($hostAlias instanceof HostAlias) { + $hostAlias = $hostAlias->toArray(); + } + } + + return $this->setSpec('hostAliases', $hostAliases); + } + + /** + * Get the volumes. + * + * @param bool $asInstance + * @return array + */ + public function getHostAliases(bool $asInstance = true) + { + $hostAliases = $this->getSpec('hostAliases', []); + + if ($asInstance) { + foreach ($hostAliases as &$hostAlias) { + $hostAlias = new HostAlias($hostAlias); + } + } + + return $hostAliases; + } } diff --git a/src/Traits/InitializesInstances.php b/src/Traits/InitializesInstances.php index db0992f8..2cb6ee43 100644 --- a/src/Traits/InitializesInstances.php +++ b/src/Traits/InitializesInstances.php @@ -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; @@ -124,4 +125,15 @@ public static function webhook(array $attributes = []) { return new Webhook($attributes); } + + /** + * Create a new webhook instance. + * + * @param array $attributes + * @return \RenokiCo\PhpK8s\Instances\HostAlias + */ + public static function hostAlias(array $attributes = []) + { + return new HostAlias($attributes); + } } diff --git a/tests/HostAlias.php b/tests/HostAlias.php new file mode 100644 index 00000000..40e812f9 --- /dev/null +++ b/tests/HostAlias.php @@ -0,0 +1,71 @@ +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()); + } +}