Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/KubernetesClient.Classic/KubernetesClient.Classic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageReference Include="System.IO.Abstractions" Version="19.1.5" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
<PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" />
</ItemGroup>
Expand All @@ -19,7 +18,7 @@

<ItemGroup>
<Compile Include="..\KubernetesClient\CertUtils.cs" />
<Compile Include="..\KubernetesClient\FileUtils.cs" />
<Compile Include="..\KubernetesClient\FileSystem.cs" />
<Compile Include="..\KubernetesClient\IKubernetes.cs" />
<Compile Include="..\KubernetesClient\Kubernetes.ConfigInit.cs" />
<Compile Include="..\KubernetesClient\Kubernetes.cs" />
Expand Down
2 changes: 1 addition & 1 deletion src/KubernetesClient/CertUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static class CertUtils
public static X509Certificate2Collection LoadPemFileCert(string file)
{
var certCollection = new X509Certificate2Collection();
using (var stream = FileUtils.FileSystem().File.OpenRead(file))
using (var stream = FileSystem.Current.OpenRead(file))
{
#if NET5_0_OR_GREATER
certCollection.ImportFromPem(new StreamReader(stream).ReadToEnd());
Expand Down
57 changes: 57 additions & 0 deletions src/KubernetesClient/FileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.IO;

namespace k8s
{
internal static class FileSystem
{
public interface IFileSystem
{
Stream OpenRead(string path);

bool Exists(string path);

string ReadAllText(string path);
}

public static IFileSystem Current { get; private set; } = new RealFileSystem();

public static IDisposable With(IFileSystem fileSystem)
{
return new InjectedFileSystem(fileSystem);
}

private class InjectedFileSystem : IDisposable
{
private readonly IFileSystem _original;

public InjectedFileSystem(IFileSystem fileSystem)
{
_original = Current;
Current = fileSystem;
}

public void Dispose()
{
Current = _original;
}
}

private class RealFileSystem : IFileSystem
{
public bool Exists(string path)
{
return File.Exists(path);
}

public Stream OpenRead(string path)
{
return File.OpenRead(path);
}

public string ReadAllText(string path)
{
return File.ReadAllText(path);
}
}
}
}
33 changes: 0 additions & 33 deletions src/KubernetesClient/FileUtils.cs

This file was deleted.

1 change: 0 additions & 1 deletion src/KubernetesClient/KubernetesClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<ItemGroup>
<PackageReference Include="prometheus-net" Version="7.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
<PackageReference Include="System.IO.Abstractions" Version="19.1.5" />
<PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public static bool IsInCluster()
}

var tokenPath = Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName);
if (!FileUtils.FileSystem().File.Exists(tokenPath))
if (!FileSystem.Current.Exists(tokenPath))
{
return false;
}

var certPath = Path.Combine(ServiceAccountPath, ServiceAccountRootCAKeyFileName);
return FileUtils.FileSystem().File.Exists(certPath);
return FileSystem.Current.Exists(certPath);
}

public static KubernetesClientConfiguration InClusterConfig()
Expand Down Expand Up @@ -68,9 +68,9 @@ public static KubernetesClientConfiguration InClusterConfig()
};

var namespaceFile = Path.Combine(ServiceAccountPath, ServiceAccountNamespaceFileName);
if (FileUtils.FileSystem().File.Exists(namespaceFile))
if (FileSystem.Current.Exists(namespaceFile))
{
result.Namespace = FileUtils.FileSystem().File.ReadAllText(namespaceFile);
result.Namespace = FileSystem.Current.ReadAllText(namespaceFile);
}

return result;
Expand Down
32 changes: 29 additions & 3 deletions tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -694,6 +695,31 @@ private static void AssertUserEqual(User expected, User actual)
}
}

private class FileSystemAdapter : FileSystem.IFileSystem
{
private readonly IFileSystem io;

public FileSystemAdapter(System.IO.Abstractions.IFileSystem io)
{
this.io = io;
}

public bool Exists(string path)
{
return io.File.Exists(path);
}

public Stream OpenRead(string path)
{
return io.File.OpenRead(path);
}

public string ReadAllText(string path)
{
return io.File.ReadAllText(path);
}
}

/// <summary>
/// Test in cluster configuration.
/// </summary>
Expand All @@ -713,7 +739,7 @@ public void IsInCluster()
{ tokenPath, new MockFileData("foo") },
{ certPath, new MockFileData("bar") },
});
using (new FileUtils.InjectedFileSystem(fileSystem))
using (FileSystem.With(new FileSystemAdapter(fileSystem)))
{
Assert.True(KubernetesClientConfiguration.IsInCluster());
}
Expand All @@ -737,7 +763,7 @@ public void LoadInCluster()
{ certPath, new MockFileData("bar") },
});

using (new FileUtils.InjectedFileSystem(fileSystem))
using (FileSystem.With(new FileSystemAdapter(fileSystem)))
{
var config = KubernetesClientConfiguration.InClusterConfig();
Assert.Equal("https://other.default.svc:443/", config.Host);
Expand All @@ -764,7 +790,7 @@ public void LoadInClusterNamespace()
{ namespacePath, new MockFileData("some namespace") },
});

using (new FileUtils.InjectedFileSystem(fileSystem))
using (FileSystem.With(new FileSystemAdapter(fileSystem)))
{
var config = KubernetesClientConfiguration.InClusterConfig();
Assert.Equal("https://kubernetes.default.svc:443/", config.Host);
Expand Down