Skip to content

Commit

Permalink
Add environment variable checking for in cluster configs. (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandburns authored Feb 1, 2022
1 parent 6bed103 commit 8f4ee45
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static bool IsInCluster()
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))
{
Expand Down
28 changes: 26 additions & 2 deletions tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,23 @@

namespace k8s.Tests
{
public class KubernetesClientConfigurationTests
public class KubernetesClientConfigurationTests : IDisposable
{
/// <summary>
/// Not all tests set these, but no harm in clearing them.
/// </summary>
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);
}

/// <summary>
/// Check if host is properly loaded, per context
/// </summary>
Expand Down Expand Up @@ -683,6 +698,9 @@ public void IsInCluster()
{
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);

Expand All @@ -703,6 +721,9 @@ public void IsInCluster()
[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);

Expand All @@ -715,7 +736,7 @@ public void LoadInCluster()
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);
}
}
Expand All @@ -726,6 +747,9 @@ public void LoadInCluster()
[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);
Expand Down

0 comments on commit 8f4ee45

Please sign in to comment.