Skip to content

Commit ba9755b

Browse files
Improve async config loading. (#353)
1 parent fa51586 commit ba9755b

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,28 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(string kub
7070
/// file is located. When <see langword="false"/>, the paths will be considered to be relative to the current working directory.</param>
7171
public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo kubeconfig,
7272
string currentContext = null, string masterUrl = null, bool useRelativePaths = true)
73+
{
74+
return BuildConfigFromConfigFileAsync(kubeconfig, currentContext, masterUrl, useRelativePaths).GetAwaiter().GetResult();
75+
}
76+
77+
/// <summary>
78+
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration" /> from config file
79+
/// </summary>
80+
/// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null</param>
81+
/// <param name="currentContext">override the context in config file, set null if do not want to override</param>
82+
/// <param name="masterUrl">override the kube api server endpoint, set null if do not want to override</param>
83+
/// <param name="useRelativePaths">When <see langword="true"/>, the paths in the kubeconfig file will be considered to be relative to the directory in which the kubeconfig
84+
/// file is located. When <see langword="false"/>, the paths will be considered to be relative to the current working directory.</param>
85+
86+
public static async Task<KubernetesClientConfiguration> BuildConfigFromConfigFileAsync(FileInfo kubeconfig,
87+
string currentContext = null, string masterUrl = null, bool useRelativePaths = true)
7388
{
7489
if (kubeconfig == null)
7590
{
7691
throw new NullReferenceException(nameof(kubeconfig));
7792
}
7893

79-
var k8SConfig = LoadKubeConfig(kubeconfig, useRelativePaths);
94+
var k8SConfig = await LoadKubeConfigAsync(kubeconfig, useRelativePaths);
8095
var k8SConfiguration = GetKubernetesClientConfiguration(currentContext, masterUrl, k8SConfig);
8196

8297
return k8SConfiguration;
@@ -90,6 +105,18 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
90105
/// <param name="masterUrl">Override the Kubernetes API server endpoint, set null if do not want to override</param>
91106
public static KubernetesClientConfiguration BuildConfigFromConfigFile(Stream kubeconfig,
92107
string currentContext = null, string masterUrl = null)
108+
{
109+
return BuildConfigFromConfigFileAsync(kubeconfig, currentContext, masterUrl).GetAwaiter().GetResult();
110+
}
111+
112+
/// <summary>
113+
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration" /> from config file
114+
/// </summary>
115+
/// <param name="kubeconfig">Stream of the kubeconfig, cannot be null</param>
116+
/// <param name="currentContext">Override the current context in config, set null if do not want to override</param>
117+
/// <param name="masterUrl">Override the Kubernetes API server endpoint, set null if do not want to override</param>
118+
public static async Task<KubernetesClientConfiguration> BuildConfigFromConfigFileAsync(Stream kubeconfig,
119+
string currentContext = null, string masterUrl = null)
93120
{
94121
if (kubeconfig == null)
95122
{
@@ -103,7 +130,7 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(Stream kub
103130

104131
kubeconfig.Position = 0;
105132

106-
var k8SConfig = Yaml.LoadFromStreamAsync<K8SConfiguration>(kubeconfig).GetAwaiter().GetResult();
133+
var k8SConfig = await Yaml.LoadFromStreamAsync<K8SConfiguration>(kubeconfig);
107134
var k8SConfiguration = GetKubernetesClientConfiguration(currentContext, masterUrl, k8SConfig);
108135

109136
return k8SConfiguration;

0 commit comments

Comments
 (0)