@@ -50,6 +50,58 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
50
50
}
51
51
52
52
var k8SConfig = LoadKubeConfig ( kubeconfig ) ;
53
+ var k8SConfiguration = GetKubernetesClientConfiguration ( currentContext , masterUrl , k8SConfig ) ;
54
+
55
+ return k8SConfiguration ;
56
+ }
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
+ /// <summary>
78
+ /// </summary>
79
+ /// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null, whitespaced or empty</param>
80
+ /// <param name="currentContext">override the context in config file, set null if do not want to override</param>
81
+ /// <param name="masterUrl">overrider kube api server endpoint, set null if do not want to override</param>
82
+ public static KubernetesClientConfiguration BuildConfigFromConfigFile ( Stream kubeconfig ,
83
+ string currentContext = null , string masterUrl = null )
84
+ {
85
+ if ( kubeconfig == null )
86
+ {
87
+ throw new NullReferenceException ( nameof ( kubeconfig ) ) ;
88
+ }
89
+
90
+ if ( ! kubeconfig . CanSeek )
91
+ {
92
+ throw new Exception ( "Stream don't support seeking!" ) ;
93
+ }
94
+
95
+ kubeconfig . Position = 0 ;
96
+
97
+ var k8SConfig = LoadKubeConfig ( kubeconfig ) ;
98
+ var k8SConfiguration = GetKubernetesClientConfiguration ( currentContext , masterUrl , k8SConfig ) ;
99
+
100
+ return k8SConfiguration ;
101
+ }
102
+
103
+ private static KubernetesClientConfiguration GetKubernetesClientConfiguration ( string currentContext , string masterUrl , K8SConfiguration k8SConfig )
104
+ {
53
105
var k8SConfiguration = new KubernetesClientConfiguration ( ) ;
54
106
55
107
currentContext = currentContext ?? k8SConfig . CurrentContext ;
@@ -228,5 +280,32 @@ private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
228
280
return deserializer . Deserialize < K8SConfiguration > ( kubeConfigTextStream ) ;
229
281
}
230
282
}
283
+
284
+ /// <summary>
285
+ /// Loads Kube Config from string
286
+ /// </summary>
287
+ /// <param name="kubeconfig">Kube config file contents</param>
288
+ /// <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 ) ;
295
+ }
296
+
297
+ /// <summary>
298
+ /// Loads Kube Config from stream.
299
+ /// </summary>
300
+ /// <param name="kubeconfig">Kube config file contents</param>
301
+ /// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
302
+ private static K8SConfiguration LoadKubeConfig ( Stream kubeconfig )
303
+ {
304
+ using ( var sr = new StreamReader ( kubeconfig ) )
305
+ {
306
+ var strKubeConfig = sr . ReadToEnd ( ) ;
307
+ return LoadKubeConfig ( strKubeConfig ) ;
308
+ }
309
+ }
231
310
}
232
311
}
0 commit comments