From 1c6cfa5735467cd03281daed08e795afda06e64d Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 20:12:55 +0200 Subject: [PATCH 01/19] Make test fail by adding secret refs. --- tests/ContainerTest.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 3495b520..da085b6e 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -17,6 +17,11 @@ public function test_container_build() $container->setImage('nginx', '1.4') ->setEnv(['key' => 'value']) ->addEnvs(['key2' => 'value2']) + ->addSecretKeyRef('SECRET_ONE', 'ref_name', 'ref_key') + ->addSecretKeyRefs([ + 'SECRET_TWO' => ['ref_name', 'ref_key'], + 'SECRET_THREE' => ['ref_name', 'ref_key'] + ]) ->setArgs(['--test']) ->addPort(80, 'TCP', 'http') ->addPort(443, 'TCP', 'https') @@ -56,6 +61,30 @@ public function test_container_build() $this->assertEquals([ ['name' => 'key', 'value' => 'value'], ['name' => 'key2', 'value' => 'value2'], + ['name' => 'SECRET_ONE', + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ], + ], + ['name' => 'SECRET_TWO', + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ], + ], + ['name' => 'SECRET_THREE', + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ], + ] ], $container->getEnv()); $this->assertEquals(['--test'], $container->getArgs()); $this->assertEquals([ From 9007310921d901b2e1abfce9d103c26d991c44dd Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 20:20:05 +0200 Subject: [PATCH 02/19] Test for setting a valueFrom ref directly in env. --- tests/ContainerTest.php | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index da085b6e..13fd88db 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -15,12 +15,22 @@ public function test_container_build() $volume = K8s::volume()->awsEbs('vol-1234', 'ext3'); $container->setImage('nginx', '1.4') - ->setEnv(['key' => 'value']) + ->setEnv([ + 'key' => 'value', + 'SECRET_ONE' => [ + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ] + ] + ]) ->addEnvs(['key2' => 'value2']) - ->addSecretKeyRef('SECRET_ONE', 'ref_name', 'ref_key') + ->addSecretKeyRef('SECRET_TWO', 'ref_name', 'ref_key') ->addSecretKeyRefs([ - 'SECRET_TWO' => ['ref_name', 'ref_key'], - 'SECRET_THREE' => ['ref_name', 'ref_key'] + 'SECRET_THREE' => ['ref_name', 'ref_key'], + 'SECRET_FOUR' => ['ref_name', 'ref_key'] ]) ->setArgs(['--test']) ->addPort(80, 'TCP', 'http') @@ -60,7 +70,6 @@ public function test_container_build() $this->assertEquals('nginx:1.4', $container->getImage()); $this->assertEquals([ ['name' => 'key', 'value' => 'value'], - ['name' => 'key2', 'value' => 'value2'], ['name' => 'SECRET_ONE', 'valueFrom' => [ 'secretKeyRef' => [ @@ -69,6 +78,7 @@ public function test_container_build() ] ], ], + ['name' => 'key2', 'value' => 'value2'], ['name' => 'SECRET_TWO', 'valueFrom' => [ 'secretKeyRef' => [ @@ -84,6 +94,14 @@ public function test_container_build() 'key' => 'ref_key' ] ], + ], + ['name' => 'SECRET_FOUR', + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ], ] ], $container->getEnv()); $this->assertEquals(['--test'], $container->getArgs()); From 4033c05aed9371fefe6313bd350962305e6b258d Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 20:20:52 +0200 Subject: [PATCH 03/19] Added functions to make container tests pass. --- src/Instances/Container.php | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/Instances/Container.php b/src/Instances/Container.php index ae863688..6edcae76 100644 --- a/src/Instances/Container.php +++ b/src/Instances/Container.php @@ -99,6 +99,44 @@ public function getMountedVolumes(bool $asInstance = true) return $mountedVolumes; } + /** + * Add an env variable by using a secret reference to the container. + * + * @param string $name + * @param string $refName + * @param string $refKey + * @return $this + */ + public function addSecretKeyRef(string $name, string $refName, string $refKey) + { + return $this->addToAttribute('env', [ + 'name' => $name, + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => $refName, + 'key' => $refKey + ] + ] + ]); + } + + /** + * Add multiple secret references to the container. + * + * @param array $refs + * @return $this + */ + public function addSecretKeyRefs(array $refs) + { + foreach ($refs as $ref => $value) { + if (is_array($value)){ + $this->addSecretKeyRef($ref, $value[0], $value[1]); + } + } + + return $this; + } + /** * Add an env variable to the container. * @@ -135,6 +173,11 @@ public function addEnvs(array $envs) public function setEnv(array $envs) { $envs = collect($envs)->map(function ($value, $name) { + + if (is_array($value) && array_key_exists('valueFrom', $value)) { + return ['name' => $name, 'valueFrom' => $value['valueFrom']]; + } + return ['name' => $name, 'value' => $value]; })->values()->toArray(); From 0db8675f5f08b215eb512a1084cc329cff6075ed Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 21:20:54 +0200 Subject: [PATCH 04/19] Created new test for adding env values from refs. --- tests/ContainerTest.php | 129 +++++++++++++++++++++++++++------------- 1 file changed, 88 insertions(+), 41 deletions(-) diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 13fd88db..8662d563 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -15,23 +15,9 @@ public function test_container_build() $volume = K8s::volume()->awsEbs('vol-1234', 'ext3'); $container->setImage('nginx', '1.4') - ->setEnv([ - 'key' => 'value', - 'SECRET_ONE' => [ - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key' - ] - ] - ] - ]) + ->setEnv(['key' => 'value']) ->addEnvs(['key2' => 'value2']) - ->addSecretKeyRef('SECRET_TWO', 'ref_name', 'ref_key') - ->addSecretKeyRefs([ - 'SECRET_THREE' => ['ref_name', 'ref_key'], - 'SECRET_FOUR' => ['ref_name', 'ref_key'] - ]) + ->addSecretKeyRef('SECRET_ONE', 'ref_name', 'ref_key') ->setArgs(['--test']) ->addPort(80, 'TCP', 'http') ->addPort(443, 'TCP', 'https') @@ -70,24 +56,8 @@ public function test_container_build() $this->assertEquals('nginx:1.4', $container->getImage()); $this->assertEquals([ ['name' => 'key', 'value' => 'value'], - ['name' => 'SECRET_ONE', - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key' - ] - ], - ], ['name' => 'key2', 'value' => 'value2'], - ['name' => 'SECRET_TWO', - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key' - ] - ], - ], - ['name' => 'SECRET_THREE', + ['name' => 'SECRET_ONE', 'valueFrom' => [ 'secretKeyRef' => [ 'name' => 'ref_name', @@ -95,14 +65,6 @@ public function test_container_build() ] ], ], - ['name' => 'SECRET_FOUR', - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key' - ] - ], - ] ], $container->getEnv()); $this->assertEquals(['--test'], $container->getArgs()); $this->assertEquals([ @@ -140,4 +102,89 @@ public function test_container_build() 'mountPath' => '/some/path', ], $container->getMountedVolumes()[0]->toArray()); } + + public function test_adding_secrets_to_container() + { + $container = K8s::container(); + + $container->setImage('nginx', '1.4') + ->setEnv([ + 'key' => 'value', + 'SECRET_ONE' => [ + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ] + ] + ]) + ->addEnvs([ + 'SECRET_TWO' => [ + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ] + ] + ]) + ->addSecretKeyRef('SECRET_THREE', 'ref_name', 'ref_key') + ->addSecretKeyRefs([ + 'SECRET_FOUR' => ['ref_name', 'ref_key'], + 'SECRET_FIVE' => ['ref_name', 'ref_key'] + ]); + + + $this->assertEquals('nginx:1.4', $container->getImage()); + $this->assertEquals([ + ['name' => 'key', 'value' => 'value'], + ['name' => 'SECRET_ONE', + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ], + ], + ['name' => 'SECRET_TWO', + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ], + ], + ['name' => 'SECRET_THREE', + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ], + ], + ['name' => 'SECRET_FOUR', + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ], + ], + ['name' => 'SECRET_FIVE', + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ], + ] + ], $container->getEnv()); + + + $container->removeEnv(); + + $this->assertEquals('nginx:1.4', $container->getImage()); + $this->assertEquals([], $container->getEnv([])); + } } From 0f42b76515619ae537f885f156a1e3f70a76e14b Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 21:21:58 +0200 Subject: [PATCH 05/19] Check if env value is array. Add valueFrom. --- src/Instances/Container.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Instances/Container.php b/src/Instances/Container.php index 6edcae76..6e22ff67 100644 --- a/src/Instances/Container.php +++ b/src/Instances/Container.php @@ -146,6 +146,9 @@ public function addSecretKeyRefs(array $refs) */ public function addEnv(string $name, $value) { + if (is_array($value) && array_key_exists('valueFrom', $value)) { + return $this->addToAttribute('env', ['name' => $name, 'valueFrom' => $value['valueFrom']]); + } return $this->addToAttribute('env', ['name' => $name, 'value' => $value]); } From a1eaba4c51dc8b6ac3268153e762de8d77fb60bf Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 21:32:21 +0200 Subject: [PATCH 06/19] Updated docs wiith new container env functions. --- docs/instances/Container.md | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/instances/Container.md b/docs/instances/Container.md index 27c5a437..df164a19 100644 --- a/docs/instances/Container.md +++ b/docs/instances/Container.md @@ -12,7 +12,42 @@ $container = K8s::container() ->addPort(3307, 'TCP', 'mysql-alt') ->setCommand(['mysqld']) ->setArgs(['--test']) - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); + ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']) +``` + +For adding a env value based on an secret, first make sure that the secret exists in the namespace that this container will be deployed in, otherwise a KubernetesAPIException will be thrown. + +```php +// Single +$container->addSecretKeyRef('SECRET_TEST', 'ref_name', 'ref_key') + +// Multiple +$container->addSecretKeyRefs([ + 'SECRET_FOUR' => ['ref_name', 'ref_key'], + 'SECRET_FIVE' => ['ref_name', 'ref_key'] + ]) +``` + +Enviornment variables can also be set using a value from the [configMapKeyRef](https://kubernetes.io/docs/concepts/configuration/configmap/#configmap-object) or [fieldRef](https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-pod-fields-as-values-for-environment-variables). When using a configMapKeyRef, also make sure the configMap exists in the same namespace as the container, otherwise a KubernetesAPIException will be thrown. + +```php +$container->addEnv([ + 'CONFIG_VARIABLE' => [ + 'valueFrom' => [ + 'configMapKeyRef' => [ + 'name' => 'ref_name', + 'key' => 'ref_key' + ] + ] + ], + 'FIELD_REF' => [ + 'valueFrom' => [ + 'fieldRef' => [ + 'fieldPath' => 'spec.nodeName' + ] + ] + ] +]) ``` ### Attaching probes From 5446501de20ee04fe3f687638cb2d76b5aece622 Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 21:34:50 +0200 Subject: [PATCH 07/19] Fixed typo. --- docs/instances/Container.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/instances/Container.md b/docs/instances/Container.md index df164a19..bc1bdf85 100644 --- a/docs/instances/Container.md +++ b/docs/instances/Container.md @@ -28,7 +28,7 @@ $container->addSecretKeyRefs([ ]) ``` -Enviornment variables can also be set using a value from the [configMapKeyRef](https://kubernetes.io/docs/concepts/configuration/configmap/#configmap-object) or [fieldRef](https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-pod-fields-as-values-for-environment-variables). When using a configMapKeyRef, also make sure the configMap exists in the same namespace as the container, otherwise a KubernetesAPIException will be thrown. +Environment variables can also be set using a value from the [configMapKeyRef](https://kubernetes.io/docs/concepts/configuration/configmap/#configmap-object) or [fieldRef](https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-pod-fields-as-values-for-environment-variables). When using a configMapKeyRef, also make sure the configMap exists in the same namespace as the container, otherwise a KubernetesAPIException will be thrown. ```php $container->addEnv([ From 65914970c8007877ad68cbd2f3da2ee594caff3f Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 21:36:05 +0200 Subject: [PATCH 08/19] Added kubernetes doc link. --- docs/instances/Container.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/instances/Container.md b/docs/instances/Container.md index bc1bdf85..325e5df3 100644 --- a/docs/instances/Container.md +++ b/docs/instances/Container.md @@ -15,7 +15,7 @@ $container = K8s::container() ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']) ``` -For adding a env value based on an secret, first make sure that the secret exists in the namespace that this container will be deployed in, otherwise a KubernetesAPIException will be thrown. +For adding a env value based on an [secretKeyRef](https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables), first make sure that the secret exists in the namespace that this container will be deployed in, otherwise a KubernetesAPIException will be thrown. ```php // Single From e360c08d0219b1b7c9a7f504f3c02cd3548ebbf0 Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 21:38:03 +0200 Subject: [PATCH 09/19] Created new doc section for env. --- docs/instances/Container.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/instances/Container.md b/docs/instances/Container.md index 325e5df3..5e5d61f1 100644 --- a/docs/instances/Container.md +++ b/docs/instances/Container.md @@ -15,6 +15,8 @@ $container = K8s::container() ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']) ``` +## Adding environment variables + For adding a env value based on an [secretKeyRef](https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables), first make sure that the secret exists in the namespace that this container will be deployed in, otherwise a KubernetesAPIException will be thrown. ```php From c91fb5d2512f8a3c0b8aec410ffc2bfddd7d326d Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 21:45:51 +0200 Subject: [PATCH 10/19] Fixed style issues. --- src/Instances/Container.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Instances/Container.php b/src/Instances/Container.php index 6e22ff67..51d8813b 100644 --- a/src/Instances/Container.php +++ b/src/Instances/Container.php @@ -114,9 +114,9 @@ public function addSecretKeyRef(string $name, string $refName, string $refKey) 'valueFrom' => [ 'secretKeyRef' => [ 'name' => $refName, - 'key' => $refKey - ] - ] + 'key' => $refKey, + ], + ], ]); } @@ -129,7 +129,7 @@ public function addSecretKeyRef(string $name, string $refName, string $refKey) public function addSecretKeyRefs(array $refs) { foreach ($refs as $ref => $value) { - if (is_array($value)){ + if (is_array($value)) { $this->addSecretKeyRef($ref, $value[0], $value[1]); } } From 15c3db8be264b19bd50de58afdf96cf6ee5493d2 Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 21:46:44 +0200 Subject: [PATCH 11/19] Fixes style issues. --- tests/ContainerTest.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 8662d563..32138b8d 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -61,7 +61,7 @@ public function test_container_build() 'valueFrom' => [ 'secretKeyRef' => [ 'name' => 'ref_name', - 'key' => 'ref_key' + 'key' => 'ref_key', ] ], ], @@ -114,7 +114,7 @@ public function test_adding_secrets_to_container() 'valueFrom' => [ 'secretKeyRef' => [ 'name' => 'ref_name', - 'key' => 'ref_key' + 'key' => 'ref_key', ] ] ] @@ -124,7 +124,7 @@ public function test_adding_secrets_to_container() 'valueFrom' => [ 'secretKeyRef' => [ 'name' => 'ref_name', - 'key' => 'ref_key' + 'key' => 'ref_key', ] ] ] @@ -132,7 +132,7 @@ public function test_adding_secrets_to_container() ->addSecretKeyRef('SECRET_THREE', 'ref_name', 'ref_key') ->addSecretKeyRefs([ 'SECRET_FOUR' => ['ref_name', 'ref_key'], - 'SECRET_FIVE' => ['ref_name', 'ref_key'] + 'SECRET_FIVE' => ['ref_name', 'ref_key'], ]); @@ -143,7 +143,7 @@ public function test_adding_secrets_to_container() 'valueFrom' => [ 'secretKeyRef' => [ 'name' => 'ref_name', - 'key' => 'ref_key' + 'key' => 'ref_key', ] ], ], @@ -151,7 +151,7 @@ public function test_adding_secrets_to_container() 'valueFrom' => [ 'secretKeyRef' => [ 'name' => 'ref_name', - 'key' => 'ref_key' + 'key' => 'ref_key', ] ], ], @@ -159,7 +159,7 @@ public function test_adding_secrets_to_container() 'valueFrom' => [ 'secretKeyRef' => [ 'name' => 'ref_name', - 'key' => 'ref_key' + 'key' => 'ref_key', ] ], ], @@ -167,7 +167,7 @@ public function test_adding_secrets_to_container() 'valueFrom' => [ 'secretKeyRef' => [ 'name' => 'ref_name', - 'key' => 'ref_key' + 'key' => 'ref_key', ] ], ], @@ -175,7 +175,7 @@ public function test_adding_secrets_to_container() 'valueFrom' => [ 'secretKeyRef' => [ 'name' => 'ref_name', - 'key' => 'ref_key' + 'key' => 'ref_key', ] ], ] From 8479699823e0fd789307fc760b8ce6e4b89abc1c Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 21:48:20 +0200 Subject: [PATCH 12/19] Fixes mnore style issues. --- tests/ContainerTest.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 32138b8d..d225c0d0 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -62,7 +62,7 @@ public function test_container_build() 'secretKeyRef' => [ 'name' => 'ref_name', 'key' => 'ref_key', - ] + ], ], ], ], $container->getEnv()); @@ -115,9 +115,9 @@ public function test_adding_secrets_to_container() 'secretKeyRef' => [ 'name' => 'ref_name', 'key' => 'ref_key', - ] - ] - ] + ], + ], + ], ]) ->addEnvs([ 'SECRET_TWO' => [ @@ -125,9 +125,9 @@ public function test_adding_secrets_to_container() 'secretKeyRef' => [ 'name' => 'ref_name', 'key' => 'ref_key', - ] - ] - ] + ], + ], + ], ]) ->addSecretKeyRef('SECRET_THREE', 'ref_name', 'ref_key') ->addSecretKeyRefs([ @@ -144,7 +144,7 @@ public function test_adding_secrets_to_container() 'secretKeyRef' => [ 'name' => 'ref_name', 'key' => 'ref_key', - ] + ], ], ], ['name' => 'SECRET_TWO', @@ -152,7 +152,7 @@ public function test_adding_secrets_to_container() 'secretKeyRef' => [ 'name' => 'ref_name', 'key' => 'ref_key', - ] + ], ], ], ['name' => 'SECRET_THREE', @@ -160,7 +160,7 @@ public function test_adding_secrets_to_container() 'secretKeyRef' => [ 'name' => 'ref_name', 'key' => 'ref_key', - ] + ], ], ], ['name' => 'SECRET_FOUR', @@ -168,7 +168,7 @@ public function test_adding_secrets_to_container() 'secretKeyRef' => [ 'name' => 'ref_name', 'key' => 'ref_key', - ] + ], ], ], ['name' => 'SECRET_FIVE', @@ -176,9 +176,9 @@ public function test_adding_secrets_to_container() 'secretKeyRef' => [ 'name' => 'ref_name', 'key' => 'ref_key', - ] + ], ], - ] + ], ], $container->getEnv()); From df245bda71fac1f0cebd5222b67cc81269ca2fa1 Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 30 Mar 2021 21:50:08 +0200 Subject: [PATCH 13/19] Fixed even more style issues. --- src/Instances/Container.php | 2 +- tests/ContainerTest.php | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Instances/Container.php b/src/Instances/Container.php index 51d8813b..a808f770 100644 --- a/src/Instances/Container.php +++ b/src/Instances/Container.php @@ -149,6 +149,7 @@ public function addEnv(string $name, $value) if (is_array($value) && array_key_exists('valueFrom', $value)) { return $this->addToAttribute('env', ['name' => $name, 'valueFrom' => $value['valueFrom']]); } + return $this->addToAttribute('env', ['name' => $name, 'value' => $value]); } @@ -176,7 +177,6 @@ public function addEnvs(array $envs) public function setEnv(array $envs) { $envs = collect($envs)->map(function ($value, $name) { - if (is_array($value) && array_key_exists('valueFrom', $value)) { return ['name' => $name, 'valueFrom' => $value['valueFrom']]; } diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index d225c0d0..58127115 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -135,7 +135,6 @@ public function test_adding_secrets_to_container() 'SECRET_FIVE' => ['ref_name', 'ref_key'], ]); - $this->assertEquals('nginx:1.4', $container->getImage()); $this->assertEquals([ ['name' => 'key', 'value' => 'value'], @@ -181,7 +180,6 @@ public function test_adding_secrets_to_container() ], ], $container->getEnv()); - $container->removeEnv(); $this->assertEquals('nginx:1.4', $container->getImage()); From 6d3b2f192d7d4df62edce613f98f7fb2c1697940 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Wed, 31 Mar 2021 11:42:38 +0300 Subject: [PATCH 14/19] Updated container methods & added CM and fieldRef functions --- src/Instances/Container.php | 113 ++++++++++++++++++++++++++++++------ tests/ContainerTest.php | 103 +++++--------------------------- 2 files changed, 112 insertions(+), 104 deletions(-) diff --git a/src/Instances/Container.php b/src/Instances/Container.php index a808f770..e841c364 100644 --- a/src/Instances/Container.php +++ b/src/Instances/Container.php @@ -103,35 +103,95 @@ public function getMountedVolumes(bool $asInstance = true) * Add an env variable by using a secret reference to the container. * * @param string $name - * @param string $refName - * @param string $refKey + * @param string $secretName + * @param string $key * @return $this */ - public function addSecretKeyRef(string $name, string $refName, string $refKey) + public function addSecretKeyRef(string $name, string $secretName, string $key) { - return $this->addToAttribute('env', [ - 'name' => $name, - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => $refName, - 'key' => $refKey, - ], - ], + return $this->addEnv($name, [ + 'valueFrom' => $this->formatValueFrom('secretKeyRef', [ + 'name' => $secretName, + 'key' => $key, + ]), ]); } /** * Add multiple secret references to the container. * - * @param array $refs + * @param array $envsWithRefs * @return $this */ - public function addSecretKeyRefs(array $refs) + public function addSecretKeyRefs(array $envsWithRefs) { - foreach ($refs as $ref => $value) { - if (is_array($value)) { - $this->addSecretKeyRef($ref, $value[0], $value[1]); - } + foreach ($envsWithRefs as $envName => $refs) { + $this->addSecretKeyRef($envName, ...$refs); + } + + return $this; + } + + /** + * Add an env variable by using a configmap reference to the container. + * + * @param string $name + * @param string $cmName + * @param string $key + * @return $this + */ + public function addConfigMapRef(string $name, string $cmName, string $key) + { + return $this->addEnv($name, [ + 'valueFrom' => $this->formatValueFrom('configMapKeyRef', [ + 'name' => $cmName, + 'key' => $key, + ]), + ]); + } + + /** + * Add multiple configmap references to the container. + * + * @param array $envsWithRefs + * @return $this + */ + public function addConfigMapRefs(array $envsWithRefs) + { + foreach ($envsWithRefs as $envName => $refs) { + $this->addConfigMapRef($envName, ...$refs); + } + + return $this; + } + + /** + * Add an env variable by using a field reference to the container. + * + * @param string $name + * @param string $cmName + * @param string $key + * @return $this + */ + public function addFieldRef(string $name, string $fieldPath) + { + return $this->addEnv($name, [ + 'valueFrom' => $this->formatValueFrom('fieldRef', [ + 'fieldPath' => $fieldPath, + ]), + ]); + } + + /** + * Add multiple field references to the container. + * + * @param array $envsWithRefs + * @return $this + */ + public function addFieldRefs(array $envsWithRefs) + { + foreach ($envsWithRefs as $envName => $refs) { + $this->addFieldRef($envName, ...$refs); } return $this; @@ -146,6 +206,7 @@ public function addSecretKeyRefs(array $refs) */ public function addEnv(string $name, $value) { + // If a valuFrom is encountered, add it instead. if (is_array($value) && array_key_exists('valueFrom', $value)) { return $this->addToAttribute('env', ['name' => $name, 'valueFrom' => $value['valueFrom']]); } @@ -177,6 +238,7 @@ public function addEnvs(array $envs) public function setEnv(array $envs) { $envs = collect($envs)->map(function ($value, $name) { + // If a valuFrom is encountered, add it instead. if (is_array($value) && array_key_exists('valueFrom', $value)) { return ['name' => $name, 'valueFrom' => $value['valueFrom']]; } @@ -366,4 +428,21 @@ public function isReady(): bool { return $this->getAttribute('ready', false); } + + /** + * Create a `valueFrom` format. + * + * @param string $type + * @param array $params + * @return array + */ + protected function formatValueFrom(string $type, array $params): array + { + return [ + 'secretKeyRef' => [ + 'name' => $secretName, + 'key' => $key, + ], + ]; + } } diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 58127115..9c5f0f80 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -17,7 +17,9 @@ public function test_container_build() $container->setImage('nginx', '1.4') ->setEnv(['key' => 'value']) ->addEnvs(['key2' => 'value2']) - ->addSecretKeyRef('SECRET_ONE', 'ref_name', 'ref_key') + ->addSecretKeyRefs(['SECRET_ONE' => ['secret_ref_name', 'secret_ref_key']]) + ->addConfigMapRefs(['SECRET_TWO' => ['cm_ref_name', 'cm_ref_key']]) + ->addFieldRefs(['NODE_NAME' => ['spec.nodeName']]) ->setArgs(['--test']) ->addPort(80, 'TCP', 'http') ->addPort(443, 'TCP', 'https') @@ -57,11 +59,21 @@ public function test_container_build() $this->assertEquals([ ['name' => 'key', 'value' => 'value'], ['name' => 'key2', 'value' => 'value2'], - ['name' => 'SECRET_ONE', + [ + 'name' => 'SECRET_ONE', 'valueFrom' => [ 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key', + 'name' => 'secret_ref_name', + 'key' => 'secret_ref_key', + ], + ], + ], + [ + 'name' => 'SECRET_TWO', + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => 'cm_ref_name', + 'key' => 'cm_ref_key', ], ], ], @@ -102,87 +114,4 @@ public function test_container_build() 'mountPath' => '/some/path', ], $container->getMountedVolumes()[0]->toArray()); } - - public function test_adding_secrets_to_container() - { - $container = K8s::container(); - - $container->setImage('nginx', '1.4') - ->setEnv([ - 'key' => 'value', - 'SECRET_ONE' => [ - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key', - ], - ], - ], - ]) - ->addEnvs([ - 'SECRET_TWO' => [ - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key', - ], - ], - ], - ]) - ->addSecretKeyRef('SECRET_THREE', 'ref_name', 'ref_key') - ->addSecretKeyRefs([ - 'SECRET_FOUR' => ['ref_name', 'ref_key'], - 'SECRET_FIVE' => ['ref_name', 'ref_key'], - ]); - - $this->assertEquals('nginx:1.4', $container->getImage()); - $this->assertEquals([ - ['name' => 'key', 'value' => 'value'], - ['name' => 'SECRET_ONE', - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key', - ], - ], - ], - ['name' => 'SECRET_TWO', - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key', - ], - ], - ], - ['name' => 'SECRET_THREE', - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key', - ], - ], - ], - ['name' => 'SECRET_FOUR', - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key', - ], - ], - ], - ['name' => 'SECRET_FIVE', - 'valueFrom' => [ - 'secretKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key', - ], - ], - ], - ], $container->getEnv()); - - $container->removeEnv(); - - $this->assertEquals('nginx:1.4', $container->getImage()); - $this->assertEquals([], $container->getEnv([])); - } } From 1e28a7db55e3143a558b5fd13fbe710a18846498 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Wed, 31 Mar 2021 11:42:41 +0300 Subject: [PATCH 15/19] Updated docs --- docs/instances/Container.md | 73 ++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/docs/instances/Container.md b/docs/instances/Container.md index 5e5d61f1..31068079 100644 --- a/docs/instances/Container.md +++ b/docs/instances/Container.md @@ -1,3 +1,11 @@ +- [Containers](#containers) + - [Creating a container](#creating-a-container) + - [Setting environment variables](#setting-environment-variables) + - [Adding variables from references](#adding-variables-from-references) + - [Attaching probes](#attaching-probes) + - [Attaching volumes](#attaching-volumes) + - [Limits & Requests](#limits--requests) + # Containers ## Creating a container @@ -11,45 +19,52 @@ $container = K8s::container() ]) ->addPort(3307, 'TCP', 'mysql-alt') ->setCommand(['mysqld']) - ->setArgs(['--test']) - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']) + ->setArgs(['--test']); +``` + +### Setting environment variables + +To set the environment variable, simply call `->setEnv()`: + +```php +$container->setEnv([ + 'MYSQL_ROOT_PASSWORD' => 'test', +]); + +$container->addEnv('MYSQL_DATABASE', 'my_db') // this will append an env ``` -## Adding environment variables +### Adding variables from references + +To add an environment variable based on [secretKeyRef](https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables), [configMapKeyRef](https://kubernetes.io/docs/concepts/configuration/configmap/#configmap-object) or [fieldRef](https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-pod-fields-as-values-for-environment-variables), refer to the following examples. -For adding a env value based on an [secretKeyRef](https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables), first make sure that the secret exists in the namespace that this container will be deployed in, otherwise a KubernetesAPIException will be thrown. +In the below examples, the `ref_key` referes to the key on which the data is stored within a configmap or a secret. ```php -// Single -$container->addSecretKeyRef('SECRET_TEST', 'ref_name', 'ref_key') +$container->addSecretKeyRef('MYSQL_ROOT_PASSWORD', 'secret-name', 'ref_key'); -// Multiple $container->addSecretKeyRefs([ - 'SECRET_FOUR' => ['ref_name', 'ref_key'], - 'SECRET_FIVE' => ['ref_name', 'ref_key'] - ]) + 'MYSQL_ROOT_PASSWORD' => ['secret-name', 'ref_key'], + 'MYSQL_DATABASE' => ['secret-name', 'ref_key'], +]); ``` -Environment variables can also be set using a value from the [configMapKeyRef](https://kubernetes.io/docs/concepts/configuration/configmap/#configmap-object) or [fieldRef](https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-pod-fields-as-values-for-environment-variables). When using a configMapKeyRef, also make sure the configMap exists in the same namespace as the container, otherwise a KubernetesAPIException will be thrown. +```php +$container->addConfigMapRef('MYSQL_ROOT_PASSWORD', 'configmap-name', 'ref_key'); + +$container->addConfigMapRefs([ + 'MYSQL_ROOT_PASSWORD' => ['cm-name', 'ref_key'], + 'MYSQL_DATABASE' => ['cm-name', 'ref_key'], +]); +``` ```php -$container->addEnv([ - 'CONFIG_VARIABLE' => [ - 'valueFrom' => [ - 'configMapKeyRef' => [ - 'name' => 'ref_name', - 'key' => 'ref_key' - ] - ] - ], - 'FIELD_REF' => [ - 'valueFrom' => [ - 'fieldRef' => [ - 'fieldPath' => 'spec.nodeName' - ] - ] - ] -]) +$container->addFieldRef('NODE_NAME', 'spec.nodeName'); + +$container->addFieldRefs([ + 'NODE_NAME' => ['spec.nodeName'], + 'POD_NAME' => ['metadata.name'], +]); ``` ### Attaching probes @@ -109,7 +124,7 @@ $pod = K8s::pod() ->addVolumes([$awsEbVolume]); ``` -### Setting resources +### Limits & Requests ```php $container->minMemory(512, 'Mi')->maxMemory(2, 'Gi'); From cc82f31c7dc3d3965089fb2ac96b2d977e8db287 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Wed, 31 Mar 2021 11:45:13 +0300 Subject: [PATCH 16/19] Reverted formatValueFrom --- src/Instances/Container.php | 49 ++++++++++++++----------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/src/Instances/Container.php b/src/Instances/Container.php index e841c364..4e8c29fa 100644 --- a/src/Instances/Container.php +++ b/src/Instances/Container.php @@ -110,10 +110,12 @@ public function getMountedVolumes(bool $asInstance = true) public function addSecretKeyRef(string $name, string $secretName, string $key) { return $this->addEnv($name, [ - 'valueFrom' => $this->formatValueFrom('secretKeyRef', [ - 'name' => $secretName, - 'key' => $key, - ]), + 'valueFrom' => [ + 'secretKeyRef' => [ + 'name' => $secretName, + 'key' => $key, + ], + ], ]); } @@ -143,10 +145,12 @@ public function addSecretKeyRefs(array $envsWithRefs) public function addConfigMapRef(string $name, string $cmName, string $key) { return $this->addEnv($name, [ - 'valueFrom' => $this->formatValueFrom('configMapKeyRef', [ - 'name' => $cmName, - 'key' => $key, - ]), + 'valueFrom' => [ + 'configMapKeyRef' => [ + 'name' => $cmName, + 'key' => $key, + ], + ], ]); } @@ -176,9 +180,11 @@ public function addConfigMapRefs(array $envsWithRefs) public function addFieldRef(string $name, string $fieldPath) { return $this->addEnv($name, [ - 'valueFrom' => $this->formatValueFrom('fieldRef', [ - 'fieldPath' => $fieldPath, - ]), + 'valueFrom' => [ + 'fieldRef' => [ + 'fieldPath' => $fieldPath, + ], + ], ]); } @@ -206,7 +212,7 @@ public function addFieldRefs(array $envsWithRefs) */ public function addEnv(string $name, $value) { - // If a valuFrom is encountered, add it instead. + // If a valuFrom is encountered, add it under valueFrom instead. if (is_array($value) && array_key_exists('valueFrom', $value)) { return $this->addToAttribute('env', ['name' => $name, 'valueFrom' => $value['valueFrom']]); } @@ -238,7 +244,7 @@ public function addEnvs(array $envs) public function setEnv(array $envs) { $envs = collect($envs)->map(function ($value, $name) { - // If a valuFrom is encountered, add it instead. + // If a valuFrom is encountered, add it under valueFrom instead. if (is_array($value) && array_key_exists('valueFrom', $value)) { return ['name' => $name, 'valueFrom' => $value['valueFrom']]; } @@ -428,21 +434,4 @@ public function isReady(): bool { return $this->getAttribute('ready', false); } - - /** - * Create a `valueFrom` format. - * - * @param string $type - * @param array $params - * @return array - */ - protected function formatValueFrom(string $type, array $params): array - { - return [ - 'secretKeyRef' => [ - 'name' => $secretName, - 'key' => $key, - ], - ]; - } } From 5f82851d1eaa015bd68ec0c9ace44d6e156c536b Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Wed, 31 Mar 2021 12:00:19 +0300 Subject: [PATCH 17/19] Fixed tests --- tests/ContainerTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index 9c5f0f80..a0523201 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -77,6 +77,14 @@ public function test_container_build() ], ], ], + [ + 'name' => 'NODE_NAME', + 'valueFrom' => [ + 'fieldRef' => [ + 'fieldPath' => 'spec.nodeName', + ], + ], + ], ], $container->getEnv()); $this->assertEquals(['--test'], $container->getArgs()); $this->assertEquals([ From a9fcfd561f536f1af1c8e1533f1768ce97ef5671 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Wed, 31 Mar 2021 12:11:01 +0300 Subject: [PATCH 18/19] Fixed tests --- tests/ContainerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ContainerTest.php b/tests/ContainerTest.php index a0523201..89657163 100644 --- a/tests/ContainerTest.php +++ b/tests/ContainerTest.php @@ -71,7 +71,7 @@ public function test_container_build() [ 'name' => 'SECRET_TWO', 'valueFrom' => [ - 'secretKeyRef' => [ + 'configMapKeyRef' => [ 'name' => 'cm_ref_name', 'key' => 'cm_ref_key', ], From b654527c845f46129fd3a40c51cfa252e875726c Mon Sep 17 00:00:00 2001 From: Sven van Hees Date: Tue, 18 May 2021 15:33:54 +0200 Subject: [PATCH 19/19] Count headers and conditionally apply headers. --- src/Instances/Probe.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Instances/Probe.php b/src/Instances/Probe.php index b44645fb..4f312bdf 100644 --- a/src/Instances/Probe.php +++ b/src/Instances/Probe.php @@ -50,14 +50,19 @@ public function getCommand() */ public function http(string $path = '/healthz', int $port = 8080, array $headers = [], string $scheme = 'HTTP') { - return $this->setAttribute('httpGet', [ + $probeData = [ 'path' => $path, 'port' => $port, - 'httpHeaders' => collect($headers)->map(function ($value, $key) { - return ['name' => $key, 'value' => $value]; - })->values()->toArray(), 'scheme' => $scheme, - ]); + ]; + + if (count($headers) > 0) { + $probeData['httpHeaders'] = collect($headers)->map(function ($value, $key) { + return ['name' => $key, 'value' => $value]; + })->values()->toArray(); + } + + return $this->setAttribute('httpGet', $probeData); } /**