Skip to content

Commit 6b104a5

Browse files
authored
remove system io abstraction (#1157)
1 parent d38302d commit 6b104a5

File tree

7 files changed

+92
-44
lines changed

7 files changed

+92
-44
lines changed

src/KubernetesClient.Classic/KubernetesClient.Classic.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
<ItemGroup>
99
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
10-
<PackageReference Include="System.IO.Abstractions" Version="19.1.5" />
1110
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
1211
<PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" />
1312
</ItemGroup>
@@ -19,7 +18,7 @@
1918

2019
<ItemGroup>
2120
<Compile Include="..\KubernetesClient\CertUtils.cs" />
22-
<Compile Include="..\KubernetesClient\FileUtils.cs" />
21+
<Compile Include="..\KubernetesClient\FileSystem.cs" />
2322
<Compile Include="..\KubernetesClient\IKubernetes.cs" />
2423
<Compile Include="..\KubernetesClient\Kubernetes.ConfigInit.cs" />
2524
<Compile Include="..\KubernetesClient\Kubernetes.cs" />

src/KubernetesClient/CertUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal static class CertUtils
2424
public static X509Certificate2Collection LoadPemFileCert(string file)
2525
{
2626
var certCollection = new X509Certificate2Collection();
27-
using (var stream = FileUtils.FileSystem().File.OpenRead(file))
27+
using (var stream = FileSystem.Current.OpenRead(file))
2828
{
2929
#if NET5_0_OR_GREATER
3030
certCollection.ImportFromPem(new StreamReader(stream).ReadToEnd());

src/KubernetesClient/FileSystem.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.IO;
2+
3+
namespace k8s
4+
{
5+
internal static class FileSystem
6+
{
7+
public interface IFileSystem
8+
{
9+
Stream OpenRead(string path);
10+
11+
bool Exists(string path);
12+
13+
string ReadAllText(string path);
14+
}
15+
16+
public static IFileSystem Current { get; private set; } = new RealFileSystem();
17+
18+
public static IDisposable With(IFileSystem fileSystem)
19+
{
20+
return new InjectedFileSystem(fileSystem);
21+
}
22+
23+
private class InjectedFileSystem : IDisposable
24+
{
25+
private readonly IFileSystem _original;
26+
27+
public InjectedFileSystem(IFileSystem fileSystem)
28+
{
29+
_original = Current;
30+
Current = fileSystem;
31+
}
32+
33+
public void Dispose()
34+
{
35+
Current = _original;
36+
}
37+
}
38+
39+
private class RealFileSystem : IFileSystem
40+
{
41+
public bool Exists(string path)
42+
{
43+
return File.Exists(path);
44+
}
45+
46+
public Stream OpenRead(string path)
47+
{
48+
return File.OpenRead(path);
49+
}
50+
51+
public string ReadAllText(string path)
52+
{
53+
return File.ReadAllText(path);
54+
}
55+
}
56+
}
57+
}

src/KubernetesClient/FileUtils.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/KubernetesClient/KubernetesClient.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<ItemGroup>
99
<PackageReference Include="prometheus-net" Version="7.0.0" />
1010
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
11-
<PackageReference Include="System.IO.Abstractions" Version="19.1.5" />
1211
<PackageReference Include="IdentityModel.OidcClient" Version="5.2.1" />
1312
</ItemGroup>
1413

src/KubernetesClient/KubernetesClientConfiguration.InCluster.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public static bool IsInCluster()
3030
}
3131

3232
var tokenPath = Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName);
33-
if (!FileUtils.FileSystem().File.Exists(tokenPath))
33+
if (!FileSystem.Current.Exists(tokenPath))
3434
{
3535
return false;
3636
}
3737

3838
var certPath = Path.Combine(ServiceAccountPath, ServiceAccountRootCAKeyFileName);
39-
return FileUtils.FileSystem().File.Exists(certPath);
39+
return FileSystem.Current.Exists(certPath);
4040
}
4141

4242
public static KubernetesClientConfiguration InClusterConfig()
@@ -68,9 +68,9 @@ public static KubernetesClientConfiguration InClusterConfig()
6868
};
6969

7070
var namespaceFile = Path.Combine(ServiceAccountPath, ServiceAccountNamespaceFileName);
71-
if (FileUtils.FileSystem().File.Exists(namespaceFile))
71+
if (FileSystem.Current.Exists(namespaceFile))
7272
{
73-
result.Namespace = FileUtils.FileSystem().File.ReadAllText(namespaceFile);
73+
result.Namespace = FileSystem.Current.ReadAllText(namespaceFile);
7474
}
7575

7676
return result;

tests/KubernetesClient.Tests/KubernetesClientConfigurationTests.cs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.IO.Abstractions;
78
using System.IO.Abstractions.TestingHelpers;
89
using System.Linq;
910
using System.Runtime.InteropServices;
@@ -694,6 +695,31 @@ private static void AssertUserEqual(User expected, User actual)
694695
}
695696
}
696697

698+
private class FileSystemAdapter : FileSystem.IFileSystem
699+
{
700+
private readonly IFileSystem io;
701+
702+
public FileSystemAdapter(System.IO.Abstractions.IFileSystem io)
703+
{
704+
this.io = io;
705+
}
706+
707+
public bool Exists(string path)
708+
{
709+
return io.File.Exists(path);
710+
}
711+
712+
public Stream OpenRead(string path)
713+
{
714+
return io.File.OpenRead(path);
715+
}
716+
717+
public string ReadAllText(string path)
718+
{
719+
return io.File.ReadAllText(path);
720+
}
721+
}
722+
697723
/// <summary>
698724
/// Test in cluster configuration.
699725
/// </summary>
@@ -713,7 +739,7 @@ public void IsInCluster()
713739
{ tokenPath, new MockFileData("foo") },
714740
{ certPath, new MockFileData("bar") },
715741
});
716-
using (new FileUtils.InjectedFileSystem(fileSystem))
742+
using (FileSystem.With(new FileSystemAdapter(fileSystem)))
717743
{
718744
Assert.True(KubernetesClientConfiguration.IsInCluster());
719745
}
@@ -737,7 +763,7 @@ public void LoadInCluster()
737763
{ certPath, new MockFileData("bar") },
738764
});
739765

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

767-
using (new FileUtils.InjectedFileSystem(fileSystem))
793+
using (FileSystem.With(new FileSystemAdapter(fileSystem)))
768794
{
769795
var config = KubernetesClientConfiguration.InClusterConfig();
770796
Assert.Equal("https://kubernetes.default.svc:443/", config.Host);

0 commit comments

Comments
 (0)