Skip to content

Commit

Permalink
Fix: Deprecated vierbergenlars/php-semver -> composer/semver
Browse files Browse the repository at this point in the history
  • Loading branch information
cuppett committed Aug 31, 2023
1 parent 9b4e9b5 commit 2d461f9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
24 changes: 15 additions & 9 deletions src/Traits/Cluster/ChecksClusterVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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']);
}

/**
Expand All @@ -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
);
}
Expand All @@ -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
);
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ChecksClusterVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace RenokiCo\PhpK8s\Test;

class ChecksClusterVersionTest extends TestCase
{
public function test_check_cluster_version(): void
{
$this->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'));
}
}

0 comments on commit 2d461f9

Please sign in to comment.