diff --git a/examples/simple/PodList.cs b/examples/simple/PodList.cs index f832b58..557c0b7 100755 --- a/examples/simple/PodList.cs +++ b/examples/simple/PodList.cs @@ -7,7 +7,7 @@ namespace simple { private static void Main(string[] args) { - var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); + var config = KubernetesClientConfiguration.BuildDefaultConfig(); IKubernetes client = new Kubernetes(config); Console.WriteLine("Starting Request!"); diff --git a/src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs b/src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs index 3d56bfc..41fc365 100644 --- a/src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs +++ b/src/KubernetesClient/KubernetesClientConfiguration.ConfigFile.cs @@ -24,6 +24,26 @@ namespace k8s /// public string CurrentContext { get; private set; } + /// + /// Initializes a new instance of the from config file + /// + public static KubernetesClientConfiguration BuildDefaultConfig() { + var kubeconfig = Environment.GetEnvironmentVariable("KUBECONFIG"); + if (kubeconfig != null) { + return BuildConfigFromConfigFile(kubeconfigPath: kubeconfig); + } + if (File.Exists(KubeConfigDefaultLocation)) { + return BuildConfigFromConfigFile(kubeconfigPath: KubeConfigDefaultLocation); + } + if (IsInCluster()) { + return InClusterConfig(); + } + var config = new KubernetesClientConfiguration(); + config.Host = "http://localhost:8080"; + return config; + } + + /// /// Initializes a new instance of the from config file /// diff --git a/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs b/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs index d658763..86c576e 100644 --- a/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs +++ b/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs @@ -6,23 +6,38 @@ namespace k8s { public partial class KubernetesClientConfiguration { - private const string ServiceaccountPath = "/var/run/secrets/kubernetes.io/serviceaccount/"; + private const string ServiceAccountPath = "/var/run/secrets/kubernetes.io/serviceaccount/"; private const string ServiceAccountTokenKeyFileName = "token"; private const string ServiceAccountRootCAKeyFileName = "ca.crt"; - public static KubernetesClientConfiguration InClusterConfig() + public static Boolean IsInCluster() { var host = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST"); var port = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_PORT"); - if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(port)) { + return false; + } + var tokenPath = Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName); + if (!File.Exists(tokenPath)) + { + return false; + } + var certPath = Path.Combine(ServiceAccountPath, ServiceAccountRootCAKeyFileName); + return File.Exists(certPath); + } + + public static KubernetesClientConfiguration InClusterConfig() + { + if (!IsInCluster()) { throw new KubeConfigException( "unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined"); } - var token = File.ReadAllText(Path.Combine(ServiceaccountPath, ServiceAccountTokenKeyFileName)); - var rootCAFile = Path.Combine(ServiceaccountPath, ServiceAccountRootCAKeyFileName); + var token = File.ReadAllText(Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName)); + var rootCAFile = Path.Combine(ServiceAccountPath, ServiceAccountRootCAKeyFileName); + var host = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST"); + var port = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_PORT"); return new KubernetesClientConfiguration {