diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5ede26cf..b64eaef3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,25 +21,20 @@ jobs: strategy: fail-fast: false matrix: - php: ['8.1', '8.2'] - kubernetes: ['1.24.12', '1.25.8', '1.26.3'] - laravel: ['9.*', '10.*', '11.*'] + php: ['8.3', '8.4'] + kubernetes: ['1.31.10', '1.32.6', '1.33.2'] + laravel: ['11.*', '12.*'] prefer: [prefer-lowest, prefer-stable] include: - - laravel: "9.*" - testbench: "7.*" - - laravel: "10.*" - testbench: "8.*" - laravel: "11.*" testbench: "9.*" - exclude: - - laravel: "11.*" - php: "8.1" + - laravel: "12.*" + testbench: "10.*" name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - K8s v${{ matrix.kubernetes }} --${{ matrix.prefer }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -48,16 +43,33 @@ jobs: extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, yaml coverage: pcov - - uses: actions/cache@v3.0.5 + - name: Get composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Prepare cache key + id: prep + run: | + PHP_VERSION=${{ matrix.php }} + LARAVEL_VERSION=${{ matrix.laravel }} + PREFER_VERSION=${{ matrix.prefer }} + + # Remove any .* from the versions + LARAVEL_VERSION=${LARAVEL_VERSION//.*} + + echo "cache-key=composer-php-$PHP_VERSION-$LARAVEL_VERSION-$PREFER_VERSION-${{ hashFiles('composer.json') }}" >> $GITHUB_OUTPUT + + - uses: actions/cache@v4 name: Cache dependencies with: - path: ~/.composer/cache/files - key: composer-php-${{ matrix.php }}-${{ matrix.laravel }}-${{ matrix.prefer }}-${{ hashFiles('composer.json') }} + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ steps.prep.outputs.cache-key }} - uses: medyagh/setup-minikube@latest name: Setup Minikube with: - minikube-version: 1.29.0 + minikube-version: 1.36.0 + driver: docker container-runtime: containerd kubernetes-version: v${{ matrix.kubernetes }} @@ -86,6 +98,6 @@ jobs: run: | vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml - - uses: codecov/codecov-action@v3.1.0 + - uses: codecov/codecov-action@v5 with: fail_ci_if_error: false diff --git a/README.md b/README.md index 735264e8..efdb43ab 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,10 @@ PHP K8s [![Total Downloads](https://poser.pugx.org/renoki-co/php-k8s/downloads)](https://packagist.org/packages/renoki-co/php-k8s) [![Monthly Downloads](https://poser.pugx.org/renoki-co/php-k8s/d/monthly)](https://packagist.org/packages/renoki-co/php-k8s) -![v1.24.12 K8s Version](https://img.shields.io/badge/K8s%20v1.24.12-Ready-%23326ce5?colorA=306CE8&colorB=green) -![v1.25.8 K8s Version](https://img.shields.io/badge/K8s%20v1.25.8-Ready-%23326ce5?colorA=306CE8&colorB=green) -![v1.26.3 K8s Version](https://img.shields.io/badge/K8s%20v1.26.3-Ready-%23326ce5?colorA=306CE8&colorB=green) +![v1.31.10 K8s Version](https://img.shields.io/badge/K8s%20v1.31.10-Ready-%23326ce5?colorA=306CE8&colorB=green) +![v1.32.6 K8s Version](https://img.shields.io/badge/K8s%20v1.32.6-Ready-%23326ce5?colorA=306CE8&colorB=green) +![v1.33.2 K8s Version](https://img.shields.io/badge/K8s%20v1.33.2-Ready-%23326ce5?colorA=306CE8&colorB=green) + [![Client Capabilities](https://img.shields.io/badge/Kubernetes%20Client-Silver-blue.svg?colorB=C0C0C0&colorA=306CE8)](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/csi-new-client-library-procedure.md#client-capabilities) [![Client Support Level](https://img.shields.io/badge/Kubernetes%20Client-stable-green.svg?colorA=306CE8)](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/csi-new-client-library-procedure.md#client-support-level) diff --git a/composer.json b/composer.json index 5a531531..391d5210 100644 --- a/composer.json +++ b/composer.json @@ -24,12 +24,14 @@ } ], "require": { - "guzzlehttp/guzzle": "^6.5|^7.0", - "illuminate/macroable": "^9.35|^10.1|^11.0", - "illuminate/support": "^9.35|^10.1|^11.0", + "php": "^8.3", + "guzzlehttp/guzzle": "^7.9", + "illuminate/macroable": "^11.0|^12.0", + "illuminate/support": "^11.0|^12.0", "ratchet/pawl": "^0.4.1", - "symfony/process": "^5.4|^6.0|^7.0", - "vierbergenlars/php-semver": "^2.1|^3.0" + "symfony/process": "^7.3.0", + "composer/semver": "^3.4", + "ext-json": "*" }, "suggest": { "ext-yaml": "YAML extension is used to read or generate YAML from PHP K8s internal classes." @@ -48,10 +50,10 @@ "test": "vendor/bin/phpunit" }, "require-dev": { - "mockery/mockery": "^1.5", - "orchestra/testbench": "^7.23|^8.1|^9.0", - "phpunit/phpunit": "^9.5.20|^10.0", - "vimeo/psalm": "^4.20|^5.22" + "mockery/mockery": "^1.6", + "orchestra/testbench": "^9.0|^10.0", + "phpunit/phpunit": "^10.0|^11.0", + "vimeo/psalm": "^6.12.0" }, "config": { "sort-packages": true diff --git a/phpunit.xml b/phpunit.xml index ef9baef1..b7a8bad5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,15 +1,29 @@ - - - - src/ - - + tests + + + src + + + + + + + + diff --git a/src/Traits/Cluster/ChecksClusterVersion.php b/src/Traits/Cluster/ChecksClusterVersion.php index eedea5f2..b6804cbe 100644 --- a/src/Traits/Cluster/ChecksClusterVersion.php +++ b/src/Traits/Cluster/ChecksClusterVersion.php @@ -2,29 +2,30 @@ namespace RenokiCo\PhpK8s\Traits\Cluster; +use Composer\Semver\Comparator; +use Composer\Semver\VersionParser; use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\GuzzleException; +use JsonException; use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException; -use vierbergenlars\SemVer\version as Semver; trait ChecksClusterVersion { /** * The Kubernetes cluster version. - * - * @var \vierbergenlars\SemVer\version|null */ - protected $kubernetesVersion; + protected string $kubernetesVersion; /** * Load the cluster version. * * @return void * - * @throws \RenokiCo\PhpK8s\Exceptions\KubernetesAPIException + * @throws KubernetesAPIException|GuzzleException|JsonException */ protected function loadClusterVersion(): void { - if ($this->kubernetesVersion) { + if (isset($this->kubernetesVersion)) { return; } @@ -42,9 +43,10 @@ protected function loadClusterVersion(): void ); } - $json = @json_decode($response->getBody(), true); + $json = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR); - $this->kubernetesVersion = new Semver($json['gitVersion']); + $parser = new VersionParser(); + $this->kubernetesVersion = $parser->normalize($json['gitVersion']); } /** @@ -53,12 +55,14 @@ protected function loadClusterVersion(): void * * @param string $kubernetesVersion * @return bool + * + * @throws KubernetesAPIException|GuzzleException|JsonException */ public function newerThan(string $kubernetesVersion): bool { $this->loadClusterVersion(); - return Semver::gte( + return Comparator::greaterThanOrEqualTo( $this->kubernetesVersion, $kubernetesVersion ); } @@ -69,12 +73,14 @@ public function newerThan(string $kubernetesVersion): bool * * @param string $kubernetesVersion * @return bool + * + * @throws KubernetesAPIException|GuzzleException|JsonException */ public function olderThan(string $kubernetesVersion): bool { $this->loadClusterVersion(); - return Semver::lt( + return Comparator::lessThan( $this->kubernetesVersion, $kubernetesVersion ); } diff --git a/tests/ChecksClusterVersionTest.php b/tests/ChecksClusterVersionTest.php new file mode 100644 index 00000000..a31d23ca --- /dev/null +++ b/tests/ChecksClusterVersionTest.php @@ -0,0 +1,14 @@ +assertFalse($this->cluster->olderThan('1.18.0')); + $this->assertTrue($this->cluster->newerThan('1.18.0')); + $this->assertFalse($this->cluster->newerThan('2.0.0')); + $this->assertTrue($this->cluster->olderThan('2.0.0')); + } +} diff --git a/tests/CronJobTest.php b/tests/CronJobTest.php index 8961c420..1b641688 100644 --- a/tests/CronJobTest.php +++ b/tests/CronJobTest.php @@ -14,10 +14,7 @@ class CronJobTest extends TestCase { public function test_cronjob_build() { - $pi = K8s::container() - ->setName('pi') - ->setImage('public.ecr.aws/docker/library/perl') - ->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']); + $pi = $this->createPerlContainer(); $pod = $this->cluster->pod() ->setName('perl') @@ -37,7 +34,7 @@ public function test_cronjob_build() ->setLabels(['tier' => 'backend']) ->setAnnotations(['perl/annotation' => 'yes']) ->setJobTemplate($job) - ->setSchedule(CronExpression::factory('* * * * *')); + ->setSchedule(new CronExpression('* * * * *')); $this->assertEquals('batch/v1', $cronjob->getApiVersion()); $this->assertEquals('pi', $cronjob->getName()); @@ -51,10 +48,7 @@ public function test_cronjob_build() public function test_cronjob_from_yaml() { - $pi = K8s::container() - ->setName('pi') - ->setImage('public.ecr.aws/docker/library/perl') - ->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']); + $pi = $this->createPerlContainer(); $pod = $this->cluster->pod() ->setName('perl') @@ -110,7 +104,7 @@ public function runCreationTests() ->setLabels(['tier' => 'useless']) ->setAnnotations(['perl/annotation' => 'no']) ->setJobTemplate($job) - ->setSchedule(CronExpression::factory('* * * * *')); + ->setSchedule(new CronExpression('* * * * *')); $this->assertFalse($cronjob->isSynced()); $this->assertFalse($cronjob->exists()); diff --git a/tests/DaemonSetTest.php b/tests/DaemonSetTest.php index 4f99af64..b5381178 100644 --- a/tests/DaemonSetTest.php +++ b/tests/DaemonSetTest.php @@ -3,7 +3,6 @@ namespace RenokiCo\PhpK8s\Test; use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException; -use RenokiCo\PhpK8s\K8s; use RenokiCo\PhpK8s\Kinds\K8sDaemonSet; use RenokiCo\PhpK8s\Kinds\K8sPod; use RenokiCo\PhpK8s\ResourcesList; @@ -12,26 +11,17 @@ class DaemonSetTest extends TestCase { public function test_daemon_set_build() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]); - - $pod = $this->cluster->pod() - ->setName('mysql') - ->setContainers([$mysql]); + $pod = $this->createMariadbPod(); $ds = $this->cluster->daemonSet() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) ->setUpdateStrategy('RollingUpdate') ->setMinReadySeconds(0) ->setTemplate($pod); $this->assertEquals('apps/v1', $ds->getApiVersion()); - $this->assertEquals('mysql', $ds->getName()); + $this->assertEquals('mariadb', $ds->getName()); $this->assertEquals(['tier' => 'backend'], $ds->getLabels()); $this->assertEquals(0, $ds->getMinReadySeconds()); $this->assertEquals($pod->getName(), $ds->getTemplate()->getName()); @@ -41,21 +31,12 @@ public function test_daemon_set_build() public function test_daemon_set_from_yaml() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]); - - $pod = $this->cluster->pod() - ->setName('mysql') - ->setContainers([$mysql]); + $pod = $this->createMariadbPod(); $ds = $this->cluster->fromYamlFile(__DIR__.'/yaml/daemonset.yaml'); $this->assertEquals('apps/v1', $ds->getApiVersion()); - $this->assertEquals('mysql', $ds->getName()); + $this->assertEquals('mariadb', $ds->getName()); $this->assertEquals(['tier' => 'backend'], $ds->getLabels()); $this->assertEquals($pod->getName(), $ds->getTemplate()->getName()); @@ -75,22 +56,16 @@ public function test_daemon_set_api_interaction() public function runCreationTests() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]) - ->addPort(3307, 'TCP', 'mysql-alt') - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); - - $pod = $this->cluster->pod() - ->setName('mysql') - ->setLabels(['tier' => 'backend', 'daemonset-name' => 'mysql']) - ->setContainers([$mysql]); + $pod = $this->createMariadbPod([ + 'labels' => ['tier' => 'backend', 'daemonset-name' => 'mariadb'], + 'container' => [ + 'additionalPort' => 3307, + 'includeEnv' => true, + ], + ]); $ds = $this->cluster->daemonSet() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) ->setSelectors(['matchLabels' => ['tier' => 'backend']]) ->setUpdateStrategy('RollingUpdate') @@ -108,7 +83,7 @@ public function runCreationTests() $this->assertInstanceOf(K8sDaemonSet::class, $ds); $this->assertEquals('apps/v1', $ds->getApiVersion()); - $this->assertEquals('mysql', $ds->getName()); + $this->assertEquals('mariadb', $ds->getName()); $this->assertEquals(['tier' => 'backend'], $ds->getLabels()); $this->assertEquals(0, $ds->getMinReadySeconds()); $this->assertEquals($pod->getName(), $ds->getTemplate()->getName()); @@ -177,14 +152,14 @@ public function runGetAllTests() public function runGetTests() { - $ds = $this->cluster->getDaemonSetByName('mysql'); + $ds = $this->cluster->getDaemonSetByName('mariadb'); $this->assertInstanceOf(K8sDaemonSet::class, $ds); $this->assertTrue($ds->isSynced()); $this->assertEquals('apps/v1', $ds->getApiVersion()); - $this->assertEquals('mysql', $ds->getName()); + $this->assertEquals('mariadb', $ds->getName()); $this->assertEquals(['tier' => 'backend'], $ds->getLabels()); $this->assertInstanceOf(K8sPod::class, $ds->getTemplate()); @@ -192,7 +167,7 @@ public function runGetTests() public function runUpdateTests() { - $ds = $this->cluster->getDaemonSetByName('mysql'); + $ds = $this->cluster->getDaemonSetByName('mariadb'); $this->assertTrue($ds->isSynced()); @@ -201,7 +176,7 @@ public function runUpdateTests() $this->assertTrue($ds->isSynced()); $this->assertEquals('apps/v1', $ds->getApiVersion()); - $this->assertEquals('mysql', $ds->getName()); + $this->assertEquals('mariadb', $ds->getName()); $this->assertEquals(['tier' => 'backend'], $ds->getLabels()); $this->assertInstanceOf(K8sPod::class, $ds->getTemplate()); @@ -209,7 +184,7 @@ public function runUpdateTests() public function runDeletionTests() { - $ds = $this->cluster->getDaemonSetByName('mysql'); + $ds = $this->cluster->getDaemonSetByName('mariadb'); $this->assertTrue($ds->delete()); @@ -225,13 +200,13 @@ public function runDeletionTests() $this->expectException(KubernetesAPIException::class); - $this->cluster->getDaemonSetByName('mysql'); + $this->cluster->getDaemonSetByName('mariadb'); } public function runWatchAllTests() { $watch = $this->cluster->daemonSet()->watchAll(function ($type, $ds) { - if ($ds->getName() === 'mysql') { + if ($ds->getName() === 'mariadb') { return true; } }, ['timeoutSeconds' => 10]); @@ -241,8 +216,8 @@ public function runWatchAllTests() public function runWatchTests() { - $watch = $this->cluster->daemonSet()->watchByName('mysql', function ($type, $ds) { - return $ds->getName() === 'mysql'; + $watch = $this->cluster->daemonSet()->watchByName('mariadb', function ($type, $ds) { + return $ds->getName() === 'mariadb'; }, ['timeoutSeconds' => 10]); $this->assertTrue($watch); diff --git a/tests/DeploymentTest.php b/tests/DeploymentTest.php index 9fd09a9a..1890fb3f 100644 --- a/tests/DeploymentTest.php +++ b/tests/DeploymentTest.php @@ -12,28 +12,23 @@ class DeploymentTest extends TestCase { public function test_deployment_build() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]); + $mariadb = $this->createMariadbContainer(); $pod = $this->cluster->pod() - ->setName('mysql') - ->setContainers([$mysql]); + ->setName('mariadb') + ->setContainers([$mariadb]); $dep = $this->cluster->deployment() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setReplicas(3) ->setTemplate($pod); $this->assertEquals('apps/v1', $dep->getApiVersion()); - $this->assertEquals('mysql', $dep->getName()); + $this->assertEquals('mariadb', $dep->getName()); $this->assertEquals(['tier' => 'backend'], $dep->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $dep->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $dep->getAnnotations()); $this->assertEquals(3, $dep->getReplicas()); $this->assertEquals($pod->getName(), $dep->getTemplate()->getName()); @@ -42,23 +37,18 @@ public function test_deployment_build() public function test_deployment_from_yaml() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]); + $mariadb = $this->createMariadbContainer(); $pod = $this->cluster->pod() - ->setName('mysql') - ->setContainers([$mysql]); + ->setName('mariadb') + ->setContainers([$mariadb]); $dep = $this->cluster->fromYamlFile(__DIR__.'/yaml/deployment.yaml'); $this->assertEquals('apps/v1', $dep->getApiVersion()); - $this->assertEquals('mysql', $dep->getName()); + $this->assertEquals('mariadb', $dep->getName()); $this->assertEquals(['tier' => 'backend'], $dep->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $dep->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $dep->getAnnotations()); $this->assertEquals(3, $dep->getReplicas()); $this->assertEquals($pod->getName(), $dep->getTemplate()->getName()); @@ -80,25 +70,24 @@ public function test_deployment_api_interaction() public function runCreationTests() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]) - ->addPort(3307, 'TCP', 'mysql-alt') - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); - - $pod = $this->cluster->pod() - ->setName('mysql') - ->setLabels(['tier' => 'backend', 'deployment-name' => 'mysql']) - ->setAnnotations(['mysql/annotation' => 'yes']) - ->setContainers([$mysql]); + $mariadb = $this->createMariadbContainer([ + 'includeEnv' => true, + 'additionalPort' => 3307 + ]); + + $pod = $this->createMariadbPod([ + 'labels' => ['tier' => 'backend', 'deployment-name' => 'mariadb'], + 'container' => [ + 'includeEnv' => true, + 'additionalPort' => 3307 + ] + ]) + ->setAnnotations(['mariadb/annotation' => 'yes']); $dep = $this->cluster->deployment() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setSelectors(['matchLabels' => ['tier' => 'backend']]) ->setReplicas(1) ->setUpdateStrategy('RollingUpdate') @@ -116,9 +105,9 @@ public function runCreationTests() $this->assertInstanceOf(K8sDeployment::class, $dep); $this->assertEquals('apps/v1', $dep->getApiVersion()); - $this->assertEquals('mysql', $dep->getName()); + $this->assertEquals('mariadb', $dep->getName()); $this->assertEquals(['tier' => 'backend'], $dep->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $dep->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $dep->getAnnotations()); $this->assertEquals(1, $dep->getReplicas()); $this->assertEquals(0, $dep->getMinReadySeconds()); $this->assertEquals($pod->getName(), $dep->getTemplate()->getName()); @@ -179,16 +168,16 @@ public function runGetAllTests() public function runGetTests() { - $dep = $this->cluster->getDeploymentByName('mysql'); + $dep = $this->cluster->getDeploymentByName('mariadb'); $this->assertInstanceOf(K8sDeployment::class, $dep); $this->assertTrue($dep->isSynced()); $this->assertEquals('apps/v1', $dep->getApiVersion()); - $this->assertEquals('mysql', $dep->getName()); + $this->assertEquals('mariadb', $dep->getName()); $this->assertEquals(['tier' => 'backend'], $dep->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes', 'deployment.kubernetes.io/revision' => '1'], $dep->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes', 'deployment.kubernetes.io/revision' => '1'], $dep->getAnnotations()); $this->assertEquals(1, $dep->getReplicas()); $this->assertInstanceOf(K8sPod::class, $dep->getTemplate()); @@ -196,12 +185,12 @@ public function runGetTests() public function attachPodAutoscaler() { - $dep = $this->cluster->getDeploymentByName('mysql'); + $dep = $this->cluster->getDeploymentByName('mariadb'); $cpuMetric = K8s::metric()->cpu()->averageUtilization(70); $hpa = $this->cluster->horizontalPodAutoscaler() - ->setName('deploy-mysql') + ->setName('deploy-mariadb') ->setResource($dep) ->addMetrics([$cpuMetric]) ->setMetrics([$cpuMetric]) @@ -220,7 +209,7 @@ public function attachPodAutoscaler() public function runUpdateTests() { - $dep = $this->cluster->getDeploymentByName('mysql'); + $dep = $this->cluster->getDeploymentByName('mariadb'); $this->assertTrue($dep->isSynced()); @@ -231,7 +220,7 @@ public function runUpdateTests() $this->assertTrue($dep->isSynced()); $this->assertEquals('apps/v1', $dep->getApiVersion()); - $this->assertEquals('mysql', $dep->getName()); + $this->assertEquals('mariadb', $dep->getName()); $this->assertEquals(['tier' => 'backend'], $dep->getLabels()); $this->assertEquals([], $dep->getAnnotations()); $this->assertEquals(2, $dep->getReplicas()); @@ -241,8 +230,8 @@ public function runUpdateTests() public function runDeletionTests() { - $dep = $this->cluster->getDeploymentByName('mysql'); - $hpa = $this->cluster->getHorizontalPodAutoscalerByName('deploy-mysql'); + $dep = $this->cluster->getDeploymentByName('mariadb'); + $hpa = $this->cluster->getHorizontalPodAutoscalerByName('deploy-mariadb'); $this->assertTrue($dep->delete()); $this->assertTrue($hpa->delete()); @@ -264,14 +253,14 @@ public function runDeletionTests() $this->expectException(KubernetesAPIException::class); - $this->cluster->getDeploymentByName('mysql'); - $this->cluster->getHorizontalPodAutoscalerByName('deploy-mysql'); + $this->cluster->getDeploymentByName('mariadb'); + $this->cluster->getHorizontalPodAutoscalerByName('deploy-mariadb'); } public function runWatchAllTests() { $watch = $this->cluster->deployment()->watchAll(function ($type, $dep) { - if ($dep->getName() === 'mysql') { + if ($dep->getName() === 'mariadb') { return true; } }, ['timeoutSeconds' => 10]); @@ -281,8 +270,8 @@ public function runWatchAllTests() public function runWatchTests() { - $watch = $this->cluster->deployment()->watchByName('mysql', function ($type, $dep) { - return $dep->getName() === 'mysql'; + $watch = $this->cluster->deployment()->watchByName('mariadb', function ($type, $dep) { + return $dep->getName() === 'mariadb'; }, ['timeoutSeconds' => 10]); $this->assertTrue($watch); @@ -290,7 +279,7 @@ public function runWatchTests() public function runScalingTests() { - $dep = $this->cluster->getDeploymentByName('mysql'); + $dep = $this->cluster->getDeploymentByName('mariadb'); $scaler = $dep->scale(2); diff --git a/tests/EventTest.php b/tests/EventTest.php index 0df2bafe..4b24b71d 100644 --- a/tests/EventTest.php +++ b/tests/EventTest.php @@ -3,7 +3,6 @@ namespace RenokiCo\PhpK8s\Test; use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException; -use RenokiCo\PhpK8s\K8s; use RenokiCo\PhpK8s\Kinds\K8sEvent; use RenokiCo\PhpK8s\ResourcesList; @@ -21,24 +20,20 @@ public function test_event_api_interaction() public function runCreationTests() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]) - ->addPort(3307, 'TCP', 'mysql-alt') - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); - - $pod = $this->cluster->pod() - ->setName('mysql') - ->setLabels(['tier' => 'backend', 'deployment-name' => 'mysql']) - ->setContainers([$mysql]); + $pod = $this->createMariadbPod([ + 'name' => 'mariadb', + 'labels' => ['tier' => 'backend', 'deployment-name' => 'mariadb'], + 'container' => [ + 'name' => 'mariadb', + 'additionalPort' => 3307, + 'includeEnv' => true, + ] + ]); $dep = $this->cluster->deployment() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setSelectors(['matchLabels' => ['tier' => 'backend']]) ->setReplicas(1) ->setUpdateStrategy('RollingUpdate') @@ -51,7 +46,7 @@ public function runCreationTests() ->setMessage('This is a test message for events.') ->setReason('SomeReason') ->setType('Normal') - ->setName('mysql-test'); + ->setName('mariadb-test'); $this->assertFalse($event->isSynced()); $this->assertFalse($event->exists()); @@ -86,7 +81,7 @@ public function runGetAllTests() public function runGetTests() { - $event = $this->cluster->getEventByName('mysql-test'); + $event = $this->cluster->getEventByName('mariadb-test'); $this->assertInstanceOf(K8sEvent::class, $event); @@ -95,7 +90,7 @@ public function runGetTests() public function runDeletionTests() { - $event = $this->cluster->getEventByName('mysql-test'); + $event = $this->cluster->getEventByName('mariadb-test'); $this->assertTrue($event->delete()); @@ -111,13 +106,13 @@ public function runDeletionTests() $this->expectException(KubernetesAPIException::class); - $this->cluster->getEventByName('mysql-test'); + $this->cluster->getEventByName('mariadb-test'); } public function runWatchAllTests() { $watch = $this->cluster->event()->watchAll(function ($type, $event) { - if ($event->getName() === 'mysql-test') { + if ($event->getName() === 'mariadb-test') { return true; } }, ['timeoutSeconds' => 10]); @@ -127,8 +122,8 @@ public function runWatchAllTests() public function runWatchTests() { - $watch = $this->cluster->event()->watchByName('mysql-test', function ($type, $event) { - return $event->getName() === 'mysql-test'; + $watch = $this->cluster->event()->watchByName('mariadb-test', function ($type, $event) { + return $event->getName() === 'mariadb-test'; }, ['timeoutSeconds' => 10]); $this->assertTrue($watch); diff --git a/tests/HorizontalPodAutoscalerTest.php b/tests/HorizontalPodAutoscalerTest.php index 44301edb..12813025 100644 --- a/tests/HorizontalPodAutoscalerTest.php +++ b/tests/HorizontalPodAutoscalerTest.php @@ -13,28 +13,23 @@ class HorizontalPodAutoscalerTest extends TestCase { public function test_horizontal_pod_autoscaler_build() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]); + $mariadb = $this->createMariadbContainer(); $pod = $this->cluster->pod() - ->setName('mysql') - ->setContainers([$mysql]); + ->setName('mariadb') + ->setContainers([$mariadb]); $dep = $this->cluster->deployment() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setReplicas(3) ->setTemplate($pod); $cpuMetric = K8s::metric()->cpu()->averageUtilization(70); $hpa = $this->cluster->horizontalPodAutoscaler() - ->setName('mysql-hpa') + ->setName('mariadb-hpa') ->setLabels(['tier' => 'backend']) ->setResource($dep) ->addMetrics([$cpuMetric]) @@ -43,7 +38,7 @@ public function test_horizontal_pod_autoscaler_build() ->max(10); $this->assertEquals('autoscaling/v2', $hpa->getApiVersion()); - $this->assertEquals('mysql-hpa', $hpa->getName()); + $this->assertEquals('mariadb-hpa', $hpa->getName()); $this->assertEquals(['tier' => 'backend'], $hpa->getLabels()); $this->assertEquals([$cpuMetric->toArray()], $hpa->getMetrics()); $this->assertEquals(1, $hpa->getMinReplicas()); @@ -52,21 +47,16 @@ public function test_horizontal_pod_autoscaler_build() public function test_horizontal_pod_autoscaler_from_yaml() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]); + $mariadb = $this->createMariadbContainer(); $pod = $this->cluster->pod() - ->setName('mysql') - ->setContainers([$mysql]); + ->setName('mariadb') + ->setContainers([$mariadb]); $dep = $this->cluster->deployment() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setReplicas(3) ->setTemplate($pod); @@ -75,7 +65,7 @@ public function test_horizontal_pod_autoscaler_from_yaml() $hpa = $this->cluster->fromYamlFile(__DIR__.'/yaml/hpa.yaml'); $this->assertEquals('autoscaling/v2', $hpa->getApiVersion()); - $this->assertEquals('mysql-hpa', $hpa->getName()); + $this->assertEquals('mariadb-hpa', $hpa->getName()); $this->assertEquals(['tier' => 'backend'], $hpa->getLabels()); $this->assertEquals([$cpuMetric->toArray()], $hpa->getMetrics()); $this->assertEquals(1, $hpa->getMinReplicas()); @@ -95,24 +85,20 @@ public function test_horizontal_pod_autoscaler_api_interaction() public function runCreationTests() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]) - ->addPort(3307, 'TCP', 'mysql-alt') - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); + $mariadb = $this->createMariadbContainer([ + 'includeEnv' => true, + 'additionalPort' => 3307, + ]); $pod = $this->cluster->pod() - ->setName('mysql') - ->setLabels(['tier' => 'backend', 'deployment-name' => 'mysql']) - ->setContainers([$mysql]); + ->setName('mariadb') + ->setLabels(['tier' => 'backend', 'deployment-name' => 'mariadb']) + ->setContainers([$mariadb]); $dep = $this->cluster->deployment() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setSelectors(['matchLabels' => ['tier' => 'backend']]) ->setReplicas(1) ->setUpdateStrategy('RollingUpdate') @@ -122,7 +108,7 @@ public function runCreationTests() $cpuMetric = K8s::metric()->cpu()->averageUtilization(70); $hpa = $this->cluster->horizontalPodAutoscaler() - ->setName('mysql-hpa') + ->setName('mariadb-hpa') ->setLabels(['tier' => 'backend']) ->setResource($dep) ->addMetrics([$cpuMetric]) @@ -142,7 +128,7 @@ public function runCreationTests() $this->assertInstanceOf(K8sHorizontalPodAutoscaler::class, $hpa); $this->assertEquals('autoscaling/v2', $hpa->getApiVersion()); - $this->assertEquals('mysql-hpa', $hpa->getName()); + $this->assertEquals('mariadb-hpa', $hpa->getName()); $this->assertEquals(['tier' => 'backend'], $hpa->getLabels()); $this->assertEquals([$cpuMetric->toArray()], $hpa->getMetrics()); $this->assertEquals(1, $hpa->getMinReplicas()); @@ -195,7 +181,7 @@ public function runGetAllTests() public function runGetTests() { - $hpa = $this->cluster->getHorizontalPodAutoscalerByName('mysql-hpa'); + $hpa = $this->cluster->getHorizontalPodAutoscalerByName('mariadb-hpa'); $this->assertInstanceOf(K8sHorizontalPodAutoscaler::class, $hpa); @@ -204,7 +190,7 @@ public function runGetTests() $cpuMetric = K8s::metric()->cpu()->averageUtilization(70); $this->assertEquals('autoscaling/v2', $hpa->getApiVersion()); - $this->assertEquals('mysql-hpa', $hpa->getName()); + $this->assertEquals('mariadb-hpa', $hpa->getName()); $this->assertEquals(['tier' => 'backend'], $hpa->getLabels()); $this->assertEquals([$cpuMetric->toArray()], $hpa->getMetrics()); $this->assertEquals(1, $hpa->getMinReplicas()); @@ -213,7 +199,7 @@ public function runGetTests() public function runUpdateTests() { - $hpa = $this->cluster->getHorizontalPodAutoscalerByName('mysql-hpa'); + $hpa = $this->cluster->getHorizontalPodAutoscalerByName('mariadb-hpa'); $this->assertTrue($hpa->isSynced()); @@ -232,7 +218,7 @@ public function runUpdateTests() $cpuMetric = K8s::metric()->cpu()->averageUtilization(70); $this->assertEquals('autoscaling/v2', $hpa->getApiVersion()); - $this->assertEquals('mysql-hpa', $hpa->getName()); + $this->assertEquals('mariadb-hpa', $hpa->getName()); $this->assertEquals(['tier' => 'backend'], $hpa->getLabels()); $this->assertEquals([$cpuMetric->toArray()], $hpa->getMetrics()); $this->assertEquals(1, $hpa->getMinReplicas()); @@ -241,7 +227,7 @@ public function runUpdateTests() public function runDeletionTests() { - $hpa = $this->cluster->getHorizontalPodAutoscalerByName('mysql-hpa'); + $hpa = $this->cluster->getHorizontalPodAutoscalerByName('mariadb-hpa'); $this->assertTrue($hpa->delete()); @@ -252,13 +238,13 @@ public function runDeletionTests() $this->expectException(KubernetesAPIException::class); - $this->cluster->getHorizontalPodAutoscalerByName('mysql-hpa'); + $this->cluster->getHorizontalPodAutoscalerByName('mariadb-hpa'); } public function runWatchAllTests() { $watch = $this->cluster->horizontalPodAutoscaler()->watchAll(function ($type, $hpa) { - if ($hpa->getName() === 'mysql-hpa') { + if ($hpa->getName() === 'mariadb-hpa') { return true; } }, ['timeoutSeconds' => 10]); @@ -268,8 +254,8 @@ public function runWatchAllTests() public function runWatchTests() { - $watch = $this->cluster->horizontalPodAutoscaler()->watchByName('mysql-hpa', function ($type, $hpa) { - return $hpa->getName() === 'mysql-hpa'; + $watch = $this->cluster->horizontalPodAutoscaler()->watchByName('mariadb-hpa', function ($type, $hpa) { + return $hpa->getName() === 'mariadb-hpa'; }, ['timeoutSeconds' => 10]); $this->assertTrue($watch); diff --git a/tests/JobTest.php b/tests/JobTest.php index 72e7664c..8ed7f3a0 100644 --- a/tests/JobTest.php +++ b/tests/JobTest.php @@ -3,7 +3,6 @@ namespace RenokiCo\PhpK8s\Test; use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException; -use RenokiCo\PhpK8s\K8s; use RenokiCo\PhpK8s\Kinds\K8sJob; use RenokiCo\PhpK8s\Kinds\K8sPod; use RenokiCo\PhpK8s\ResourcesList; @@ -12,16 +11,9 @@ class JobTest extends TestCase { public function test_job_build() { - $pi = K8s::container() - ->setName('pi') - ->setImage('public.ecr.aws/docker/library/perl') - ->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']); - - $pod = $this->cluster->pod() - ->setName('perl') - ->setContainers([$pi]) - ->restartOnFailure() - ->neverRestart(); + $pod = $this->createPerlPod([ + 'restartPolicy' => 'Never', + ]); $job = $this->cluster->job() ->setName('pi') @@ -42,16 +34,9 @@ public function test_job_build() public function test_job_from_yaml() { - $pi = K8s::container() - ->setName('pi') - ->setImage('public.ecr.aws/docker/library/perl') - ->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']); - - $pod = $this->cluster->pod() - ->setName('perl') - ->setContainers([$pi]) - ->restartOnFailure() - ->neverRestart(); + $pod = $this->createPerlPod([ + 'restartPolicy' => 'Never', + ]); $job = $this->cluster->fromYamlFile(__DIR__.'/yaml/job.yaml'); @@ -78,16 +63,10 @@ public function test_job_api_interaction() public function runCreationTests() { - $pi = K8s::container() - ->setName('pi') - ->setImage('public.ecr.aws/docker/library/perl', '5.36') - ->setCommand(['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']); - - $pod = $this->cluster->pod() - ->setName('perl') - ->setLabels(['tier' => 'compute']) - ->setContainers([$pi]) - ->neverRestart(); + $pod = $this->createPerlPod([ + 'container' => ['tag' => '5.36'], + 'restartPolicy' => 'Never', + ]); $job = $this->cluster->job() ->setName('pi') diff --git a/tests/KubeConfigTest.php b/tests/KubeConfigTest.php index 82a657ec..3f7dd7f9 100644 --- a/tests/KubeConfigTest.php +++ b/tests/KubeConfigTest.php @@ -90,7 +90,6 @@ public function test_cluster_can_get_correct_config_for_token_socket_connection( $cluster = KubernetesCluster::fromUrl('http://127.0.0.1:8080')->loadTokenFromFile(__DIR__.'/cluster/token.txt'); $reflectionMethod = new \ReflectionMethod($cluster, 'buildStreamContextOptions'); - $reflectionMethod->setAccessible(true); $options = $reflectionMethod->invoke($cluster); @@ -110,7 +109,6 @@ public function test_cluster_can_get_correct_config_for_user_pass_socket_connect $cluster = KubernetesCluster::fromUrl('http://127.0.0.1:8080')->httpAuthentication('some-user', 'some-password'); $reflectionMethod = new \ReflectionMethod($cluster, 'buildStreamContextOptions'); - $reflectionMethod->setAccessible(true); $options = $reflectionMethod->invoke($cluster); @@ -130,7 +128,6 @@ public function test_cluster_can_get_correct_config_for_ssl_socket_connection() $cluster = KubernetesCluster::fromKubeConfigYamlFile(__DIR__.'/cluster/kubeconfig.yaml', 'minikube-2'); $reflectionMethod = new \ReflectionMethod($cluster, 'buildStreamContextOptions'); - $reflectionMethod->setAccessible(true); $options = $reflectionMethod->invoke($cluster); @@ -220,7 +217,7 @@ public function test_from_environment_variable(string $context = null, string $e $this->assertSame("https://{$expectedDomain}:8443/?", $cluster->getCallableUrl('/', [])); } - public function environmentVariableContextProvider(): iterable + public static function environmentVariableContextProvider(): iterable { yield [null, 'minikube']; yield ['minikube-2', 'minikube-2']; diff --git a/tests/PodDisruptionBudgetTest.php b/tests/PodDisruptionBudgetTest.php index a6dcc67e..aec94e75 100644 --- a/tests/PodDisruptionBudgetTest.php +++ b/tests/PodDisruptionBudgetTest.php @@ -3,7 +3,6 @@ namespace RenokiCo\PhpK8s\Test; use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException; -use RenokiCo\PhpK8s\K8s; use RenokiCo\PhpK8s\Kinds\K8sDeployment; use RenokiCo\PhpK8s\Kinds\K8sPodDisruptionBudget; use RenokiCo\PhpK8s\ResourcesList; @@ -13,18 +12,18 @@ class PodDisruptionBudgetTest extends TestCase public function test_pod_disruption_budget_build() { $pdb = $this->cluster->podDisruptionBudget() - ->setName('mysql-pdb') + ->setName('mariadb-pdb') ->setSelectors(['matchLabels' => ['tier' => 'backend']]) ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setMinAvailable(1) ->setMaxUnavailable('25%'); $this->assertEquals('policy/v1', $pdb->getApiVersion()); - $this->assertEquals('mysql-pdb', $pdb->getName()); + $this->assertEquals('mariadb-pdb', $pdb->getName()); $this->assertEquals(['matchLabels' => ['tier' => 'backend']], $pdb->getSelectors()); $this->assertEquals(['tier' => 'backend'], $pdb->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $pdb->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $pdb->getAnnotations()); $this->assertEquals('25%', $pdb->getMaxUnavailable()); $this->assertEquals(null, $pdb->getMinAvailable()); } @@ -34,18 +33,18 @@ public function test_pod_disruption_budget_from_yaml() [$pdb1, $pdb2] = $this->cluster->fromYamlFile(__DIR__.'/yaml/pdb.yaml'); $this->assertEquals('policy/v1', $pdb1->getApiVersion()); - $this->assertEquals('mysql-pdb', $pdb1->getName()); + $this->assertEquals('mariadb-pdb', $pdb1->getName()); $this->assertEquals(['matchLabels' => ['tier' => 'backend']], $pdb1->getSelectors()); $this->assertEquals(['tier' => 'backend'], $pdb1->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $pdb1->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $pdb1->getAnnotations()); $this->assertEquals('25%', $pdb1->getMaxUnavailable()); $this->assertEquals(null, $pdb1->getMinAvailable()); $this->assertEquals('policy/v1', $pdb2->getApiVersion()); - $this->assertEquals('mysql-pdb', $pdb2->getName()); + $this->assertEquals('mariadb-pdb', $pdb2->getName()); $this->assertEquals(['matchLabels' => ['tier' => 'backend']], $pdb2->getSelectors()); $this->assertEquals(['tier' => 'backend'], $pdb2->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $pdb2->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $pdb2->getAnnotations()); $this->assertEquals(null, $pdb2->getMaxUnavailable()); $this->assertEquals('25%', $pdb2->getMinAvailable()); } @@ -63,24 +62,20 @@ public function test_pod_disruption_budget_api_interaction() public function runCreationTests() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]) - ->addPort(3307, 'TCP', 'mysql-alt') - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); + $mariadb = $this->createMariadbContainer([ + 'env' => ['MARIADB_ROOT_PASSWORD' => 'test'], + 'additionalPort' => 3307, + ]); $pod = $this->cluster->pod() - ->setName('mysql') - ->setLabels(['tier' => 'backend', 'deployment-name' => 'mysql']) - ->setContainers([$mysql]); + ->setName('mariadb') + ->setLabels(['tier' => 'backend', 'deployment-name' => 'mariadb']) + ->setContainers([$mariadb]); $dep = $this->cluster->deployment() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setSelectors(['matchLabels' => ['tier' => 'backend']]) ->setReplicas(1) ->setUpdateStrategy('RollingUpdate') @@ -88,10 +83,10 @@ public function runCreationTests() ->setTemplate($pod); $pdb = $this->cluster->podDisruptionBudget() - ->setName('mysql-pdb') + ->setName('mariadb-pdb') ->setSelectors(['matchLabels' => ['tier' => 'backend']]) ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setMinAvailable(1) ->setMaxUnavailable('25%'); @@ -108,10 +103,10 @@ public function runCreationTests() $this->assertInstanceOf(K8sPodDisruptionBudget::class, $pdb); $this->assertEquals('policy/v1', $pdb->getApiVersion()); - $this->assertEquals('mysql-pdb', $pdb->getName()); + $this->assertEquals('mariadb-pdb', $pdb->getName()); $this->assertEquals(['matchLabels' => ['tier' => 'backend']], $pdb->getSelectors()); $this->assertEquals(['tier' => 'backend'], $pdb->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $pdb->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $pdb->getAnnotations()); $this->assertEquals('25%', $pdb->getMaxUnavailable()); $this->assertEquals(null, $pdb->getMinAvailable()); @@ -136,17 +131,17 @@ public function runGetAllTests() public function runGetTests() { - $pdb = $this->cluster->getPodDisruptionBudgetByName('mysql-pdb'); + $pdb = $this->cluster->getPodDisruptionBudgetByName('mariadb-pdb'); $this->assertInstanceOf(K8sPodDisruptionBudget::class, $pdb); $this->assertTrue($pdb->isSynced()); $this->assertEquals('policy/v1', $pdb->getApiVersion()); - $this->assertEquals('mysql-pdb', $pdb->getName()); + $this->assertEquals('mariadb-pdb', $pdb->getName()); $this->assertEquals(['matchLabels' => ['tier' => 'backend']], $pdb->getSelectors()); $this->assertEquals(['tier' => 'backend'], $pdb->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $pdb->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $pdb->getAnnotations()); $this->assertEquals('25%', $pdb->getMaxUnavailable()); $this->assertEquals(null, $pdb->getMinAvailable()); } @@ -156,7 +151,7 @@ public function runUpdateTests() $backoff = 0; do { try { - $pdb = $this->cluster->getPodDisruptionBudgetByName('mysql-pdb')->setMinAvailable('25%')->createOrUpdate(); + $pdb = $this->cluster->getPodDisruptionBudgetByName('mariadb-pdb')->setMinAvailable('25%')->createOrUpdate(); } catch (KubernetesAPIException $e) { if ($e->getCode() == 409) { sleep(2 * $backoff); @@ -173,17 +168,17 @@ public function runUpdateTests() $this->assertTrue($pdb->isSynced()); $this->assertEquals('policy/v1', $pdb->getApiVersion()); - $this->assertEquals('mysql-pdb', $pdb->getName()); + $this->assertEquals('mariadb-pdb', $pdb->getName()); $this->assertEquals(['matchLabels' => ['tier' => 'backend']], $pdb->getSelectors()); $this->assertEquals(['tier' => 'backend'], $pdb->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $pdb->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $pdb->getAnnotations()); $this->assertEquals(null, $pdb->getMaxUnavailable()); $this->assertEquals('25%', $pdb->getMinAvailable()); } public function runDeletionTests() { - $pdb = $this->cluster->getPodDisruptionBudgetByName('mysql-pdb'); + $pdb = $this->cluster->getPodDisruptionBudgetByName('mariadb-pdb'); $this->assertTrue($pdb->delete()); @@ -194,13 +189,13 @@ public function runDeletionTests() $this->expectException(KubernetesAPIException::class); - $this->cluster->getPodDisruptionBudgetByName('mysql-pdb'); + $this->cluster->getPodDisruptionBudgetByName('mariadb-pdb'); } public function runWatchAllTests() { $watch = $this->cluster->podDisruptionBudget()->watchAll(function ($type, $pdb) { - if ($pdb->getName() === 'mysql-pdb') { + if ($pdb->getName() === 'mariadb-pdb') { return true; } }, ['timeoutSeconds' => 10]); @@ -210,8 +205,8 @@ public function runWatchAllTests() public function runWatchTests() { - $watch = $this->cluster->podDisruptionBudget()->watchByName('mysql-pdb', function ($type, $pdb) { - return $pdb->getName() === 'mysql-pdb'; + $watch = $this->cluster->podDisruptionBudget()->watchByName('mariadb-pdb', function ($type, $pdb) { + return $pdb->getName() === 'mariadb-pdb'; }, ['timeoutSeconds' => 10]); $this->assertTrue($watch); diff --git a/tests/PodTest.php b/tests/PodTest.php index 6cbd737c..706c8988 100644 --- a/tests/PodTest.php +++ b/tests/PodTest.php @@ -5,7 +5,6 @@ use Illuminate\Support\Str; use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException; use RenokiCo\PhpK8s\Instances\Container; -use RenokiCo\PhpK8s\K8s; use RenokiCo\PhpK8s\Kinds\K8sPod; use RenokiCo\PhpK8s\ResourcesList; @@ -13,42 +12,35 @@ class PodTest extends TestCase { public function test_pod_build() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]) - ->addPort(3307, 'TCP', 'mysql-alt') - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); - - $busybox = K8s::container() - ->setName('busybox') - ->setImage('public.ecr.aws/docker/library/busybox') - ->setCommand(['/bin/sh']); + $mariadb = $this->createMariadbContainer([ + 'additionalPort' => 3307, + 'includeEnv' => true, + ]); + + $busybox = $this->createBusyboxContainer(); $pod = $this->cluster->pod() - ->setName('mysql') + ->setName('mariadb') ->setOrUpdateLabels(['tier' => 'test']) ->setOrUpdateLabels(['tier' => 'backend', 'type' => 'test']) - ->setOrUpdateAnnotations(['mysql/annotation' => 'no']) - ->setOrUpdateAnnotations(['mysql/annotation' => 'yes', 'mongodb/annotation' => 'no']) + ->setOrUpdateAnnotations(['mariadb/annotation' => 'no']) + ->setOrUpdateAnnotations(['mariadb/annotation' => 'yes', 'mongodb/annotation' => 'no']) ->addPulledSecrets(['secret1', 'secret2']) ->setInitContainers([$busybox]) - ->setContainers([$mysql]); + ->setContainers([$mariadb]); $this->assertEquals('v1', $pod->getApiVersion()); - $this->assertEquals('mysql', $pod->getName()); + $this->assertEquals('mariadb', $pod->getName()); $this->assertEquals(['tier' => 'backend', 'type' => 'test'], $pod->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes', 'mongodb/annotation' => 'no'], $pod->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes', 'mongodb/annotation' => 'no'], $pod->getAnnotations()); $this->assertEquals([['name' => 'secret1'], ['name' => 'secret2']], $pod->getPulledSecrets()); $this->assertEquals([$busybox->toArray()], $pod->getInitContainers(false)); - $this->assertEquals([$mysql->toArray()], $pod->getContainers(false)); + $this->assertEquals([$mariadb->toArray()], $pod->getContainers(false)); $this->assertEquals('backend', $pod->getLabel('tier')); $this->assertNull($pod->getLabel('inexistentLabel')); - $this->assertEquals('yes', $pod->getAnnotation('mysql/annotation')); + $this->assertEquals('yes', $pod->getAnnotation('mariadb/annotation')); $this->assertEquals('no', $pod->getAnnotation('mongodb/annotation')); $this->assertNull($pod->getAnnotation('inexistentAnnot')); @@ -63,28 +55,21 @@ public function test_pod_build() public function test_pod_from_yaml() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]) - ->addPort(3307, 'TCP', 'mysql-alt') - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); - - $busybox = K8s::container() - ->setName('busybox') - ->setImage('public.ecr.aws/docker/library/busybox') - ->setCommand(['/bin/sh']); + $mariadb = $this->createMariadbContainer([ + 'additionalPort' => 3307, + 'includeEnv' => true, + ]); + + $busybox = $this->createBusyboxContainer(); $pod = $this->cluster->fromYamlFile(__DIR__.'/yaml/pod.yaml'); $this->assertEquals('v1', $pod->getApiVersion()); - $this->assertEquals('mysql', $pod->getName()); + $this->assertEquals('mariadb', $pod->getName()); $this->assertEquals(['tier' => 'backend'], $pod->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $pod->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $pod->getAnnotations()); $this->assertEquals([$busybox->toArray()], $pod->getInitContainers(false)); - $this->assertEquals([$mysql->toArray()], $pod->getContainers(false)); + $this->assertEquals([$mariadb->toArray()], $pod->getContainers(false)); foreach ($pod->getInitContainers() as $container) { $this->assertInstanceOf(Container::class, $container); @@ -110,10 +95,10 @@ public function test_pod_api_interaction() public function test_pod_exec() { - $busybox = K8s::container() - ->setName('busybox-exec') - ->setImage('public.ecr.aws/docker/library/busybox') - ->setCommand(['/bin/sh', '-c', 'sleep 7200']); + $busybox = $this->createBusyboxContainer([ + 'name' => 'busybox-exec', + 'command' => ['/bin/sh', '-c', 'sleep 7200'], + ]); $pod = $this->cluster->pod() ->setName('busybox-exec') @@ -137,17 +122,14 @@ public function test_pod_exec() public function test_pod_attach() { - $mysql = K8s::container() - ->setName('mysql-attach') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]) - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); + $mariadb = $this->createMariadbContainer([ + 'name' => 'mariadb-attach', + 'includeEnv' => true, + ]); $pod = $this->cluster->pod() - ->setName('mysql-attach') - ->setContainers([$mysql]) + ->setName('mariadb-attach') + ->setContainers([$mariadb]) ->createOrUpdate(); while (! $pod->isRunning()) { @@ -168,27 +150,20 @@ public function test_pod_attach() public function runCreationTests() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]) - ->addPort(3307, 'TCP', 'mysql-alt') - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); - - $busybox = K8s::container() - ->setName('busybox') - ->setImage('public.ecr.aws/docker/library/busybox') - ->setCommand(['/bin/sh']); + $mariadb = $this->createMariadbContainer([ + 'additionalPort' => 3307, + 'includeEnv' => true, + ]); + + $busybox = $this->createBusyboxContainer(); $pod = $this->cluster->pod() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->addPulledSecrets(['secret1', 'secret2']) ->setInitContainers([$busybox]) - ->setContainers([$mysql]); + ->setContainers([$mariadb]); $this->assertFalse($pod->isSynced()); $this->assertFalse($pod->exists()); @@ -201,9 +176,9 @@ public function runCreationTests() $this->assertInstanceOf(K8sPod::class, $pod); $this->assertEquals('v1', $pod->getApiVersion()); - $this->assertEquals('mysql', $pod->getName()); + $this->assertEquals('mariadb', $pod->getName()); $this->assertEquals(['tier' => 'backend'], $pod->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $pod->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $pod->getAnnotations()); while (! $pod->isRunning()) { dump("Waiting for pod {$pod->getName()} to be up and running..."); @@ -214,7 +189,7 @@ public function runCreationTests() $pod->refresh(); $this->assertStringEndsWith('busybox:latest', $pod->getInitContainer('busybox')->getImage()); - $this->assertStringEndsWith('mysql:5.7', $pod->getContainer('mysql')->getImage()); + $this->assertStringEndsWith('mariadb:11.8', $pod->getContainer('mariadb')->getImage()); $this->assertTrue($pod->containersAreReady()); $this->assertTrue($pod->initContainersAreReady()); @@ -243,21 +218,21 @@ public function runGetAllTests() public function runGetTests() { - $pod = $this->cluster->getPodByName('mysql'); + $pod = $this->cluster->getPodByName('mariadb'); $this->assertInstanceOf(K8sPod::class, $pod); $this->assertTrue($pod->isSynced()); $this->assertEquals('v1', $pod->getApiVersion()); - $this->assertEquals('mysql', $pod->getName()); + $this->assertEquals('mariadb', $pod->getName()); $this->assertEquals(['tier' => 'backend'], $pod->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $pod->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $pod->getAnnotations()); } public function runUpdateTests() { - $pod = $this->cluster->getPodByName('mysql'); + $pod = $this->cluster->getPodByName('mariadb'); $this->assertTrue($pod->isSynced()); @@ -269,14 +244,14 @@ public function runUpdateTests() $this->assertTrue($pod->isSynced()); $this->assertEquals('v1', $pod->getApiVersion()); - $this->assertEquals('mysql', $pod->getName()); + $this->assertEquals('mariadb', $pod->getName()); $this->assertEquals([], $pod->getLabels()); $this->assertEquals([], $pod->getAnnotations()); } public function runDeletionTests() { - $pod = $this->cluster->getPodByName('mysql'); + $pod = $this->cluster->getPodByName('mariadb'); $this->assertTrue($pod->delete()); @@ -287,13 +262,13 @@ public function runDeletionTests() $this->expectException(KubernetesAPIException::class); - $this->cluster->getPodByName('mysql'); + $this->cluster->getPodByName('mariadb'); } public function runWatchAllTests() { $watch = $this->cluster->pod()->watchAll(function ($type, $pod) { - if ($pod->getName() === 'mysql') { + if ($pod->getName() === 'mariadb') { return true; } }, ['timeoutSeconds' => 10]); @@ -303,8 +278,8 @@ public function runWatchAllTests() public function runWatchTests() { - $watch = $this->cluster->pod()->watchByName('mysql', function ($type, $pod) { - return $pod->getName() === 'mysql'; + $watch = $this->cluster->pod()->watchByName('mariadb', function ($type, $pod) { + return $pod->getName() === 'mariadb'; }, ['timeoutSeconds' => 10]); $this->assertTrue($watch); @@ -312,7 +287,7 @@ public function runWatchTests() public function runWatchLogsTests() { - $this->cluster->pod()->watchContainerLogsByName('mysql', 'mysql', function ($data) { + $this->cluster->pod()->watchContainerLogsByName('mariadb', 'mariadb', function ($data) { // Debugging data to CI. :D dump($data); @@ -324,7 +299,7 @@ public function runWatchLogsTests() public function runGetLogsTests() { - $logs = $this->cluster->pod()->containerLogsByName('mysql', 'mysql'); + $logs = $this->cluster->pod()->containerLogsByName('mariadb', 'mariadb'); // Debugging data to CI. :D dump($logs); diff --git a/tests/StatefulSetTest.php b/tests/StatefulSetTest.php index ea0e213e..e4941a9e 100644 --- a/tests/StatefulSetTest.php +++ b/tests/StatefulSetTest.php @@ -13,19 +13,10 @@ class StatefulSetTest extends TestCase { public function test_stateful_set_build() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]); - - $pod = $this->cluster->pod() - ->setName('mysql') - ->setContainers([$mysql]); + $pod = $this->createMariadbPod(); $svc = $this->cluster->service() - ->setName('mysql') + ->setName('mariadb') ->setPorts([ ['protocol' => 'TCP', 'port' => 3306, 'targetPort' => 3306], ]); @@ -33,15 +24,15 @@ public function test_stateful_set_build() $standard = $this->cluster->getStorageClassByName('standard'); $pvc = $this->cluster->persistentVolumeClaim() - ->setName('mysql-pvc') + ->setName('mariadb-pvc') ->setCapacity(1, 'Gi') ->setAccessModes(['ReadWriteOnce']) ->setStorageClass($standard); $sts = $this->cluster->statefulSet() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setReplicas(3) ->setService($svc) ->setTemplate($pod) @@ -49,9 +40,9 @@ public function test_stateful_set_build() ->setVolumeClaims([$pvc]); $this->assertEquals('apps/v1', $sts->getApiVersion()); - $this->assertEquals('mysql', $sts->getName()); + $this->assertEquals('mariadb', $sts->getName()); $this->assertEquals(['tier' => 'backend'], $sts->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $sts->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $sts->getAnnotations()); $this->assertEquals(3, $sts->getReplicas()); $this->assertEquals($svc->getName(), $sts->getService()); $this->assertEquals($pod->getName(), $sts->getTemplate()->getName()); @@ -63,19 +54,10 @@ public function test_stateful_set_build() public function test_stateful_set_from_yaml() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]); - - $pod = $this->cluster->pod() - ->setName('mysql') - ->setContainers([$mysql]); + $pod = $this->createMariadbPod(); $svc = $this->cluster->service() - ->setName('mysql') + ->setName('mariadb') ->setPorts([ ['protocol' => 'TCP', 'port' => 3306, 'targetPort' => 3306], ]); @@ -83,7 +65,7 @@ public function test_stateful_set_from_yaml() $standard = $this->cluster->getStorageClassByName('standard'); $pvc = $this->cluster->persistentVolumeClaim() - ->setName('mysql-pvc') + ->setName('mariadb-pvc') ->setCapacity(1, 'Gi') ->setAccessModes(['ReadWriteOnce']) ->setStorageClass($standard); @@ -91,9 +73,9 @@ public function test_stateful_set_from_yaml() $sts = $this->cluster->fromYamlFile(__DIR__.'/yaml/statefulset.yaml'); $this->assertEquals('apps/v1', $sts->getApiVersion()); - $this->assertEquals('mysql', $sts->getName()); + $this->assertEquals('mariadb', $sts->getName()); $this->assertEquals(['tier' => 'backend'], $sts->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $sts->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $sts->getAnnotations()); $this->assertEquals(3, $sts->getReplicas()); $this->assertEquals($svc->getName(), $sts->getService()); $this->assertEquals($pod->getName(), $sts->getTemplate()->getName()); @@ -118,23 +100,17 @@ public function test_stateful_set_api_interaction() public function runCreationTests() { - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') - ->setPorts([ - ['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306], - ]) - ->addPort(3307, 'TCP', 'mysql-alt') - ->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']); - - $pod = $this->cluster->pod() - ->setName('mysql') - ->setLabels(['tier' => 'backend', 'statefulset-name' => 'mysql']) - ->setAnnotations(['mysql/annotation' => 'yes']) - ->setContainers([$mysql]); + $pod = $this->createMariadbPod([ + 'labels' => ['tier' => 'backend', 'statefulset-name' => 'mariadb'], + 'container' => [ + 'includeEnv' => true, + 'additionalPort' => 3307, + ], + ]) + ->setAnnotations(['mariadb/annotation' => 'yes']); $svc = $this->cluster->service() - ->setName('mysql') + ->setName('mariadb') ->setPorts([ ['protocol' => 'TCP', 'port' => 3306, 'targetPort' => 3306], ]) @@ -143,15 +119,15 @@ public function runCreationTests() $standard = $this->cluster->getStorageClassByName('standard'); $pvc = $this->cluster->persistentVolumeClaim() - ->setName('mysql-pvc') + ->setName('mariadb-pvc') ->setCapacity(1, 'Gi') ->setAccessModes(['ReadWriteOnce']) ->setStorageClass($standard); $sts = $this->cluster->statefulSet() - ->setName('mysql') + ->setName('mariadb') ->setLabels(['tier' => 'backend']) - ->setAnnotations(['mysql/annotation' => 'yes']) + ->setAnnotations(['mariadb/annotation' => 'yes']) ->setSelectors(['matchLabels' => ['tier' => 'backend']]) ->setReplicas(1) ->setService($svc) @@ -169,9 +145,9 @@ public function runCreationTests() $this->assertInstanceOf(K8sStatefulSet::class, $sts); $this->assertEquals('apps/v1', $sts->getApiVersion()); - $this->assertEquals('mysql', $sts->getName()); + $this->assertEquals('mariadb', $sts->getName()); $this->assertEquals(['tier' => 'backend'], $sts->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $sts->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $sts->getAnnotations()); $this->assertEquals(1, $sts->getReplicas()); $this->assertEquals($svc->getName(), $sts->getService()); $this->assertEquals($pod->getName(), $sts->getTemplate()->getName()); @@ -233,16 +209,16 @@ public function runGetAllTests() public function runGetTests() { - $sts = $this->cluster->getStatefulSetByName('mysql'); + $sts = $this->cluster->getStatefulSetByName('mariadb'); $this->assertInstanceOf(K8sStatefulSet::class, $sts); $this->assertTrue($sts->isSynced()); $this->assertEquals('apps/v1', $sts->getApiVersion()); - $this->assertEquals('mysql', $sts->getName()); + $this->assertEquals('mariadb', $sts->getName()); $this->assertEquals(['tier' => 'backend'], $sts->getLabels()); - $this->assertEquals(['mysql/annotation' => 'yes'], $sts->getAnnotations()); + $this->assertEquals(['mariadb/annotation' => 'yes'], $sts->getAnnotations()); $this->assertEquals(1, $sts->getReplicas()); $this->assertInstanceOf(K8sPod::class, $sts->getTemplate()); @@ -251,7 +227,7 @@ public function runGetTests() public function attachPodAutoscaler() { - $sts = $this->cluster->getStatefulSetByName('mysql'); + $sts = $this->cluster->getStatefulSetByName('mariadb'); $cpuMetric = K8s::metric()->cpu()->averageUtilization(70); @@ -261,7 +237,7 @@ public function attachPodAutoscaler() ->averageValue('1k'); $hpa = $this->cluster->horizontalPodAutoscaler() - ->setName('sts-mysql') + ->setName('sts-mariadb') ->setResource($sts) ->addMetrics([$cpuMetric, $svcMetric]) ->min(1) @@ -279,7 +255,7 @@ public function attachPodAutoscaler() public function runUpdateTests() { - $sts = $this->cluster->getStatefulSetByName('mysql'); + $sts = $this->cluster->getStatefulSetByName('mariadb'); $this->assertTrue($sts->isSynced()); @@ -290,7 +266,7 @@ public function runUpdateTests() $this->assertTrue($sts->isSynced()); $this->assertEquals('apps/v1', $sts->getApiVersion()); - $this->assertEquals('mysql', $sts->getName()); + $this->assertEquals('mariadb', $sts->getName()); $this->assertEquals(['tier' => 'backend'], $sts->getLabels()); $this->assertEquals([], $sts->getAnnotations()); $this->assertEquals(2, $sts->getReplicas()); @@ -301,8 +277,8 @@ public function runUpdateTests() public function runDeletionTests() { - $sts = $this->cluster->getStatefulSetByName('mysql'); - $hpa = $this->cluster->getHorizontalPodAutoscalerByName('sts-mysql'); + $sts = $this->cluster->getStatefulSetByName('mariadb'); + $hpa = $this->cluster->getHorizontalPodAutoscalerByName('sts-mariadb'); $this->assertTrue($sts->delete()); $this->assertTrue($hpa->delete()); @@ -324,14 +300,14 @@ public function runDeletionTests() $this->expectException(KubernetesAPIException::class); - $this->cluster->getStatefulSetByName('mysql'); - $this->cluster->getHorizontalPodAutoscalerByName('sts-mysql'); + $this->cluster->getStatefulSetByName('mariadb'); + $this->cluster->getHorizontalPodAutoscalerByName('sts-mariadb'); } public function runWatchAllTests() { $watch = $this->cluster->statefulSet()->watchAll(function ($type, $sts) { - if ($sts->getName() === 'mysql') { + if ($sts->getName() === 'mariadb') { return true; } }, ['timeoutSeconds' => 10]); @@ -341,8 +317,8 @@ public function runWatchAllTests() public function runWatchTests() { - $watch = $this->cluster->statefulSet()->watchByName('mysql', function ($type, $sts) { - return $sts->getName() === 'mysql'; + $watch = $this->cluster->statefulSet()->watchByName('mariadb', function ($type, $sts) { + return $sts->getName() === 'mariadb'; }, ['timeoutSeconds' => 10]); $this->assertTrue($watch); @@ -350,7 +326,7 @@ public function runWatchTests() public function runScalingTests() { - $sts = $this->cluster->getStatefulSetByName('mysql'); + $sts = $this->cluster->getStatefulSetByName('mariadb'); $scaler = $sts->scale(2); diff --git a/tests/TestCase.php b/tests/TestCase.php index 5cf00c4a..daff7a47 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -5,6 +5,8 @@ use Orchestra\Testbench\TestCase as Orchestra; use RenokiCo\PhpK8s\Exceptions\PhpK8sException; use RenokiCo\PhpK8s\K8s; +use RenokiCo\PhpK8s\Instances\Container; +use RenokiCo\PhpK8s\Kinds\K8sPod; use RenokiCo\PhpK8s\KubernetesCluster; abstract class TestCase extends Orchestra @@ -12,7 +14,7 @@ abstract class TestCase extends Orchestra /** * The cluster to the Kubernetes cluster. * - * @var \RenokiCo\PhpK8s\KubernetesCluster + * @var KubernetesCluster */ protected $cluster; @@ -62,4 +64,137 @@ public function getEnvironmentSetUp($app) { // } + + /** + * Create a standard mariadb container with common configuration. + * + * @param array $options Override options for customization + * @return Container + */ + protected function createMariadbContainer(array $options = []): Container + { + $container = K8s::container() + ->setName($options['name'] ?? 'mariadb') + ->setImage($options['image'] ?? 'public.ecr.aws/docker/library/mariadb', $options['tag'] ?? '11.8') + ->setPorts([ + ['name' => 'mariadb', 'protocol' => 'TCP', 'containerPort' => 3306], + ]); + + if (isset($options['env']) || isset($options['includeEnv']) && $options['includeEnv']) { + $container->setEnv($options['env'] ?? ['MARIADB_ROOT_PASSWORD' => 'test']); + } + + if (isset($options['additionalPort'])) { + $container->addPort($options['additionalPort'], 'TCP', 'mariadb-alt'); + } + + return $container; + } + + /** + * Create a standard Perl container for computation tasks. + * + * @param array $options Override options for customization + * @return Container + */ + protected function createPerlContainer(array $options = []): Container + { + $container = K8s::container() + ->setName($options['name'] ?? 'pi') + ->setCommand($options['command'] ?? ['perl', '-Mbignum=bpi', '-wle', 'print bpi(200)']); + + if (isset($options['tag'])) { + $container->setImage($options['image'] ?? 'public.ecr.aws/docker/library/perl', $options['tag']); + } else { + $container->setImage($options['image'] ?? 'public.ecr.aws/docker/library/perl'); + } + + return $container; + } + + /** + * Create a standard Busybox container. + * + * @param array $options Override options for customization + * @return Container + */ + protected function createBusyboxContainer(array $options = []): Container + { + $container = K8s::container() + ->setName($options['name'] ?? 'busybox') + ->setCommand($options['command'] ?? ['/bin/sh']); + + if (isset($options['tag'])) { + $container->setImage($options['image'] ?? 'public.ecr.aws/docker/library/busybox', $options['tag']); + } else { + $container->setImage($options['image'] ?? 'public.ecr.aws/docker/library/busybox'); + } + + return $container; + } + + /** + * Create a standard Nginx container. + * + * @param array $options Override options for customization + * @return Container + */ + protected function createNginxContainer(array $options = []): Container + { + $container = K8s::container() + ->setName($options['name'] ?? 'nginx') + ->setPorts([ + ['name' => 'http', 'protocol' => 'TCP', 'containerPort' => 80], + ]); + + if (isset($options['tag'])) { + $container->setImage($options['image'] ?? 'public.ecr.aws/docker/library/nginx', $options['tag']); + } else { + $container->setImage($options['image'] ?? 'public.ecr.aws/docker/library/nginx'); + } + + return $container; + } + + /** + * Create a standard mariadb pod with common configuration. + * + * @param array $options Override options for customization + * @return K8sPod + */ + protected function createMariadbPod(array $options = []): K8sPod + { + $mariadb = $this->createMariadbContainer($options['container'] ?? []); + + return $this->cluster->pod() + ->setName($options['name'] ?? 'mariadb') + ->setLabels($options['labels'] ?? ['tier' => 'backend']) + ->setContainers([$mariadb]); + } + + /** + * Create a standard Perl pod for computation tasks. + * + * @param array $options Override options for customization + * @return K8sPod + */ + protected function createPerlPod(array $options = []): K8sPod + { + $perl = $this->createPerlContainer($options['container'] ?? []); + + $pod = $this->cluster->pod() + ->setName($options['name'] ?? 'perl') + ->setLabels($options['labels'] ?? ['tier' => 'compute']) + ->setContainers([$perl]); + + if (isset($options['restartPolicy'])) { + if ($options['restartPolicy'] === 'Never') { + $pod->neverRestart(); + } elseif ($options['restartPolicy'] === 'OnFailure') { + $pod->restartOnFailure(); + } + } + + return $pod; + } } diff --git a/tests/VolumeTest.php b/tests/VolumeTest.php index 8b3d4820..c7bbd8e6 100644 --- a/tests/VolumeTest.php +++ b/tests/VolumeTest.php @@ -13,15 +13,13 @@ public function test_volume_empty_directory() $mountedVolume = $volume->mountTo('/some-path'); - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') + $mariadb = $this->createMariadbContainer() ->addMountedVolumes([$mountedVolume]) ->setMountedVolumes([$mountedVolume]); $pod = K8s::pod() - ->setName('mysql') - ->setContainers([$mysql]) + ->setName('mariadb') + ->setContainers([$mariadb]) ->addVolumes([$volume]) ->setVolumes([$volume]); @@ -36,7 +34,7 @@ public function test_volume_empty_directory() ], $mountedVolume->toArray()); $this->assertEquals($pod->getVolumes()[0]->toArray(), $volume->toArray()); - $this->assertEquals($mysql->getMountedVolumes()[0]->toArray(), $mountedVolume->toArray()); + $this->assertEquals($mariadb->getMountedVolumes()[0]->toArray(), $mountedVolume->toArray()); } public function test_volume_config_map() @@ -52,14 +50,12 @@ public function test_volume_config_map() $mountedVolume = $volume->mountTo('/some-path', 'some-key'); - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') + $mariadb = $this->createMariadbContainer() ->addMountedVolumes([$mountedVolume]); $pod = K8s::pod() - ->setName('mysql') - ->setContainers([$mysql]) + ->setName('mariadb') + ->setContainers([$mariadb]) ->addVolumes([$volume]); $this->assertEquals([ @@ -74,7 +70,7 @@ public function test_volume_config_map() ], $mountedVolume->toArray()); $this->assertEquals($pod->getVolumes()[0]->toArray(), $volume->toArray()); - $this->assertEquals($mysql->getMountedVolumes()[0]->toArray(), $mountedVolume->toArray()); + $this->assertEquals($mariadb->getMountedVolumes()[0]->toArray(), $mountedVolume->toArray()); } public function test_volume_secret() @@ -90,14 +86,12 @@ public function test_volume_secret() $mountedVolume = $volume->mountTo('/some-path', 'some-key'); - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') + $mariadb = $this->createMariadbContainer() ->addMountedVolumes([$mountedVolume]); $pod = K8s::pod() - ->setName('mysql') - ->setContainers([$mysql]) + ->setName('mariadb') + ->setContainers([$mariadb]) ->addVolumes([$volume]); $this->assertEquals([ @@ -112,7 +106,7 @@ public function test_volume_secret() ], $mountedVolume->toArray()); $this->assertEquals($pod->getVolumes()[0]->toArray(), $volume->toArray()); - $this->assertEquals($mysql->getMountedVolumes()[0]->toArray(), $mountedVolume->toArray()); + $this->assertEquals($mariadb->getMountedVolumes()[0]->toArray(), $mountedVolume->toArray()); } public function test_volume_gce_pd() @@ -121,14 +115,12 @@ public function test_volume_gce_pd() $mountedVolume = $volume->mountTo('/some-path'); - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') + $mariadb = $this->createMariadbContainer() ->addMountedVolumes([$mountedVolume]); $pod = K8s::pod() - ->setName('mysql') - ->setContainers([$mysql]) + ->setName('mariadb') + ->setContainers([$mariadb]) ->addVolumes([$volume]); $this->assertEquals([ @@ -145,7 +137,7 @@ public function test_volume_gce_pd() ], $mountedVolume->toArray()); $this->assertEquals($pod->getVolumes()[0]->toArray(), $volume->toArray()); - $this->assertEquals($mysql->getMountedVolumes()[0]->toArray(), $mountedVolume->toArray()); + $this->assertEquals($mariadb->getMountedVolumes()[0]->toArray(), $mountedVolume->toArray()); } public function test_volume_aws_ebs() @@ -154,14 +146,12 @@ public function test_volume_aws_ebs() $mountedVolume = $volume->mountTo('/some-path'); - $mysql = K8s::container() - ->setName('mysql') - ->setImage('public.ecr.aws/docker/library/mysql', '5.7') + $mariadb = $this->createMariadbContainer() ->addMountedVolumes([$mountedVolume]); $pod = K8s::pod() - ->setName('mysql') - ->setContainers([$mysql]) + ->setName('mariadb') + ->setContainers([$mariadb]) ->addVolumes([$volume]); $this->assertEquals([ @@ -178,6 +168,6 @@ public function test_volume_aws_ebs() ], $mountedVolume->toArray()); $this->assertEquals($pod->getVolumes()[0]->toArray(), $volume->toArray()); - $this->assertEquals($mysql->getMountedVolumes()[0]->toArray(), $mountedVolume->toArray()); + $this->assertEquals($mariadb->getMountedVolumes()[0]->toArray(), $mountedVolume->toArray()); } } diff --git a/tests/yaml/clusterrole.yaml b/tests/yaml/clusterrole.yaml index c783443d..84352330 100644 --- a/tests/yaml/clusterrole.yaml +++ b/tests/yaml/clusterrole.yaml @@ -3,7 +3,7 @@ kind: ClusterRole metadata: name: admin-cr annotations: - mysql/annotation: "yes" + mariadb/annotation: "yes" rules: - apiGroups: [""] resources: diff --git a/tests/yaml/daemonset.yaml b/tests/yaml/daemonset.yaml index 1fa4ced2..3dd9d994 100644 --- a/tests/yaml/daemonset.yaml +++ b/tests/yaml/daemonset.yaml @@ -1,22 +1,23 @@ apiVersion: apps/v1 kind: DaemonSet metadata: - name: mysql + name: mariadb labels: tier: backend spec: selector: matchLabels: - name: mysql-daemonset + name: mariadb-daemonset template: metadata: - name: mysql + name: mariadb labels: - name: mysql-daemonset + name: mariadb-daemonset spec: containers: - - name: mysql - image: public.ecr.aws/docker/library/mysql:5.7 + - name: mariadb + image: public.ecr.aws/docker/library/mariadb:11.8 ports: - - name: mysql + - name: mariadb protocol: TCP + containerPort: 3306 diff --git a/tests/yaml/deployment.yaml b/tests/yaml/deployment.yaml index cde0d05f..e90070b1 100644 --- a/tests/yaml/deployment.yaml +++ b/tests/yaml/deployment.yaml @@ -1,26 +1,26 @@ apiVersion: apps/v1 kind: Deployment metadata: - name: mysql + name: mariadb labels: tier: backend annotations: - mysql/annotation: "yes" + mariadb/annotation: "yes" spec: selector: matchLabels: - name: mysql-deployment + name: mariadb-deployment replicas: 3 template: metadata: - name: mysql + name: mariadb labels: - name: mysql-deployment + name: mariadb-deployment spec: containers: - - name: mysql - image: public.ecr.aws/docker/library/mysql:5.7 + - name: mariadb + image: public.ecr.aws/docker/library/mariadb:11.8 ports: - - name: mysql + - name: mariadb protocol: TCP containerPort: 3306 diff --git a/tests/yaml/hpa.yaml b/tests/yaml/hpa.yaml index 33c55e6e..1b33888d 100644 --- a/tests/yaml/hpa.yaml +++ b/tests/yaml/hpa.yaml @@ -1,13 +1,13 @@ apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: - name: mysql-hpa + name: mariadb-hpa labels: tier: backend spec: scaleTargetRef: kind: Deployment - name: mysql + name: mariadb apiVersion: apps/v1 metrics: - resource: diff --git a/tests/yaml/pdb.yaml b/tests/yaml/pdb.yaml index d6583b37..fd8faa8f 100644 --- a/tests/yaml/pdb.yaml +++ b/tests/yaml/pdb.yaml @@ -1,11 +1,11 @@ apiVersion: policy/v1 kind: PodDisruptionBudget metadata: - name: mysql-pdb + name: mariadb-pdb labels: tier: backend annotations: - mysql/annotation: "yes" + mariadb/annotation: "yes" spec: selector: matchLabels: @@ -15,11 +15,11 @@ spec: apiVersion: policy/v1 kind: PodDisruptionBudget metadata: - name: mysql-pdb + name: mariadb-pdb labels: tier: backend annotations: - mysql/annotation: "yes" + mariadb/annotation: "yes" spec: selector: matchLabels: diff --git a/tests/yaml/pod.yaml b/tests/yaml/pod.yaml index 8f5840c3..e75420f9 100644 --- a/tests/yaml/pod.yaml +++ b/tests/yaml/pod.yaml @@ -1,11 +1,11 @@ apiVersion: v1 kind: Pod metadata: - name: mysql + name: mariadb labels: tier: backend annotations: - mysql/annotation: "yes" + mariadb/annotation: "yes" spec: initContainers: - name: busybox @@ -13,15 +13,15 @@ spec: command: - /bin/sh containers: - - name: mysql - image: public.ecr.aws/docker/library/mysql:5.7 + - name: mariadb + image: public.ecr.aws/docker/library/mariadb:11.8 ports: - - name: mysql + - name: mariadb protocol: TCP containerPort: 3306 - - name: mysql-alt + - name: mariadb-alt protocol: TCP containerPort: 3307 env: - - name: MYSQL_ROOT_PASSWORD + - name: MARIADB_ROOT_PASSWORD value: test diff --git a/tests/yaml/statefulset.yaml b/tests/yaml/statefulset.yaml index 024fb0f6..8815918e 100644 --- a/tests/yaml/statefulset.yaml +++ b/tests/yaml/statefulset.yaml @@ -1,33 +1,33 @@ apiVersion: apps/v1 kind: StatefulSet metadata: - name: mysql + name: mariadb labels: tier: backend annotations: - mysql/annotation: "yes" + mariadb/annotation: "yes" spec: selector: matchLabels: - name: mysql-statefulset + name: mariadb-statefulset replicas: 3 - serviceName: mysql + serviceName: mariadb template: metadata: - name: mysql + name: mariadb labels: - name: mysql-statefulset + name: mariadb-statefulset spec: containers: - - name: mysql - image: public.ecr.aws/docker/library/mysql:5.7 + - name: mariadb + image: public.ecr.aws/docker/library/mariadb:11.8 ports: - - name: mysql + - name: mariadb protocol: TCP containerPort: 3306 volumeClaimTemplates: - metadata: - name: mysql-pvc + name: mariadb-pvc spec: resources: requests: