Add environment variable checking for in cluster configs. (#766)

This commit is contained in:
Brendan Burns
2022-02-01 10:41:42 -08:00
committed by GitHub
parent 6bed103992
commit 8f4ee45cf3
2 changed files with 31 additions and 2 deletions

View File

@@ -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))
{

View File

@@ -13,8 +13,23 @@ using Xunit;
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>
@@ -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);