Skip to content

Commit 9f72121

Browse files
authored
Merge pull request #179 from renoki-co/feature/skip-tls-checks-from-kubeconfig
[feature] Add support for insecure-skip-tls-verify
2 parents 2949399 + 5356a66 commit 9f72121

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

src/Traits/Cluster/LoadsFromKubeConfig.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static function setTempFolder(string $tempFolder)
4444
*/
4545
public static function fromKubeConfigVariable(string $context = null)
4646
{
47+
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */
4748
$cluster = new static;
4849

4950
if (! isset($_SERVER['KUBECONFIG'])) {
@@ -81,6 +82,7 @@ public static function fromKubeConfigVariable(string $context = null)
8182
*/
8283
public static function fromKubeConfigYaml(string $yaml, string $context = null)
8384
{
85+
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */
8486
$cluster = new static;
8587

8688
return $cluster->loadKubeConfigFromArray(yaml_parse($yaml), $context);
@@ -126,6 +128,8 @@ public static function fromKubeConfigArray(array $kubeConfigArray, string $conte
126128
*/
127129
protected function loadKubeConfigFromArray(array $kubeconfig, string $context = null)
128130
{
131+
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */
132+
129133
// Compute the context from the method, or in case it is passed as null
130134
// try to find it from the current kubeconfig's "current-context" field.
131135
$context = $context ?: ($kubeconfig['current-context'] ?? null);
@@ -186,6 +190,10 @@ protected function loadKubeConfigFromArray(array $kubeconfig, string $context =
186190
$this->withToken($userConfig['user']['token']);
187191
}
188192

193+
if (isset($clusterConfig['cluster']['insecure-skip-tls-verify']) && $clusterConfig['cluster']['insecure-skip-tls-verify']) {
194+
$this->withoutSslChecks();
195+
}
196+
189197
return $this;
190198
}
191199

@@ -202,6 +210,7 @@ protected function loadKubeConfigFromArray(array $kubeconfig, string $context =
202210
*/
203211
protected function writeTempFileForContext(string $context, string $fileName, string $contents)
204212
{
213+
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */
205214
$tempFolder = static::$tempFolder ?: sys_get_temp_dir();
206215

207216
$tempFilePath = $tempFolder.DIRECTORY_SEPARATOR."ctx-{$context}-{$fileName}";
@@ -226,6 +235,7 @@ protected function writeTempFileForContext(string $context, string $fileName, st
226235
*/
227236
protected static function mergeKubeconfigContents(array $kubeconfig1, array $kubeconfig2): array
228237
{
238+
/** @var \RenokiCo\PhpK8s\KubernetesCluster $this */
229239
$kubeconfig1 += $kubeconfig2;
230240

231241
foreach ($kubeconfig1 as $key => $value) {

tests/KubeConfigTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ public function test_kube_config_from_yaml_file_with_paths_to_ssl()
6060
$this->assertEquals('/path/to/.minikube/client.key', $keyPath);
6161
}
6262

63+
public function test_kube_config_from_yaml_file_with_skip_tols()
64+
{
65+
$cluster = KubernetesCluster::fromKubeConfigYamlFile(__DIR__.'/cluster/kubeconfig.yaml', 'minikube-skip-tls');
66+
67+
[
68+
'verify' => $verify,
69+
'cert' => $certPath,
70+
'ssl_key' => $keyPath,
71+
] = $cluster->getClient()->getConfig();
72+
73+
$this->assertFalse($verify);
74+
$this->assertEquals('/path/to/.minikube/client3.crt', $certPath);
75+
$this->assertEquals('/path/to/.minikube/client3.key', $keyPath);
76+
}
77+
6378
public function test_cluster_can_get_correct_config_for_token_socket_connection()
6479
{
6580
$cluster = KubernetesCluster::fromUrl('http://127.0.0.1:8080')->loadTokenFromFile(__DIR__.'/cluster/token.txt');

tests/cluster/kubeconfig.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ clusters:
88
certificate-authority: /path/to/.minikube/ca.crt
99
server: https://minikube-2:8443
1010
name: minikube-2
11+
- cluster:
12+
certificate-authority: /path/to/.minikube/ca.crt
13+
server: https://minikube-2:8443
14+
insecure-skip-tls-verify: true
15+
name: minikube-skip-tls
1116
contexts:
1217
- context:
1318
cluster: minikube
@@ -19,6 +24,11 @@ contexts:
1924
user: minikube-2
2025
name: minikube-2
2126
namespace: some-namespace
27+
- context:
28+
cluster: minikube-skip-tls
29+
user: minikube-skip-tls
30+
name: minikube-skip-tls
31+
namespace: some-namespace
2232
- context:
2333
cluster: no-cluster
2434
user: minikube
@@ -41,3 +51,7 @@ users:
4151
user:
4252
client-certificate: /path/to/.minikube/client.crt
4353
client-key: /path/to/.minikube/client.key
54+
- name: minikube-skip-tls
55+
user:
56+
client-certificate: /path/to/.minikube/client3.crt
57+
client-key: /path/to/.minikube/client3.key

0 commit comments

Comments
 (0)