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
```
This commit is contained in:
Ryan Nowak
2020-03-17 09:48:23 -07:00
committed by GitHub
parent c1bab3caed
commit da3bff5b3e
5 changed files with 66 additions and 4 deletions

View File

@@ -193,6 +193,13 @@ namespace k8s
throw new KubeConfigException($"CurrentContext: {currentContext} not found in contexts in kubeconfig");
}
if (string.IsNullOrEmpty(activeContext.ContextDetails?.Cluster))
{
// This serves as validation for any of the properties of ContextDetails being set.
// Other locations in code assume that ContextDetails is non-null.
throw new KubeConfigException($"Cluster not set for context `{currentContext}` in kubeconfig");
}
CurrentContext = activeContext.Name;
// cluster
@@ -202,7 +209,7 @@ namespace k8s
SetUserDetails(k8SConfig, activeContext);
// namespace
Namespace = activeContext.Namespace;
Namespace = activeContext.ContextDetails?.Namespace;
}
private void SetClusterDetails(K8SConfiguration k8SConfig, Context activeContext)
@@ -254,7 +261,7 @@ namespace k8s
if (userDetails == null)
{
throw new KubeConfigException("User not found for context {activeContext.Name} in kubeconfig");
throw new KubeConfigException($"User not found for context {activeContext.Name} in kubeconfig");
}
if (userDetails.UserCredentials == null)