Class KubeConfig
Used to manage serialization of Kubernetes kubeconfig files. These are used to manage cluster contexts on client machines: more information.
Namespace: Neon.Kube.Config
Assembly: Neon.Kube.dll
Syntax
public class KubeConfig
Constructors
KubeConfig()
Default constructor.
Declaration
public KubeConfig()
Properties
ApiVersion
Specifies cluster API server protocol version (defaults to v1).
Declaration
[JsonProperty(PropertyName = "apiVersion", Required = Required.Default, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[YamlMember(Alias = "apiVersion", ApplyNamingConventions = false)]
public string ApiVersion { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Cluster
Returns the current cluster or null.
Declaration
[JsonIgnore]
[YamlIgnore]
public KubeConfigCluster Cluster { get; }
Property Value
| Type | Description |
|---|---|
| KubeConfigCluster |
Clusters
Lists cluster configurations.
Declaration
[JsonProperty(PropertyName = "clusters", Required = Required.Always)]
[YamlMember(Alias = "clusters", ApplyNamingConventions = false)]
public List<KubeConfigCluster> Clusters { get; set; }
Property Value
| Type | Description |
|---|---|
| List<KubeConfigCluster> |
Context
Returns the current context or null.
Declaration
[JsonIgnore]
[YamlIgnore]
public KubeConfigContext Context { get; }
Property Value
| Type | Description |
|---|---|
| KubeConfigContext |
Contexts
Lists config contexts.
Declaration
[JsonProperty(PropertyName = "contexts", Required = Required.Always)]
[YamlMember(Alias = "contexts", ApplyNamingConventions = false)]
public List<KubeConfigContext> Contexts { get; set; }
Property Value
| Type | Description |
|---|---|
| List<KubeConfigContext> |
CurrentContext
Specifies the name of the current context or null when there is no current context.
Declaration
[JsonProperty(PropertyName = "current-context", Required = Required.Default, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[YamlMember(Alias = "current-context", ApplyNamingConventions = false)]
public string CurrentContext { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Kind
Identifies the document type: Config.
Declaration
[JsonProperty(PropertyName = "kind", Required = Required.Default, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[YamlMember(Alias = "kind", ApplyNamingConventions = false)]
public string Kind { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Preferences
Optional global kubeconfig preferences.
Declaration
[JsonProperty(PropertyName = "preferences", Required = Required.Default, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[YamlMember(Alias = "preferences", ApplyNamingConventions = false)]
public KubeConfigPreferences Preferences { get; set; }
Property Value
| Type | Description |
|---|---|
| KubeConfigPreferences |
User
Returns the current user or null.
Declaration
[JsonIgnore]
[YamlIgnore]
public KubeConfigUser User { get; }
Property Value
| Type | Description |
|---|---|
| KubeConfigUser |
Users
Lists the user configurations.
Declaration
[JsonProperty(PropertyName = "users", Required = Required.Always)]
[YamlMember(Alias = "users", ApplyNamingConventions = false)]
public List<KubeConfigUser> Users { get; set; }
Property Value
| Type | Description |
|---|---|
| List<KubeConfigUser> |
Methods
Clone(bool)
Constructs a deep clone of the instance.
Declaration
public KubeConfig Clone(bool currentOnly = false)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | currentOnly | Optionally strips out the non-current contexts, clusters, and users. |
Returns
| Type | Description |
|---|---|
| KubeConfig | The deep cloned KubeConfig. |
CloneAndSetContext(string)
Searches the config for the named context. If it's present, the method will clone
the config, make the named context as current and then remove all other contexts and
users. null will be returned if the named context doesn't exist.
This is handy when you need to operate on a cluster that's not the current one.
Declaration
public KubeConfig CloneAndSetContext(string contextName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | contextName |
Returns
| Type | Description |
|---|---|
| KubeConfig | The new KubeConfig with the specified context set or |
GetCluster(string)
Returns a Kubernetes context by name.
Declaration
public KubeConfigCluster GetCluster(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The cluster name. |
Returns
| Type | Description |
|---|---|
| KubeConfigCluster | The KubeConfigCluster or |
GetContext(KubeContextName)
Returns a Kubernetes context by name.
Declaration
public KubeConfigContext GetContext(KubeContextName name)
Parameters
| Type | Name | Description |
|---|---|---|
| KubeContextName | name | The raw context name. |
Returns
| Type | Description |
|---|---|
| KubeConfigContext | The KubeConfigContext or |
GetContext(string)
Returns a Kubernetes context using a raw context name.
Declaration
public KubeConfigContext GetContext(string rawName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | rawName | The raw context name. |
Returns
| Type | Description |
|---|---|
| KubeConfigContext | The KubeConfigContext or |
GetCurrent(out KubeConfigContext, out KubeConfigCluster, out KubeConfigUser)
Obtains the current context, cluster, and user in one go.
Declaration
public void GetCurrent(out KubeConfigContext context, out KubeConfigCluster cluster, out KubeConfigUser user)
Parameters
| Type | Name | Description |
|---|---|---|
| KubeConfigContext | context | Returns as the current context or |
| KubeConfigCluster | cluster | Returns as the current cluster or |
| KubeConfigUser | user | Returns as the current user or |
Remarks
note
This method returns null for all values when there is no current context
and when there is a current context, it ensures that the referenced context, cluster
and user actually exists, throwing an InvalidDataException when any
are missing.
Exceptions
| Type | Condition |
|---|---|
| InvalidDataException | Thrown when one or both of the referenced current context, cluster or user doesn't exist. |
GetUser(string)
Returns a Kubernetes user by name.
Declaration
public KubeConfigUser GetUser(string name)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The user name. |
Returns
| Type | Description |
|---|---|
| KubeConfigUser | The KubeConfigUser or |
Load(string)
Reads and returns information loaded from the user's ~/.kube/config file or the specified file.
Declaration
public static KubeConfig Load(string configPath = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | configPath | Optionally specifies the location of the kubeconfig file. This defaults to the user's ~/.kube/config file. |
Returns
| Type | Description |
|---|---|
| KubeConfig | The parsed KubeConfig or an empty config if the file doesn't exist. |
Exceptions
| Type | Condition |
|---|---|
| NeonKubeException | Thrown when the current config is invalid. |
Reload()
Reloads the kubeconfig from the global config file.
Declaration
public void Reload()
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when the config is not backed by a file. |
RemoveCluster(string)
Removes a cluster from the config as well as any contexts referencing it.
Declaration
public void RemoveCluster(string clusterName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | clusterName |
RemoveContext(KubeConfigContext, bool, bool)
Removes a Kubernetes context, if it exists.
Declaration
public void RemoveContext(KubeConfigContext context, bool removeClusterAndUser = true, bool noSave = false)
Parameters
| Type | Name | Description |
|---|---|---|
| KubeConfigContext | context | The context to be removed. |
| bool | removeClusterAndUser | Optionally disable removal of the referenced cluster and user if
they're not referenced elsewhere (defaults to |
| bool | noSave | Optionally prevent context save after the change. |
RemoveContext(KubeContextName, bool)
Removes a Kubernetes context by name, if it exists.
Declaration
public void RemoveContext(KubeContextName name, bool noSave = false)
Parameters
| Type | Name | Description |
|---|---|---|
| KubeContextName | name | The context name. |
| bool | noSave | Optionally prevent context save after the change. |
Save(string)
Persists the KubeConfig to the user's ~/.kube/config file or the specified file.
Declaration
public void Save(string configPath = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | configPath | Optionally specifies the location of the kubeconfig file. This defaults to the path the config was loaded from. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when the config was not loaded from a file and |
SetContext(string)
Sets the current context.
Declaration
public void SetContext(string contextName = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | contextName | The name of the current context or |
Exceptions
| Type | Condition |
|---|---|
| NeonKubeException | Thrown if the context does not exist. |
Validate(bool)
Validates the kubeconfig.
Declaration
public void Validate(bool needsCurrentCluster = false)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | needsCurrentCluster | Optionally specifies that the config must have a current context identifying the cluster and user. |
Exceptions
| Type | Condition |
|---|---|
| NeonKubeException | Thrown when the current config is invalid. |