@@ -68,6 +68,41 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
68
68
throw new KubeConfigException ( "Cannot infer server host url either from context or masterUrl" ) ;
69
69
}
70
70
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
+
71
106
return k8SConfiguration ;
72
107
}
73
108
@@ -227,6 +262,19 @@ private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
227
262
{
228
263
return deserializer . Deserialize < K8SConfiguration > ( kubeConfigTextStream ) ;
229
264
}
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 ) ;
230
278
}
231
279
}
232
280
}
0 commit comments