Skip to content

Commit 7c9cc88

Browse files
itowlsonk8s-ci-robot
authored andcommitted
Create KubernetesClientConfiguration from pre-loaded K8SConfiguration (#213)
* Create KubernetesClientConfiguration from pre-loaded K8SConfiguration * Tweaked a couple more doc strings * Fix duplicated tests * Give the new method a more informative name
1 parent 355d4a3 commit 7c9cc88

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(string kub
3939
}
4040

4141
/// <summary>
42+
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration" /> from config file
4243
/// </summary>
4344
/// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null</param>
4445
/// <param name="currentContext">override the context in config file, set null if do not want to override</param>
@@ -60,10 +61,11 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
6061
}
6162

6263
/// <summary>
64+
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration" /> from config file
6365
/// </summary>
64-
/// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null, whitespaced or empty</param>
65-
/// <param name="currentContext">override the context in config file, set null if do not want to override</param>
66-
/// <param name="masterUrl">overrider kube api server endpoint, set null if do not want to override</param>
66+
/// <param name="kubeconfig">Stream of the kubeconfig, cannot be null</param>
67+
/// <param name="currentContext">Override the current context in config, set null if do not want to override</param>
68+
/// <param name="masterUrl">Override the Kubernetes API server endpoint, set null if do not want to override</param>
6769
public static KubernetesClientConfiguration BuildConfigFromConfigFile(Stream kubeconfig,
6870
string currentContext = null, string masterUrl = null)
6971
{
@@ -85,6 +87,15 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(Stream kub
8587
return k8SConfiguration;
8688
}
8789

90+
/// <summary>
91+
/// Initializes a new instance of <see cref="KubernetesClientConfiguration"/> from pre-loaded config object.
92+
/// </summary>
93+
/// <param name="k8sConfig">A <see cref="K8SConfiguration"/>, for example loaded from <see cref="LoadKubeConfigAsync(string, bool)" /></param>
94+
/// <param name="currentContext">Override the current context in config, set null if do not want to override</param>
95+
/// <param name="masterUrl">Override the Kubernetes API server endpoint, set null if do not want to override</param>
96+
public static KubernetesClientConfiguration BuildConfigFromConfigObject(K8SConfiguration k8SConfig, string currentContext = null, string masterUrl = null)
97+
=> GetKubernetesClientConfiguration(currentContext, masterUrl, k8SConfig);
98+
8899
private static KubernetesClientConfiguration GetKubernetesClientConfiguration(string currentContext, string masterUrl, K8SConfiguration k8SConfig)
89100
{
90101
var k8SConfiguration = new KubernetesClientConfiguration();

tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ public void ContextNotFound()
138138
KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context-not-found"));
139139
}
140140

141+
[Fact]
142+
public void CreatedFromPreLoadedConfig()
143+
{
144+
var k8sConfig = KubernetesClientConfiguration.LoadKubeConfig(new FileInfo("assets/kubeconfig.yml"), useRelativePaths: false);
145+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigObject(k8sConfig);
146+
Assert.NotNull(cfg.Host);
147+
}
148+
141149
/// <summary>
142150
/// Checks Host is loaded from the default configuration file
143151
/// </summary>

0 commit comments

Comments
 (0)