|
| 1 | +// WARNING: DO NOT LEAVE COMMENTED CODE IN THIS FILE. IT GETS SCANNED BY GEN PROJECT SO IT CAN EXCLUDE ANY MANUALLY DEFINED MAPS |
| 2 | + |
| 3 | +using AutoMapper; |
| 4 | +#if NET6_0_OR_GREATER |
| 5 | +using AutoMapper.Internal; |
| 6 | +#endif |
| 7 | +using k8s.Models; |
| 8 | +using System; |
| 9 | +using System.Collections.Generic; |
| 10 | +using System.Linq; |
| 11 | +using System.Reflection; |
| 12 | + |
| 13 | +namespace k8s.ModelConverter.AutoMapper; |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// Provides mappers that converts Kubernetes models between different versions |
| 17 | +/// </summary> |
| 18 | +internal static partial class VersionConverter |
| 19 | +{ |
| 20 | + static VersionConverter() |
| 21 | + { |
| 22 | + UpdateMappingConfiguration(expression => { }); |
| 23 | + } |
| 24 | + |
| 25 | + public static IMapper Mapper { get; private set; } |
| 26 | + internal static MapperConfiguration MapperConfiguration { get; private set; } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Two level lookup of model types by Kind and then Version |
| 30 | + /// </summary> |
| 31 | + internal static Dictionary<string, Dictionary<string, Type>> KindVersionsMap { get; private set; } |
| 32 | + |
| 33 | + public static Type GetTypeForVersion<T>(string version) |
| 34 | + { |
| 35 | + return GetTypeForVersion(typeof(T), version); |
| 36 | + } |
| 37 | + |
| 38 | + public static Type GetTypeForVersion(Type type, string version) |
| 39 | + { |
| 40 | + return KindVersionsMap[type.GetKubernetesTypeMetadata().Kind][version]; |
| 41 | + } |
| 42 | + |
| 43 | + public static void UpdateMappingConfiguration(Action<IMapperConfigurationExpression> configuration) |
| 44 | + { |
| 45 | + MapperConfiguration = new MapperConfiguration(cfg => |
| 46 | + { |
| 47 | + GetConfigurations(cfg); |
| 48 | + configuration(cfg); |
| 49 | + }); |
| 50 | + Mapper = MapperConfiguration |
| 51 | +#if NET6_0_OR_GREATER |
| 52 | + .Internal() |
| 53 | +#endif |
| 54 | + .CreateMapper(); |
| 55 | + KindVersionsMap = MapperConfiguration |
| 56 | +#if NET6_0_OR_GREATER |
| 57 | + .Internal() |
| 58 | +#endif |
| 59 | + .GetAllTypeMaps() |
| 60 | + .SelectMany(x => new[] { x.Types.SourceType, x.Types.DestinationType }) |
| 61 | + .Where(x => x.GetCustomAttribute<KubernetesEntityAttribute>() != null) |
| 62 | + .Select(x => |
| 63 | + { |
| 64 | + var attr = GetKubernetesEntityAttribute(x); |
| 65 | + return new { attr.Kind, attr.ApiVersion, Type = x }; |
| 66 | + }) |
| 67 | + .GroupBy(x => x.Kind) |
| 68 | + .ToDictionary(x => x.Key, kindGroup => kindGroup |
| 69 | + .GroupBy(x => x.ApiVersion) |
| 70 | + .ToDictionary( |
| 71 | + x => x.Key, |
| 72 | + versionGroup => versionGroup.Select(x => x.Type).Distinct().Single())); // should only be one type for each Kind/Version combination |
| 73 | + } |
| 74 | + |
| 75 | + public static object ConvertToVersion(object source, string apiVersion) |
| 76 | + { |
| 77 | + if (source == null) |
| 78 | + { |
| 79 | + throw new ArgumentNullException(nameof(source)); |
| 80 | + } |
| 81 | + |
| 82 | + var type = source.GetType(); |
| 83 | + var attr = GetKubernetesEntityAttribute(type); |
| 84 | + if (attr.ApiVersion == apiVersion) |
| 85 | + { |
| 86 | + return source; |
| 87 | + } |
| 88 | + |
| 89 | + if (!KindVersionsMap.TryGetValue(attr.Kind, out var kindVersions)) |
| 90 | + { |
| 91 | + throw new InvalidOperationException($"Version converter does not have any registered types for Kind `{attr.Kind}`"); |
| 92 | + } |
| 93 | + |
| 94 | + if (!kindVersions.TryGetValue(apiVersion, out var targetType) || !kindVersions.TryGetValue(attr.ApiVersion, out var sourceType) || |
| 95 | + MapperConfiguration |
| 96 | +#if NET6_0_OR_GREATER |
| 97 | + .Internal() |
| 98 | +#endif |
| 99 | + .FindTypeMapFor(sourceType, targetType) == null) |
| 100 | + { |
| 101 | + throw new InvalidOperationException($"There is no conversion mapping registered for Kind `{attr.Kind}` from ApiVersion {attr.ApiVersion} to {apiVersion}"); |
| 102 | + } |
| 103 | + |
| 104 | + return Mapper.Map(source, sourceType, targetType); |
| 105 | + } |
| 106 | + |
| 107 | + private static KubernetesEntityAttribute GetKubernetesEntityAttribute(Type type) |
| 108 | + { |
| 109 | + if (type == null) |
| 110 | + { |
| 111 | + throw new ArgumentNullException(nameof(type)); |
| 112 | + } |
| 113 | + |
| 114 | + var attr = type.GetCustomAttribute<KubernetesEntityAttribute>(); |
| 115 | + if (attr == null) |
| 116 | + { |
| 117 | + throw new InvalidOperationException($"Type {type} does not have {nameof(KubernetesEntityAttribute)}"); |
| 118 | + } |
| 119 | + |
| 120 | + return attr; |
| 121 | + } |
| 122 | + |
| 123 | + internal static void GetConfigurations(IMapperConfigurationExpression cfg) |
| 124 | + { |
| 125 | + AutoConfigurations(cfg); |
| 126 | + ManualConfigurations(cfg); |
| 127 | + } |
| 128 | + |
| 129 | + private static void ManualConfigurations(IMapperConfigurationExpression cfg) |
| 130 | + { |
| 131 | + cfg.AllowNullCollections = true; |
| 132 | + cfg.DisableConstructorMapping(); |
| 133 | + cfg |
| 134 | +#if NET6_0_OR_GREATER |
| 135 | + .Internal() |
| 136 | +#endif |
| 137 | + .ForAllMaps((typeMap, opt) => |
| 138 | + { |
| 139 | + if (!typeof(IKubernetesObject).IsAssignableFrom(typeMap.Types.DestinationType)) |
| 140 | + { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + var metadata = typeMap.Types.DestinationType.GetKubernetesTypeMetadata(); |
| 145 | + opt.ForMember(nameof(IKubernetesObject.ApiVersion), x => x.Ignore()); |
| 146 | + opt.ForMember(nameof(IKubernetesObject.Kind), x => x.Ignore()); |
| 147 | + opt.AfterMap((from, to) => |
| 148 | + { |
| 149 | + var obj = (IKubernetesObject)to; |
| 150 | + obj.ApiVersion = !string.IsNullOrEmpty(metadata.Group) ? $"{metadata.Group}/{metadata.ApiVersion}" : metadata.ApiVersion; |
| 151 | + obj.Kind = metadata.Kind; |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + cfg.CreateMap<V1Subject, V1beta2Subject>() |
| 156 | + .ForMember(dest => dest.Group, opt => opt.Ignore()) |
| 157 | + .ForMember(dest => dest.ServiceAccount, opt => opt.Ignore()) |
| 158 | + .ForMember(dest => dest.User, opt => opt.Ignore()) |
| 159 | + .ReverseMap(); |
| 160 | + |
| 161 | + cfg.CreateMap<V1Subject, V1beta3Subject>() |
| 162 | + .ForMember(dest => dest.Group, opt => opt.Ignore()) |
| 163 | + .ForMember(dest => dest.ServiceAccount, opt => opt.Ignore()) |
| 164 | + .ForMember(dest => dest.User, opt => opt.Ignore()) |
| 165 | + .ReverseMap(); |
| 166 | + |
| 167 | + cfg.CreateMap<V1HorizontalPodAutoscalerSpec, V2HorizontalPodAutoscalerSpec>() |
| 168 | + .ForMember(dest => dest.Metrics, opt => opt.Ignore()) |
| 169 | + .ForMember(dest => dest.Behavior, opt => opt.Ignore()) |
| 170 | + .ReverseMap(); |
| 171 | + |
| 172 | + |
| 173 | + cfg.CreateMap<V1HorizontalPodAutoscalerStatus, V2HorizontalPodAutoscalerStatus>() |
| 174 | + .ForMember(dest => dest.Conditions, opt => opt.Ignore()) |
| 175 | + .ForMember(dest => dest.CurrentMetrics, opt => opt.Ignore()) |
| 176 | + .ReverseMap(); |
| 177 | + |
| 178 | + cfg.CreateMap<V1alpha1ResourceClaim, V1ResourceClaim>() |
| 179 | + .ForMember(dest => dest.Name, opt => opt.Ignore()) |
| 180 | + .ReverseMap(); |
| 181 | + |
| 182 | + cfg.CreateMap<V1beta2LimitedPriorityLevelConfiguration, V1beta3LimitedPriorityLevelConfiguration>() |
| 183 | + .ForMember(dest => dest.NominalConcurrencyShares, opt => opt.Ignore()) |
| 184 | + .ReverseMap(); |
| 185 | + } |
| 186 | +} |
0 commit comments