Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated package #351

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
}
],
"require": {
"ext-json": "*",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would advise to remove this extension as it is already existing in other packages as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not rely on other packages' dependencies if we are using json_* functions in this package's code.

Check this for more details

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rennokki let me know what you think, thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got encouraged by PhpStorm to add this package as well in #380 but didn't. I'd also recommend putting it explicitly in the requires section.

"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."
Expand Down
23 changes: 15 additions & 8 deletions src/Traits/Cluster/ChecksClusterVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

/**
Expand All @@ -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)
);
}

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

namespace RenokiCo\PhpK8s\Test;

class CheckClusterVersionTest extends TestCase
{
protected $clusterVersion;

public function setUp(): void
{
parent::setUp();
$this->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);
}
}
Loading