Support for conversions between models of different versions (#420)

This commit is contained in:
Andrew Stakhov
2020-05-20 19:28:19 -04:00
committed by GitHub
parent a4ff002bde
commit f1a3586c43
12 changed files with 2832 additions and 3 deletions

View File

@@ -22,6 +22,12 @@
<None Update="IKubernetes.Watch.cs.template">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="ModelOperators.cs.template">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="VersionConverter.cs.template">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -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);
}
{{/.}}
}

View File

@@ -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<(?<T1>.+?),\s?(?<T2>.+?)>")
.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<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
@@ -166,6 +199,18 @@ namespace KubernetesWatchGenerator
}
}
static void GetTuple(RenderContext context, IList<object> arguments, IDictionary<string, object> options, RenderBlock fn, RenderBlock inverse)
{
if (arguments != null && arguments.Count > 0 && arguments[0] is ITuple && options.TryGetValue("index", out var indexObj) && int.TryParse(indexObj?.ToString(), out var index))
{
var pair = (ITuple)arguments[0];
var value = pair[index];
context.Write(value.ToString());
}
}
static void GetClassName(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
RenderBlock fn, RenderBlock inverse)
{
@@ -343,6 +388,7 @@ namespace KubernetesWatchGenerator
}
}
static string GetMethodName(SwaggerOperation watchOperation)
{
var tag = watchOperation.Tags[0];

View File

@@ -0,0 +1,19 @@
using AutoMapper;
using k8s.Models;
namespace k8s.Versioning
{
public static partial class VersionConverter
{
private static void AutoConfigurations(IMapperConfigurationExpression cfg)
{
{{#.}}
cfg.CreateMap<{{GetTuple . index="0"}}, {{GetTuple . index="1"}}>().ReverseMap();
{{/.}}
}
}
}

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using k8s.Models;
using Microsoft.Rest;
using Microsoft.Rest.TransientFaultHandling;
using Newtonsoft.Json.Converters;
using VersionConverter = k8s.Versioning.VersionConverter;
namespace k8s
{
public static class Extensions
{
public static KubernetesEntityAttribute GetKubernetesTypeMetadata<T>(this T obj) where T : IKubernetesObject =>
obj.GetType().GetKubernetesTypeMetadata();
public static KubernetesEntityAttribute GetKubernetesTypeMetadata(this Type currentType)
{
var attr = currentType.GetCustomAttribute<KubernetesEntityAttribute>();
if (attr == null)
{
throw new InvalidOperationException($"Custom resource must have {nameof(KubernetesEntityAttribute)} applied to it");
}
return attr;
}
public static T Initialize<T>(this T obj) where T : IKubernetesObject
{
var metadata = obj.GetKubernetesTypeMetadata();
obj.ApiVersion = !string.IsNullOrEmpty(metadata.Group) ? $"{metadata.Group}/{metadata.ApiVersion}" : metadata.ApiVersion;
obj.Kind = metadata.Kind ?? obj.GetType().Name;
if (obj is IMetadata<V1ObjectMeta> withMetadata && withMetadata.Metadata == null)
{
withMetadata.Metadata = new V1ObjectMeta();
}
return obj;
}
internal static bool IsValidKubernetesName(this string value) => !Regex.IsMatch(value, "^[a-z0-9-]+$");
}
}

View File

@@ -27,6 +27,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="7.0.1" Condition="'$(TargetFramework)' == 'net452'" />
<PackageReference Include="AutoMapper" Version="9.0.0" Condition="'$(TargetFramework)' != 'net452'" />
<PackageReference Include="Fractions" Version="4.0.1" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="1.1.2" Condition="'$(TargetFramework)' != 'netstandard2.0' and '$(TargetFramework)' != 'netcoreapp2.1'" />
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.0.0" Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp2.1'" />
@@ -41,6 +43,7 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
<Reference Include="System.Net.Http.WebRequest" />
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace k8s.Versioning
{
public class KubernetesVersionComparer : IComparer<string>
{
public static KubernetesVersionComparer Instance { get; private set; }
static readonly Regex _kubernetesVersionRegex;
static KubernetesVersionComparer()
{
_kubernetesVersionRegex = new Regex(@"^v(?<major>[0-9]+)((?<stream>alpha|beta)(?<minor>[0-9]+))?$", RegexOptions.Compiled);
Instance = new KubernetesVersionComparer();
}
internal KubernetesVersionComparer()
{
}
public int Compare(string x, string y)
{
if (x == null || y == null)
{
return StringComparer.CurrentCulture.Compare(x, y);
}
var matchX = _kubernetesVersionRegex.Match(x);
if (!matchX.Success)
{
return StringComparer.CurrentCulture.Compare(x, y);
}
var matchY = _kubernetesVersionRegex.Match(y);
if (!matchY.Success)
{
return StringComparer.CurrentCulture.Compare(x, y);
}
var versionX = ExtractVersion(matchX);
var versionY = ExtractVersion(matchY);
return versionX.CompareTo(versionY);
}
private Version ExtractVersion(Match match)
{
var major = int.Parse(match.Groups["major"].Value);
if (!Enum.TryParse<Stream>(match.Groups["stream"].Value, true, out var stream))
{
stream = Stream.Final;
}
int.TryParse(match.Groups["minor"].Value, out var minor);
return new Version(major, (int)stream, minor);
}
private enum Stream
{
Alpha = 1,
Beta = 2,
Final = 3
}
}
}

View File

@@ -0,0 +1,726 @@
// using k8s.Versioning;
// namespace k8s.Models
// {
// public partial class V1MutatingWebhookConfiguration
// {
// public static explicit operator V1MutatingWebhookConfiguration(V1beta1MutatingWebhookConfiguration s) => VersionConverter.Mapper.Map<V1MutatingWebhookConfiguration>(s);
// }
// public partial class V1beta1MutatingWebhookConfiguration
// {
// public static explicit operator V1beta1MutatingWebhookConfiguration(V1MutatingWebhookConfiguration s) => VersionConverter.Mapper.Map<V1beta1MutatingWebhookConfiguration>(s);
// }
//
// public partial class V1MutatingWebhookConfigurationList
// {
// public static explicit operator V1MutatingWebhookConfigurationList(V1beta1MutatingWebhookConfigurationList s) => VersionConverter.Mapper.Map<V1MutatingWebhookConfigurationList>(s);
// }
// public partial class V1beta1MutatingWebhookConfigurationList
// {
// public static explicit operator V1beta1MutatingWebhookConfigurationList(V1MutatingWebhookConfigurationList s) => VersionConverter.Mapper.Map<V1beta1MutatingWebhookConfigurationList>(s);
// }
//
// public partial class V1ValidatingWebhookConfiguration
// {
// public static explicit operator V1ValidatingWebhookConfiguration(V1beta1ValidatingWebhookConfiguration s) => VersionConverter.Mapper.Map<V1ValidatingWebhookConfiguration>(s);
// }
// public partial class V1beta1ValidatingWebhookConfiguration
// {
// public static explicit operator V1beta1ValidatingWebhookConfiguration(V1ValidatingWebhookConfiguration s) => VersionConverter.Mapper.Map<V1beta1ValidatingWebhookConfiguration>(s);
// }
//
// public partial class V1ValidatingWebhookConfigurationList
// {
// public static explicit operator V1ValidatingWebhookConfigurationList(V1beta1ValidatingWebhookConfigurationList s) => VersionConverter.Mapper.Map<V1ValidatingWebhookConfigurationList>(s);
// }
// public partial class V1beta1ValidatingWebhookConfigurationList
// {
// public static explicit operator V1beta1ValidatingWebhookConfigurationList(V1ValidatingWebhookConfigurationList s) => VersionConverter.Mapper.Map<V1beta1ValidatingWebhookConfigurationList>(s);
// }
//
// public partial class V1TokenReview
// {
// public static explicit operator V1TokenReview(V1beta1TokenReview s) => VersionConverter.Mapper.Map<V1TokenReview>(s);
// }
// public partial class V1beta1TokenReview
// {
// public static explicit operator V1beta1TokenReview(V1TokenReview s) => VersionConverter.Mapper.Map<V1beta1TokenReview>(s);
// }
//
// public partial class V1LocalSubjectAccessReview
// {
// public static explicit operator V1LocalSubjectAccessReview(V1beta1LocalSubjectAccessReview s) => VersionConverter.Mapper.Map<V1LocalSubjectAccessReview>(s);
// }
// public partial class V1beta1LocalSubjectAccessReview
// {
// public static explicit operator V1beta1LocalSubjectAccessReview(V1LocalSubjectAccessReview s) => VersionConverter.Mapper.Map<V1beta1LocalSubjectAccessReview>(s);
// }
//
// public partial class V1SelfSubjectAccessReview
// {
// public static explicit operator V1SelfSubjectAccessReview(V1beta1SelfSubjectAccessReview s) => VersionConverter.Mapper.Map<V1SelfSubjectAccessReview>(s);
// }
// public partial class V1beta1SelfSubjectAccessReview
// {
// public static explicit operator V1beta1SelfSubjectAccessReview(V1SelfSubjectAccessReview s) => VersionConverter.Mapper.Map<V1beta1SelfSubjectAccessReview>(s);
// }
//
// public partial class V1SelfSubjectRulesReview
// {
// public static explicit operator V1SelfSubjectRulesReview(V1beta1SelfSubjectRulesReview s) => VersionConverter.Mapper.Map<V1SelfSubjectRulesReview>(s);
// }
// public partial class V1beta1SelfSubjectRulesReview
// {
// public static explicit operator V1beta1SelfSubjectRulesReview(V1SelfSubjectRulesReview s) => VersionConverter.Mapper.Map<V1beta1SelfSubjectRulesReview>(s);
// }
//
// public partial class V1SubjectAccessReview
// {
// public static explicit operator V1SubjectAccessReview(V1beta1SubjectAccessReview s) => VersionConverter.Mapper.Map<V1SubjectAccessReview>(s);
// }
// public partial class V1beta1SubjectAccessReview
// {
// public static explicit operator V1beta1SubjectAccessReview(V1SubjectAccessReview s) => VersionConverter.Mapper.Map<V1beta1SubjectAccessReview>(s);
// }
//
// public partial class V1Lease
// {
// public static explicit operator V1Lease(V1beta1Lease s) => VersionConverter.Mapper.Map<V1Lease>(s);
// }
// public partial class V1beta1Lease
// {
// public static explicit operator V1beta1Lease(V1Lease s) => VersionConverter.Mapper.Map<V1beta1Lease>(s);
// }
//
// public partial class V1LeaseList
// {
// public static explicit operator V1LeaseList(V1beta1LeaseList s) => VersionConverter.Mapper.Map<V1LeaseList>(s);
// }
// public partial class V1beta1LeaseList
// {
// public static explicit operator V1beta1LeaseList(V1LeaseList s) => VersionConverter.Mapper.Map<V1beta1LeaseList>(s);
// }
//
// public partial class V1Event
// {
// public static explicit operator V1Event(V1beta1Event s) => VersionConverter.Mapper.Map<V1Event>(s);
// }
// public partial class V1beta1Event
// {
// public static explicit operator V1beta1Event(V1Event s) => VersionConverter.Mapper.Map<V1beta1Event>(s);
// }
//
// public partial class V1EventList
// {
// public static explicit operator V1EventList(V1beta1EventList s) => VersionConverter.Mapper.Map<V1EventList>(s);
// }
// public partial class V1beta1EventList
// {
// public static explicit operator V1beta1EventList(V1EventList s) => VersionConverter.Mapper.Map<V1beta1EventList>(s);
// }
//
// public partial class V1ClusterRole
// {
// public static explicit operator V1ClusterRole(V1beta1ClusterRole s) => VersionConverter.Mapper.Map<V1ClusterRole>(s);
// }
// public partial class V1beta1ClusterRole
// {
// public static explicit operator V1beta1ClusterRole(V1ClusterRole s) => VersionConverter.Mapper.Map<V1beta1ClusterRole>(s);
// }
//
// public partial class V1ClusterRoleBinding
// {
// public static explicit operator V1ClusterRoleBinding(V1beta1ClusterRoleBinding s) => VersionConverter.Mapper.Map<V1ClusterRoleBinding>(s);
// }
// public partial class V1beta1ClusterRoleBinding
// {
// public static explicit operator V1beta1ClusterRoleBinding(V1ClusterRoleBinding s) => VersionConverter.Mapper.Map<V1beta1ClusterRoleBinding>(s);
// }
//
// public partial class V1ClusterRoleBindingList
// {
// public static explicit operator V1ClusterRoleBindingList(V1beta1ClusterRoleBindingList s) => VersionConverter.Mapper.Map<V1ClusterRoleBindingList>(s);
// }
// public partial class V1beta1ClusterRoleBindingList
// {
// public static explicit operator V1beta1ClusterRoleBindingList(V1ClusterRoleBindingList s) => VersionConverter.Mapper.Map<V1beta1ClusterRoleBindingList>(s);
// }
//
// public partial class V1ClusterRoleList
// {
// public static explicit operator V1ClusterRoleList(V1beta1ClusterRoleList s) => VersionConverter.Mapper.Map<V1ClusterRoleList>(s);
// }
// public partial class V1beta1ClusterRoleList
// {
// public static explicit operator V1beta1ClusterRoleList(V1ClusterRoleList s) => VersionConverter.Mapper.Map<V1beta1ClusterRoleList>(s);
// }
//
// public partial class V1Role
// {
// public static explicit operator V1Role(V1beta1Role s) => VersionConverter.Mapper.Map<V1Role>(s);
// }
// public partial class V1beta1Role
// {
// public static explicit operator V1beta1Role(V1Role s) => VersionConverter.Mapper.Map<V1beta1Role>(s);
// }
//
// public partial class V1RoleBinding
// {
// public static explicit operator V1RoleBinding(V1beta1RoleBinding s) => VersionConverter.Mapper.Map<V1RoleBinding>(s);
// }
// public partial class V1beta1RoleBinding
// {
// public static explicit operator V1beta1RoleBinding(V1RoleBinding s) => VersionConverter.Mapper.Map<V1beta1RoleBinding>(s);
// }
//
// public partial class V1RoleBindingList
// {
// public static explicit operator V1RoleBindingList(V1beta1RoleBindingList s) => VersionConverter.Mapper.Map<V1RoleBindingList>(s);
// }
// public partial class V1beta1RoleBindingList
// {
// public static explicit operator V1beta1RoleBindingList(V1RoleBindingList s) => VersionConverter.Mapper.Map<V1beta1RoleBindingList>(s);
// }
//
// public partial class V1RoleList
// {
// public static explicit operator V1RoleList(V1beta1RoleList s) => VersionConverter.Mapper.Map<V1RoleList>(s);
// }
// public partial class V1beta1RoleList
// {
// public static explicit operator V1beta1RoleList(V1RoleList s) => VersionConverter.Mapper.Map<V1beta1RoleList>(s);
// }
//
// public partial class V1PriorityClass
// {
// public static explicit operator V1PriorityClass(V1beta1PriorityClass s) => VersionConverter.Mapper.Map<V1PriorityClass>(s);
// }
// public partial class V1beta1PriorityClass
// {
// public static explicit operator V1beta1PriorityClass(V1PriorityClass s) => VersionConverter.Mapper.Map<V1beta1PriorityClass>(s);
// }
//
// public partial class V1PriorityClassList
// {
// public static explicit operator V1PriorityClassList(V1beta1PriorityClassList s) => VersionConverter.Mapper.Map<V1PriorityClassList>(s);
// }
// public partial class V1beta1PriorityClassList
// {
// public static explicit operator V1beta1PriorityClassList(V1PriorityClassList s) => VersionConverter.Mapper.Map<V1beta1PriorityClassList>(s);
// }
//
// public partial class V1CSIDriver
// {
// public static explicit operator V1CSIDriver(V1beta1CSIDriver s) => VersionConverter.Mapper.Map<V1CSIDriver>(s);
// }
// public partial class V1beta1CSIDriver
// {
// public static explicit operator V1beta1CSIDriver(V1CSIDriver s) => VersionConverter.Mapper.Map<V1beta1CSIDriver>(s);
// }
//
// public partial class V1CSIDriverList
// {
// public static explicit operator V1CSIDriverList(V1beta1CSIDriverList s) => VersionConverter.Mapper.Map<V1CSIDriverList>(s);
// }
// public partial class V1beta1CSIDriverList
// {
// public static explicit operator V1beta1CSIDriverList(V1CSIDriverList s) => VersionConverter.Mapper.Map<V1beta1CSIDriverList>(s);
// }
//
// public partial class V1CSINode
// {
// public static explicit operator V1CSINode(V1beta1CSINode s) => VersionConverter.Mapper.Map<V1CSINode>(s);
// }
// public partial class V1beta1CSINode
// {
// public static explicit operator V1beta1CSINode(V1CSINode s) => VersionConverter.Mapper.Map<V1beta1CSINode>(s);
// }
//
// public partial class V1CSINodeList
// {
// public static explicit operator V1CSINodeList(V1beta1CSINodeList s) => VersionConverter.Mapper.Map<V1CSINodeList>(s);
// }
// public partial class V1beta1CSINodeList
// {
// public static explicit operator V1beta1CSINodeList(V1CSINodeList s) => VersionConverter.Mapper.Map<V1beta1CSINodeList>(s);
// }
//
// public partial class V1StorageClass
// {
// public static explicit operator V1StorageClass(V1beta1StorageClass s) => VersionConverter.Mapper.Map<V1StorageClass>(s);
// }
// public partial class V1beta1StorageClass
// {
// public static explicit operator V1beta1StorageClass(V1StorageClass s) => VersionConverter.Mapper.Map<V1beta1StorageClass>(s);
// }
//
// public partial class V1StorageClassList
// {
// public static explicit operator V1StorageClassList(V1beta1StorageClassList s) => VersionConverter.Mapper.Map<V1StorageClassList>(s);
// }
// public partial class V1beta1StorageClassList
// {
// public static explicit operator V1beta1StorageClassList(V1StorageClassList s) => VersionConverter.Mapper.Map<V1beta1StorageClassList>(s);
// }
//
// public partial class V1VolumeAttachment
// {
// public static explicit operator V1VolumeAttachment(V1beta1VolumeAttachment s) => VersionConverter.Mapper.Map<V1VolumeAttachment>(s);
// }
// public partial class V1beta1VolumeAttachment
// {
// public static explicit operator V1beta1VolumeAttachment(V1VolumeAttachment s) => VersionConverter.Mapper.Map<V1beta1VolumeAttachment>(s);
// }
//
// public partial class V1VolumeAttachmentList
// {
// public static explicit operator V1VolumeAttachmentList(V1beta1VolumeAttachmentList s) => VersionConverter.Mapper.Map<V1VolumeAttachmentList>(s);
// }
// public partial class V1beta1VolumeAttachmentList
// {
// public static explicit operator V1beta1VolumeAttachmentList(V1VolumeAttachmentList s) => VersionConverter.Mapper.Map<V1beta1VolumeAttachmentList>(s);
// }
//
// public partial class V1CustomResourceDefinition
// {
// public static explicit operator V1CustomResourceDefinition(V1beta1CustomResourceDefinition s) => VersionConverter.Mapper.Map<V1CustomResourceDefinition>(s);
// }
// public partial class V1beta1CustomResourceDefinition
// {
// public static explicit operator V1beta1CustomResourceDefinition(V1CustomResourceDefinition s) => VersionConverter.Mapper.Map<V1beta1CustomResourceDefinition>(s);
// }
//
// public partial class V1CustomResourceDefinitionList
// {
// public static explicit operator V1CustomResourceDefinitionList(V1beta1CustomResourceDefinitionList s) => VersionConverter.Mapper.Map<V1CustomResourceDefinitionList>(s);
// }
// public partial class V1beta1CustomResourceDefinitionList
// {
// public static explicit operator V1beta1CustomResourceDefinitionList(V1CustomResourceDefinitionList s) => VersionConverter.Mapper.Map<V1beta1CustomResourceDefinitionList>(s);
// }
//
// public partial class V1APIService
// {
// public static explicit operator V1APIService(V1beta1APIService s) => VersionConverter.Mapper.Map<V1APIService>(s);
// }
// public partial class V1beta1APIService
// {
// public static explicit operator V1beta1APIService(V1APIService s) => VersionConverter.Mapper.Map<V1beta1APIService>(s);
// }
//
// public partial class V1APIServiceList
// {
// public static explicit operator V1APIServiceList(V1beta1APIServiceList s) => VersionConverter.Mapper.Map<V1APIServiceList>(s);
// }
// public partial class V1beta1APIServiceList
// {
// public static explicit operator V1beta1APIServiceList(V1APIServiceList s) => VersionConverter.Mapper.Map<V1beta1APIServiceList>(s);
// }
//
// public partial class V1AggregationRule
// {
// public static explicit operator V1AggregationRule(V1beta1AggregationRule s) => VersionConverter.Mapper.Map<V1AggregationRule>(s);
// }
// public partial class V1beta1AggregationRule
// {
// public static explicit operator V1beta1AggregationRule(V1AggregationRule s) => VersionConverter.Mapper.Map<V1beta1AggregationRule>(s);
// }
//
// public partial class V1APIServiceCondition
// {
// public static explicit operator V1APIServiceCondition(V1beta1APIServiceCondition s) => VersionConverter.Mapper.Map<V1APIServiceCondition>(s);
// }
// public partial class V1beta1APIServiceCondition
// {
// public static explicit operator V1beta1APIServiceCondition(V1APIServiceCondition s) => VersionConverter.Mapper.Map<V1beta1APIServiceCondition>(s);
// }
//
// public partial class V1APIServiceSpec
// {
// public static explicit operator V1APIServiceSpec(V1beta1APIServiceSpec s) => VersionConverter.Mapper.Map<V1APIServiceSpec>(s);
// }
// public partial class V1beta1APIServiceSpec
// {
// public static explicit operator V1beta1APIServiceSpec(V1APIServiceSpec s) => VersionConverter.Mapper.Map<V1beta1APIServiceSpec>(s);
// }
//
// public partial class V1APIServiceStatus
// {
// public static explicit operator V1APIServiceStatus(V1beta1APIServiceStatus s) => VersionConverter.Mapper.Map<V1APIServiceStatus>(s);
// }
// public partial class V1beta1APIServiceStatus
// {
// public static explicit operator V1beta1APIServiceStatus(V1APIServiceStatus s) => VersionConverter.Mapper.Map<V1beta1APIServiceStatus>(s);
// }
//
// public partial class V1CSIDriverSpec
// {
// public static explicit operator V1CSIDriverSpec(V1beta1CSIDriverSpec s) => VersionConverter.Mapper.Map<V1CSIDriverSpec>(s);
// }
// public partial class V1beta1CSIDriverSpec
// {
// public static explicit operator V1beta1CSIDriverSpec(V1CSIDriverSpec s) => VersionConverter.Mapper.Map<V1beta1CSIDriverSpec>(s);
// }
//
// public partial class V1CSINodeDriver
// {
// public static explicit operator V1CSINodeDriver(V1beta1CSINodeDriver s) => VersionConverter.Mapper.Map<V1CSINodeDriver>(s);
// }
// public partial class V1beta1CSINodeDriver
// {
// public static explicit operator V1beta1CSINodeDriver(V1CSINodeDriver s) => VersionConverter.Mapper.Map<V1beta1CSINodeDriver>(s);
// }
//
// public partial class V1CSINodeSpec
// {
// public static explicit operator V1CSINodeSpec(V1beta1CSINodeSpec s) => VersionConverter.Mapper.Map<V1CSINodeSpec>(s);
// }
// public partial class V1beta1CSINodeSpec
// {
// public static explicit operator V1beta1CSINodeSpec(V1CSINodeSpec s) => VersionConverter.Mapper.Map<V1beta1CSINodeSpec>(s);
// }
//
// public partial class V1CustomResourceColumnDefinition
// {
// public static explicit operator V1CustomResourceColumnDefinition(V1beta1CustomResourceColumnDefinition s) => VersionConverter.Mapper.Map<V1CustomResourceColumnDefinition>(s);
// }
// public partial class V1beta1CustomResourceColumnDefinition
// {
// public static explicit operator V1beta1CustomResourceColumnDefinition(V1CustomResourceColumnDefinition s) => VersionConverter.Mapper.Map<V1beta1CustomResourceColumnDefinition>(s);
// }
//
// public partial class V1CustomResourceConversion
// {
// public static explicit operator V1CustomResourceConversion(V1beta1CustomResourceConversion s) => VersionConverter.Mapper.Map<V1CustomResourceConversion>(s);
// }
// public partial class V1beta1CustomResourceConversion
// {
// public static explicit operator V1beta1CustomResourceConversion(V1CustomResourceConversion s) => VersionConverter.Mapper.Map<V1beta1CustomResourceConversion>(s);
// }
//
// public partial class V1CustomResourceDefinitionCondition
// {
// public static explicit operator V1CustomResourceDefinitionCondition(V1beta1CustomResourceDefinitionCondition s) => VersionConverter.Mapper.Map<V1CustomResourceDefinitionCondition>(s);
// }
// public partial class V1beta1CustomResourceDefinitionCondition
// {
// public static explicit operator V1beta1CustomResourceDefinitionCondition(V1CustomResourceDefinitionCondition s) => VersionConverter.Mapper.Map<V1beta1CustomResourceDefinitionCondition>(s);
// }
//
// public partial class V1CustomResourceDefinitionNames
// {
// public static explicit operator V1CustomResourceDefinitionNames(V1beta1CustomResourceDefinitionNames s) => VersionConverter.Mapper.Map<V1CustomResourceDefinitionNames>(s);
// }
// public partial class V1beta1CustomResourceDefinitionNames
// {
// public static explicit operator V1beta1CustomResourceDefinitionNames(V1CustomResourceDefinitionNames s) => VersionConverter.Mapper.Map<V1beta1CustomResourceDefinitionNames>(s);
// }
//
// public partial class V1CustomResourceDefinitionSpec
// {
// public static explicit operator V1CustomResourceDefinitionSpec(V1beta1CustomResourceDefinitionSpec s) => VersionConverter.Mapper.Map<V1CustomResourceDefinitionSpec>(s);
// }
// public partial class V1beta1CustomResourceDefinitionSpec
// {
// public static explicit operator V1beta1CustomResourceDefinitionSpec(V1CustomResourceDefinitionSpec s) => VersionConverter.Mapper.Map<V1beta1CustomResourceDefinitionSpec>(s);
// }
//
// public partial class V1CustomResourceDefinitionStatus
// {
// public static explicit operator V1CustomResourceDefinitionStatus(V1beta1CustomResourceDefinitionStatus s) => VersionConverter.Mapper.Map<V1CustomResourceDefinitionStatus>(s);
// }
// public partial class V1beta1CustomResourceDefinitionStatus
// {
// public static explicit operator V1beta1CustomResourceDefinitionStatus(V1CustomResourceDefinitionStatus s) => VersionConverter.Mapper.Map<V1beta1CustomResourceDefinitionStatus>(s);
// }
//
// public partial class V1CustomResourceDefinitionVersion
// {
// public static explicit operator V1CustomResourceDefinitionVersion(V1beta1CustomResourceDefinitionVersion s) => VersionConverter.Mapper.Map<V1CustomResourceDefinitionVersion>(s);
// }
// public partial class V1beta1CustomResourceDefinitionVersion
// {
// public static explicit operator V1beta1CustomResourceDefinitionVersion(V1CustomResourceDefinitionVersion s) => VersionConverter.Mapper.Map<V1beta1CustomResourceDefinitionVersion>(s);
// }
//
// public partial class V1CustomResourceSubresources
// {
// public static explicit operator V1CustomResourceSubresources(V1beta1CustomResourceSubresources s) => VersionConverter.Mapper.Map<V1CustomResourceSubresources>(s);
// }
// public partial class V1beta1CustomResourceSubresources
// {
// public static explicit operator V1beta1CustomResourceSubresources(V1CustomResourceSubresources s) => VersionConverter.Mapper.Map<V1beta1CustomResourceSubresources>(s);
// }
//
// public partial class V1CustomResourceSubresourceScale
// {
// public static explicit operator V1CustomResourceSubresourceScale(V1beta1CustomResourceSubresourceScale s) => VersionConverter.Mapper.Map<V1CustomResourceSubresourceScale>(s);
// }
// public partial class V1beta1CustomResourceSubresourceScale
// {
// public static explicit operator V1beta1CustomResourceSubresourceScale(V1CustomResourceSubresourceScale s) => VersionConverter.Mapper.Map<V1beta1CustomResourceSubresourceScale>(s);
// }
//
// public partial class V1CustomResourceValidation
// {
// public static explicit operator V1CustomResourceValidation(V1beta1CustomResourceValidation s) => VersionConverter.Mapper.Map<V1CustomResourceValidation>(s);
// }
// public partial class V1beta1CustomResourceValidation
// {
// public static explicit operator V1beta1CustomResourceValidation(V1CustomResourceValidation s) => VersionConverter.Mapper.Map<V1beta1CustomResourceValidation>(s);
// }
//
// public partial class V1EndpointPort
// {
// public static explicit operator V1EndpointPort(V1beta1EndpointPort s) => VersionConverter.Mapper.Map<V1EndpointPort>(s);
// }
// public partial class V1beta1EndpointPort
// {
// public static explicit operator V1beta1EndpointPort(V1EndpointPort s) => VersionConverter.Mapper.Map<V1beta1EndpointPort>(s);
// }
//
// public partial class V1EventSeries
// {
// public static explicit operator V1EventSeries(V1beta1EventSeries s) => VersionConverter.Mapper.Map<V1EventSeries>(s);
// }
// public partial class V1beta1EventSeries
// {
// public static explicit operator V1beta1EventSeries(V1EventSeries s) => VersionConverter.Mapper.Map<V1beta1EventSeries>(s);
// }
//
// public partial class V1ExternalDocumentation
// {
// public static explicit operator V1ExternalDocumentation(V1beta1ExternalDocumentation s) => VersionConverter.Mapper.Map<V1ExternalDocumentation>(s);
// }
// public partial class V1beta1ExternalDocumentation
// {
// public static explicit operator V1beta1ExternalDocumentation(V1ExternalDocumentation s) => VersionConverter.Mapper.Map<V1beta1ExternalDocumentation>(s);
// }
//
// public partial class V1JSONSchemaProps
// {
// public static explicit operator V1JSONSchemaProps(V1beta1JSONSchemaProps s) => VersionConverter.Mapper.Map<V1JSONSchemaProps>(s);
// }
// public partial class V1beta1JSONSchemaProps
// {
// public static explicit operator V1beta1JSONSchemaProps(V1JSONSchemaProps s) => VersionConverter.Mapper.Map<V1beta1JSONSchemaProps>(s);
// }
//
// public partial class V1LeaseSpec
// {
// public static explicit operator V1LeaseSpec(V1beta1LeaseSpec s) => VersionConverter.Mapper.Map<V1LeaseSpec>(s);
// }
// public partial class V1beta1LeaseSpec
// {
// public static explicit operator V1beta1LeaseSpec(V1LeaseSpec s) => VersionConverter.Mapper.Map<V1beta1LeaseSpec>(s);
// }
//
// public partial class V1MutatingWebhook
// {
// public static explicit operator V1MutatingWebhook(V1beta1MutatingWebhook s) => VersionConverter.Mapper.Map<V1MutatingWebhook>(s);
// }
// public partial class V1beta1MutatingWebhook
// {
// public static explicit operator V1beta1MutatingWebhook(V1MutatingWebhook s) => VersionConverter.Mapper.Map<V1beta1MutatingWebhook>(s);
// }
//
// public partial class V1NonResourceAttributes
// {
// public static explicit operator V1NonResourceAttributes(V1beta1NonResourceAttributes s) => VersionConverter.Mapper.Map<V1NonResourceAttributes>(s);
// }
// public partial class V1beta1NonResourceAttributes
// {
// public static explicit operator V1beta1NonResourceAttributes(V1NonResourceAttributes s) => VersionConverter.Mapper.Map<V1beta1NonResourceAttributes>(s);
// }
//
// public partial class V1NonResourceRule
// {
// public static explicit operator V1NonResourceRule(V1beta1NonResourceRule s) => VersionConverter.Mapper.Map<V1NonResourceRule>(s);
// }
// public partial class V1beta1NonResourceRule
// {
// public static explicit operator V1beta1NonResourceRule(V1NonResourceRule s) => VersionConverter.Mapper.Map<V1beta1NonResourceRule>(s);
// }
//
// public partial class V1PolicyRule
// {
// public static explicit operator V1PolicyRule(V1beta1PolicyRule s) => VersionConverter.Mapper.Map<V1PolicyRule>(s);
// }
// public partial class V1beta1PolicyRule
// {
// public static explicit operator V1beta1PolicyRule(V1PolicyRule s) => VersionConverter.Mapper.Map<V1beta1PolicyRule>(s);
// }
//
// public partial class V1ResourceAttributes
// {
// public static explicit operator V1ResourceAttributes(V1beta1ResourceAttributes s) => VersionConverter.Mapper.Map<V1ResourceAttributes>(s);
// }
// public partial class V1beta1ResourceAttributes
// {
// public static explicit operator V1beta1ResourceAttributes(V1ResourceAttributes s) => VersionConverter.Mapper.Map<V1beta1ResourceAttributes>(s);
// }
//
// public partial class V1ResourceRule
// {
// public static explicit operator V1ResourceRule(V1beta1ResourceRule s) => VersionConverter.Mapper.Map<V1ResourceRule>(s);
// }
// public partial class V1beta1ResourceRule
// {
// public static explicit operator V1beta1ResourceRule(V1ResourceRule s) => VersionConverter.Mapper.Map<V1beta1ResourceRule>(s);
// }
//
// public partial class V1RoleRef
// {
// public static explicit operator V1RoleRef(V1beta1RoleRef s) => VersionConverter.Mapper.Map<V1RoleRef>(s);
// }
// public partial class V1beta1RoleRef
// {
// public static explicit operator V1beta1RoleRef(V1RoleRef s) => VersionConverter.Mapper.Map<V1beta1RoleRef>(s);
// }
//
// public partial class V1RuleWithOperations
// {
// public static explicit operator V1RuleWithOperations(V1beta1RuleWithOperations s) => VersionConverter.Mapper.Map<V1RuleWithOperations>(s);
// }
// public partial class V1beta1RuleWithOperations
// {
// public static explicit operator V1beta1RuleWithOperations(V1RuleWithOperations s) => VersionConverter.Mapper.Map<V1beta1RuleWithOperations>(s);
// }
//
// public partial class V1SelfSubjectAccessReviewSpec
// {
// public static explicit operator V1SelfSubjectAccessReviewSpec(V1beta1SelfSubjectAccessReviewSpec s) => VersionConverter.Mapper.Map<V1SelfSubjectAccessReviewSpec>(s);
// }
// public partial class V1beta1SelfSubjectAccessReviewSpec
// {
// public static explicit operator V1beta1SelfSubjectAccessReviewSpec(V1SelfSubjectAccessReviewSpec s) => VersionConverter.Mapper.Map<V1beta1SelfSubjectAccessReviewSpec>(s);
// }
//
// public partial class V1SelfSubjectRulesReviewSpec
// {
// public static explicit operator V1SelfSubjectRulesReviewSpec(V1beta1SelfSubjectRulesReviewSpec s) => VersionConverter.Mapper.Map<V1SelfSubjectRulesReviewSpec>(s);
// }
// public partial class V1beta1SelfSubjectRulesReviewSpec
// {
// public static explicit operator V1beta1SelfSubjectRulesReviewSpec(V1SelfSubjectRulesReviewSpec s) => VersionConverter.Mapper.Map<V1beta1SelfSubjectRulesReviewSpec>(s);
// }
//
// public partial class V1Subject
// {
// public static explicit operator V1Subject(V1beta1Subject s) => VersionConverter.Mapper.Map<V1Subject>(s);
// }
// public partial class V1beta1Subject
// {
// public static explicit operator V1beta1Subject(V1Subject s) => VersionConverter.Mapper.Map<V1beta1Subject>(s);
// }
//
// public partial class V1SubjectAccessReviewSpec
// {
// public static explicit operator V1SubjectAccessReviewSpec(V1beta1SubjectAccessReviewSpec s) => VersionConverter.Mapper.Map<V1SubjectAccessReviewSpec>(s);
// }
// public partial class V1beta1SubjectAccessReviewSpec
// {
// public static explicit operator V1beta1SubjectAccessReviewSpec(V1SubjectAccessReviewSpec s) => VersionConverter.Mapper.Map<V1beta1SubjectAccessReviewSpec>(s);
// }
//
// public partial class V1SubjectAccessReviewStatus
// {
// public static explicit operator V1SubjectAccessReviewStatus(V1beta1SubjectAccessReviewStatus s) => VersionConverter.Mapper.Map<V1SubjectAccessReviewStatus>(s);
// }
// public partial class V1beta1SubjectAccessReviewStatus
// {
// public static explicit operator V1beta1SubjectAccessReviewStatus(V1SubjectAccessReviewStatus s) => VersionConverter.Mapper.Map<V1beta1SubjectAccessReviewStatus>(s);
// }
//
// public partial class V1SubjectRulesReviewStatus
// {
// public static explicit operator V1SubjectRulesReviewStatus(V1beta1SubjectRulesReviewStatus s) => VersionConverter.Mapper.Map<V1SubjectRulesReviewStatus>(s);
// }
// public partial class V1beta1SubjectRulesReviewStatus
// {
// public static explicit operator V1beta1SubjectRulesReviewStatus(V1SubjectRulesReviewStatus s) => VersionConverter.Mapper.Map<V1beta1SubjectRulesReviewStatus>(s);
// }
//
// public partial class V1TokenReviewSpec
// {
// public static explicit operator V1TokenReviewSpec(V1beta1TokenReviewSpec s) => VersionConverter.Mapper.Map<V1TokenReviewSpec>(s);
// }
// public partial class V1beta1TokenReviewSpec
// {
// public static explicit operator V1beta1TokenReviewSpec(V1TokenReviewSpec s) => VersionConverter.Mapper.Map<V1beta1TokenReviewSpec>(s);
// }
//
// public partial class V1TokenReviewStatus
// {
// public static explicit operator V1TokenReviewStatus(V1beta1TokenReviewStatus s) => VersionConverter.Mapper.Map<V1TokenReviewStatus>(s);
// }
// public partial class V1beta1TokenReviewStatus
// {
// public static explicit operator V1beta1TokenReviewStatus(V1TokenReviewStatus s) => VersionConverter.Mapper.Map<V1beta1TokenReviewStatus>(s);
// }
//
// public partial class V1UserInfo
// {
// public static explicit operator V1UserInfo(V1beta1UserInfo s) => VersionConverter.Mapper.Map<V1UserInfo>(s);
// }
// public partial class V1beta1UserInfo
// {
// public static explicit operator V1beta1UserInfo(V1UserInfo s) => VersionConverter.Mapper.Map<V1beta1UserInfo>(s);
// }
//
// public partial class V1ValidatingWebhook
// {
// public static explicit operator V1ValidatingWebhook(V1beta1ValidatingWebhook s) => VersionConverter.Mapper.Map<V1ValidatingWebhook>(s);
// }
// public partial class V1beta1ValidatingWebhook
// {
// public static explicit operator V1beta1ValidatingWebhook(V1ValidatingWebhook s) => VersionConverter.Mapper.Map<V1beta1ValidatingWebhook>(s);
// }
//
// public partial class V1VolumeAttachmentSource
// {
// public static explicit operator V1VolumeAttachmentSource(V1beta1VolumeAttachmentSource s) => VersionConverter.Mapper.Map<V1VolumeAttachmentSource>(s);
// }
// public partial class V1beta1VolumeAttachmentSource
// {
// public static explicit operator V1beta1VolumeAttachmentSource(V1VolumeAttachmentSource s) => VersionConverter.Mapper.Map<V1beta1VolumeAttachmentSource>(s);
// }
//
// public partial class V1VolumeAttachmentSpec
// {
// public static explicit operator V1VolumeAttachmentSpec(V1beta1VolumeAttachmentSpec s) => VersionConverter.Mapper.Map<V1VolumeAttachmentSpec>(s);
// }
// public partial class V1beta1VolumeAttachmentSpec
// {
// public static explicit operator V1beta1VolumeAttachmentSpec(V1VolumeAttachmentSpec s) => VersionConverter.Mapper.Map<V1beta1VolumeAttachmentSpec>(s);
// }
//
// public partial class V1VolumeAttachmentStatus
// {
// public static explicit operator V1VolumeAttachmentStatus(V1beta1VolumeAttachmentStatus s) => VersionConverter.Mapper.Map<V1VolumeAttachmentStatus>(s);
// }
// public partial class V1beta1VolumeAttachmentStatus
// {
// public static explicit operator V1beta1VolumeAttachmentStatus(V1VolumeAttachmentStatus s) => VersionConverter.Mapper.Map<V1beta1VolumeAttachmentStatus>(s);
// }
//
// public partial class V1VolumeError
// {
// public static explicit operator V1VolumeError(V1beta1VolumeError s) => VersionConverter.Mapper.Map<V1VolumeError>(s);
// }
// public partial class V1beta1VolumeError
// {
// public static explicit operator V1beta1VolumeError(V1VolumeError s) => VersionConverter.Mapper.Map<V1beta1VolumeError>(s);
// }
//
// public partial class V1VolumeNodeResources
// {
// public static explicit operator V1VolumeNodeResources(V1beta1VolumeNodeResources s) => VersionConverter.Mapper.Map<V1VolumeNodeResources>(s);
// }
// public partial class V1beta1VolumeNodeResources
// {
// public static explicit operator V1beta1VolumeNodeResources(V1VolumeNodeResources s) => VersionConverter.Mapper.Map<V1beta1VolumeNodeResources>(s);
// }
//
//
//
// }

View File

@@ -0,0 +1,416 @@
// WARNING: DO NOT LEAVE COMMENTED CODE IN THIS FILE. IT GETS SCANNED BY GEN PROJECT SO IT CAN EXCLUDE ANY MANUALLY DEFINED MAPS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using AutoMapper;
using k8s.Models;
using Newtonsoft.Json;
namespace k8s.Versioning
{
/// <summary>
/// Provides mappers that converts Kubernetes models between different versions
/// </summary>
public static partial class VersionConverter
{
static VersionConverter()
{
UpdateMappingConfiguration(expression => { });
}
public static IMapper Mapper { get; private set; }
internal static MapperConfiguration MapperConfiguration { get; private set; }
/// <summary>
/// Two level lookup of model types by Kind and then Version
/// </summary>
internal static Dictionary<string, Dictionary<string, Type>> KindVersionsMap { get; private set; }
public static Type GetTypeForVersion<T>(string version)
{
return GetTypeForVersion(typeof(T), version);
}
public static Type GetTypeForVersion(Type type, string version)
{
return KindVersionsMap[type.GetKubernetesTypeMetadata().Kind][version];
}
public static void UpdateMappingConfiguration(Action<IMapperConfigurationExpression> configuration)
{
MapperConfiguration = new MapperConfiguration(cfg =>
{
GetConfigurations(cfg);
configuration(cfg);
});
Mapper = MapperConfiguration.CreateMapper();
KindVersionsMap = MapperConfiguration
.GetAllTypeMaps()
.SelectMany(x => new[] { x.Types.SourceType, x.Types.DestinationType })
.Where(x => x.GetCustomAttribute<KubernetesEntityAttribute>() != null)
.Select(x =>
{
var attr = GetKubernetesEntityAttribute(x);
return new { attr.Kind, attr.ApiVersion, Type = x };
})
.GroupBy(x => x.Kind)
.ToDictionary(x => x.Key, kindGroup => kindGroup
.GroupBy(x => x.ApiVersion)
.ToDictionary(x => x.Key,
versionGroup => versionGroup.Select(x => x.Type).Distinct().Single())); // should only be one type for each Kind/Version combination
}
public static object ConvertToVersion(object source, string apiVersion)
{
var type = source.GetType();
var attr = GetKubernetesEntityAttribute(type);
if (attr.ApiVersion == apiVersion)
{
return source;
}
if (!KindVersionsMap.TryGetValue(attr.Kind, out var kindVersions))
{
throw new InvalidOperationException($"Version converter does not have any registered types for Kind `{attr.Kind}`");
}
if (!kindVersions.TryGetValue(apiVersion, out var targetType) || !kindVersions.TryGetValue(attr.ApiVersion, out var sourceType) || MapperConfiguration.FindTypeMapFor(sourceType, targetType) == null)
{
throw new InvalidOperationException($"There is no conversion mapping registered for Kind `{attr.Kind}` from ApiVersion {attr.ApiVersion} to {apiVersion}");
}
return Mapper.Map(source, sourceType, targetType);
}
private static KubernetesEntityAttribute GetKubernetesEntityAttribute(Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
var attr = type.GetCustomAttribute<KubernetesEntityAttribute>();
if (attr == null)
{
throw new InvalidOperationException($"Type {type} does not have {nameof(KubernetesEntityAttribute)}");
}
return attr;
}
internal static void GetConfigurations(IMapperConfigurationExpression cfg)
{
AutoConfigurations(cfg);
ManualConfigurations(cfg);
}
private static void ManualConfigurations(IMapperConfigurationExpression cfg)
{
cfg.AllowNullCollections = true;
cfg.DisableConstructorMapping();
cfg.ForAllMaps((typeMap, opt) =>
{
if (!typeof(IKubernetesObject).IsAssignableFrom(typeMap.Types.DestinationType))
{
return;
}
var metadata = typeMap.Types.DestinationType.GetKubernetesTypeMetadata();
opt.ForMember(nameof(IKubernetesObject.ApiVersion), x => x.Ignore());
opt.ForMember(nameof(IKubernetesObject.Kind), x => x.Ignore());
opt.AfterMap((from, to) =>
{
var obj = (IKubernetesObject)to;
obj.ApiVersion = !string.IsNullOrEmpty(metadata.Group) ? $"{metadata.Group}/{metadata.ApiVersion}" : metadata.ApiVersion;
obj.Kind = metadata.Kind;
});
});
cfg.CreateMap<V1Subject, Rbacv1alpha1Subject>()
.ForMember(dest => dest.ApiVersion, opt => opt.Ignore())
.ReverseMap();
cfg.CreateMap<V1beta1Subject, Rbacv1alpha1Subject>()
.ForMember(dest => dest.ApiVersion, opt => opt.Ignore())
.ReverseMap();
cfg.CreateMap<V1alpha1RuntimeClass, V1beta1RuntimeClass>()
.ForMember(dest => dest.Handler, opt => opt.MapFrom(src => src.Spec.RuntimeHandler))
.ForMember(dest => dest.Overhead, opt => opt.MapFrom(src => src.Spec.Overhead))
.ForMember(dest => dest.Scheduling, opt => opt.MapFrom(src => src.Spec.Scheduling))
.ReverseMap();
cfg.CreateMap<V2beta1ResourceMetricStatus, V2beta2MetricValueStatus>()
.ForMember(dest => dest.AverageValue, opt => opt.MapFrom(src => src.CurrentAverageValue))
.ForMember(dest => dest.AverageUtilization, opt => opt.MapFrom(src => src.CurrentAverageUtilization))
.ForMember(dest => dest.Value, opt => opt.Ignore());
cfg.CreateMap<V2beta1ResourceMetricStatus, V2beta2ResourceMetricStatus>()
.ForMember(dest => dest.Current, opt => opt.MapFrom(src => src));
cfg.CreateMap<V2beta2ResourceMetricStatus, V2beta1ResourceMetricStatus>()
.ForMember(dest => dest.CurrentAverageValue, opt => opt.MapFrom(src => src.Current.AverageValue))
.ForMember(dest => dest.CurrentAverageUtilization, opt => opt.MapFrom(src => src.Current.AverageUtilization));
cfg.CreateMap<V2beta1ResourceMetricSource, V2beta2MetricTarget>()
.ForMember(dest => dest.AverageValue, opt => opt.MapFrom(src => src.TargetAverageValue))
.ForMember(dest => dest.Value, opt => opt.Ignore())
#if NET452
.ForMember(dest => dest.Type, opt => opt.ResolveUsing(src => src.TargetAverageValue != null ? "AverageValue" : "Utilization" ))
#else
.ForMember(dest => dest.Type, opt => opt.MapFrom((src, dest) => src.TargetAverageValue != null ? "AverageValue" : "Utilization"))
#endif
.ForMember(dest => dest.AverageUtilization, opt => opt.MapFrom(src => src.TargetAverageUtilization));
cfg.CreateMap<V2beta1ResourceMetricSource, V2beta2ResourceMetricSource>()
.ForMember(dest => dest.Target, opt => opt.MapFrom(src => src));
cfg.CreateMap<V2beta2ResourceMetricSource, V2beta1ResourceMetricSource>()
.ForMember(dest => dest.TargetAverageUtilization, opt => opt.MapFrom(src => src.Target.AverageUtilization))
.ForMember(dest => dest.TargetAverageValue, opt => opt.MapFrom(src => src.Target.Value));
cfg.CreateMap<V2beta1PodsMetricStatus, V2beta2MetricValueStatus>()
.ForMember(dest => dest.AverageValue, opt => opt.MapFrom(src => src.CurrentAverageValue))
.ForMember(dest => dest.Value, opt => opt.Ignore())
.ForMember(dest => dest.AverageUtilization, opt => opt.Ignore());
cfg.CreateMap<V2beta1PodsMetricStatus, V2beta2MetricIdentifier>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.MetricName))
.ForMember(dest => dest.Selector, opt => opt.MapFrom(src => src.Selector));
cfg.CreateMap<V2beta1PodsMetricStatus, V2beta2PodsMetricStatus>()
.ForMember(dest => dest.Current, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.Metric, opt => opt.MapFrom(src => src));
cfg.CreateMap<V2beta2PodsMetricStatus, V2beta1PodsMetricStatus>()
.ForMember(dest => dest.Selector, opt => opt.MapFrom(src => src.Metric.Selector))
.ForMember(dest => dest.CurrentAverageValue, opt => opt.MapFrom(src => src.Current.AverageValue))
.ForMember(dest => dest.MetricName, opt => opt.MapFrom(src => src.Metric.Name));
cfg.CreateMap<V2beta1PodsMetricSource, V2beta2MetricIdentifier>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.MetricName))
.ForMember(dest => dest.Selector, opt => opt.MapFrom(src => src.Selector))
.ReverseMap();
cfg.CreateMap<V2beta1PodsMetricSource, V2beta2MetricTarget>()
.ForMember(dest => dest.AverageValue, opt => opt.MapFrom(src => src.TargetAverageValue))
#if NET452
.ForMember(dest => dest.Type, opt => opt.UseValue("AverageValue"))
#else
.ForMember(dest => dest.Type, opt => opt.MapFrom((src, dest) => "AverageValue"))
#endif
.ForMember(dest => dest.Value, opt => opt.Ignore())
.ForMember(dest => dest.AverageUtilization, opt => opt.Ignore());
cfg.CreateMap<V2beta1PodsMetricSource, V2beta2PodsMetricSource>()
.ForMember(dest => dest.Metric, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.Target, opt => opt.MapFrom(src => src));
cfg.CreateMap<V2beta2PodsMetricSource, V2beta1PodsMetricSource>()
.ForMember(x => x.Selector, opt => opt.MapFrom(src => src.Metric.Selector))
.ForMember(x => x.MetricName, opt => opt.MapFrom(src => src.Metric.Name))
.ForMember(x => x.TargetAverageValue, opt => opt.MapFrom(src => src.Target.AverageValue));
cfg.CreateMap<V2beta1ObjectMetricStatus, V2beta2MetricIdentifier>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.MetricName))
.ForMember(dest => dest.Selector, opt => opt.MapFrom(src => src.Selector))
.ReverseMap();
cfg.CreateMap<V2beta1ObjectMetricStatus, V2beta2MetricValueStatus>()
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.CurrentValue))
.ForMember(dest => dest.AverageValue, opt => opt.MapFrom(src => src.AverageValue))
.ForMember(dest => dest.AverageUtilization, opt => opt.Ignore())
.ReverseMap();
cfg.CreateMap<V2beta1ObjectMetricStatus, V2beta2ObjectMetricStatus>()
.ForMember(x => x.Current, opt => opt.MapFrom(src => src))
.ForMember(x => x.Metric, opt => opt.MapFrom(src => src))
.ForMember(x => x.DescribedObject, opt => opt.MapFrom(src => src.Target));
cfg.CreateMap<V2beta2ObjectMetricStatus, V2beta1ObjectMetricStatus>()
.ForMember(x => x.CurrentValue, opt => opt.MapFrom(src => src.Current.Value))
.ForMember(x => x.AverageValue, opt => opt.MapFrom(src => src.Current.AverageValue))
.ForMember(x => x.MetricName, opt => opt.MapFrom(src => src.Metric.Name))
.ForMember(x => x.Target, opt => opt.MapFrom(src => src.DescribedObject))
.ForMember(x => x.Selector, opt => opt.MapFrom(src => src.Metric.Selector));
cfg.CreateMap<V2beta1ExternalMetricSource, V2beta2MetricTarget>()
.ForMember(x => x.Value, opt => opt.MapFrom(src => src.TargetValue))
.ForMember(x => x.AverageValue, opt => opt.MapFrom(src => src.TargetAverageValue))
.ForMember(x => x.AverageUtilization, opt => opt.Ignore())
#if NET452
.ForMember(x => x.Type, opt => opt.ResolveUsing(src => src.TargetValue != null ? "Value" : "AverageValue"));
#else
.ForMember(x => x.Type, opt => opt.MapFrom((src, dest) => src.TargetValue != null ? "Value" : "AverageValue"));
#endif
cfg.CreateMap<V2beta1ExternalMetricSource, V2beta2ExternalMetricSource>()
.ForMember(x => x.Metric, opt => opt.MapFrom(src => src))
.ForMember(x => x.Target, opt => opt.MapFrom(src => src));
cfg.CreateMap<V2beta2ExternalMetricSource, V2beta1ExternalMetricSource>()
.ForMember(x => x.TargetValue, opt => opt.MapFrom(src => src.Target.Value))
.ForMember(x => x.TargetAverageValue, opt => opt.MapFrom(src => src.Target.AverageValue))
.ForMember(x => x.MetricName, opt => opt.MapFrom(src => src.Metric.Name))
.ForMember(x => x.MetricSelector, opt => opt.MapFrom(src => src.Metric.Selector));
cfg.CreateMap<V2beta1ExternalMetricStatus, V2beta2ExternalMetricStatus>()
.ForMember(x => x.Current, opt => opt.MapFrom(src => src))
.ForMember(x => x.Metric, opt => opt.MapFrom(src => src));
cfg.CreateMap<V2beta2ExternalMetricStatus, V2beta1ExternalMetricStatus>()
.ForMember(x => x.CurrentValue, opt => opt.MapFrom(src => src.Current.Value))
.ForMember(x => x.CurrentAverageValue, opt => opt.MapFrom(src => src.Current.AverageValue))
.ForMember(x => x.MetricName, opt => opt.MapFrom(src => src.Metric.Name))
.ForMember(x => x.MetricSelector, opt => opt.MapFrom(src => src.Metric.Selector));
cfg.CreateMap<V2beta1ExternalMetricStatus, V2beta2MetricIdentifier>()
.ForMember(x => x.Name, opt => opt.MapFrom(src => src.MetricName))
.ForMember(x => x.Selector, opt => opt.MapFrom(src => src.MetricSelector))
.ReverseMap();
cfg.CreateMap<V2beta1ExternalMetricStatus, V2beta2MetricValueStatus>()
.ForMember(x => x.Value, opt => opt.MapFrom(src => src.CurrentValue))
.ForMember(x => x.AverageValue, opt => opt.MapFrom(src => src.CurrentAverageValue))
.ForMember(x => x.AverageUtilization, opt => opt.Ignore())
.ReverseMap();
cfg.CreateMap<V2beta1ObjectMetricSource, V2beta2MetricTarget>()
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.TargetValue))
.ForMember(dest => dest.AverageUtilization, opt => opt.Ignore())
.ForMember(dest => dest.AverageValue, opt => opt.MapFrom(src => src.AverageValue))
#if NET452
.ForMember(dest => dest.Type, opt => opt.ResolveUsing(src => src.TargetValue != null ? "Value" : "AverageValue"));
#else
.ForMember(dest => dest.Type, opt => opt.MapFrom((src, dest) => src.TargetValue != null ? "Value" : "AverageValue"));
#endif
cfg.CreateMap<V2beta1ObjectMetricSource, V2beta2ObjectMetricSource>()
.ForMember(dest => dest.Metric, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.Target, opt => opt.MapFrom(src => src))
.ForMember(dest => dest.DescribedObject, opt => opt.MapFrom(src => src.Target));
cfg.CreateMap<V2beta2ObjectMetricSource, V2beta1ObjectMetricSource>()
.ForMember(dest => dest.Target, opt => opt.MapFrom(src => src.DescribedObject))
.ForMember(dest => dest.MetricName, opt => opt.MapFrom(src => src.Metric.Name))
.ForMember(dest => dest.TargetValue, opt => opt.MapFrom(src => src.Target.Value))
.ForMember(dest => dest.AverageValue, opt => opt.MapFrom(src => src.Target.AverageValue))
.ForMember(dest => dest.Selector, opt => opt.MapFrom(src => src.Metric.Selector));
cfg.CreateMap<V2beta1ObjectMetricSource, V2beta2MetricIdentifier>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.MetricName))
.ForMember(dest => dest.Selector, opt => opt.MapFrom(src => src.Selector))
.ReverseMap();
cfg.CreateMap<V2beta1ExternalMetricSource, V2beta2MetricIdentifier>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.MetricName))
.ForMember(dest => dest.Selector, opt => opt.MapFrom(src => src.MetricSelector));
cfg.CreateMap<V2beta2MetricTarget, V2beta1ExternalMetricSource>() // todo: not needed
.ForMember(dest => dest.MetricName, opt => opt.Ignore())
.ForMember(dest => dest.MetricSelector, opt => opt.Ignore())
.ForMember(dest => dest.TargetValue, opt => opt.MapFrom(src => src.Value))
.ForMember(dest => dest.TargetValue, opt => opt.MapFrom(src => src.Value))
.ForMember(dest => dest.TargetAverageValue, opt => opt.MapFrom(src => src.AverageValue));
cfg.CreateMap<V1beta1CustomResourceConversion, V1WebhookConversion>()
.ForMember(dest => dest.ClientConfig, opt => opt.MapFrom(src => src.WebhookClientConfig))
.ReverseMap();
cfg.CreateMap<V1SubjectAccessReviewSpec, V1beta1SubjectAccessReviewSpec>()
.ForMember(dest => dest.Group, opt => opt.MapFrom(src => src.Groups))
.ReverseMap();
cfg.CreateMap<V1CustomResourceDefinitionSpec, V1beta1CustomResourceDefinitionSpec>()
.ForMember(dest => dest.AdditionalPrinterColumns, opt => opt.Ignore())
.ForMember(dest => dest.Subresources, opt => opt.Ignore())
.ForMember(dest => dest.Validation, opt => opt.Ignore())
.ForMember(dest => dest.Version, opt => opt.Ignore())
.AfterMap((from, to) =>
{
// in v1beta1, if all versions share the same common attributes, they should be declared once at parent level
if (to.Versions == null)
{
return;
}
if (to.Versions.Select(x => JsonConvert.SerializeObject(x.Schema)).Distinct().Count() == 1)
{
to.Validation = to.Versions.First().Schema;
foreach (var version in to.Versions)
{
version.Schema = null;
}
}
var allPrintColumnsInAllVersionsTheSame = to.Versions
.GroupBy(x => x.Name)
.Select(v => v
.OrderBy(x => x.Name)
.Select(x => x.AdditionalPrinterColumns)
.Select(JsonConvert.SerializeObject)
.Aggregate(new StringBuilder(), (sb, s) => sb.Append(s), sb => sb.ToString()))
.Distinct()
.Count() == 1;
if (allPrintColumnsInAllVersionsTheSame)
{
to.AdditionalPrinterColumns = to.Versions[0].AdditionalPrinterColumns;
foreach (var version in to.Versions)
{
version.AdditionalPrinterColumns = null;
}
}
var allSubresourcesInAllVersionsTheSame = to.Versions
.GroupBy(x => x.Name)
.Select(v => v
.OrderBy(x => x.Name)
.Select(x => x.Subresources)
.Select(JsonConvert.SerializeObject)
.Aggregate(new StringBuilder(), (sb, s) => sb.Append(s), sb => sb.ToString()))
.Distinct()
.Count() == 1;
if (allSubresourcesInAllVersionsTheSame)
{
to.Subresources = to.Versions[0].Subresources;
foreach (var version in to.Versions)
{
version.Subresources = null;
}
}
})
.ReverseMap()
.AfterMap((from, to) =>
{
if (from.Validation?.OpenAPIV3Schema != null)
{
foreach (var version in to.Versions)
{
version.Schema = (V1CustomResourceValidation)@from.Validation;
}
}
if (from.Subresources != null)
{
foreach (var version in to.Versions)
{
version.Subresources = (V1CustomResourceSubresources)@from.Subresources;
}
}
if (from.AdditionalPrinterColumns != null)
{
foreach (var version in to.Versions)
{
version.AdditionalPrinterColumns = @from.AdditionalPrinterColumns.Select(x => (V1CustomResourceColumnDefinition)x).ToList();
}
}
});
cfg.CreateMap<V1CustomResourceConversion, V1beta1CustomResourceConversion>()
.ForMember(dest => dest.ConversionReviewVersions, opt => opt.MapFrom(src => src.Webhook.ConversionReviewVersions))
.ForMember(dest => dest.WebhookClientConfig, opt => opt.MapFrom(src => src.Webhook.ClientConfig))
.ReverseMap();
cfg.CreateMap<V1HorizontalPodAutoscalerSpec, V2beta2HorizontalPodAutoscalerSpec>()
.ForMember(dest => dest.Metrics, opt => opt.Ignore())
.ForMember(dest => dest.Behavior, opt => opt.Ignore())
.ReverseMap();
cfg.CreateMap<V1HorizontalPodAutoscalerSpec, V2beta1HorizontalPodAutoscalerSpec>()
.ForMember(dest => dest.Metrics, opt => opt.Ignore())
.ReverseMap();
cfg.CreateMap<V2beta1HorizontalPodAutoscalerSpec, V2beta2HorizontalPodAutoscalerSpec>()
.ForMember(dest => dest.Behavior, opt => opt.Ignore())
.ReverseMap();
cfg.CreateMap<V1HorizontalPodAutoscalerStatus, V2beta1HorizontalPodAutoscalerStatus>()
.ForMember(dest => dest.Conditions, opt => opt.Ignore())
.ForMember(dest => dest.CurrentMetrics, opt => opt.Ignore())
.ReverseMap();
cfg.CreateMap<V1HorizontalPodAutoscalerStatus, V2beta2HorizontalPodAutoscalerStatus>()
.ForMember(dest => dest.Conditions, opt => opt.Ignore())
.ForMember(dest => dest.CurrentMetrics, opt => opt.Ignore())
.ReverseMap();
cfg.CreateMap<V1Event, V1beta1Event>()
.ForMember(dest => dest.DeprecatedCount, opt => opt.Ignore())
.ForMember(dest => dest.DeprecatedFirstTimestamp, opt => opt.MapFrom(src => src.FirstTimestamp))
.ForMember(dest => dest.DeprecatedLastTimestamp, opt => opt.MapFrom(src => src.LastTimestamp))
.ForMember(dest => dest.DeprecatedSource, opt => opt.MapFrom(src => src.Source))
.ForMember(dest => dest.Note, opt => opt.MapFrom(src => src.Message))
.ForMember(dest => dest.Regarding, opt => opt.MapFrom(src => src.InvolvedObject))
.ForMember(dest => dest.ReportingController, opt => opt.MapFrom(src => src.ReportingComponent))
.ReverseMap();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,160 @@
using AutoMapper;
using k8s.Models;
namespace k8s.Versioning
{
public static partial class VersionConverter
{
private static void AutoConfigurations(IMapperConfigurationExpression cfg)
{
cfg.CreateMap<Admissionregistrationv1beta1ServiceReference, Admissionregistrationv1ServiceReference>().ReverseMap();
cfg.CreateMap<Admissionregistrationv1beta1WebhookClientConfig, Admissionregistrationv1WebhookClientConfig>().ReverseMap();
cfg.CreateMap<V1AggregationRule, V1alpha1AggregationRule>().ReverseMap();
cfg.CreateMap<V1AggregationRule, V1beta1AggregationRule>().ReverseMap();
cfg.CreateMap<V1alpha1AggregationRule, V1beta1AggregationRule>().ReverseMap();
cfg.CreateMap<Apiextensionsv1beta1ServiceReference, Apiextensionsv1ServiceReference>().ReverseMap();
cfg.CreateMap<Apiextensionsv1beta1WebhookClientConfig, Apiextensionsv1WebhookClientConfig>().ReverseMap();
cfg.CreateMap<Apiregistrationv1beta1ServiceReference, Apiregistrationv1ServiceReference>().ReverseMap();
cfg.CreateMap<V1APIService, V1beta1APIService>().ReverseMap();
cfg.CreateMap<V1APIServiceCondition, V1beta1APIServiceCondition>().ReverseMap();
cfg.CreateMap<V1APIServiceList, V1beta1APIServiceList>().ReverseMap();
cfg.CreateMap<V1APIServiceSpec, V1beta1APIServiceSpec>().ReverseMap();
cfg.CreateMap<V1APIServiceStatus, V1beta1APIServiceStatus>().ReverseMap();
cfg.CreateMap<V1alpha1ClusterRole, V1beta1ClusterRole>().ReverseMap();
cfg.CreateMap<V1alpha1ClusterRole, V1ClusterRole>().ReverseMap();
cfg.CreateMap<V1beta1ClusterRole, V1ClusterRole>().ReverseMap();
cfg.CreateMap<V1alpha1ClusterRoleBinding, V1beta1ClusterRoleBinding>().ReverseMap();
cfg.CreateMap<V1alpha1ClusterRoleBinding, V1ClusterRoleBinding>().ReverseMap();
cfg.CreateMap<V1beta1ClusterRoleBinding, V1ClusterRoleBinding>().ReverseMap();
cfg.CreateMap<V1alpha1ClusterRoleBindingList, V1beta1ClusterRoleBindingList>().ReverseMap();
cfg.CreateMap<V1alpha1ClusterRoleBindingList, V1ClusterRoleBindingList>().ReverseMap();
cfg.CreateMap<V1beta1ClusterRoleBindingList, V1ClusterRoleBindingList>().ReverseMap();
cfg.CreateMap<V1alpha1ClusterRoleList, V1beta1ClusterRoleList>().ReverseMap();
cfg.CreateMap<V1alpha1ClusterRoleList, V1ClusterRoleList>().ReverseMap();
cfg.CreateMap<V1beta1ClusterRoleList, V1ClusterRoleList>().ReverseMap();
cfg.CreateMap<V1beta1CronJob, V2alpha1CronJob>().ReverseMap();
cfg.CreateMap<V1beta1CronJobList, V2alpha1CronJobList>().ReverseMap();
cfg.CreateMap<V1beta1CronJobSpec, V2alpha1CronJobSpec>().ReverseMap();
cfg.CreateMap<V1beta1CronJobStatus, V2alpha1CronJobStatus>().ReverseMap();
cfg.CreateMap<V1CrossVersionObjectReference, V2beta1CrossVersionObjectReference>().ReverseMap();
cfg.CreateMap<V1CrossVersionObjectReference, V2beta2CrossVersionObjectReference>().ReverseMap();
cfg.CreateMap<V2beta1CrossVersionObjectReference, V2beta2CrossVersionObjectReference>().ReverseMap();
cfg.CreateMap<V1beta1CSIDriver, V1CSIDriver>().ReverseMap();
cfg.CreateMap<V1beta1CSIDriverList, V1CSIDriverList>().ReverseMap();
cfg.CreateMap<V1beta1CSIDriverSpec, V1CSIDriverSpec>().ReverseMap();
cfg.CreateMap<V1beta1CSINode, V1CSINode>().ReverseMap();
cfg.CreateMap<V1beta1CSINodeDriver, V1CSINodeDriver>().ReverseMap();
cfg.CreateMap<V1beta1CSINodeList, V1CSINodeList>().ReverseMap();
cfg.CreateMap<V1beta1CSINodeSpec, V1CSINodeSpec>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceColumnDefinition, V1CustomResourceColumnDefinition>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceConversion, V1CustomResourceConversion>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceDefinition, V1CustomResourceDefinition>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceDefinitionCondition, V1CustomResourceDefinitionCondition>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceDefinitionList, V1CustomResourceDefinitionList>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceDefinitionNames, V1CustomResourceDefinitionNames>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceDefinitionSpec, V1CustomResourceDefinitionSpec>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceDefinitionStatus, V1CustomResourceDefinitionStatus>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceDefinitionVersion, V1CustomResourceDefinitionVersion>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceSubresources, V1CustomResourceSubresources>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceSubresourceScale, V1CustomResourceSubresourceScale>().ReverseMap();
cfg.CreateMap<V1beta1CustomResourceValidation, V1CustomResourceValidation>().ReverseMap();
cfg.CreateMap<V1beta1EndpointPort, V1EndpointPort>().ReverseMap();
cfg.CreateMap<V1beta1Event, V1Event>().ReverseMap();
cfg.CreateMap<V1beta1EventList, V1EventList>().ReverseMap();
cfg.CreateMap<V1beta1EventSeries, V1EventSeries>().ReverseMap();
cfg.CreateMap<V1beta1ExternalDocumentation, V1ExternalDocumentation>().ReverseMap();
cfg.CreateMap<V1HorizontalPodAutoscaler, V2beta1HorizontalPodAutoscaler>().ReverseMap();
cfg.CreateMap<V1HorizontalPodAutoscaler, V2beta2HorizontalPodAutoscaler>().ReverseMap();
cfg.CreateMap<V2beta1HorizontalPodAutoscaler, V2beta2HorizontalPodAutoscaler>().ReverseMap();
cfg.CreateMap<V2beta1HorizontalPodAutoscalerCondition, V2beta2HorizontalPodAutoscalerCondition>().ReverseMap();
cfg.CreateMap<V1HorizontalPodAutoscalerList, V2beta1HorizontalPodAutoscalerList>().ReverseMap();
cfg.CreateMap<V1HorizontalPodAutoscalerList, V2beta2HorizontalPodAutoscalerList>().ReverseMap();
cfg.CreateMap<V2beta1HorizontalPodAutoscalerList, V2beta2HorizontalPodAutoscalerList>().ReverseMap();
cfg.CreateMap<V2beta1HorizontalPodAutoscalerStatus, V2beta2HorizontalPodAutoscalerStatus>().ReverseMap();
cfg.CreateMap<V1beta1JobTemplateSpec, V2alpha1JobTemplateSpec>().ReverseMap();
cfg.CreateMap<V1beta1JSONSchemaProps, V1JSONSchemaProps>().ReverseMap();
cfg.CreateMap<V1beta1Lease, V1Lease>().ReverseMap();
cfg.CreateMap<V1beta1LeaseList, V1LeaseList>().ReverseMap();
cfg.CreateMap<V1beta1LeaseSpec, V1LeaseSpec>().ReverseMap();
cfg.CreateMap<V1beta1LocalSubjectAccessReview, V1LocalSubjectAccessReview>().ReverseMap();
cfg.CreateMap<V2beta1MetricSpec, V2beta2MetricSpec>().ReverseMap();
cfg.CreateMap<V2beta1MetricStatus, V2beta2MetricStatus>().ReverseMap();
cfg.CreateMap<V1beta1MutatingWebhook, V1MutatingWebhook>().ReverseMap();
cfg.CreateMap<V1beta1MutatingWebhookConfiguration, V1MutatingWebhookConfiguration>().ReverseMap();
cfg.CreateMap<V1beta1MutatingWebhookConfigurationList, V1MutatingWebhookConfigurationList>().ReverseMap();
cfg.CreateMap<V1beta1NonResourceAttributes, V1NonResourceAttributes>().ReverseMap();
cfg.CreateMap<V1beta1NonResourceRule, V1NonResourceRule>().ReverseMap();
cfg.CreateMap<V1alpha1Overhead, V1beta1Overhead>().ReverseMap();
cfg.CreateMap<V1alpha1PolicyRule, V1beta1PolicyRule>().ReverseMap();
cfg.CreateMap<V1alpha1PolicyRule, V1PolicyRule>().ReverseMap();
cfg.CreateMap<V1beta1PolicyRule, V1PolicyRule>().ReverseMap();
cfg.CreateMap<V1alpha1PriorityClass, V1beta1PriorityClass>().ReverseMap();
cfg.CreateMap<V1alpha1PriorityClass, V1PriorityClass>().ReverseMap();
cfg.CreateMap<V1beta1PriorityClass, V1PriorityClass>().ReverseMap();
cfg.CreateMap<V1alpha1PriorityClassList, V1beta1PriorityClassList>().ReverseMap();
cfg.CreateMap<V1alpha1PriorityClassList, V1PriorityClassList>().ReverseMap();
cfg.CreateMap<V1beta1PriorityClassList, V1PriorityClassList>().ReverseMap();
cfg.CreateMap<V1beta1ResourceAttributes, V1ResourceAttributes>().ReverseMap();
cfg.CreateMap<V1beta1ResourceRule, V1ResourceRule>().ReverseMap();
cfg.CreateMap<V1alpha1Role, V1beta1Role>().ReverseMap();
cfg.CreateMap<V1alpha1Role, V1Role>().ReverseMap();
cfg.CreateMap<V1beta1Role, V1Role>().ReverseMap();
cfg.CreateMap<V1alpha1RoleBinding, V1beta1RoleBinding>().ReverseMap();
cfg.CreateMap<V1alpha1RoleBinding, V1RoleBinding>().ReverseMap();
cfg.CreateMap<V1beta1RoleBinding, V1RoleBinding>().ReverseMap();
cfg.CreateMap<V1alpha1RoleBindingList, V1beta1RoleBindingList>().ReverseMap();
cfg.CreateMap<V1alpha1RoleBindingList, V1RoleBindingList>().ReverseMap();
cfg.CreateMap<V1beta1RoleBindingList, V1RoleBindingList>().ReverseMap();
cfg.CreateMap<V1alpha1RoleList, V1beta1RoleList>().ReverseMap();
cfg.CreateMap<V1alpha1RoleList, V1RoleList>().ReverseMap();
cfg.CreateMap<V1beta1RoleList, V1RoleList>().ReverseMap();
cfg.CreateMap<V1alpha1RoleRef, V1beta1RoleRef>().ReverseMap();
cfg.CreateMap<V1alpha1RoleRef, V1RoleRef>().ReverseMap();
cfg.CreateMap<V1beta1RoleRef, V1RoleRef>().ReverseMap();
cfg.CreateMap<V1beta1RuleWithOperations, V1RuleWithOperations>().ReverseMap();
cfg.CreateMap<V1alpha1RuntimeClassList, V1beta1RuntimeClassList>().ReverseMap();
cfg.CreateMap<V1alpha1Scheduling, V1beta1Scheduling>().ReverseMap();
cfg.CreateMap<V1beta1SelfSubjectAccessReview, V1SelfSubjectAccessReview>().ReverseMap();
cfg.CreateMap<V1beta1SelfSubjectAccessReviewSpec, V1SelfSubjectAccessReviewSpec>().ReverseMap();
cfg.CreateMap<V1beta1SelfSubjectRulesReview, V1SelfSubjectRulesReview>().ReverseMap();
cfg.CreateMap<V1beta1SelfSubjectRulesReviewSpec, V1SelfSubjectRulesReviewSpec>().ReverseMap();
cfg.CreateMap<V1beta1StorageClass, V1StorageClass>().ReverseMap();
cfg.CreateMap<V1beta1StorageClassList, V1StorageClassList>().ReverseMap();
cfg.CreateMap<V1beta1Subject, V1Subject>().ReverseMap();
cfg.CreateMap<V1beta1SubjectAccessReview, V1SubjectAccessReview>().ReverseMap();
cfg.CreateMap<V1beta1SubjectAccessReviewSpec, V1SubjectAccessReviewSpec>().ReverseMap();
cfg.CreateMap<V1beta1SubjectAccessReviewStatus, V1SubjectAccessReviewStatus>().ReverseMap();
cfg.CreateMap<V1beta1SubjectRulesReviewStatus, V1SubjectRulesReviewStatus>().ReverseMap();
cfg.CreateMap<V1beta1TokenReview, V1TokenReview>().ReverseMap();
cfg.CreateMap<V1beta1TokenReviewSpec, V1TokenReviewSpec>().ReverseMap();
cfg.CreateMap<V1beta1TokenReviewStatus, V1TokenReviewStatus>().ReverseMap();
cfg.CreateMap<V1beta1UserInfo, V1UserInfo>().ReverseMap();
cfg.CreateMap<V1beta1ValidatingWebhook, V1ValidatingWebhook>().ReverseMap();
cfg.CreateMap<V1beta1ValidatingWebhookConfiguration, V1ValidatingWebhookConfiguration>().ReverseMap();
cfg.CreateMap<V1beta1ValidatingWebhookConfigurationList, V1ValidatingWebhookConfigurationList>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeAttachment, V1beta1VolumeAttachment>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeAttachment, V1VolumeAttachment>().ReverseMap();
cfg.CreateMap<V1beta1VolumeAttachment, V1VolumeAttachment>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeAttachmentList, V1beta1VolumeAttachmentList>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeAttachmentList, V1VolumeAttachmentList>().ReverseMap();
cfg.CreateMap<V1beta1VolumeAttachmentList, V1VolumeAttachmentList>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeAttachmentSource, V1beta1VolumeAttachmentSource>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeAttachmentSource, V1VolumeAttachmentSource>().ReverseMap();
cfg.CreateMap<V1beta1VolumeAttachmentSource, V1VolumeAttachmentSource>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeAttachmentSpec, V1beta1VolumeAttachmentSpec>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeAttachmentSpec, V1VolumeAttachmentSpec>().ReverseMap();
cfg.CreateMap<V1beta1VolumeAttachmentSpec, V1VolumeAttachmentSpec>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeAttachmentStatus, V1beta1VolumeAttachmentStatus>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeAttachmentStatus, V1VolumeAttachmentStatus>().ReverseMap();
cfg.CreateMap<V1beta1VolumeAttachmentStatus, V1VolumeAttachmentStatus>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeError, V1beta1VolumeError>().ReverseMap();
cfg.CreateMap<V1alpha1VolumeError, V1VolumeError>().ReverseMap();
cfg.CreateMap<V1beta1VolumeError, V1VolumeError>().ReverseMap();
cfg.CreateMap<V1beta1VolumeNodeResources, V1VolumeNodeResources>().ReverseMap();
}
}
}

View File

@@ -0,0 +1,55 @@
using System.Collections.Generic;
using k8s.Models;
using Xunit;
using FluentAssertions;
using k8s.Versioning;
using AutoMapper;
namespace k8s.Tests
{
public class VersionConverterTests
{
[Fact]
public void CanExplicitlyConvert()
{
var a = new V1APIService { Spec = new V1APIServiceSpec { Group = "blah" } };
var b = (V1beta1APIService)a;
b.Spec.Group.Should().Be("blah");
}
[Fact]
public void ConfigurationsAreValid()
{
var config = new MapperConfiguration(VersionConverter.GetConfigurations);
config.AssertConfigurationIsValid();
}
[Theory]
[InlineData("v1", "v1beta1", 1)]
[InlineData("v1beta1", "v1", -1)]
[InlineData("v1beta1", "v1alpha1", 1)]
[InlineData("v1alpha1", "v1beta1", -1)]
[InlineData("v1", "v1alpha1", 1)]
[InlineData("v2alpha1", "v1", 1)]
[InlineData("v1", "v2alpha1", -1)]
[InlineData("v1", "v1", 0)]
[InlineData("v2", "v2", 0)]
[InlineData("v1beta1", "v1beta1", 0)]
[InlineData("v1beta2", "v1beta2", 0)]
[InlineData("v2beta2", "v2beta2", 0)]
public void KubernetesVersionCompare(string x, string y, int expected)
{
KubernetesVersionComparer.Instance.Compare(x, y).Should().Be(expected);
}
[Fact]
public void ConvertToVersion()
{
var src = new V1beta1APIService().Initialize();
src.ApiVersion.Should().Be("apiregistration.k8s.io/v1beta1");
var sut = (V1APIService)VersionConverter.ConvertToVersion(src, "v1");
sut.Should().NotBeNull();
sut.ApiVersion.Should().Be("apiregistration.k8s.io/v1");
}
}
}