From 4929c19cc3af147c253b12dbabdfd46986719444 Mon Sep 17 00:00:00 2001 From: leo108 Date: Sat, 20 May 2023 12:40:03 +0800 Subject: [PATCH] Replace deprecated package --- .github/workflows/ci.yml | 2 ++ composer.json | 5 +-- src/Traits/Cluster/ChecksClusterVersion.php | 23 +++++++----- tests/CheckClusterVersionTest.php | 39 +++++++++++++++++++++ 4 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 tests/CheckClusterVersionTest.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5ede26cf..b729adea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -83,6 +83,8 @@ jobs: kubectl apply -f https://raw.githubusercontent.com/bitnami-labs/sealed-secrets/main/helm/sealed-secrets/crds/bitnami.com_sealedsecrets.yaml - name: Run tests + env: + KUBERNETES_VERSION: ${{ matrix.kubernetes }} run: | vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml diff --git a/composer.json b/composer.json index 5a531531..5b39c4a8 100644 --- a/composer.json +++ b/composer.json @@ -24,12 +24,13 @@ } ], "require": { + "ext-json": "*", + "composer/semver": "^2.0|^3.0", "guzzlehttp/guzzle": "^6.5|^7.0", "illuminate/macroable": "^9.35|^10.1|^11.0", "illuminate/support": "^9.35|^10.1|^11.0", "ratchet/pawl": "^0.4.1", - "symfony/process": "^5.4|^6.0|^7.0", - "vierbergenlars/php-semver": "^2.1|^3.0" + "symfony/process": "^5.4|^6.0|^7.0" }, "suggest": { "ext-yaml": "YAML extension is used to read or generate YAML from PHP K8s internal classes." diff --git a/src/Traits/Cluster/ChecksClusterVersion.php b/src/Traits/Cluster/ChecksClusterVersion.php index eedea5f2..988d49ac 100644 --- a/src/Traits/Cluster/ChecksClusterVersion.php +++ b/src/Traits/Cluster/ChecksClusterVersion.php @@ -2,16 +2,16 @@ namespace RenokiCo\PhpK8s\Traits\Cluster; +use Composer\Semver\Comparator; use GuzzleHttp\Exception\ClientException; use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException; -use vierbergenlars\SemVer\version as Semver; trait ChecksClusterVersion { /** * The Kubernetes cluster version. * - * @var \vierbergenlars\SemVer\version|null + * @var string|null */ protected $kubernetesVersion; @@ -42,9 +42,9 @@ protected function loadClusterVersion(): void ); } - $json = @json_decode($response->getBody(), true); + $json = json_decode($response->getBody(), true); - $this->kubernetesVersion = new Semver($json['gitVersion']); + $this->kubernetesVersion = self::trimVersion($json['gitVersion']); } /** @@ -58,8 +58,9 @@ public function newerThan(string $kubernetesVersion): bool { $this->loadClusterVersion(); - return Semver::gte( - $this->kubernetesVersion, $kubernetesVersion + return Comparator::greaterThanOrEqualTo( + $this->kubernetesVersion, + self::trimVersion($kubernetesVersion) ); } @@ -74,8 +75,14 @@ public function olderThan(string $kubernetesVersion): bool { $this->loadClusterVersion(); - return Semver::lt( - $this->kubernetesVersion, $kubernetesVersion + return Comparator::lessThan( + $this->kubernetesVersion, + self::trimVersion($kubernetesVersion) ); } + + private static function trimVersion(string $version): string + { + return preg_replace('/^v/', '', $version); + } } diff --git a/tests/CheckClusterVersionTest.php b/tests/CheckClusterVersionTest.php new file mode 100644 index 00000000..2c0140cc --- /dev/null +++ b/tests/CheckClusterVersionTest.php @@ -0,0 +1,39 @@ +clusterVersion = $_SERVER['KUBERNETES_VERSION']; + } + + public function test_newer_than() + { + $this->assertTrue($this->cluster->newerThan($this->clusterVersion)); + $this->assertTrue($this->cluster->newerThan('v'.$this->clusterVersion)); + $this->assertTrue($this->cluster->newerThan($this->change_version(1, 0))); + $this->assertFalse($this->cluster->newerThan($this->change_version(2, 99))); + } + + public function test_old_than() + { + $this->assertFalse($this->cluster->olderThan($this->clusterVersion)); + $this->assertFalse($this->cluster->olderThan('v'.$this->clusterVersion)); + $this->assertTrue($this->cluster->olderThan($this->change_version(1, 99))); + $this->assertTrue($this->cluster->olderThan($this->change_version(0, 2))); + $this->assertFalse($this->cluster->olderThan($this->change_version(1, 0))); + } + + protected function change_version(int $position, int $version): string + { + $parts = explode('.', $this->clusterVersion); + $parts[$position] = $version; + + return implode('.', $parts); + } +}