remove ctor api of KubernetesClientConfiguration, should use factory style
This commit is contained in:
@@ -24,17 +24,6 @@ namespace k8s
|
|||||||
? Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), @".kube\config")
|
? Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), @".kube\config")
|
||||||
: Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".kube/config");
|
: Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".kube/config");
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration"/> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="kubeconfig">kubeconfig file info</param>
|
|
||||||
/// <param name="currentContext">Context to use from kube config</param>
|
|
||||||
public KubernetesClientConfiguration(FileInfo kubeconfig = null, string currentContext = null)
|
|
||||||
{
|
|
||||||
var k8SConfig = LoadKubeConfig(kubeconfig ?? new FileInfo(KubeConfigDefaultLocation));
|
|
||||||
this.Initialize(k8SConfig, currentContext);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration"/> from config file
|
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration"/> from config file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -60,7 +49,7 @@ namespace k8s
|
|||||||
|
|
||||||
var k8SConfig = LoadKubeConfig(kubeconfig);
|
var k8SConfig = LoadKubeConfig(kubeconfig);
|
||||||
var k8SConfiguration = new KubernetesClientConfiguration();
|
var k8SConfiguration = new KubernetesClientConfiguration();
|
||||||
k8SConfiguration.Initialize(k8SConfig);
|
k8SConfiguration.Initialize(k8SConfig, currentContext);
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(masterUrl))
|
if (!string.IsNullOrWhiteSpace(masterUrl))
|
||||||
{
|
{
|
||||||
@@ -200,7 +189,7 @@ namespace k8s
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Loads Kube Config
|
/// Loads Kube Config
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="config">Kube config file contents</param>
|
/// <param name="kubeconfig">Kube config file contents</param>
|
||||||
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
|
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
|
||||||
private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
|
private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,78 +1,67 @@
|
|||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
|
||||||
namespace k8s
|
namespace k8s
|
||||||
{
|
{
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
using k8s.Exceptions;
|
|
||||||
using k8s.KubeConfigModels;
|
|
||||||
using YamlDotNet.Serialization;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a set of kubernetes client configuration settings
|
/// Represents a set of kubernetes client configuration settings
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class KubernetesClientConfiguration
|
public partial class KubernetesClientConfiguration
|
||||||
{
|
{
|
||||||
public KubernetesClientConfiguration()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets Host
|
/// Gets Host
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Host { get; set; }
|
public string Host { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets SslCaCert
|
/// Gets SslCaCert
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public X509Certificate2 SslCaCert { get; set; }
|
public X509Certificate2 SslCaCert { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets ClientCertificateData
|
/// Gets ClientCertificateData
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string ClientCertificateData { get; set; }
|
public string ClientCertificateData { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets ClientCertificate Key
|
/// Gets ClientCertificate Key
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string ClientCertificateKeyData { get; set; }
|
public string ClientCertificateKeyData { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets ClientCertificate filename
|
/// Gets ClientCertificate filename
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string ClientCertificateFilePath { get; set; }
|
public string ClientCertificateFilePath { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets ClientCertificate Key filename
|
/// Gets ClientCertificate Key filename
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string ClientKeyFilePath { get; set; }
|
public string ClientKeyFilePath { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a value indicating whether to skip ssl server cert validation
|
/// Gets a value indicating whether to skip ssl server cert validation
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool SkipTlsVerify { get; set; }
|
public bool SkipTlsVerify { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the HTTP user agent.
|
/// Gets or sets the HTTP user agent.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>Http user agent.</value>
|
/// <value>Http user agent.</value>
|
||||||
public string UserAgent { get; set; }
|
public string UserAgent { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the username (HTTP basic authentication).
|
/// Gets or sets the username (HTTP basic authentication).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The username.</value>
|
/// <value>The username.</value>
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the password (HTTP basic authentication).
|
/// Gets or sets the password (HTTP basic authentication).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The password.</value>
|
/// <value>The password.</value>
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the access token for OAuth2 authentication.
|
/// Gets or sets the access token for OAuth2 authentication.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The access token.</value>
|
/// <value>The access token.</value>
|
||||||
public string AccessToken { get; set; }
|
public string AccessToken { get; set; }
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace k8s.Tests
|
|||||||
public void LoadFromFiles()
|
public void LoadFromFiles()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigFileName);
|
var fi = new FileInfo(kubeConfigFileName);
|
||||||
var cfg = new KubernetesClientConfiguration(fi, "federal-context");
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "federal-context");
|
||||||
|
|
||||||
// Just validate that this doesn't throw and private key is non-null
|
// Just validate that this doesn't throw and private key is non-null
|
||||||
var cert = CertUtils.GeneratePfx(cfg);
|
var cert = CertUtils.GeneratePfx(cfg);
|
||||||
@@ -33,7 +33,7 @@ namespace k8s.Tests
|
|||||||
public void LoadFromInlineData()
|
public void LoadFromInlineData()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigFileName);
|
var fi = new FileInfo(kubeConfigFileName);
|
||||||
var cfg = new KubernetesClientConfiguration(fi, "victorian-context");
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "victorian-context");
|
||||||
|
|
||||||
// Just validate that this doesn't throw and private key is non-null
|
// Just validate that this doesn't throw and private key is non-null
|
||||||
var cert = CertUtils.GeneratePfx(cfg);
|
var cert = CertUtils.GeneratePfx(cfg);
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ namespace k8s.Tests
|
|||||||
public void ConfigurationFileNotFound()
|
public void ConfigurationFileNotFound()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo("/path/to/nowhere");
|
var fi = new FileInfo("/path/to/nowhere");
|
||||||
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
|
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -73,7 +73,7 @@ namespace k8s.Tests
|
|||||||
[Fact]
|
[Fact]
|
||||||
public void DefaultConfigurationLoaded()
|
public void DefaultConfigurationLoaded()
|
||||||
{
|
{
|
||||||
var cfg = new KubernetesClientConfiguration(new FileInfo(kubeConfigFileName));
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(new FileInfo(kubeConfigFileName));
|
||||||
Assert.NotNull(cfg.Host);
|
Assert.NotNull(cfg.Host);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -86,7 +86,7 @@ namespace k8s.Tests
|
|||||||
public void ContextHost(string context, string host)
|
public void ContextHost(string context, string host)
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigFileName);
|
var fi = new FileInfo(kubeConfigFileName);
|
||||||
var cfg = new KubernetesClientConfiguration(fi, context);
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
|
||||||
Assert.Equal(host, cfg.Host);
|
Assert.Equal(host, cfg.Host);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +100,7 @@ namespace k8s.Tests
|
|||||||
public void ContextUserToken(string context, string token)
|
public void ContextUserToken(string context, string token)
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigFileName);
|
var fi = new FileInfo(kubeConfigFileName);
|
||||||
var cfg = new KubernetesClientConfiguration(fi, context);
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
|
||||||
Assert.Equal(context, cfg.CurrentContext);
|
Assert.Equal(context, cfg.CurrentContext);
|
||||||
Assert.Null(cfg.Username);
|
Assert.Null(cfg.Username);
|
||||||
Assert.Equal(token, cfg.AccessToken);
|
Assert.Equal(token, cfg.AccessToken);
|
||||||
@@ -117,7 +117,7 @@ namespace k8s.Tests
|
|||||||
public void ContextCertificateTest(string context, string clientCert, string clientCertKey)
|
public void ContextCertificateTest(string context, string clientCert, string clientCertKey)
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigFileName);
|
var fi = new FileInfo(kubeConfigFileName);
|
||||||
var cfg = new KubernetesClientConfiguration(fi, context);
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
|
||||||
Assert.Equal(context, cfg.CurrentContext);
|
Assert.Equal(context, cfg.CurrentContext);
|
||||||
Assert.Equal(cfg.ClientCertificateFilePath, clientCert);
|
Assert.Equal(cfg.ClientCertificateFilePath, clientCert);
|
||||||
Assert.Equal(cfg.ClientKeyFilePath, clientCertKey);
|
Assert.Equal(cfg.ClientKeyFilePath, clientCertKey);
|
||||||
@@ -132,7 +132,7 @@ namespace k8s.Tests
|
|||||||
public void ClientDataTest(string context)
|
public void ClientDataTest(string context)
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigFileName);
|
var fi = new FileInfo(kubeConfigFileName);
|
||||||
var cfg = new KubernetesClientConfiguration(fi, context);
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
|
||||||
Assert.Equal(context, cfg.CurrentContext);
|
Assert.Equal(context, cfg.CurrentContext);
|
||||||
Assert.NotNull(cfg.SslCaCert);
|
Assert.NotNull(cfg.SslCaCert);
|
||||||
Assert.Equal(readLine("assets/client-certificate-data.txt"), cfg.ClientCertificateData);
|
Assert.Equal(readLine("assets/client-certificate-data.txt"), cfg.ClientCertificateData);
|
||||||
@@ -147,7 +147,7 @@ namespace k8s.Tests
|
|||||||
public void ContextNotFound()
|
public void ContextNotFound()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigFileName);
|
var fi = new FileInfo(kubeConfigFileName);
|
||||||
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context-not-found"));
|
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context-not-found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -157,7 +157,7 @@ namespace k8s.Tests
|
|||||||
public void NoContexts()
|
public void NoContexts()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigNoContexts);
|
var fi = new FileInfo(kubeConfigNoContexts);
|
||||||
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
|
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -167,7 +167,7 @@ namespace k8s.Tests
|
|||||||
public void NoContextsExplicit()
|
public void NoContextsExplicit()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigNoContexts);
|
var fi = new FileInfo(kubeConfigNoContexts);
|
||||||
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context"));
|
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -177,7 +177,7 @@ namespace k8s.Tests
|
|||||||
public void UserPasswordAuthentication()
|
public void UserPasswordAuthentication()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigUserPassword);
|
var fi = new FileInfo(kubeConfigUserPassword);
|
||||||
var cfg = new KubernetesClientConfiguration(fi);
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
|
||||||
Assert.Equal("admin", cfg.Username);
|
Assert.Equal("admin", cfg.Username);
|
||||||
Assert.Equal("secret", cfg.Password);
|
Assert.Equal("secret", cfg.Password);
|
||||||
}
|
}
|
||||||
@@ -189,7 +189,7 @@ namespace k8s.Tests
|
|||||||
public void IncompleteUserCredentials()
|
public void IncompleteUserCredentials()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigNoCredentials);
|
var fi = new FileInfo(kubeConfigNoCredentials);
|
||||||
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
|
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -199,7 +199,7 @@ namespace k8s.Tests
|
|||||||
public void ServerNotFound()
|
public void ServerNotFound()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigNoServer);
|
var fi = new FileInfo(kubeConfigNoServer);
|
||||||
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
|
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -209,7 +209,7 @@ namespace k8s.Tests
|
|||||||
public void ClusterNotFound()
|
public void ClusterNotFound()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigNoCluster);
|
var fi = new FileInfo(kubeConfigNoCluster);
|
||||||
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
|
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -219,7 +219,7 @@ namespace k8s.Tests
|
|||||||
public void ClusterNameMissmatch()
|
public void ClusterNameMissmatch()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigClusterMissmatch);
|
var fi = new FileInfo(kubeConfigClusterMissmatch);
|
||||||
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
|
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -229,7 +229,7 @@ namespace k8s.Tests
|
|||||||
public void CheckClusterTlsCorrectness()
|
public void CheckClusterTlsCorrectness()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigTlsNoSkipError);
|
var fi = new FileInfo(kubeConfigTlsNoSkipError);
|
||||||
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
|
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -239,7 +239,7 @@ namespace k8s.Tests
|
|||||||
public void CheckClusterTlsSkipCorrectness()
|
public void CheckClusterTlsSkipCorrectness()
|
||||||
{
|
{
|
||||||
var fi = new FileInfo(kubeConfigTlsSkip);
|
var fi = new FileInfo(kubeConfigTlsSkip);
|
||||||
var cfg = new KubernetesClientConfiguration(fi);
|
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
|
||||||
Assert.NotNull(cfg.Host);
|
Assert.NotNull(cfg.Host);
|
||||||
Assert.Null(cfg.SslCaCert);
|
Assert.Null(cfg.SslCaCert);
|
||||||
Assert.True(cfg.SkipTlsVerify);
|
Assert.True(cfg.SkipTlsVerify);
|
||||||
@@ -251,7 +251,7 @@ namespace k8s.Tests
|
|||||||
// [Fact]
|
// [Fact]
|
||||||
// public void ListDefaultNamespacedPod()
|
// public void ListDefaultNamespacedPod()
|
||||||
// {
|
// {
|
||||||
// var k8sClientConfig = new KubernetesClientConfiguration();
|
// var k8sClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
// IKubernetes client = new Kubernetes(k8sClientConfig);
|
// IKubernetes client = new Kubernetes(k8sClientConfig);
|
||||||
// var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result;
|
// var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result;
|
||||||
// var list = listTask.Body;
|
// var list = listTask.Body;
|
||||||
|
|||||||
Reference in New Issue
Block a user