Skip to content

Commit

Permalink
Dev (#3)
Browse files Browse the repository at this point in the history
* Fix manticore connection
* Add ready condition checks
* Fix cluster restoring
  • Loading branch information
djklim87 authored Oct 15, 2022
1 parent 96c8bdc commit 1aecdf2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
30 changes: 23 additions & 7 deletions src/K8s/Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Resources
public function __construct(ApiClient $api, array $labels, NotificationInterface $notification)
{
$this->setLabels($labels);
$this->api = $api;
$this->api = $api;
$this->notification = $notification;
}

Expand All @@ -35,18 +35,34 @@ private function getLabels(): array
*/
public function getPods(): array
{
if ( ! $this->pods) {
if (!$this->pods) {
$pods = $this->api->getManticorePods($this->getLabels());
if ( ! isset($pods['items'])) {
if (!isset($pods['items'])) {
Analog::log('K8s api don\'t respond');
exit(1);
}

foreach ($pods['items'] as $pod) {
if ($pod['status']['phase'] === 'Running' || $pod['status']['phase'] === 'Pending') {

if (!empty($pod['status']['conditions'])) {
$readyCondition = false;
foreach ($pod['status']['conditions'] as $condition) {
if ($condition['type'] === 'Ready' && $condition['status'] === 'True') {
$readyCondition = true;
}
}

if (!$readyCondition){
continue;
}
}

$this->pods[] = $pod;
} else {
$this->notification->sendMessage("Bad pod phase for ".$pod['metadata']['name'].' phase '.$pod['status']['phase']);
$this->notification->sendMessage(
"Bad pod phase for ".$pod['metadata']['name'].' phase '.$pod['status']['phase']
);
Analog::log('Error pod phase '.json_encode($pod));
}
}
Expand Down Expand Up @@ -100,7 +116,7 @@ public function getPodsIp(): array
$ips[$pod['metadata']['name']] = $pod['status']['podIP'];
} elseif ($pod['metadata']['name'] === $hostname) {
$selfIp = getHostByName($hostname);
if ( ! empty($selfIp)) {
if (!empty($selfIp)) {
$ips[$hostname] = $selfIp;
}
}
Expand Down Expand Up @@ -151,7 +167,7 @@ public function getMinAvailableReplica()
public function getMinReplicaName(): string
{
$hostname = gethostname();
$parts = explode("-", $hostname);
$parts = explode("-", $hostname);
array_pop($parts);
$parts[] = 0;

Expand All @@ -164,7 +180,7 @@ public function getCurrentReplica(): int
return 0;
}
$hostname = gethostname();
$parts = explode("-", $hostname);
$parts = explode("-", $hostname);

return (int)array_pop($parts);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Manticore/ManticoreConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct($host, $port, $clusterName, $maxAttempts)
sleep(1);
}

if ($this->connection->connect_errno) {
if ($this->connection == null || $this->connection->connect_errno) {
throw new \RuntimeException("Can't connect to Manticore at ".$host.':'.$port);
}
}
Expand Down Expand Up @@ -164,7 +164,7 @@ public function getNotInClusterTables()

public function restoreCluster($log = true): bool
{
$this->query("SET CLUSTER GLOBAL 'pc.bootstrap' = 1", $log);
$this->query("SET CLUSTER ".$this->clusterName." GLOBAL 'pc.bootstrap' = 1", $log);

if ($this->getConnectionError()) {
return false;
Expand Down

0 comments on commit 1aecdf2

Please sign in to comment.