Skip to content

Commit a7ee07f

Browse files
qmfrederikbrendandburns
authored andcommitted
Update the Kubernetes configuration model (#94)
* Add documentaton * Add Extensions properties Add as, as-groups, as-user-extra properties to UserCredentials
1 parent 4f80289 commit a7ee07f

File tree

9 files changed

+224
-48
lines changed

9 files changed

+224
-48
lines changed

src/KubeConfigModels/Cluster.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
namespace k8s.KubeConfigModels
1+
namespace k8s.KubeConfigModels
22
{
33
using YamlDotNet.Serialization;
4-
4+
5+
/// <summary>
6+
/// Relates nicknames to cluster information.
7+
/// </summary>
58
public class Cluster
6-
{
9+
{
10+
/// <summary>
11+
/// Gets or sets the cluster information.
12+
/// </summary>
713
[YamlMember(Alias = "cluster")]
814
public ClusterEndpoint ClusterEndpoint { get; set; }
9-
15+
16+
/// <summary>
17+
/// Gets or sets the nickname for this Cluster.
18+
/// </summary>
1019
[YamlMember(Alias = "name")]
1120
public string Name { get; set; }
1221
}
Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,42 @@
1-
namespace k8s.KubeConfigModels
2-
{
1+
namespace k8s.KubeConfigModels
2+
{
3+
using System.Collections.Generic;
34
using YamlDotNet.Serialization;
4-
5+
6+
/// <summary>
7+
/// Contains information about how to communicate with a kubernetes cluster
8+
/// </summary>
59
public class ClusterEndpoint
6-
{
10+
{
11+
/// <summary>
12+
/// Gets or sets the path to a cert file for the certificate authority.
13+
/// </summary>
714
[YamlMember(Alias = "certificate-authority")]
815
public string CertificateAuthority {get; set; }
9-
16+
17+
/// <summary>
18+
/// Gets or sets =PEM-encoded certificate authority certificates. Overrides <see cref="CertificateAuthority"/>.
19+
/// </summary>
1020
[YamlMember(Alias = "certificate-authority-data")]
1121
public string CertificateAuthorityData { get; set; }
12-
22+
23+
/// <summary>
24+
/// Gets or sets the address of the kubernetes cluster (https://hostname:port).
25+
/// </summary>
1326
[YamlMember(Alias = "server")]
1427
public string Server { get; set; }
15-
28+
29+
/// <summary>
30+
/// Gets or sets a value indicating whether to skip the validity check for the server's certificate.
31+
/// This will make your HTTPS connections insecure.
32+
/// </summary>
1633
[YamlMember(Alias = "insecure-skip-tls-verify")]
17-
public bool SkipTlsVerify { get; set; }
34+
public bool SkipTlsVerify { get; set; }
35+
36+
/// <summary>
37+
/// Gets or sets additional information. This is useful for extenders so that reads and writes don't clobber unknown fields.
38+
/// </summary>
39+
[YamlMember(Alias = "extensions")]
40+
public IDictionary<string, dynamic> Extensions { get; set; }
1841
}
1942
}

src/KubeConfigModels/Context.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
namespace k8s.KubeConfigModels
1+
namespace k8s.KubeConfigModels
22
{
33
using YamlDotNet.Serialization;
4-
4+
5+
/// <summary>
6+
/// Relates nicknames to context information.
7+
/// </summary>
58
public class Context
6-
{
9+
{
10+
/// <summary>
11+
/// Gets or sets the context information.
12+
/// </summary>
713
[YamlMember(Alias = "context")]
814
public ContextDetails ContextDetails { get; set; }
9-
15+
16+
/// <summary>
17+
/// Gets or sets the nickname for this context.
18+
/// </summary>
1019
[YamlMember(Alias = "name")]
1120
public string Name { get; set; }
1221

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
1-
namespace k8s.KubeConfigModels
2-
{
3-
using YamlDotNet.RepresentationModel;
1+
namespace k8s.KubeConfigModels
2+
{
3+
using System.Collections.Generic;
44
using YamlDotNet.Serialization;
5-
5+
6+
/// <summary>
7+
/// Represents a tuple of references to a cluster (how do I communicate with a kubernetes cluster),
8+
/// a user (how do I identify myself), and a namespace (what subset of resources do I want to work with)
9+
/// </summary>
610
public class ContextDetails
7-
{
11+
{
12+
/// <summary>
13+
/// Gets or sets the name of the cluster for this context.
14+
/// </summary>
815
[YamlMember(Alias = "cluster")]
916
public string Cluster { get; set; }
10-
17+
18+
/// <summary>
19+
/// Gets or sets the anem of the user for this context.
20+
/// </summary>
1121
[YamlMember(Alias = "user")]
1222
public string User { get; set; }
13-
23+
24+
/// <summary>
25+
/// /Gets or sets the default namespace to use on unspecified requests.
26+
/// </summary>
1427
[YamlMember(Alias = "namespace")]
15-
public string Namespace { get; set; }
28+
public string Namespace { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets additional information. This is useful for extenders so that reads and writes don't clobber unknown fields.
32+
/// </summary>
33+
[YamlMember(Alias = "extensions")]
34+
public IDictionary<string, dynamic> Extensions { get; set; }
1635
}
1736
}

src/KubeConfigModels/K8SConfiguration.cs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ namespace k8s.KubeConfigModels
44
using YamlDotNet.Serialization;
55

66
/// <summary>
7-
/// kubeconfig configuration model
8-
/// </summary>
7+
/// kubeconfig configuration model. Holds the information needed to build connect to remote
8+
/// Kubernetes clusters as a given user.
9+
/// </summary>
10+
/// <remarks>
11+
/// Should be kept in sync with https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/client-go/tools/clientcmd/api/v1/types.go
12+
/// </remarks>
913
public class K8SConfiguration
10-
{
14+
{
15+
/// <summary>
16+
/// Gets or sets general information to be use for CLI interactions
17+
/// </summary>
1118
[YamlMember(Alias = "preferences")]
1219
public IDictionary<string, object> Preferences{ get; set; }
1320

@@ -16,17 +23,35 @@ public class K8SConfiguration
1623

1724
[YamlMember(Alias = "kind")]
1825
public string Kind { get; set; }
19-
26+
27+
/// <summary>
28+
/// Gets or sets the name of the context that you would like to use by default.
29+
/// </summary>
2030
[YamlMember(Alias = "current-context")]
2131
public string CurrentContext { get; set; }
22-
32+
33+
/// <summary>
34+
/// Gets or sets a map of referencable names to context configs.
35+
/// </summary>
2336
[YamlMember(Alias = "contexts")]
2437
public IEnumerable<Context> Contexts { get; set; } = new Context[0];
25-
38+
39+
/// <summary>
40+
/// Gets or sets a map of referencable names to cluster configs.
41+
/// </summary>
2642
[YamlMember(Alias = "clusters")]
2743
public IEnumerable<Cluster> Clusters { get; set; } = new Cluster[0];
28-
44+
45+
/// <summary>
46+
/// Gets or sets a map of referencable names to user configs
47+
/// </summary>
2948
[YamlMember(Alias = "users")]
30-
public IEnumerable<User> Users { get; set; } = new User[0];
49+
public IEnumerable<User> Users { get; set; } = new User[0];
50+
51+
/// <summary>
52+
/// Gets or sets additional information. This is useful for extenders so that reads and writes don't clobber unknown fields.
53+
/// </summary>
54+
[YamlMember(Alias = "extensions")]
55+
public IDictionary<string, dynamic> Extensions { get; set; }
3156
}
3257
}

src/KubeConfigModels/User.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
namespace k8s.KubeConfigModels
1+
namespace k8s.KubeConfigModels
22
{
3-
using YamlDotNet.RepresentationModel;
43
using YamlDotNet.Serialization;
5-
4+
5+
/// <summary>
6+
/// Relates nicknames to auth information.
7+
/// </summary>
68
public class User
7-
{
9+
{
10+
/// <summary>
11+
/// Gets or sets the auth information.
12+
/// </summary>
813
[YamlMember(Alias = "user")]
914
public UserCredentials UserCredentials { get; set; }
10-
15+
16+
/// <summary>
17+
/// Gets or sets the nickname for this auth information.
18+
/// </summary>
1119
[YamlMember(Alias = "name")]
1220
public string Name { get; set; }
1321
}
Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,84 @@
1-
namespace k8s.KubeConfigModels
1+
namespace k8s.KubeConfigModels
22
{
33
using System.Collections.Generic;
44
using YamlDotNet.RepresentationModel;
55
using YamlDotNet.Serialization;
6-
6+
7+
/// <summary>
8+
/// Contains information that describes identity information. This is use to tell the kubernetes cluster who you are.
9+
/// </summary>
710
public class UserCredentials
8-
{
11+
{
12+
/// <summary>
13+
/// Gets or sets PEM-encoded data from a client cert file for TLS. Overrides <see cref="ClientCertificate"/>.
14+
/// </summary>
915
[YamlMember(Alias = "client-certificate-data")]
1016
public string ClientCertificateData { get; set; }
11-
17+
18+
/// <summary>
19+
/// Gets or sets the path to a client cert file for TLS.
20+
/// </summary>
1221
[YamlMember(Alias = "client-certificate")]
1322
public string ClientCertificate { get; set; }
14-
23+
24+
/// <summary>
25+
/// Gets or sets PEM-encoded data from a client key file for TLS. Overrides <see cref="ClientKey"/>.
26+
/// </summary>
1527
[YamlMember(Alias = "client-key-data")]
1628
public string ClientKeyData { get; set; }
17-
29+
30+
/// <summary>
31+
/// Gets or sets the path to a client key file for TLS.
32+
/// </summary>
1833
[YamlMember(Alias = "client-key")]
1934
public string ClientKey { get; set; }
20-
35+
36+
/// <summary>
37+
/// Gets or sets the bearer token for authentication to the kubernetes cluster.
38+
/// </summary>
2139
[YamlMember(Alias = "token")]
2240
public string Token { get; set; }
23-
41+
42+
/// <summary>
43+
/// Gets or sets the username to imperonate. The name matches the flag.
44+
/// </summary>
45+
[YamlMember(Alias = "as")]
46+
public string Impersonate { get; set; }
47+
48+
/// <summary>
49+
/// Gets or sets the groups to imperonate.
50+
/// </summary>
51+
[YamlMember(Alias = "as-groups")]
52+
public IEnumerable<string> ImpersonateGroups { get; set; } = new string[0];
53+
54+
/// <summary>
55+
/// Gets or sets additional information for impersonated user.
56+
/// </summary>
57+
[YamlMember(Alias = "as-user-extra")]
58+
public Dictionary<string, string> ImpersonateUserExtra { get; set; } = new Dictionary<string, string>();
59+
60+
/// <summary>
61+
/// Gets or sets the username for basic authentication to the kubernetes cluster.
62+
/// </summary>
2463
[YamlMember(Alias = "username")]
2564
public string UserName { get; set; }
26-
65+
66+
/// <summary>
67+
/// Gets or sets the password for basic authentication to the kubernetes cluster.
68+
/// </summary>
2769
[YamlMember(Alias = "password")]
2870
public string Password { get; set; }
29-
71+
72+
/// <summary>
73+
/// Gets or sets custom authentication plugin for the kubernetes cluster.
74+
/// </summary>
3075
[YamlMember(Alias = "auth-provider")]
31-
public Dictionary<string, dynamic> AuthProvider { get; set; }
76+
public Dictionary<string, dynamic> AuthProvider { get; set; }
77+
78+
/// <summary>
79+
/// Gets or sets additional information. This is useful for extenders so that reads and writes don't clobber unknown fields.
80+
/// </summary>
81+
[YamlMember(Alias = "extensions")]
82+
public IDictionary<string, dynamic> Extensions { get; set; }
3283
}
3384
}

tests/KubernetesClientConfigurationTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,5 +311,17 @@ public void DefaultConfigurationAsStreamLoaded()
311311
Assert.NotNull(cfg.Host);
312312
}
313313
}
314+
315+
/// <summary>
316+
/// Checks users.as-user-extra is loaded correctly from a configuration file.
317+
/// </summary>
318+
[Fact]
319+
public void AsUserExtra()
320+
{
321+
var txt = File.ReadAllText("assets/kubeconfig.as-user-extra.yml");
322+
323+
var cfg = KubernetesClientConfiguration.BuildConfigFromConfigFile(txt, null, null);
324+
Assert.NotNull(cfg.Host);
325+
}
314326
}
315327
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: v1
2+
clusters:
3+
- cluster:
4+
certificate-authority: assets/ca.crt
5+
server: https://10.20.0.56:8443
6+
name: minikube
7+
contexts:
8+
- context:
9+
cluster: minikube
10+
user: minikube
11+
name: minikube
12+
current-context: minikube
13+
kind: Config
14+
preferences: {}
15+
users:
16+
- name: minikube
17+
user:
18+
as-user-extra: {}
19+
client-certificate: assets/client.crt
20+
client-key: assets/client.key

0 commit comments

Comments
 (0)