Skip to content

Commit d692f2e

Browse files
committed
clean up and add testcases for less sensitive config loader
1 parent 4571723 commit d692f2e

7 files changed

+223
-143
lines changed

src/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,25 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
5151

5252
var k8SConfig = LoadKubeConfig(kubeconfig);
5353
var k8SConfiguration = new KubernetesClientConfiguration();
54-
k8SConfiguration.Initialize(k8SConfig, currentContext);
54+
55+
currentContext = currentContext ?? k8SConfig.CurrentContext;
56+
57+
// only init context if context if set
58+
if (currentContext != null)
59+
{
60+
k8SConfiguration.InitializeContext(k8SConfig, currentContext);
61+
}
5562

5663
if (!string.IsNullOrWhiteSpace(masterUrl))
5764
{
5865
k8SConfiguration.Host = masterUrl;
5966
}
67+
68+
if (string.IsNullOrWhiteSpace(k8SConfiguration.Host))
69+
{
70+
throw new KubeConfigException("Cannot infer server host url either from context or masterUrl");
71+
}
72+
6073
return k8SConfiguration;
6174
}
6275

@@ -65,11 +78,9 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
6578
/// </summary>
6679
/// <param name="k8SConfig">Kubernetes Configuration</param>
6780
/// <param name="currentContext">Current Context</param>
68-
private void Initialize(K8SConfiguration k8SConfig, string currentContext = null)
81+
private void InitializeContext(K8SConfiguration k8SConfig, string currentContext)
6982
{
7083
// current context
71-
currentContext = currentContext ?? k8SConfig.CurrentContext;
72-
7384
var activeContext =
7485
k8SConfig.Contexts.FirstOrDefault(
7586
c => c.Name.Equals(currentContext, StringComparison.OrdinalIgnoreCase));

tests/KubernetesClientConfigurationTests.cs

Lines changed: 141 additions & 139 deletions
Large diffs are not rendered by default.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
apiVersion: v1
3+
clusters:
4+
- cluster:
5+
certificate-authority: assets/ca.crt
6+
server: https://horse.org:4443
7+
name: horse-cluster
8+
contexts:
9+
- context:
10+
cluster: horse-cluster
11+
namespace: chisel-ns
12+
user: green-user
13+
name: federal-context
14+
kind: Config
15+
users:
16+
- name: green-user
17+
user:
18+
password: secret
19+
username: admin

tests/assets/kubeconfig.no-user.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
current-context: federal-context
3+
apiVersion: v1
4+
clusters:
5+
- cluster:
6+
certificate-authority: assets/ca.crt
7+
server: https://horse.org:4443
8+
name: horse-cluster
9+
contexts:
10+
- context:
11+
cluster: horse-cluster
12+
namespace: chisel-ns
13+
name: federal-context
14+
kind: Config
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
current-context: federal-context
3+
apiVersion: v1
4+
clusters:
5+
- cluster:
6+
server: http://horse.org
7+
name: horse-cluster
8+
contexts:
9+
- context:
10+
cluster: horse-cluster
11+
namespace: chisel-ns
12+
user: green-user
13+
name: federal-context
14+
kind: Config
15+
users:
16+
- name: green-user
17+
user:
18+
password: secret
19+
username: admin
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
current-context: federal-context
3+
apiVersion: v1
4+
clusters:
5+
- cluster:
6+
certificate-authority: assets/ca.crt
7+
server: https://horse.org:4443
8+
name: horse-cluster
9+
contexts:
10+
- context:
11+
cluster: horse-cluster
12+
namespace: chisel-ns
13+
user: green-user
14+
name: federal-context
15+
kind: Config

0 commit comments

Comments
 (0)