diff --git a/src/KubernetesClientConfiguration.ConfigFile.cs b/src/KubernetesClientConfiguration.ConfigFile.cs index 1dc5b07..8024764 100644 --- a/src/KubernetesClientConfiguration.ConfigFile.cs +++ b/src/KubernetesClientConfiguration.ConfigFile.cs @@ -24,17 +24,6 @@ namespace k8s ? Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), @".kube\config") : Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".kube/config"); - /// - /// Initializes a new instance of the class. - /// - /// kubeconfig file info - /// Context to use from kube config - public KubernetesClientConfiguration(FileInfo kubeconfig = null, string currentContext = null) - { - var k8SConfig = LoadKubeConfig(kubeconfig ?? new FileInfo(KubeConfigDefaultLocation)); - this.Initialize(k8SConfig, currentContext); - } - /// /// Initializes a new instance of the from config file /// @@ -60,7 +49,7 @@ namespace k8s var k8SConfig = LoadKubeConfig(kubeconfig); var k8SConfiguration = new KubernetesClientConfiguration(); - k8SConfiguration.Initialize(k8SConfig); + k8SConfiguration.Initialize(k8SConfig, currentContext); if (!string.IsNullOrWhiteSpace(masterUrl)) { @@ -200,7 +189,7 @@ namespace k8s /// /// Loads Kube Config /// - /// Kube config file contents + /// Kube config file contents /// Instance of the class private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig) { diff --git a/src/KubernetesClientConfiguration.cs b/src/KubernetesClientConfiguration.cs index de0aaa3..b053d51 100644 --- a/src/KubernetesClientConfiguration.cs +++ b/src/KubernetesClientConfiguration.cs @@ -1,78 +1,67 @@ +using System.Security.Cryptography.X509Certificates; + 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; - /// - /// Represents a set of kubernetes client configuration settings + /// Represents a set of kubernetes client configuration settings /// public partial class KubernetesClientConfiguration { - public KubernetesClientConfiguration() - { - } - /// - /// Gets Host + /// Gets Host /// public string Host { get; set; } /// - /// Gets SslCaCert + /// Gets SslCaCert /// public X509Certificate2 SslCaCert { get; set; } /// - /// Gets ClientCertificateData + /// Gets ClientCertificateData /// public string ClientCertificateData { get; set; } /// - /// Gets ClientCertificate Key + /// Gets ClientCertificate Key /// public string ClientCertificateKeyData { get; set; } /// - /// Gets ClientCertificate filename + /// Gets ClientCertificate filename /// public string ClientCertificateFilePath { get; set; } /// - /// Gets ClientCertificate Key filename + /// Gets ClientCertificate Key filename /// public string ClientKeyFilePath { get; set; } /// - /// Gets a value indicating whether to skip ssl server cert validation + /// Gets a value indicating whether to skip ssl server cert validation /// public bool SkipTlsVerify { get; set; } /// - /// Gets or sets the HTTP user agent. + /// Gets or sets the HTTP user agent. /// /// Http user agent. public string UserAgent { get; set; } /// - /// Gets or sets the username (HTTP basic authentication). + /// Gets or sets the username (HTTP basic authentication). /// /// The username. public string Username { get; set; } /// - /// Gets or sets the password (HTTP basic authentication). + /// Gets or sets the password (HTTP basic authentication). /// /// The password. public string Password { get; set; } /// - /// Gets or sets the access token for OAuth2 authentication. + /// Gets or sets the access token for OAuth2 authentication. /// /// The access token. public string AccessToken { get; set; } diff --git a/tests/CertUtilsTests.cs b/tests/CertUtilsTests.cs index 3b3b83e..ce290a7 100644 --- a/tests/CertUtilsTests.cs +++ b/tests/CertUtilsTests.cs @@ -19,7 +19,7 @@ namespace k8s.Tests public void LoadFromFiles() { 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 var cert = CertUtils.GeneratePfx(cfg); @@ -33,7 +33,7 @@ namespace k8s.Tests public void LoadFromInlineData() { 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 var cert = CertUtils.GeneratePfx(cfg); diff --git a/tests/KubernetesClientConfigurationTests.cs b/tests/KubernetesClientConfigurationTests.cs index d21b847..68b7832 100755 --- a/tests/KubernetesClientConfigurationTests.cs +++ b/tests/KubernetesClientConfigurationTests.cs @@ -64,7 +64,7 @@ namespace k8s.Tests public void ConfigurationFileNotFound() { var fi = new FileInfo("/path/to/nowhere"); - Assert.Throws(() => new KubernetesClientConfiguration(fi)); + Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// @@ -73,7 +73,7 @@ namespace k8s.Tests [Fact] public void DefaultConfigurationLoaded() { - var cfg = new KubernetesClientConfiguration(new FileInfo(kubeConfigFileName)); + var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(new FileInfo(kubeConfigFileName)); Assert.NotNull(cfg.Host); } @@ -86,7 +86,7 @@ namespace k8s.Tests public void ContextHost(string context, string host) { var fi = new FileInfo(kubeConfigFileName); - var cfg = new KubernetesClientConfiguration(fi, context); + var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context); Assert.Equal(host, cfg.Host); } @@ -100,7 +100,7 @@ namespace k8s.Tests public void ContextUserToken(string context, string token) { var fi = new FileInfo(kubeConfigFileName); - var cfg = new KubernetesClientConfiguration(fi, context); + var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context); Assert.Equal(context, cfg.CurrentContext); Assert.Null(cfg.Username); Assert.Equal(token, cfg.AccessToken); @@ -117,7 +117,7 @@ namespace k8s.Tests public void ContextCertificateTest(string context, string clientCert, string clientCertKey) { var fi = new FileInfo(kubeConfigFileName); - var cfg = new KubernetesClientConfiguration(fi, context); + var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context); Assert.Equal(context, cfg.CurrentContext); Assert.Equal(cfg.ClientCertificateFilePath, clientCert); Assert.Equal(cfg.ClientKeyFilePath, clientCertKey); @@ -132,7 +132,7 @@ namespace k8s.Tests public void ClientDataTest(string context) { var fi = new FileInfo(kubeConfigFileName); - var cfg = new KubernetesClientConfiguration(fi, context); + var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context); Assert.Equal(context, cfg.CurrentContext); Assert.NotNull(cfg.SslCaCert); Assert.Equal(readLine("assets/client-certificate-data.txt"), cfg.ClientCertificateData); @@ -147,7 +147,7 @@ namespace k8s.Tests public void ContextNotFound() { var fi = new FileInfo(kubeConfigFileName); - Assert.Throws(() => new KubernetesClientConfiguration(fi, "context-not-found")); + Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context-not-found")); } /// @@ -157,7 +157,7 @@ namespace k8s.Tests public void NoContexts() { var fi = new FileInfo(kubeConfigNoContexts); - Assert.Throws(() => new KubernetesClientConfiguration(fi)); + Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// @@ -167,7 +167,7 @@ namespace k8s.Tests public void NoContextsExplicit() { var fi = new FileInfo(kubeConfigNoContexts); - Assert.Throws(() => new KubernetesClientConfiguration(fi, "context")); + Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context")); } /// @@ -177,7 +177,7 @@ namespace k8s.Tests public void UserPasswordAuthentication() { var fi = new FileInfo(kubeConfigUserPassword); - var cfg = new KubernetesClientConfiguration(fi); + var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi); Assert.Equal("admin", cfg.Username); Assert.Equal("secret", cfg.Password); } @@ -189,7 +189,7 @@ namespace k8s.Tests public void IncompleteUserCredentials() { var fi = new FileInfo(kubeConfigNoCredentials); - Assert.Throws(() => new KubernetesClientConfiguration(fi)); + Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// @@ -199,7 +199,7 @@ namespace k8s.Tests public void ServerNotFound() { var fi = new FileInfo(kubeConfigNoServer); - Assert.Throws(() => new KubernetesClientConfiguration(fi)); + Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// @@ -209,7 +209,7 @@ namespace k8s.Tests public void ClusterNotFound() { var fi = new FileInfo(kubeConfigNoCluster); - Assert.Throws(() => new KubernetesClientConfiguration(fi)); + Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// @@ -219,7 +219,7 @@ namespace k8s.Tests public void ClusterNameMissmatch() { var fi = new FileInfo(kubeConfigClusterMissmatch); - Assert.Throws(() => new KubernetesClientConfiguration(fi)); + Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// @@ -229,7 +229,7 @@ namespace k8s.Tests public void CheckClusterTlsCorrectness() { var fi = new FileInfo(kubeConfigTlsNoSkipError); - Assert.Throws(() => new KubernetesClientConfiguration(fi)); + Assert.Throws(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi)); } /// @@ -239,7 +239,7 @@ namespace k8s.Tests public void CheckClusterTlsSkipCorrectness() { var fi = new FileInfo(kubeConfigTlsSkip); - var cfg = new KubernetesClientConfiguration(fi); + var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi); Assert.NotNull(cfg.Host); Assert.Null(cfg.SslCaCert); Assert.True(cfg.SkipTlsVerify); @@ -251,7 +251,7 @@ namespace k8s.Tests // [Fact] // public void ListDefaultNamespacedPod() // { - // var k8sClientConfig = new KubernetesClientConfiguration(); + // var k8sClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile(); // IKubernetes client = new Kubernetes(k8sClientConfig); // var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result; // var list = listTask.Body;