Skip to content

Commit da3bff5

Browse files
authored
Use value from ContextDetails to populate Namespace (#373)
* Use value from ContextDetails to populate Namespace This is a fix for: #372 This change uses the value from ContextDetails.Namespace to populate KubernetesClientConfiguration.Namespace. The issue is there's a Namespace property on both Context and ContextDetails - The property on Context is used today - The property on ContextDetails is not - The property on ContextDetails maps to the actual yaml config * Obsolete Context.Namespace This property doesn't map to anything in the YAML and thus will never be set. Other clients I checked (java, golang) don't look for a property at this level. I think this was likely a mistake, and it should be obsoleted because it will never be populated. Example: ```yaml contexts: - context: cluster: ... namespace: ... # this is ContextDetails.Namespace user: ... name: foo ``` ```yaml contexts: - context: cluster: ... namespace: ... user: ... name: foo namespace: ... # this is Context.Namespace ```
1 parent c1bab3c commit da3bff5

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

src/KubernetesClient/KubeConfigModels/Context.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace k8s.KubeConfigModels
22
{
3+
using System;
34
using YamlDotNet.Serialization;
45

56
/// <summary>
@@ -19,6 +20,7 @@ public class Context
1920
[YamlMember(Alias = "name")]
2021
public string Name { get; set; }
2122

23+
[Obsolete("This property is not set by the YAML config. Use ContextDetails.Namespace instead.")]
2224
[YamlMember(Alias = "namespace")]
2325
public string Namespace { get; set; }
2426
}

src/KubernetesClient/KubeConfigModels/ContextDetails.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class ContextDetails
1616
public string Cluster { get; set; }
1717

1818
/// <summary>
19-
/// Gets or sets the anem of the user for this context.
19+
/// Gets or sets the name of the user for this context.
2020
/// </summary>
2121
[YamlMember(Alias = "user")]
2222
public string User { get; set; }

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ private void InitializeContext(K8SConfiguration k8SConfig, string currentContext
193193
throw new KubeConfigException($"CurrentContext: {currentContext} not found in contexts in kubeconfig");
194194
}
195195

196+
if (string.IsNullOrEmpty(activeContext.ContextDetails?.Cluster))
197+
{
198+
// This serves as validation for any of the properties of ContextDetails being set.
199+
// Other locations in code assume that ContextDetails is non-null.
200+
throw new KubeConfigException($"Cluster not set for context `{currentContext}` in kubeconfig");
201+
}
202+
196203
CurrentContext = activeContext.Name;
197204

198205
// cluster
@@ -202,7 +209,7 @@ private void InitializeContext(K8SConfiguration k8SConfig, string currentContext
202209
SetUserDetails(k8SConfig, activeContext);
203210

204211
// namespace
205-
Namespace = activeContext.Namespace;
212+
Namespace = activeContext.ContextDetails?.Namespace;
206213
}
207214

208215
private void SetClusterDetails(K8SConfiguration k8SConfig, Context activeContext)
@@ -254,7 +261,7 @@ private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
254261

255262
if (userDetails == null)
256263
{
257-
throw new KubeConfigException("User not found for context {activeContext.Name} in kubeconfig");
264+
throw new KubeConfigException($"User not found for context {activeContext.Name} in kubeconfig");
258265
}
259266

260267
if (userDetails.UserCredentials == null)

tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ public void ContextHost(string context, string host)
2121
Assert.Equal(host, cfg.Host);
2222
}
2323

24+
/// <summary>
25+
/// Check if namespace is properly loaded, per context
26+
/// </summary>
27+
[Theory]
28+
[InlineData("federal-context", "chisel-ns")]
29+
[InlineData("queen-anne-context", "saw-ns")]
30+
public void ContextNamespace(string context, string @namespace)
31+
{
32+
var fi = new FileInfo("assets/kubeconfig.yml");
33+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context, useRelativePaths: false);
34+
Assert.Equal(@namespace, cfg.Namespace);
35+
}
36+
2437
/// <summary>
2538
/// Checks if user-based token is loaded properly from the config file, per context
2639
/// </summary>
@@ -190,6 +203,16 @@ public void NoContextsExplicit()
190203
KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context"));
191204
}
192205

206+
/// <summary>
207+
/// Checks that a KubeConfigException is thrown when the current context exists but has no details specified
208+
/// </summary>
209+
[Fact]
210+
public void ContextNoDetails()
211+
{
212+
var fi = new FileInfo("assets/kubeconfig.no-context-details.yml");
213+
Assert.Throws<KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
214+
}
215+
193216
/// <summary>
194217
/// Checks that a KubeConfigException is thrown when the server property is not set in cluster
195218
/// </summary>
@@ -413,7 +436,6 @@ private void AssertConfigEqual(K8SConfiguration expected, K8SConfiguration actua
413436
private void AssertContextEqual(Context expected, Context actual)
414437
{
415438
Assert.Equal(expected.Name, actual.Name);
416-
Assert.Equal(expected.Namespace, actual.Namespace);
417439
Assert.Equal(expected.ContextDetails.Cluster, actual.ContextDetails.Cluster);
418440
Assert.Equal(expected.ContextDetails.User, actual.ContextDetails.User);
419441
Assert.Equal(expected.ContextDetails.Namespace, actual.ContextDetails.Namespace);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Sample file based on https://kubernetes.io/docs/tasks/access-application-cluster/authenticate-across-clusters-kubeconfig/
2+
# WARNING: File includes minor fixes
3+
---
4+
current-context: federal-context
5+
apiVersion: v1
6+
clusters:
7+
- cluster:
8+
server: http://cow.org:8080
9+
name: cow-cluster
10+
- cluster:
11+
certificate-authority: assets/ca.crt
12+
server: https://horse.org:4443
13+
name: horse-cluster
14+
- cluster:
15+
insecure-skip-tls-verify: true
16+
server: https://pig.org:443
17+
name: pig-cluster
18+
contexts:
19+
- name: federal-context
20+
kind: Config
21+
users:
22+
- name: blue-user
23+
user:
24+
token: blue-token
25+
- name: green-user
26+
user:
27+
client-certificate: assets/client.crt
28+
client-key: assets/client.key
29+
- name: black-user
30+
user:
31+
token: black-token

0 commit comments

Comments
 (0)