From e1b6e2effded4b6ebb36e7076de298afed16be7c Mon Sep 17 00:00:00 2001 From: Boshi LIAN Date: Fri, 30 May 2025 11:45:46 -0700 Subject: [PATCH 1/2] Add VersionTestAsync to validate Kubernetes client version retrieval --- tests/E2E.Aot.Tests/MinikubeTests.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/E2E.Aot.Tests/MinikubeTests.cs b/tests/E2E.Aot.Tests/MinikubeTests.cs index 2c4ca933b..505d9a4b2 100644 --- a/tests/E2E.Aot.Tests/MinikubeTests.cs +++ b/tests/E2E.Aot.Tests/MinikubeTests.cs @@ -370,6 +370,15 @@ async Task AssertMd5sumAsync(string file, byte[] orig) } } + + [MinikubeFact] + public async Task VersionTestAsync() + { + using var client = CreateClient(); + var version = await client.Version.GetCodeAsync().ConfigureAwait(false); + Assert.NotNull(version); + } + public static IKubernetes CreateClient() { return new Kubernetes(KubernetesClientConfiguration.BuildDefaultConfig()); From 2de48e82f981a6d0a3c2ca497ed781a7164bd309 Mon Sep 17 00:00:00 2001 From: Boshi LIAN Date: Fri, 30 May 2025 12:10:58 -0700 Subject: [PATCH 2/2] Add SourceGenerationContextGenerator and update registration in KubernetesClientSourceGenerator --- .../KubernetesClientSourceGenerator.cs | 2 ++ .../SourceGenerationContextGenerator.cs | 24 +++++++++++++++++++ .../templates/ModelExtensions.cs.template | 12 ---------- .../SourceGenerationContext.cs.template | 15 ++++++++++++ 4 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 src/LibKubernetesGenerator/SourceGenerationContextGenerator.cs create mode 100644 src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template diff --git a/src/LibKubernetesGenerator/KubernetesClientSourceGenerator.cs b/src/LibKubernetesGenerator/KubernetesClientSourceGenerator.cs index 6ea216f28..7adf6ef1d 100644 --- a/src/LibKubernetesGenerator/KubernetesClientSourceGenerator.cs +++ b/src/LibKubernetesGenerator/KubernetesClientSourceGenerator.cs @@ -59,6 +59,7 @@ private static IContainer BuildContainer(OpenApiDocument swagger) ; builder.RegisterType(); + builder.RegisterType(); builder.RegisterType(); builder.RegisterType(); builder.RegisterType(); @@ -79,6 +80,7 @@ public void Initialize(IncrementalGeneratorInitializationContext generatorContex container.Resolve().Generate(swagger, ctx); container.Resolve().Generate(swagger, ctx); + container.Resolve().Generate(swagger, ctx); container.Resolve().Generate(swagger, ctx); container.Resolve().Generate(swagger, ctx); container.Resolve().Generate(swagger, ctx); diff --git a/src/LibKubernetesGenerator/SourceGenerationContextGenerator.cs b/src/LibKubernetesGenerator/SourceGenerationContextGenerator.cs new file mode 100644 index 000000000..c73c020a1 --- /dev/null +++ b/src/LibKubernetesGenerator/SourceGenerationContextGenerator.cs @@ -0,0 +1,24 @@ +using Microsoft.CodeAnalysis; +using NSwag; + +namespace LibKubernetesGenerator +{ + internal class SourceGenerationContextGenerator + { + private readonly ScriptObjectFactory scriptObjectFactory; + + public SourceGenerationContextGenerator(ScriptObjectFactory scriptObjectFactory) + { + this.scriptObjectFactory = scriptObjectFactory; + } + + public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializationContext context) + { + var definitions = swagger.Definitions.Values; + var sc = scriptObjectFactory.CreateScriptObject(); + sc.SetValue("definitions", definitions, true); + + context.RenderToContext("SourceGenerationContext.cs.template", sc, "SourceGenerationContext.g.cs"); + } + } +} diff --git a/src/LibKubernetesGenerator/templates/ModelExtensions.cs.template b/src/LibKubernetesGenerator/templates/ModelExtensions.cs.template index bad5507bc..d52a3e714 100644 --- a/src/LibKubernetesGenerator/templates/ModelExtensions.cs.template +++ b/src/LibKubernetesGenerator/templates/ModelExtensions.cs.template @@ -15,15 +15,3 @@ namespace k8s.Models } {{ end }} } - -#if NET8_0_OR_GREATER -namespace k8s -{ - {{ for definition in definitions }} - [JsonSerializable(typeof({{ GetClassName definition }}))] - {{ end }} - internal partial class SourceGenerationContext : JsonSerializerContext - { - } -} -#endif \ No newline at end of file diff --git a/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template b/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template new file mode 100644 index 000000000..82942f915 --- /dev/null +++ b/src/LibKubernetesGenerator/templates/SourceGenerationContext.cs.template @@ -0,0 +1,15 @@ +// +// Code generated by https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator +// Changes may cause incorrect behavior and will be lost if the code is regenerated. +// +#if NET8_0_OR_GREATER +namespace k8s +{ + {{ for definition in definitions }} + [JsonSerializable(typeof({{ GetClassName definition }}))] + {{ end }} + internal partial class SourceGenerationContext : JsonSerializerContext + { + } +} +#endif \ No newline at end of file