Skip to content

Commit 8f4ee45

Browse files
Add environment variable checking for in cluster configs. (#766)
1 parent 6bed103 commit 8f4ee45

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public static bool IsInCluster()
2424
var host = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST");
2525
var port = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_PORT");
2626

27+
if (String.IsNullOrEmpty(host) || String.IsNullOrEmpty(port))
28+
{
29+
return false;
30+
}
31+
2732
var tokenPath = Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName);
2833
if (!FileUtils.FileSystem().File.Exists(tokenPath))
2934
{

tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs

+26-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,23 @@
1313

1414
namespace k8s.Tests
1515
{
16-
public class KubernetesClientConfigurationTests
16+
public class KubernetesClientConfigurationTests : IDisposable
1717
{
18+
/// <summary>
19+
/// Not all tests set these, but no harm in clearing them.
20+
/// </summary>
21+
public void Dispose()
22+
{
23+
Dispose(true);
24+
GC.SuppressFinalize(this);
25+
}
26+
27+
protected virtual void Dispose(bool disposing)
28+
{
29+
Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_HOST", null);
30+
Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_PORT", null);
31+
}
32+
1833
/// <summary>
1934
/// Check if host is properly loaded, per context
2035
/// </summary>
@@ -683,6 +698,9 @@ public void IsInCluster()
683698
{
684699
Assert.False(KubernetesClientConfiguration.IsInCluster());
685700

701+
Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_HOST", "kubernetes");
702+
Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_PORT", "443");
703+
686704
var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
687705
var certPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);
688706

@@ -703,6 +721,9 @@ public void IsInCluster()
703721
[Fact]
704722
public void LoadInCluster()
705723
{
724+
Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_HOST", "other.default.svc");
725+
Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_PORT", "443");
726+
706727
var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
707728
var certPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);
708729

@@ -715,7 +736,7 @@ public void LoadInCluster()
715736
using (new FileUtils.InjectedFileSystem(fileSystem))
716737
{
717738
var config = KubernetesClientConfiguration.InClusterConfig();
718-
Assert.Equal("https://kubernetes.default.svc:443/", config.Host);
739+
Assert.Equal("https://other.default.svc:443/", config.Host);
719740
Assert.Null(config.Namespace);
720741
}
721742
}
@@ -726,6 +747,9 @@ public void LoadInCluster()
726747
[Fact]
727748
public void LoadInClusterNamespace()
728749
{
750+
Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_HOST", "kubernetes.default.svc");
751+
Environment.SetEnvironmentVariable("KUBERNETES_SERVICE_PORT", "443");
752+
729753
var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
730754
var certPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);
731755
var namespacePath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountNamespaceFileName);

0 commit comments

Comments
 (0)