Skip to content

Commit 8c8d5b3

Browse files
committed
remove ctor api of KubernetesClientConfiguration, should use factory style
1 parent 2a622bb commit 8c8d5b3

File tree

4 files changed

+37
-59
lines changed

4 files changed

+37
-59
lines changed

src/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@ public partial class KubernetesClientConfiguration
2424
? Path.Combine(Environment.GetEnvironmentVariable("USERPROFILE"), @".kube\config")
2525
: Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".kube/config");
2626

27-
/// <summary>
28-
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration"/> class.
29-
/// </summary>
30-
/// <param name="kubeconfig">kubeconfig file info</param>
31-
/// <param name="currentContext">Context to use from kube config</param>
32-
public KubernetesClientConfiguration(FileInfo kubeconfig = null, string currentContext = null)
33-
{
34-
var k8SConfig = LoadKubeConfig(kubeconfig ?? new FileInfo(KubeConfigDefaultLocation));
35-
this.Initialize(k8SConfig, currentContext);
36-
}
37-
3827
/// <summary>
3928
/// Initializes a new instance of the <see cref="KubernetesClientConfiguration"/> from config file
4029
/// </summary>
@@ -60,7 +49,7 @@ public static KubernetesClientConfiguration BuildConfigFromConfigFile(FileInfo k
6049

6150
var k8SConfig = LoadKubeConfig(kubeconfig);
6251
var k8SConfiguration = new KubernetesClientConfiguration();
63-
k8SConfiguration.Initialize(k8SConfig);
52+
k8SConfiguration.Initialize(k8SConfig, currentContext);
6453

6554
if (!string.IsNullOrWhiteSpace(masterUrl))
6655
{
@@ -200,7 +189,7 @@ private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
200189
/// <summary>
201190
/// Loads Kube Config
202191
/// </summary>
203-
/// <param name="config">Kube config file contents</param>
192+
/// <param name="kubeconfig">Kube config file contents</param>
204193
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
205194
private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
206195
{
@@ -215,4 +204,4 @@ private static K8SConfiguration LoadKubeConfig(FileInfo kubeconfig)
215204
return deserializer.Deserialize<K8SConfiguration>(kubeconfigContent);
216205
}
217206
}
218-
}
207+
}

src/KubernetesClientConfiguration.cs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,67 @@
1+
using System.Security.Cryptography.X509Certificates;
2+
13
namespace k8s
24
{
3-
using System;
4-
using System.IO;
5-
using System.Linq;
6-
using System.Security.Cryptography.X509Certificates;
7-
using k8s.Exceptions;
8-
using k8s.KubeConfigModels;
9-
using YamlDotNet.Serialization;
10-
using System.Runtime.InteropServices;
11-
125
/// <summary>
13-
/// Represents a set of kubernetes client configuration settings
6+
/// Represents a set of kubernetes client configuration settings
147
/// </summary>
158
public partial class KubernetesClientConfiguration
169
{
17-
public KubernetesClientConfiguration()
18-
{
19-
}
20-
2110
/// <summary>
22-
/// Gets Host
11+
/// Gets Host
2312
/// </summary>
2413
public string Host { get; set; }
2514

2615
/// <summary>
27-
/// Gets SslCaCert
16+
/// Gets SslCaCert
2817
/// </summary>
2918
public X509Certificate2 SslCaCert { get; set; }
3019

3120
/// <summary>
32-
/// Gets ClientCertificateData
21+
/// Gets ClientCertificateData
3322
/// </summary>
3423
public string ClientCertificateData { get; set; }
3524

3625
/// <summary>
37-
/// Gets ClientCertificate Key
26+
/// Gets ClientCertificate Key
3827
/// </summary>
3928
public string ClientCertificateKeyData { get; set; }
4029

4130
/// <summary>
42-
/// Gets ClientCertificate filename
31+
/// Gets ClientCertificate filename
4332
/// </summary>
4433
public string ClientCertificateFilePath { get; set; }
4534

4635
/// <summary>
47-
/// Gets ClientCertificate Key filename
36+
/// Gets ClientCertificate Key filename
4837
/// </summary>
4938
public string ClientKeyFilePath { get; set; }
5039

5140
/// <summary>
52-
/// Gets a value indicating whether to skip ssl server cert validation
41+
/// Gets a value indicating whether to skip ssl server cert validation
5342
/// </summary>
5443
public bool SkipTlsVerify { get; set; }
5544

5645
/// <summary>
57-
/// Gets or sets the HTTP user agent.
46+
/// Gets or sets the HTTP user agent.
5847
/// </summary>
5948
/// <value>Http user agent.</value>
6049
public string UserAgent { get; set; }
6150

6251
/// <summary>
63-
/// Gets or sets the username (HTTP basic authentication).
52+
/// Gets or sets the username (HTTP basic authentication).
6453
/// </summary>
6554
/// <value>The username.</value>
6655
public string Username { get; set; }
6756

6857
/// <summary>
69-
/// Gets or sets the password (HTTP basic authentication).
58+
/// Gets or sets the password (HTTP basic authentication).
7059
/// </summary>
7160
/// <value>The password.</value>
7261
public string Password { get; set; }
7362

7463
/// <summary>
75-
/// Gets or sets the access token for OAuth2 authentication.
64+
/// Gets or sets the access token for OAuth2 authentication.
7665
/// </summary>
7766
/// <value>The access token.</value>
7867
public string AccessToken { get; set; }

tests/KubernetesClientConfigurationTests.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static string readLine(string fileName)
6464
public void ConfigurationFileNotFound()
6565
{
6666
var fi = new FileInfo("/path/to/nowhere");
67-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
67+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
6868
}
6969

7070
/// <summary>
@@ -73,7 +73,7 @@ public void ConfigurationFileNotFound()
7373
[Fact]
7474
public void DefaultConfigurationLoaded()
7575
{
76-
var cfg = new KubernetesClientConfiguration(new FileInfo(kubeConfigFileName));
76+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(new FileInfo(kubeConfigFileName));
7777
Assert.NotNull(cfg.Host);
7878
}
7979

@@ -86,7 +86,7 @@ public void DefaultConfigurationLoaded()
8686
public void ContextHostTest(string context, string host)
8787
{
8888
var fi = new FileInfo(kubeConfigFileName);
89-
var cfg = new KubernetesClientConfiguration(fi, context);
89+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
9090
Assert.Equal(host, cfg.Host);
9191
}
9292

@@ -101,7 +101,7 @@ public void ContextHostTest(string context, string host)
101101
public void ContextUserTokenTest(string context, string token)
102102
{
103103
var fi = new FileInfo(kubeConfigFileName);
104-
var cfg = new KubernetesClientConfiguration(fi, context);
104+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
105105
Assert.Equal(context, cfg.CurrentContext);
106106
Assert.Null(cfg.Username);
107107
Assert.Equal(token, cfg.AccessToken);
@@ -118,7 +118,7 @@ public void ContextUserTokenTest(string context, string token)
118118
public void ContextCertificateTest(string context, string clientCert, string clientCertKey)
119119
{
120120
var fi = new FileInfo(kubeConfigFileName);
121-
var cfg = new KubernetesClientConfiguration(fi, context);
121+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
122122
Assert.Equal(context, cfg.CurrentContext);
123123
Assert.Equal(cfg.ClientCertificateFilePath, clientCert);
124124
Assert.Equal(cfg.ClientKeyFilePath, clientCertKey);
@@ -133,7 +133,7 @@ public void ContextCertificateTest(string context, string clientCert, string cli
133133
public void ClientDataTest(string context)
134134
{
135135
var fi = new FileInfo(kubeConfigFileName);
136-
var cfg = new KubernetesClientConfiguration(fi, context);
136+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
137137
Assert.Equal(context, cfg.CurrentContext);
138138
Assert.NotNull(cfg.SslCaCert);
139139
Assert.Equal(readLine("assets/client-certificate-data.txt"), cfg.ClientCertificateData);
@@ -148,7 +148,7 @@ public void ClientDataTest(string context)
148148
public void ContextNotFoundTest()
149149
{
150150
var fi = new FileInfo(kubeConfigFileName);
151-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context-not-found"));
151+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context-not-found"));
152152
}
153153

154154
/// <summary>
@@ -158,7 +158,7 @@ public void ContextNotFoundTest()
158158
public void NoContexts()
159159
{
160160
var fi = new FileInfo(kubeConfigNoContexts);
161-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
161+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
162162
}
163163

164164
/// <summary>
@@ -168,7 +168,7 @@ public void NoContexts()
168168
public void NoContextsExplicit()
169169
{
170170
var fi = new FileInfo(kubeConfigNoContexts);
171-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context"));
171+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context"));
172172
}
173173

174174
/// <summary>
@@ -178,7 +178,7 @@ public void NoContextsExplicit()
178178
public void UserPasswordAuthentication()
179179
{
180180
var fi = new FileInfo(kubeConfigUserPassword);
181-
var cfg = new KubernetesClientConfiguration(fi);
181+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
182182
Assert.Equal("admin", cfg.Username);
183183
Assert.Equal("secret", cfg.Password);
184184
}
@@ -190,7 +190,7 @@ public void UserPasswordAuthentication()
190190
public void IncompleteUserCredentials()
191191
{
192192
var fi = new FileInfo(kubeConfigNoCredentials);
193-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
193+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
194194
}
195195

196196
/// <summary>
@@ -200,7 +200,7 @@ public void IncompleteUserCredentials()
200200
public void ServerNotFound()
201201
{
202202
var fi = new FileInfo(kubeConfigNoServer);
203-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
203+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
204204
}
205205

206206
/// <summary>
@@ -210,7 +210,7 @@ public void ServerNotFound()
210210
public void ClusterNotFound()
211211
{
212212
var fi = new FileInfo(kubeConfigNoCluster);
213-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
213+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
214214
}
215215

216216
/// <summary>
@@ -220,7 +220,7 @@ public void ClusterNotFound()
220220
public void ClusterNameMissmatch()
221221
{
222222
var fi = new FileInfo(kubeConfigClusterMissmatch);
223-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
223+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
224224
}
225225

226226
/// <summary>
@@ -230,7 +230,7 @@ public void ClusterNameMissmatch()
230230
public void CheckClusterTlsCorrectness()
231231
{
232232
var fi = new FileInfo(kubeConfigTlsNoSkipError);
233-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
233+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
234234
}
235235

236236
/// <summary>
@@ -240,7 +240,7 @@ public void CheckClusterTlsCorrectness()
240240
public void CheckClusterTlsSkipCorrectness()
241241
{
242242
var fi = new FileInfo(kubeConfigTlsSkip);
243-
var cfg = new KubernetesClientConfiguration(fi);
243+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
244244
Assert.NotNull(cfg.Host);
245245
Assert.Null(cfg.SslCaCert);
246246
Assert.True(cfg.SkipTlsVerify);
@@ -252,7 +252,7 @@ public void CheckClusterTlsSkipCorrectness()
252252
// [Fact]
253253
// public void ListDefaultNamespacedPod()
254254
// {
255-
// var k8sClientConfig = new KubernetesClientConfiguration();
255+
// var k8sClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
256256
// IKubernetes client = new Kubernetes(k8sClientConfig);
257257
// var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result;
258258
// var list = listTask.Body;

tests/UtilTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class UtilsTests
1919
public void LoadFromFiles()
2020
{
2121
var fi = new FileInfo(kubeConfigFileName);
22-
var cfg = new KubernetesClientConfiguration(fi, "federal-context");
22+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "federal-context");
2323

2424
// Just validate that this doesn't throw and private key is non-null
2525
var cert = Utils.GeneratePfx(cfg);
@@ -33,11 +33,11 @@ public void LoadFromFiles()
3333
public void LoadFromInlineData()
3434
{
3535
var fi = new FileInfo(kubeConfigFileName);
36-
var cfg = new KubernetesClientConfiguration(fi, "victorian-context");
36+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "victorian-context");
3737

3838
// Just validate that this doesn't throw and private key is non-null
3939
var cert = Utils.GeneratePfx(cfg);
4040
Assert.NotNull(cert.PrivateKey);
4141
}
4242
}
43-
}
43+
}

0 commit comments

Comments
 (0)