-
Notifications
You must be signed in to change notification settings - Fork 303
Support for conversions between models of different versions #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
k8s-ci-robot
merged 1 commit into
kubernetes-client:master
from
macsux:feature/version-converters
May 20, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
{{/.}} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
{{/.}} | ||
} | ||
} | ||
|
||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-]+$"); | ||
|
||
|
||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/KubernetesClient/Versioning/KubernetesVersionComparitor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
System.Version
?you may want to reuse it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there's an open PR for creating and installed CRDs from me. I've ran into this problem when installing into my test cluster which is actually using an older version of CRDs. I'm refactoring that code to be based on this one, and automatically do conversion based on server version. I'm partly blocked on that one until we figure out what to do with #417