Make LoadKubeConfig public and refactor to use YAML helper methods (#133)
* Made LoadKubeConfig public and refactored to use YAML helper methods * Addressing Code review feedback
This commit is contained in:
committed by
Brendan Burns
parent
ea62ca00e0
commit
df4d5dc31a
@@ -3,10 +3,10 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading.Tasks;
|
||||
using k8s.Exceptions;
|
||||
using k8s.KubeConfigModels;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
|
||||
namespace k8s
|
||||
{
|
||||
public partial class KubernetesClientConfiguration
|
||||
@@ -28,9 +28,9 @@ namespace k8s
|
||||
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration" /> from config file
|
||||
/// </summary>
|
||||
/// <param name="masterUrl">kube api server endpoint</param>
|
||||
/// <param name="kubeconfigPath">kubeconfig filepath</param>
|
||||
public static KubernetesClientConfiguration BuildConfigFromConfigFile(string masterUrl = null,
|
||||
string kubeconfigPath = null)
|
||||
/// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
|
||||
public static KubernetesClientConfiguration BuildConfigFromConfigFile(string kubeconfigPath,
|
||||
string currentContext = null, string masterUrl = null)
|
||||
{
|
||||
return BuildConfigFromConfigFile(new FileInfo(kubeconfigPath ?? KubeConfigDefaultLocation), null,
|
||||
masterUrl);
|
||||
@@ -55,25 +55,6 @@ namespace k8s
|
||||
return k8SConfiguration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null, whitespaced or empty</param>
|
||||
/// <param name="currentContext">override the context in config file, set null if do not want to override</param>
|
||||
/// <param name="masterUrl">overrider kube api server endpoint, set null if do not want to override</param>
|
||||
public static KubernetesClientConfiguration BuildConfigFromConfigFile(string kubeconfig,
|
||||
string currentContext = null, string masterUrl = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(kubeconfig))
|
||||
{
|
||||
throw new NullReferenceException(nameof(kubeconfig));
|
||||
}
|
||||
|
||||
var k8SConfig = LoadKubeConfig(kubeconfig);
|
||||
var k8SConfiguration = GetKubernetesClientConfiguration(currentContext, masterUrl, k8SConfig);
|
||||
|
||||
return k8SConfiguration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <param name="kubeconfig">Fileinfo of the kubeconfig, cannot be null, whitespaced or empty</param>
|
||||
@@ -94,7 +75,7 @@ namespace k8s
|
||||
|
||||
kubeconfig.Position = 0;
|
||||
|
||||
var k8SConfig = LoadKubeConfig(kubeconfig);
|
||||
var k8SConfig = Yaml.LoadFromStreamAsync<K8SConfiguration>(kubeconfig).GetAwaiter().GetResult();
|
||||
var k8SConfiguration = GetKubernetesClientConfiguration(currentContext, masterUrl, k8SConfig);
|
||||
|
||||
return k8SConfiguration;
|
||||
@@ -259,6 +240,46 @@ namespace k8s
|
||||
throw new KubeConfigException(
|
||||
$"User: {userDetails.Name} does not have appropriate auth credentials in kubeconfig");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads entire Kube Config from default or explicit file path
|
||||
/// </summary>
|
||||
/// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<K8SConfiguration> LoadKubeConfigAsync(string kubeconfigPath = null)
|
||||
{
|
||||
var fileInfo = new FileInfo(kubeconfigPath ?? KubeConfigDefaultLocation);
|
||||
|
||||
return await LoadKubeConfigAsync(fileInfo);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads entire Kube Config from default or explicit file path
|
||||
/// </summary>
|
||||
/// <param name="kubeconfigPath">Explicit file path to kubeconfig. Set to null to use the default file path</param>
|
||||
/// <returns></returns>
|
||||
public static K8SConfiguration LoadKubeConfig(string kubeconfigPath = null)
|
||||
{
|
||||
return LoadKubeConfigAsync(kubeconfigPath).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
// <summary>
|
||||
/// Loads Kube Config
|
||||
/// </summary>
|
||||
/// <param name="kubeconfig">Kube config file contents</param>
|
||||
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
|
||||
public static async Task<K8SConfiguration> LoadKubeConfigAsync(FileInfo kubeconfig)
|
||||
{
|
||||
if (!kubeconfig.Exists)
|
||||
{
|
||||
throw new KubeConfigException($"kubeconfig file not found at {kubeconfig.FullName}");
|
||||
}
|
||||
|
||||
using (var stream = kubeconfig.OpenRead())
|
||||
{
|
||||
return await Yaml.LoadFromStreamAsync<K8SConfiguration>(stream);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -266,46 +287,29 @@ namespace k8s
|
||||
/// </summary>
|
||||
/// <param name="kubeconfig">Kube config file contents</param>
|
||||
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
|
||||
private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
|
||||
{
|
||||
if (!kubeconfig.Exists)
|
||||
{
|
||||
throw new KubeConfigException($"kubeconfig file not found at {kubeconfig.FullName}");
|
||||
}
|
||||
|
||||
var deserializeBuilder = new DeserializerBuilder();
|
||||
var deserializer = deserializeBuilder.Build();
|
||||
using (var kubeConfigTextStream = kubeconfig.OpenText())
|
||||
{
|
||||
return deserializer.Deserialize<K8SConfiguration>(kubeConfigTextStream);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads Kube Config from string
|
||||
/// </summary>
|
||||
/// <param name="kubeconfig">Kube config file contents</param>
|
||||
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
|
||||
private static K8SConfiguration LoadKubeConfig(string kubeconfig)
|
||||
{
|
||||
|
||||
var deserializeBuilder = new DeserializerBuilder();
|
||||
var deserializer = deserializeBuilder.Build();
|
||||
return deserializer.Deserialize<K8SConfiguration>(kubeconfig);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads Kube Config from stream.
|
||||
/// </summary>
|
||||
/// <param name="kubeconfig">Kube config file contents</param>
|
||||
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
|
||||
private static K8SConfiguration LoadKubeConfig(Stream kubeconfig)
|
||||
public static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
|
||||
{
|
||||
using (var sr = new StreamReader(kubeconfig))
|
||||
{
|
||||
var strKubeConfig = sr.ReadToEnd();
|
||||
return LoadKubeConfig(strKubeConfig);
|
||||
}
|
||||
return LoadKubeConfigAsync(kubeconfig).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
// <summary>
|
||||
/// Loads Kube Config
|
||||
/// </summary>
|
||||
/// <param name="kubeconfigStream">Kube config file contents stream</param>
|
||||
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
|
||||
public static async Task<K8SConfiguration> LoadKubeConfigAsync(Stream kubeconfigStream)
|
||||
{
|
||||
return await Yaml.LoadFromStreamAsync<K8SConfiguration>(kubeconfigStream);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads Kube Config
|
||||
/// </summary>
|
||||
/// <param name="kubeconfig">Kube config file contents stream</param>
|
||||
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
|
||||
public static K8SConfiguration LoadKubeConfig(Stream kubeconfigStream)
|
||||
{
|
||||
return LoadKubeConfigAsync(kubeconfigStream).GetAwaiter().GetResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user