Skip to content

Commit f68ceee

Browse files
committed
adds supports for loading string config content in KubernetesClientConfiguration
1 parent edc0cfb commit f68ceee

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,41 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
6868
throw new KubeConfigException("Cannot infer server host url either from context or masterUrl");
6969
}
7070

71+
return k8SConfiguration;
72+
}
73+
74+
/// <summary>
75+
/// </summary>
76+
/// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null, whitespaced or empty</param>
77+
/// <param name="currentContext">override the context in config file, set null if do not want to override</param>
78+
/// <param name="masterUrl">overrider kube api server endpoint, set null if do not want to override</param>
79+
public static KubernetesClientConfiguration BuildConfigFromConfigFileString(string kubeconfig,
80+
string currentContext = null, string masterUrl = null)
81+
{
82+
if (string.IsNullOrWhiteSpace(kubeconfig))
83+
{
84+
throw new NullReferenceException(nameof(kubeconfig));
85+
}
86+
87+
var k8SConfig = LoadKubeConfig(kubeconfig);
88+
var k8SConfiguration = new KubernetesClientConfiguration();
89+
90+
currentContext = currentContext ?? k8SConfig.CurrentContext;
91+
// only init context if context if set
92+
if (currentContext != null)
93+
{
94+
k8SConfiguration.InitializeContext(k8SConfig, currentContext);
95+
}
96+
if (!string.IsNullOrWhiteSpace(masterUrl))
97+
{
98+
k8SConfiguration.Host = masterUrl;
99+
}
100+
101+
if (string.IsNullOrWhiteSpace(k8SConfiguration.Host))
102+
{
103+
throw new KubeConfigException("Cannot infer server host url either from context or masterUrl");
104+
}
105+
71106
return k8SConfiguration;
72107
}
73108

@@ -227,6 +262,19 @@ private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
227262
{
228263
return deserializer.Deserialize<K8SConfiguration>(kubeConfigTextStream);
229264
}
265+
}
266+
267+
/// <summary>
268+
/// Loads Kube Config from string
269+
/// </summary>
270+
/// <param name="kubeconfig">Kube config file contents</param>
271+
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
272+
private static K8SConfiguration LoadKubeConfig(string kubeconfig)
273+
{
274+
275+
var deserializeBuilder = new DeserializerBuilder();
276+
var deserializer = deserializeBuilder.Build();
277+
return deserializer.Deserialize<K8SConfiguration>(kubeconfig);
230278
}
231279
}
232280
}

tests/KubernetesClientConfigurationTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,17 @@ public void DeletedConfigurationFile()
284284
{
285285
File.Delete(tempFileInfo.FullName);
286286
}
287+
}
288+
289+
/// <summary>
290+
/// Checks Host is loaded from the default configuration file as a string
291+
/// </summary>
292+
[Fact]
293+
public void DefaultConfigurationAsStringLoaded()
294+
{
295+
var txt = File.ReadAllText("assets/kubeconfig.yml");
296+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFileString(txt);
297+
Assert.NotNull(cfg.Host);
287298
}
288299
}
289300
}

0 commit comments

Comments
 (0)