Skip to content

Commit 5cb18e9

Browse files
committed
remove ctor api of KubernetesClientConfiguration, should use factory style
1 parent e06dcc5 commit 5cb18e9

File tree

4 files changed

+35
-57
lines changed

4 files changed

+35
-57
lines changed

src/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 2 additions & 13 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
{

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/CertUtilsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class CertUtilsTests
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 = CertUtils.GeneratePfx(cfg);
@@ -33,7 +33,7 @@ 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 = CertUtils.GeneratePfx(cfg);

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 ContextHost(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

@@ -100,7 +100,7 @@ public void ContextHost(string context, string host)
100100
public void ContextUserToken(string context, string token)
101101
{
102102
var fi = new FileInfo(kubeConfigFileName);
103-
var cfg = new KubernetesClientConfiguration(fi, context);
103+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
104104
Assert.Equal(context, cfg.CurrentContext);
105105
Assert.Null(cfg.Username);
106106
Assert.Equal(token, cfg.AccessToken);
@@ -117,7 +117,7 @@ public void ContextUserToken(string context, string token)
117117
public void ContextCertificateTest(string context, string clientCert, string clientCertKey)
118118
{
119119
var fi = new FileInfo(kubeConfigFileName);
120-
var cfg = new KubernetesClientConfiguration(fi, context);
120+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
121121
Assert.Equal(context, cfg.CurrentContext);
122122
Assert.Equal(cfg.ClientCertificateFilePath, clientCert);
123123
Assert.Equal(cfg.ClientKeyFilePath, clientCertKey);
@@ -132,7 +132,7 @@ public void ContextCertificateTest(string context, string clientCert, string cli
132132
public void ClientDataTest(string context)
133133
{
134134
var fi = new FileInfo(kubeConfigFileName);
135-
var cfg = new KubernetesClientConfiguration(fi, context);
135+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, context);
136136
Assert.Equal(context, cfg.CurrentContext);
137137
Assert.NotNull(cfg.SslCaCert);
138138
Assert.Equal(readLine("assets/client-certificate-data.txt"), cfg.ClientCertificateData);
@@ -147,7 +147,7 @@ public void ClientDataTest(string context)
147147
public void ContextNotFound()
148148
{
149149
var fi = new FileInfo(kubeConfigFileName);
150-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context-not-found"));
150+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context-not-found"));
151151
}
152152

153153
/// <summary>
@@ -157,7 +157,7 @@ public void ContextNotFound()
157157
public void NoContexts()
158158
{
159159
var fi = new FileInfo(kubeConfigNoContexts);
160-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
160+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
161161
}
162162

163163
/// <summary>
@@ -167,7 +167,7 @@ public void NoContexts()
167167
public void NoContextsExplicit()
168168
{
169169
var fi = new FileInfo(kubeConfigNoContexts);
170-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi, "context"));
170+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi, "context"));
171171
}
172172

173173
/// <summary>
@@ -177,7 +177,7 @@ public void NoContextsExplicit()
177177
public void UserPasswordAuthentication()
178178
{
179179
var fi = new FileInfo(kubeConfigUserPassword);
180-
var cfg = new KubernetesClientConfiguration(fi);
180+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
181181
Assert.Equal("admin", cfg.Username);
182182
Assert.Equal("secret", cfg.Password);
183183
}
@@ -189,7 +189,7 @@ public void UserPasswordAuthentication()
189189
public void IncompleteUserCredentials()
190190
{
191191
var fi = new FileInfo(kubeConfigNoCredentials);
192-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
192+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
193193
}
194194

195195
/// <summary>
@@ -199,7 +199,7 @@ public void IncompleteUserCredentials()
199199
public void ServerNotFound()
200200
{
201201
var fi = new FileInfo(kubeConfigNoServer);
202-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
202+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
203203
}
204204

205205
/// <summary>
@@ -209,7 +209,7 @@ public void ServerNotFound()
209209
public void ClusterNotFound()
210210
{
211211
var fi = new FileInfo(kubeConfigNoCluster);
212-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
212+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
213213
}
214214

215215
/// <summary>
@@ -219,7 +219,7 @@ public void ClusterNotFound()
219219
public void ClusterNameMissmatch()
220220
{
221221
var fi = new FileInfo(kubeConfigClusterMissmatch);
222-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
222+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
223223
}
224224

225225
/// <summary>
@@ -229,7 +229,7 @@ public void ClusterNameMissmatch()
229229
public void CheckClusterTlsCorrectness()
230230
{
231231
var fi = new FileInfo(kubeConfigTlsNoSkipError);
232-
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => new KubernetesClientConfiguration(fi));
232+
Assert.Throws<k8s.Exceptions.KubeConfigException>(() => KubernetesClientConfiguration.BuildConfigFromConfigFile(fi));
233233
}
234234

235235
/// <summary>
@@ -239,7 +239,7 @@ public void CheckClusterTlsCorrectness()
239239
public void CheckClusterTlsSkipCorrectness()
240240
{
241241
var fi = new FileInfo(kubeConfigTlsSkip);
242-
var cfg = new KubernetesClientConfiguration(fi);
242+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(fi);
243243
Assert.NotNull(cfg.Host);
244244
Assert.Null(cfg.SslCaCert);
245245
Assert.True(cfg.SkipTlsVerify);
@@ -251,7 +251,7 @@ public void CheckClusterTlsSkipCorrectness()
251251
// [Fact]
252252
// public void ListDefaultNamespacedPod()
253253
// {
254-
// var k8sClientConfig = new KubernetesClientConfiguration();
254+
// var k8sClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
255255
// IKubernetes client = new Kubernetes(k8sClientConfig);
256256
// var listTask = client.ListNamespacedPodWithHttpMessagesAsync("default").Result;
257257
// var list = listTask.Body;

0 commit comments

Comments
 (0)