Skip to content

Commit

Permalink
Hosts using support (#6)
Browse files Browse the repository at this point in the history
* Improve hostname support
  • Loading branch information
djklim87 authored Jun 12, 2023
1 parent fbd8096 commit 077b7a6
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
vendor
composer.lock
index.php
25 changes: 25 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
### Local development

Create `index.php` file for local debug

```php

require('vendor/autoload.php');


$labels = [
'app.kubernetes.io/component' => 'worker',
'app.kubernetes.io/instance' => 'helm',
];

$api = new ApiClient();
$api->setApiUrl('http://localhost:8080');
$api->setNamespace('helm');
$api->setMode(ApiClient::DEV_MODE);

```

Run k8s proxy
```
kubectl proxy --port=8080
```
51 changes: 41 additions & 10 deletions src/K8s/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class ApiClient
public const TYPE_SECRETS = 'secrets';
public const TYPE_PVC = 'persistentvolumeclaims';

public const PROD_MODE = 'prod';

public const DEV_MODE = 'dev';


private string $apiUrl = 'https://kubernetes.default.svc';
private string $cert = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt';
Expand All @@ -39,7 +43,10 @@ class ApiClient
private string $bearer;
private Client $httpClient;
private string $userAgent;
private $namespace;

private string $namespace;

private string $mode = self::PROD_MODE;

public function __construct()
{
Expand All @@ -64,6 +71,25 @@ public function getManticorePods(array $labels = null)
);
}

public function setApiUrl($apiUrl): void
{
$this->apiUrl = $apiUrl;
}

public function setNamespace($namespace): void
{
$this->namespace = $namespace;
}

public function setMode($mode): void
{
if (in_array($mode, ['prod', 'dev'])) {
$this->mode = $mode;
} else {
throw new \RuntimeException('Wrong mode. Allowed only "prod" and "dev" modes');
}
}

/**
* @throws JsonException
*/
Expand All @@ -74,15 +100,20 @@ public function getNodes()

private function request($section, $type = "GET", $noNamespace = false, array $labels = null)
{
$params = [
'verify' => $this->cert,
'version' => 2.0,
'headers' => [
'Authorization' => 'Bearer '.$this->bearer,
'Accept' => 'application/json',
'User-Agent' => $this->userAgent,
],
];
$params = [];

if ($this->mode === self::PROD_MODE){
$params = [
'verify' => $this->cert,
'version' => 2.0,
'headers' => [
'Authorization' => 'Bearer '.$this->bearer,
'Accept' => 'application/json',
'User-Agent' => $this->userAgent,
],
];
}


$url = $this->getUrl($section, $noNamespace);
if ($labels) {
Expand Down
20 changes: 20 additions & 0 deletions src/K8s/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,26 @@ public function getPodsHostnames(): array
return $hostnames;
}

public function getPodsFullHostnames(): array
{
if (defined('DEV') && DEV === true) {
return [];
}
$hostnames = [];
$this->getPods();

foreach ($this->pods as $pod) {
if ($pod['status']['phase'] === 'Running' || $pod['status']['phase'] === 'Pending') {
$hostnames[] = $pod['metadata']['name'].
'.'.$pod['spec']['subdomain'].
'.'.$pod['metadata']['namespace'].
'.svc.cluster.local';
}
}

return $hostnames;
}


public function getMinAvailableReplica($skipSelf = true)
{
Expand Down
18 changes: 8 additions & 10 deletions src/Manticore/ManticoreJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,33 +101,31 @@ public function startManticore()

public function checkNodesAvailability(Resources $resources, $port, $shortClusterName, $attempts): void
{
$nodes = $resources->getPodsIp();
$nodes = $resources->getPodsFullHostnames();
$availableNodes = [];

$skipSelf = true;
if (count($nodes) > 1) {
$skipSelf = false;
}
foreach ($nodes as $hostname => $ip) {
foreach ($nodes as $hostname) {
// Skip current node

if ($hostname === gethostname()) {
if ( ! $skipSelf) {
$availableNodes[] = $ip.':'.$this->binaryPort;
}
if (!$skipSelf && strpos($hostname, gethostname()) === 0) {
$availableNodes[] = $hostname.':'.$this->binaryPort;
continue;
}


try {
$connection = new ManticoreConnector($ip, $port, $shortClusterName, $attempts);
$connection = new ManticoreConnector($hostname, $port, $shortClusterName, $attempts);
if ( ! $connection->checkClusterName()) {
Analog::log("Cluster name mismatch at $ip");
Analog::log("Cluster name mismatch at $hostname");
continue;
}
$availableNodes[] = $ip.':'.$this->binaryPort;
$availableNodes[] = $hostname.':'.$this->binaryPort;
} catch (\RuntimeException $exception) {
Analog::log("Node at $ip no more available\n".$exception->getMessage());
Analog::log("Node at $hostname no more available\n".$exception->getMessage());
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/Manticore/ManticoreStreamsJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public function __construct($clusterName, NotificationInterface $notification, $

$this->notification = $notification;
$testsIndexMetadata = '/var/lib/manticore/tests/tests.meta';
if (file_exists($testsIndexMetadata)){
Analog::log('Tests metadata ' . file_get_contents($testsIndexMetadata));
}else{
if (!file_exists($testsIndexMetadata)){
Analog::log('Tests metadata not found');
}

Expand Down

0 comments on commit 077b7a6

Please sign in to comment.