diff --git a/composer.json b/composer.json index 845befa3..d72cd2f7 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "illuminate/support": "^9.35|^10.1", "ratchet/pawl": "^0.4.1", "symfony/process": "^5.4|^6.0", - "vierbergenlars/php-semver": "^2.1|^3.0" + "composer/semver": "^3.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..bfc7581b 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 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 */ protected function loadClusterVersion(): void { - if ($this->kubernetesVersion) { + if (isset($this->kubernetesVersion)) { return; } @@ -44,7 +45,8 @@ protected function loadClusterVersion(): void $json = @json_decode($response->getBody(), true); - $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 */ 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 */ 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')); + } +}