Skip to content

Commit 6c62105

Browse files
committed
Enhance certificate handling for .NET 9 compatibility in KubernetesClientConfiguration
1 parent f1125e9 commit 6c62105

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/KubernetesClient.Aot/KubernetesClient.Aot.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<PublishAot>true</PublishAot>
77
<IsAotCompatible>true</IsAotCompatible>
88
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
9+
<DefineConstants>$(DefineConstants);K8S_AOT</DefineConstants>
910
</PropertyGroup>
1011

1112
<ItemGroup>
@@ -104,10 +105,6 @@
104105
<Compile Include="..\KubernetesClient\Authentication\TokenCredentials.cs" />
105106
</ItemGroup>
106107

107-
<ItemGroup>
108-
<Reference Include="System.Net.Http" Condition="'$(TargetFramework)' == 'net48'" />
109-
</ItemGroup>
110-
111108
<ItemGroup>
112109
<ProjectReference Include="..\LibKubernetesGenerator\generators\LibKubernetesGenerator\LibKubernetesGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
113110
</ItemGroup>

src/KubernetesClient.Aot/KubernetesClientConfiguration.ConfigFile.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,21 +306,32 @@ private void SetClusterDetails(K8SConfiguration k8SConfig, Context activeContext
306306
{
307307
if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthorityData))
308308
{
309+
var data = clusterDetails.ClusterEndpoint.CertificateAuthorityData;
310+
#if NET9_0_OR_GREATER
311+
SslCaCerts = new X509Certificate2Collection(X509CertificateLoader.LoadCertificate(Convert.FromBase64String(data)));
312+
#else
313+
string nullPassword = null;
309314
// This null password is to change the constructor to fix this KB:
310315
// https://support.microsoft.com/en-us/topic/kb5025823-change-in-how-net-applications-import-x-509-certificates-bf81c936-af2b-446e-9f7a-016f4713b46b
311-
string nullPassword = null;
312-
var data = clusterDetails.ClusterEndpoint.CertificateAuthorityData;
313316
SslCaCerts = new X509Certificate2Collection(new X509Certificate2(Convert.FromBase64String(data), nullPassword));
317+
#endif
314318
}
315319
else if (!string.IsNullOrEmpty(clusterDetails.ClusterEndpoint.CertificateAuthority))
316320
{
321+
#if NET9_0_OR_GREATER
322+
SslCaCerts = new X509Certificate2Collection(X509CertificateLoader.LoadCertificateFromFile(GetFullPath(
323+
k8SConfig,
324+
clusterDetails.ClusterEndpoint.CertificateAuthority)));
325+
#else
317326
SslCaCerts = new X509Certificate2Collection(new X509Certificate2(GetFullPath(
318327
k8SConfig,
319328
clusterDetails.ClusterEndpoint.CertificateAuthority)));
329+
#endif
320330
}
321331
}
322332
}
323333

334+
324335
private void SetUserDetails(K8SConfiguration k8SConfig, Context activeContext)
325336
{
326337
if (string.IsNullOrWhiteSpace(activeContext.ContextDetails.User))

0 commit comments

Comments
 (0)