Skip to content

Commit

Permalink
Fixes "Upgrade request required" on Pod exec and requestUri (specifie…
Browse files Browse the repository at this point in the history
…d twice) bug (#129)
  • Loading branch information
patriceckhart authored Mar 14, 2024
1 parent 94aa37b commit 9a334d3
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ public function setOptions(array $options, bool $reset = false): void
if ($reset) {
$this->master = null;
$this->verify = null;
$this->caCert = null;
$this->clientCert = null;
$this->clientKey = null;
$this->token = null;
$this->username = null;
$this->password = null;
Expand All @@ -173,6 +176,17 @@ public function setOptions(array $options, bool $reset = false): void
if (isset($options['master'])) {
$this->master = $options['master'];
}
if (isset($options['verify'])) {
$this->verify = $options['verify'];
} elseif (isset($options['ca_cert'])) {
$this->caCert = $options['ca_cert'];
}
if (isset($options['client_cert'])) {
$this->clientCert = $options['client_cert'];
}
if (isset($options['client_key'])) {
$this->clientKey = $options['client_key'];
}
if (isset($options['token'])) {
$this->token = $options['token'];
}
Expand All @@ -189,7 +203,7 @@ public function setOptions(array $options, bool $reset = false): void

/**
* Parse a kubeconfig.
*
*
* @param string|array $content Mixed type, based on the second input argument
* @throws \InvalidArgumentException
*/
Expand Down Expand Up @@ -311,7 +325,7 @@ public static function parseKubeconfig($content, string $contentType = 'yaml'):

/**
* Parse a kubeconfig file.
*
*
* @throws \InvalidArgumentException
*/
public static function parseKubeconfigFile(string $filePath): array
Expand Down Expand Up @@ -375,7 +389,7 @@ public function sendRequest(string $method, string $uri, array $query = [], $bod
if ($namespace) {
$baseUri .= '/namespaces/' . $this->namespace;
}

if ($uri === '/healthz' || $uri === '/version') {
$requestUrl = $this->master . '/' . $uri;
} else {
Expand Down Expand Up @@ -424,6 +438,10 @@ public function sendRequest(string $method, string $uri, array $query = [], $bod
$responseBody = (string) $response->getBody();
$jsonResponse = json_decode($responseBody, true);

if ($jsonResponse !== null && array_key_exists('code', $jsonResponse) && $this->isUpgradeRequestRequired($jsonResponse)) {
return $this->sendUpgradeRequest($requestUrl, $query);
}

return is_array($jsonResponse) ? $jsonResponse : $responseBody;
} catch (HttpTransferException $e) {
$response = $e->getResponse();
Expand Down Expand Up @@ -455,7 +473,7 @@ protected function isUpgradeRequestRequired(array $response): bool
*/
protected function sendUpgradeRequest(string $requestUri, array $query): array
{
$fullUrl = $this->master .'/' . $requestUri . '?' . implode('&', $this->parseQueryParams($query));
$fullUrl = $requestUri . '?' . implode('&', $this->parseQueryParams($query));
if (parse_url($fullUrl, PHP_URL_SCHEME) === 'https') {
$fullUrl = str_replace('https://', 'wss://', $fullUrl);
} else {
Expand Down Expand Up @@ -562,15 +580,15 @@ public function health()
{
return $this->sendRequest('GET', '/healthz');
}

/**
* Check the version.
*/
public function version(): array
{
return $this->sendRequest('GET', '/version');
}

/**
* Magic call method to grab a class instance.
*
Expand Down

0 comments on commit 9a334d3

Please sign in to comment.