introduce go client style config creating functions

This commit is contained in:
Boshi Lian
2017-10-11 21:27:22 +08:00
parent a5af10a282
commit adeab58f58
4 changed files with 258 additions and 160 deletions

View File

@@ -0,0 +1,35 @@
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using k8s.Exceptions;
namespace k8s
{
public partial class KubernetesClientConfiguration
{
private const string ServiceAccountTokenKey = "token";
private const string ServiceAccountRootCAKey = "ca.crt";
public static KubernetesClientConfiguration InClusterConfig()
{
var host = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST");
var port = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_PORT");
if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(port))
{
throw new KubeConfigException(
"unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined");
}
var token = File.ReadAllText("/var/run/secrets/kubernetes.io/serviceaccount/" + ServiceAccountTokenKey);
var rootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/" + ServiceAccountRootCAKey;
return new KubernetesClientConfiguration
{
Host = new UriBuilder("https", host, Convert.ToInt32(port)).ToString(),
AccessToken = token,
SslCaCert = Utils.LoadPemFileCert(rootCAFile)
};
}
}
}