@@ -49,24 +49,27 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
49
49
throw new NullReferenceException ( nameof ( kubeconfig ) ) ;
50
50
}
51
51
52
- var k8SConfig = LoadKubeConfig ( kubeconfig ) ;
53
- var k8SConfiguration = new KubernetesClientConfiguration ( ) ;
54
-
55
- currentContext = currentContext ?? k8SConfig . CurrentContext ;
56
- // only init context if context if set
57
- if ( currentContext != null )
58
- {
59
- k8SConfiguration . InitializeContext ( k8SConfig , currentContext ) ;
60
- }
61
- if ( ! string . IsNullOrWhiteSpace ( masterUrl ) )
52
+ var k8SConfig = LoadKubeConfig ( kubeconfig ) ;
53
+ var k8SConfiguration = GetKubernetesClientConfiguration ( ref 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 ) )
62
67
{
63
- k8SConfiguration . Host = masterUrl ;
68
+ throw new NullReferenceException ( nameof ( kubeconfig ) ) ;
64
69
}
65
70
66
- if ( string . IsNullOrWhiteSpace ( k8SConfiguration . Host ) )
67
- {
68
- throw new KubeConfigException ( "Cannot infer server host url either from context or masterUrl" ) ;
69
- }
71
+ var k8SConfig = LoadKubeConfig ( kubeconfig ) ;
72
+ var k8SConfiguration = GetKubernetesClientConfiguration ( ref currentContext , masterUrl , k8SConfig ) ;
70
73
71
74
return k8SConfiguration ;
72
75
}
@@ -76,15 +79,22 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
76
79
/// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null, whitespaced or empty</param>
77
80
/// <param name="currentContext">override the context in config file, set null if do not want to override</param>
78
81
/// <param name="masterUrl">overrider kube api server endpoint, set null if do not want to override</param>
79
- public static KubernetesClientConfiguration BuildConfigFromConfigFileString ( string kubeconfig ,
82
+ public static KubernetesClientConfiguration BuildConfigFromConfigFile ( Stream kubeconfig ,
80
83
string currentContext = null , string masterUrl = null )
81
84
{
82
- if ( string . IsNullOrWhiteSpace ( kubeconfig ) )
85
+ if ( kubeconfig == null && kubeconfig . Length == 0 )
83
86
{
84
87
throw new NullReferenceException ( nameof ( kubeconfig ) ) ;
85
88
}
86
89
87
90
var k8SConfig = LoadKubeConfig ( kubeconfig ) ;
91
+ var k8SConfiguration = GetKubernetesClientConfiguration ( ref currentContext , masterUrl , k8SConfig ) ;
92
+
93
+ return k8SConfiguration ;
94
+ }
95
+
96
+ private static KubernetesClientConfiguration GetKubernetesClientConfiguration ( ref string currentContext , string masterUrl , K8SConfiguration k8SConfig )
97
+ {
88
98
var k8SConfiguration = new KubernetesClientConfiguration ( ) ;
89
99
90
100
currentContext = currentContext ?? k8SConfig . CurrentContext ;
@@ -101,11 +111,11 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFileString(stri
101
111
if ( string . IsNullOrWhiteSpace ( k8SConfiguration . Host ) )
102
112
{
103
113
throw new KubeConfigException ( "Cannot infer server host url either from context or masterUrl" ) ;
104
- }
105
-
106
- return k8SConfiguration ;
107
- }
108
-
114
+ }
115
+
116
+ return k8SConfiguration ;
117
+ }
118
+
109
119
/// <summary>
110
120
/// Validates and Intializes Client Configuration
111
121
/// </summary>
@@ -275,6 +285,18 @@ private static K8SConfiguration LoadKubeConfig(string kubeconfig)
275
285
var deserializeBuilder = new DeserializerBuilder ( ) ;
276
286
var deserializer = deserializeBuilder . Build ( ) ;
277
287
return deserializer . Deserialize < K8SConfiguration > ( kubeconfig ) ;
288
+ }
289
+
290
+ /// <summary>
291
+ /// Loads Kube Config from stream.
292
+ /// </summary>
293
+ /// <param name="kubeconfig">Kube config file contents</param>
294
+ /// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
295
+ private static K8SConfiguration LoadKubeConfig ( Stream kubeconfig )
296
+ {
297
+ StreamReader sr = new StreamReader ( kubeconfig ) ;
298
+ string strKubeConfig = sr . ReadToEnd ( ) ;
299
+ return LoadKubeConfig ( strKubeConfig ) ;
278
300
}
279
301
}
280
302
}
0 commit comments