Add support for loading namespaces from pod configuration. (#640)

This commit is contained in:
Brendan Burns
2021-06-14 16:22:08 -07:00
committed by GitHub
parent d2fa4dee25
commit 762c8af640
6 changed files with 149 additions and 22 deletions

View File

@@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="System.IO.Abstractions.TestingHelpers" Version="13.2.33" />
<PackageReference Include="System.Reactive" Version="4.3.2" />
<PackageReference Include="Nito.AsyncEx" Version="5.1.0" />
</ItemGroup>

View File

@@ -2,7 +2,9 @@ using k8s.Authentication;
using k8s.Exceptions;
using k8s.KubeConfigModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
@@ -672,5 +674,74 @@ namespace k8s.Tests
Assert.True(expectedCreds.AuthProvider.Config.All(x => actualCreds.AuthProvider.Config.Contains(x)));
}
}
/// <summary>
/// Test in cluster configuration.
/// </summary>
[Fact]
public void IsInCluster()
{
Assert.False(KubernetesClientConfiguration.IsInCluster());
var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
var certPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ tokenPath, new MockFileData("foo") },
{ certPath, new MockFileData("bar") },
});
using (new FileUtils.InjectedFileSystem(fileSystem))
{
Assert.True(KubernetesClientConfiguration.IsInCluster());
}
}
/// <summary>
/// Test in cluster configuration loading.
/// </summary>
[Fact]
public void LoadInCluster()
{
var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
var certPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ tokenPath, new MockFileData("foo") },
{ certPath, new MockFileData("bar") },
});
using (new FileUtils.InjectedFileSystem(fileSystem))
{
var config = KubernetesClientConfiguration.InClusterConfig();
Assert.Equal("https://kubernetes.default.svc:443/", config.Host);
Assert.Null(config.Namespace);
}
}
/// <summary>
/// Test in cluster configuration loading of namespaces.
/// </summary>
[Fact]
public void LoadInClusterNamespace()
{
var tokenPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountTokenKeyFileName);
var certPath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountRootCAKeyFileName);
var namespacePath = Path.Combine(KubernetesClientConfiguration.ServiceAccountPath, KubernetesClientConfiguration.ServiceAccountNamespaceFileName);
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ tokenPath, new MockFileData("foo") },
{ certPath, new MockFileData("bar") },
{ namespacePath, new MockFileData("some namespace") },
});
using (new FileUtils.InjectedFileSystem(fileSystem))
{
var config = KubernetesClientConfiguration.InClusterConfig();
Assert.Equal("https://kubernetes.default.svc:443/", config.Host);
Assert.Equal("some namespace", config.Namespace);
}
}
}
}