diff --git a/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs b/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs
index fb3605d..a31b496 100644
--- a/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs
+++ b/src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs
@@ -24,6 +24,11 @@ namespace k8s
var host = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST");
var port = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_PORT");
+ if (String.IsNullOrEmpty(host) || String.IsNullOrEmpty(port))
+ {
+ return false;
+ }
+
var tokenPath = Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName);
if (!FileUtils.FileSystem().File.Exists(tokenPath))
{
diff --git a/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs b/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs
index d4fbdcd..1720bd3 100644
--- a/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs
+++ b/tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs
@@ -13,8 +13,23 @@ using Xunit;
namespace k8s.Tests
{
- public class KubernetesClientConfigurationTests
+ public class KubernetesClientConfigurationTests : IDisposable
{
+ ///
+ /// Not all tests set these, but no harm in clearing them.
+ ///
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ protected virtual void Dispose(bool disposing)
+ {
+ Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_HOST", null);
+ Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_PORT", null);
+ }
+
///
/// Check if host is properly loaded, per context
///
@@ -683,6 +698,9 @@ namespace k8s.Tests
{
Assert.False(KubernetesClientConfiguration.IsInCluster());
+ Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_HOST", "kubernetes");
+ Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_PORT", "443");
+
var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
var certPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);
@@ -703,6 +721,9 @@ namespace k8s.Tests
[Fact]
public void LoadInCluster()
{
+ Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_HOST", "other.default.svc");
+ Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_PORT", "443");
+
var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
var certPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);
@@ -715,7 +736,7 @@ namespace k8s.Tests
using (new FileUtils.InjectedFileSystem(fileSystem))
{
var config = KubernetesClientConfiguration.InClusterConfig();
- Assert.Equal("https://kubernetes.default.svc:443/", config.Host);
+ Assert.Equal("https://other.default.svc:443/", config.Host);
Assert.Null(config.Namespace);
}
}
@@ -726,6 +747,9 @@ namespace k8s.Tests
[Fact]
public void LoadInClusterNamespace()
{
+ Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_HOST", "kubernetes.default.svc");
+ Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_PORT", "443");
+
var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
var certPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);
var namespacePath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountNamespaceFileName);