3
3
using System . Linq ;
4
4
using System . Runtime . InteropServices ;
5
5
using System . Security . Cryptography . X509Certificates ;
6
+ using System . Threading . Tasks ;
6
7
using k8s . Exceptions ;
7
8
using k8s . KubeConfigModels ;
8
- using YamlDotNet . Serialization ;
9
-
9
+
10
10
namespace k8s
11
11
{
12
12
public partial class KubernetesClientConfiguration
@@ -28,9 +28,9 @@ public partial class KubernetesClientConfiguration
28
28
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration" /> from config file
29
29
/// </summary>
30
30
/// <param name="masterUrl">kube api server endpoint</param>
31
- /// <param name="kubeconfigPath">kubeconfig filepath </param>
32
- public static KubernetesClientConfiguration BuildConfigFromConfigFile ( string masterUrl = null ,
33
- string kubeconfigPath = null )
31
+ /// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path </param>
32
+ public static KubernetesClientConfiguration BuildConfigFromConfigFile ( string kubeconfigPath ,
33
+ string currentContext = null , string masterUrl = null )
34
34
{
35
35
return BuildConfigFromConfigFile ( new FileInfo ( kubeconfigPath ?? KubeConfigDefaultLocation ) , null ,
36
36
masterUrl ) ;
@@ -55,25 +55,6 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
55
55
return k8SConfiguration ;
56
56
}
57
57
58
- /// <summary>
59
- /// </summary>
60
- /// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null, whitespaced or empty</param>
61
- /// <param name="currentContext">override the context in config file, set null if do not want to override</param>
62
- /// <param name="masterUrl">overrider kube api server endpoint, set null if do not want to override</param>
63
- public static KubernetesClientConfiguration BuildConfigFromConfigFile ( string kubeconfig ,
64
- string currentContext = null , string masterUrl = null )
65
- {
66
- if ( string . IsNullOrWhiteSpace ( kubeconfig ) )
67
- {
68
- throw new NullReferenceException ( nameof ( kubeconfig ) ) ;
69
- }
70
-
71
- var k8SConfig = LoadKubeConfig ( kubeconfig ) ;
72
- var k8SConfiguration = GetKubernetesClientConfiguration ( currentContext , masterUrl , k8SConfig ) ;
73
-
74
- return k8SConfiguration ;
75
- }
76
-
77
58
/// <summary>
78
59
/// </summary>
79
60
/// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null, whitespaced or empty</param>
@@ -94,7 +75,7 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(Stream kub
94
75
95
76
kubeconfig . Position = 0 ;
96
77
97
- var k8SConfig = LoadKubeConfig ( kubeconfig ) ;
78
+ var k8SConfig = Yaml . LoadFromStreamAsync < K8SConfiguration > ( kubeconfig ) . GetAwaiter ( ) . GetResult ( ) ;
98
79
var k8SConfiguration = GetKubernetesClientConfiguration ( currentContext , masterUrl , k8SConfig ) ;
99
80
100
81
return k8SConfiguration ;
@@ -259,53 +240,76 @@ private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
259
240
throw new KubeConfigException (
260
241
$ "User: { userDetails . Name } does not have appropriate auth credentials in kubeconfig") ;
261
242
}
262
- }
263
-
243
+ }
244
+
264
245
/// <summary>
246
+ /// Loads entire Kube Config from default or explicit file path
247
+ /// </summary>
248
+ /// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
249
+ /// <returns></returns>
250
+ public static async Task < K8SConfiguration > LoadKubeConfigAsync ( string kubeconfigPath = null )
251
+ {
252
+ var fileInfo = new FileInfo ( kubeconfigPath ?? KubeConfigDefaultLocation ) ;
253
+
254
+ return await LoadKubeConfigAsync ( fileInfo ) ;
255
+ }
256
+
257
+ /// <summary>
258
+ /// Loads entire Kube Config from default or explicit file path
259
+ /// </summary>
260
+ /// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
261
+ /// <returns></returns>
262
+ public static K8SConfiguration LoadKubeConfig ( string kubeconfigPath = null )
263
+ {
264
+ return LoadKubeConfigAsync ( kubeconfigPath ) . GetAwaiter ( ) . GetResult ( ) ;
265
+ }
266
+
267
+ // <summary>
265
268
/// Loads Kube Config
266
269
/// </summary>
267
270
/// <param name="kubeconfig">Kube config file contents</param>
268
271
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
269
- private static K8SConfiguration LoadKubeConfig ( FileInfo kubeconfig )
272
+ public static async Task < K8SConfiguration > LoadKubeConfigAsync ( FileInfo kubeconfig )
270
273
{
271
274
if ( ! kubeconfig . Exists )
272
275
{
273
276
throw new KubeConfigException ( $ "kubeconfig file not found at { kubeconfig . FullName } ") ;
274
- }
275
-
276
- var deserializeBuilder = new DeserializerBuilder ( ) ;
277
- var deserializer = deserializeBuilder . Build ( ) ;
278
- using ( var kubeConfigTextStream = kubeconfig . OpenText ( ) )
279
- {
280
- return deserializer . Deserialize < K8SConfiguration > ( kubeConfigTextStream ) ;
277
+ }
278
+
279
+ using ( var stream = kubeconfig . OpenRead ( ) )
280
+ {
281
+ return await Yaml . LoadFromStreamAsync < K8SConfiguration > ( stream ) ;
281
282
}
282
283
}
283
284
284
285
/// <summary>
285
- /// Loads Kube Config from string
286
+ /// Loads Kube Config
286
287
/// </summary>
287
288
/// <param name="kubeconfig">Kube config file contents</param>
288
289
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
289
- private static K8SConfiguration LoadKubeConfig ( string kubeconfig )
290
- {
291
-
292
- var deserializeBuilder = new DeserializerBuilder ( ) ;
293
- var deserializer = deserializeBuilder . Build ( ) ;
294
- return deserializer . Deserialize < K8SConfiguration > ( kubeconfig ) ;
290
+ public static K8SConfiguration LoadKubeConfig ( FileInfo kubeconfig )
291
+ {
292
+ return LoadKubeConfigAsync ( kubeconfig ) . GetAwaiter ( ) . GetResult ( ) ;
293
+ }
294
+
295
+ // <summary>
296
+ /// Loads Kube Config
297
+ /// </summary>
298
+ /// <param name="kubeconfigStream">Kube config file contents stream</param>
299
+ /// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
300
+ public static async Task < K8SConfiguration > LoadKubeConfigAsync ( Stream kubeconfigStream )
301
+ {
302
+ return await Yaml . LoadFromStreamAsync < K8SConfiguration > ( kubeconfigStream ) ;
295
303
}
296
304
297
305
/// <summary>
298
- /// Loads Kube Config from stream.
306
+ /// Loads Kube Config
299
307
/// </summary>
300
- /// <param name="kubeconfig">Kube config file contents</param>
308
+ /// <param name="kubeconfig">Kube config file contents stream </param>
301
309
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
302
- private static K8SConfiguration LoadKubeConfig ( Stream kubeconfig )
310
+ public static K8SConfiguration LoadKubeConfig ( Stream kubeconfigStream )
303
311
{
304
- using ( var sr = new StreamReader ( kubeconfig ) )
305
- {
306
- var strKubeConfig = sr . ReadToEnd ( ) ;
307
- return LoadKubeConfig ( strKubeConfig ) ;
308
- }
312
+ return LoadKubeConfigAsync ( kubeconfigStream ) . GetAwaiter ( ) . GetResult ( ) ;
309
313
}
310
314
}
311
315
}
0 commit comments