diff --git a/gen/KubernetesWatchGenerator/KubernetesWatchGenerator.csproj b/gen/KubernetesWatchGenerator/KubernetesWatchGenerator.csproj
index c020db5..f6c1011 100644
--- a/gen/KubernetesWatchGenerator/KubernetesWatchGenerator.csproj
+++ b/gen/KubernetesWatchGenerator/KubernetesWatchGenerator.csproj
@@ -22,6 +22,12 @@
PreserveNewest
+
+ PreserveNewest
+
+
+ PreserveNewest
+
diff --git a/gen/KubernetesWatchGenerator/ModelOperators.cs.template b/gen/KubernetesWatchGenerator/ModelOperators.cs.template
new file mode 100644
index 0000000..029adea
--- /dev/null
+++ b/gen/KubernetesWatchGenerator/ModelOperators.cs.template
@@ -0,0 +1,15 @@
+using k8s.Versioning;
+
+namespace k8s.Models
+{
+{{#.}}
+ public partial class {{GetTuple . index="0"}}
+ {
+ public static explicit operator {{GetTuple . index="0"}}({{GetTuple . index="1"}} s) => VersionConverter.Mapper.Map<{{GetTuple . index="0"}}>(s);
+ }
+ public partial class {{GetTuple . index="1"}}
+ {
+ public static explicit operator {{GetTuple . index="1"}}({{GetTuple . index="0"}} s) => VersionConverter.Mapper.Map<{{GetTuple . index="1"}}>(s);
+ }
+{{/.}}
+}
diff --git a/gen/KubernetesWatchGenerator/Program.cs b/gen/KubernetesWatchGenerator/Program.cs
index ac71147..1649a49 100644
--- a/gen/KubernetesWatchGenerator/Program.cs
+++ b/gen/KubernetesWatchGenerator/Program.cs
@@ -5,6 +5,8 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace KubernetesWatchGenerator
@@ -101,6 +103,7 @@ namespace KubernetesWatchGenerator
Helpers.Register(nameof(GetApiVersion), GetApiVersion);
Helpers.Register(nameof(GetKind), GetKind);
Helpers.Register(nameof(GetPlural), GetPlural);
+ Helpers.Register(nameof(GetTuple), GetTuple);
// Generate the Watcher operations
// We skip operations where the name of the class in the C# client could not be determined correctly.
@@ -134,10 +137,40 @@ namespace KubernetesWatchGenerator
.Select(x => x.Class)
.ToHashSet();
- Render.FileToFile("ModelExtensions.cs.template", definitions,
- Path.Combine(outputDirectory, "ModelExtensions.cs"));
+ Render.FileToFile("ModelExtensions.cs.template", definitions, Path.Combine(outputDirectory, "ModelExtensions.cs"));
+
+ // generate version converter maps
+ var allGeneratedModelClassNames = Directory
+ .EnumerateFiles(Path.Combine(outputDirectory, "Models"))
+ .Select(Path.GetFileNameWithoutExtension)
+ .ToList();
+
+ var versionRegex = @"(^V|v)[0-9]+((alpha|beta)[0-9]+)?";
+ var typePairs = allGeneratedModelClassNames
+ .OrderBy(x => x)
+ .Select(x => new { Version = Regex.Match(x, versionRegex).Value?.ToLower(), Kinda = Regex.Replace(x, versionRegex, string.Empty), Type = x })
+ .Where(x => !string.IsNullOrEmpty(x.Version))
+ .GroupBy(x => x.Kinda)
+ .Where(x => x.Count() > 1)
+ .SelectMany(x => x.SelectMany((value, index) => x.Skip(index + 1), (first, second) => new { first, second }))
+ .OrderBy(x => x.first.Kinda)
+ .ThenBy(x => x.first.Version)
+ .Select(x => (ITuple)Tuple.Create(x.first.Type, x.second.Type))
+ .ToList();
+
+ var versionFile = File.ReadAllText(Path.Combine(outputDirectory, "..", "Versioning", "VersionConverter.cs"));
+ var manualMaps = Regex.Matches(versionFile, @"\.CreateMap<(?.+?),\s?(?.+?)>")
+ .Select(x => Tuple.Create(x.Groups["T1"].Value, x.Groups["T2"].Value))
+ .ToList();
+ var versionConverterPairs = typePairs.Except(manualMaps).ToList();
+
+ Render.FileToFile("VersionConverter.cs.template", versionConverterPairs, Path.Combine(outputDirectory, "VersionConverter.cs"));
+ Render.FileToFile("ModelOperators.cs.template", typePairs, Path.Combine(outputDirectory, "ModelOperators.cs"));
+
}
+
+
static void ToXmlDoc(RenderContext context, IList