Skip to content

Commit

Permalink
Merge pull request #182 from renoki-co/feature/fix-temporary-files-fo…
Browse files Browse the repository at this point in the history
…r-host-and-user

[feature] Added support for different hosts with same contexts
  • Loading branch information
rennokki authored Jan 3, 2022
2 parents 8232e44 + 20fe1a1 commit 990acd6
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/Traits/Cluster/LoadsFromKubeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use RenokiCo\PhpK8s\Exceptions\KubeConfigBaseEncodedDataInvalid;
use RenokiCo\PhpK8s\Exceptions\KubeConfigClusterNotFound;
use RenokiCo\PhpK8s\Exceptions\KubeConfigContextNotFound;
Expand Down Expand Up @@ -151,29 +152,45 @@ protected function loadKubeConfigFromArray(array $kubeconfig, string $context =
throw new KubeConfigClusterNotFound("The cluster {$cluster} does not exist in the provided Kube Config file.");
}

$url = $clusterConfig['cluster']['server'];

if (! $userConfig = collect($kubeconfig['users'] ?? [])->where('name', $user)->first()) {
throw new KubeConfigUserNotFound("The user {$user} does not exist in the provided Kube Config file.");
}

$userName = $userConfig['name'];

if (isset($clusterConfig['cluster']['certificate-authority'])) {
$this->withCaCertificate($clusterConfig['cluster']['certificate-authority']);
}

if (isset($clusterConfig['cluster']['certificate-authority-data'])) {
$this->withCaCertificate(
$this->writeTempFileForContext($context, 'ca-cert.pem', $clusterConfig['cluster']['certificate-authority-data'])
$this->writeTempFileForContext(
$context,
$userName,
$url,
'ca-cert.pem',
$clusterConfig['cluster']['certificate-authority-data']
)
);
}

$this->url = $clusterConfig['cluster']['server'];
$this->url = $url;

if (isset($userConfig['user']['client-certificate'])) {
$this->withCertificate($userConfig['user']['client-certificate']);
}

if (isset($userConfig['user']['client-certificate-data'])) {
$this->withCertificate(
$this->writeTempFileForContext($context, 'client-cert.pem', $userConfig['user']['client-certificate-data'])
$this->writeTempFileForContext(
$context,
$userName,
$url,
'client-cert.pem',
$userConfig['user']['client-certificate-data']
)
);
}

Expand All @@ -183,7 +200,13 @@ protected function loadKubeConfigFromArray(array $kubeconfig, string $context =

if (isset($userConfig['user']['client-key-data'])) {
$this->withPrivateKey(
$this->writeTempFileForContext($context, 'client-key.pem', $userConfig['user']['client-key-data'])
$this->writeTempFileForContext(
$context,
$userName,
$url,
'client-key.pem',
$userConfig['user']['client-key-data']
)
);
}

Expand All @@ -203,18 +226,25 @@ protected function loadKubeConfigFromArray(array $kubeconfig, string $context =
* coming from the KubeConfig file.
*
* @param string $context
* @param string $userName
* @param string $url
* @param string $fileName
* @param string $contents
* @return string
*
* @throws \Exception
*/
protected function writeTempFileForContext(string $context, string $fileName, string $contents)
{
protected function writeTempFileForContext(
string $context,
string $userName,
string $url,
string $fileName,
string $contents
) {
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */
$tempFolder = static::$tempFolder ?: sys_get_temp_dir();

$tempFilePath = $tempFolder.DIRECTORY_SEPARATOR."ctx-{$context}-{$fileName}";
$tempFilePath = $tempFolder.DIRECTORY_SEPARATOR.Str::slug("ctx-{$context}-{$userName}-{$url}-{$fileName}");

if (file_exists($tempFilePath)) {
return $tempFilePath;
Expand Down

0 comments on commit 990acd6

Please sign in to comment.