2018-03-12 14:55:35 -07:00
|
|
|
using System.IO;
|
2021-03-03 06:35:20 +10:00
|
|
|
using System.Reflection;
|
2018-03-31 07:02:29 +02:00
|
|
|
using System.Text;
|
2018-03-12 14:55:35 -07:00
|
|
|
using System.Threading.Tasks;
|
2018-03-31 07:02:29 +02:00
|
|
|
using YamlDotNet.Core;
|
|
|
|
|
using YamlDotNet.Core.Events;
|
2018-03-12 14:55:35 -07:00
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
2019-09-30 00:47:38 -07:00
|
|
|
using k8s.Models;
|
2018-03-12 14:55:35 -07:00
|
|
|
|
|
|
|
|
namespace k8s
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is a utility class that helps you load objects from YAML files.
|
|
|
|
|
/// </summary>
|
2020-04-23 11:40:06 -07:00
|
|
|
public static class Yaml
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
2021-03-03 06:35:20 +10:00
|
|
|
private static readonly IDeserializer Deserializer =
|
|
|
|
|
new DeserializerBuilder()
|
|
|
|
|
.WithNamingConvention(CamelCaseNamingConvention.Instance)
|
|
|
|
|
.WithTypeConverter(new IntOrStringYamlConverter())
|
|
|
|
|
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
|
|
|
|
.WithOverridesFromJsonPropertyAttributes()
|
|
|
|
|
.IgnoreUnmatchedProperties()
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
private static readonly IValueSerializer Serializer =
|
|
|
|
|
new SerializerBuilder()
|
|
|
|
|
.DisableAliases()
|
|
|
|
|
.WithNamingConvention(CamelCaseNamingConvention.Instance)
|
|
|
|
|
.WithTypeConverter(new IntOrStringYamlConverter())
|
|
|
|
|
.WithTypeConverter(new ByteArrayStringYamlConverter())
|
|
|
|
|
.WithEventEmitter(e => new StringQuotingEmitter(e))
|
|
|
|
|
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
|
|
|
|
|
.WithOverridesFromJsonPropertyAttributes()
|
|
|
|
|
.BuildValueSerializer();
|
|
|
|
|
|
2021-04-06 07:43:19 +12:00
|
|
|
private static readonly IDictionary<string, Type> ModelTypeMap = typeof(KubernetesEntityAttribute).Assembly
|
|
|
|
|
.GetTypes()
|
|
|
|
|
.Where(t => t.GetCustomAttributes(typeof(KubernetesEntityAttribute), true).Any())
|
|
|
|
|
.ToDictionary(
|
|
|
|
|
t =>
|
|
|
|
|
{
|
|
|
|
|
var attr = (KubernetesEntityAttribute)t.GetCustomAttribute(
|
|
|
|
|
typeof(KubernetesEntityAttribute), true);
|
|
|
|
|
var groupPrefix = string.IsNullOrEmpty(attr.Group) ? "" : $"{attr.Group}/";
|
|
|
|
|
return $"{groupPrefix}{attr.ApiVersion}/{attr.Kind}";
|
|
|
|
|
},
|
|
|
|
|
t => t);
|
|
|
|
|
|
2020-09-28 02:42:48 -07:00
|
|
|
public class ByteArrayStringYamlConverter : IYamlTypeConverter
|
|
|
|
|
{
|
|
|
|
|
public bool Accepts(Type type)
|
|
|
|
|
{
|
2020-10-23 08:31:57 -07:00
|
|
|
return type == typeof(byte[]);
|
2020-09-28 02:42:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ReadYaml(IParser parser, Type type)
|
|
|
|
|
{
|
2020-11-22 14:52:09 -08:00
|
|
|
if (parser?.Current is Scalar scalar)
|
2020-09-28 02:42:48 -07:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(scalar.Value))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Encoding.UTF8.GetBytes(scalar.Value);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
parser.MoveNext();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new InvalidOperationException(parser.Current?.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteYaml(IEmitter emitter, object value, Type type)
|
|
|
|
|
{
|
2020-10-23 08:31:57 -07:00
|
|
|
var obj = (byte[])value;
|
2020-11-22 14:52:09 -08:00
|
|
|
emitter?.Emit(new Scalar(Encoding.UTF8.GetString(obj)));
|
2020-09-28 02:42:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 13:59:37 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Load a collection of objects from a stream asynchronously
|
2021-02-24 21:35:24 -08:00
|
|
|
///
|
|
|
|
|
/// caller is responsible for closing the stream
|
2020-01-10 13:59:37 -08:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="stream">
|
|
|
|
|
/// The stream to load the objects from.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="typeMap">
|
2021-04-06 07:43:19 +12:00
|
|
|
/// A map from apiVersion/kind to Type. For example "v1/Pod" -> typeof(V1Pod). If null, a default mapping will
|
|
|
|
|
/// be used.
|
2020-01-10 13:59:37 -08:00
|
|
|
/// </param>
|
2020-11-22 14:52:09 -08:00
|
|
|
/// <returns>collection of objects</returns>
|
2021-04-06 07:43:19 +12:00
|
|
|
public static async Task<List<object>> LoadAllFromStreamAsync(Stream stream, IDictionary<string, Type> typeMap = null)
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
2020-01-10 13:59:37 -08:00
|
|
|
var reader = new StreamReader(stream);
|
2020-03-19 04:54:44 +00:00
|
|
|
var content = await reader.ReadToEndAsync().ConfigureAwait(false);
|
2020-01-10 13:59:37 -08:00
|
|
|
return LoadAllFromString(content, typeMap);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 14:52:09 -08:00
|
|
|
|
2020-01-10 13:59:37 -08:00
|
|
|
/// <summary>
|
|
|
|
|
/// Load a collection of objects from a file asynchronously
|
|
|
|
|
/// </summary>
|
2020-11-22 14:52:09 -08:00
|
|
|
/// <param name="fileName">The name of the file to load from.</param>
|
2021-04-06 07:43:19 +12:00
|
|
|
/// <param name="typeMap">
|
|
|
|
|
/// A map from apiVersion/kind to Type. For example "v1/Pod" -> typeof(V1Pod). If null, a default mapping will
|
|
|
|
|
/// be used.
|
|
|
|
|
/// </param>
|
2020-11-22 14:52:09 -08:00
|
|
|
/// <returns>collection of objects</returns>
|
2021-04-06 07:43:19 +12:00
|
|
|
public static async Task<List<object>> LoadAllFromFileAsync(string fileName, IDictionary<string, Type> typeMap = null)
|
2020-01-10 13:59:37 -08:00
|
|
|
{
|
2021-02-27 17:21:43 +10:00
|
|
|
using (var fileStream = File.OpenRead(fileName))
|
2021-02-24 21:35:24 -08:00
|
|
|
{
|
2021-02-27 17:21:43 +10:00
|
|
|
return await LoadAllFromStreamAsync(fileStream, typeMap).ConfigureAwait(false);
|
2021-02-24 21:35:24 -08:00
|
|
|
}
|
2020-01-10 13:59:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Load a collection of objects from a string
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="content">
|
|
|
|
|
/// The string to load the objects from.
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="typeMap">
|
2021-04-06 07:43:19 +12:00
|
|
|
/// A map from apiVersion/kind to Type. For example "v1/Pod" -> typeof(V1Pod). If null, a default mapping will
|
|
|
|
|
/// be used.
|
2020-01-10 13:59:37 -08:00
|
|
|
/// </param>
|
2020-11-22 14:52:09 -08:00
|
|
|
/// <returns>collection of objects</returns>
|
2021-04-06 07:43:19 +12:00
|
|
|
public static List<object> LoadAllFromString(string content, IDictionary<string, Type> typeMap = null)
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
2021-04-06 07:43:19 +12:00
|
|
|
var mergedTypeMap = new Dictionary<string, Type>(ModelTypeMap);
|
|
|
|
|
// merge in KVPs from typeMap, overriding any in ModelTypeMap
|
|
|
|
|
typeMap?.ToList().ForEach(x => mergedTypeMap[x.Key] = x.Value);
|
2020-11-22 14:52:09 -08:00
|
|
|
|
2020-01-10 13:59:37 -08:00
|
|
|
var types = new List<Type>();
|
|
|
|
|
var parser = new Parser(new StringReader(content));
|
2020-11-22 14:52:09 -08:00
|
|
|
parser.Consume<StreamStart>();
|
|
|
|
|
while (parser.Accept<DocumentStart>(out _))
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
2021-03-03 06:35:20 +10:00
|
|
|
var obj = Deserializer.Deserialize<KubernetesObject>(parser);
|
2021-04-06 07:43:19 +12:00
|
|
|
types.Add(mergedTypeMap[obj.ApiVersion + "/" + obj.Kind]);
|
2020-01-10 13:59:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser = new Parser(new StringReader(content));
|
2020-11-22 14:52:09 -08:00
|
|
|
parser.Consume<StreamStart>();
|
2020-01-10 13:59:37 -08:00
|
|
|
var ix = 0;
|
|
|
|
|
var results = new List<object>();
|
2020-11-22 14:52:09 -08:00
|
|
|
while (parser.Accept<DocumentStart>(out _))
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
2020-01-10 13:59:37 -08:00
|
|
|
var objType = types[ix++];
|
2021-03-03 06:35:20 +10:00
|
|
|
var obj = Deserializer.Deserialize(parser, objType);
|
2020-01-10 13:59:37 -08:00
|
|
|
results.Add(obj);
|
|
|
|
|
}
|
2020-04-23 11:40:06 -07:00
|
|
|
|
2020-01-10 13:59:37 -08:00
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 12:15:45 -07:00
|
|
|
public static async Task<T> LoadFromStreamAsync<T>(Stream stream)
|
|
|
|
|
{
|
2018-03-12 14:55:35 -07:00
|
|
|
var reader = new StreamReader(stream);
|
2020-03-19 04:54:44 +00:00
|
|
|
var content = await reader.ReadToEndAsync().ConfigureAwait(false);
|
2018-03-12 14:55:35 -07:00
|
|
|
return LoadFromString<T>(content);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 12:15:45 -07:00
|
|
|
public static async Task<T> LoadFromFileAsync<T>(string file)
|
|
|
|
|
{
|
2020-11-22 14:52:09 -08:00
|
|
|
using (var fs = File.OpenRead(file))
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
2020-03-19 04:54:44 +00:00
|
|
|
return await LoadFromStreamAsync<T>(fs).ConfigureAwait(false);
|
2018-03-12 14:55:35 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 12:15:45 -07:00
|
|
|
public static T LoadFromString<T>(string content)
|
|
|
|
|
{
|
2021-03-03 06:35:20 +10:00
|
|
|
var obj = Deserializer.Deserialize<T>(content);
|
2018-03-12 14:55:35 -07:00
|
|
|
return obj;
|
|
|
|
|
}
|
2018-03-31 07:02:29 +02:00
|
|
|
|
|
|
|
|
public static string SaveToString<T>(T value)
|
|
|
|
|
{
|
|
|
|
|
var stringBuilder = new StringBuilder();
|
|
|
|
|
var writer = new StringWriter(stringBuilder);
|
|
|
|
|
var emitter = new Emitter(writer);
|
|
|
|
|
|
|
|
|
|
emitter.Emit(new StreamStart());
|
|
|
|
|
emitter.Emit(new DocumentStart());
|
2021-03-03 06:35:20 +10:00
|
|
|
Serializer.SerializeValue(emitter, value, typeof(T));
|
2018-03-31 07:02:29 +02:00
|
|
|
|
|
|
|
|
return stringBuilder.ToString();
|
|
|
|
|
}
|
2019-02-12 18:07:00 +13:00
|
|
|
|
2021-03-03 06:35:20 +10:00
|
|
|
private static TBuilder WithOverridesFromJsonPropertyAttributes<TBuilder>(this TBuilder builder)
|
|
|
|
|
where TBuilder : BuilderSkeleton<TBuilder>
|
2019-02-12 18:07:00 +13:00
|
|
|
{
|
2021-03-03 06:35:20 +10:00
|
|
|
// Use VersionInfo from the model namespace as that should be stable.
|
|
|
|
|
// If this is not generated in the future we will get an obvious compiler error.
|
|
|
|
|
var targetNamespace = typeof(VersionInfo).Namespace;
|
|
|
|
|
|
|
|
|
|
// Get all the concrete model types from the code generated namespace.
|
|
|
|
|
var types = typeof(KubernetesEntityAttribute).Assembly
|
|
|
|
|
.ExportedTypes
|
|
|
|
|
.Where(type => type.Namespace == targetNamespace &&
|
|
|
|
|
!type.IsInterface &&
|
|
|
|
|
!type.IsAbstract);
|
|
|
|
|
|
|
|
|
|
// Map any JsonPropertyAttribute instances to YamlMemberAttribute instances.
|
|
|
|
|
foreach (var type in types)
|
2019-02-12 18:07:00 +13:00
|
|
|
{
|
2021-03-03 06:35:20 +10:00
|
|
|
foreach (var property in type.GetProperties())
|
2019-02-12 18:07:00 +13:00
|
|
|
{
|
2021-12-13 07:31:59 -08:00
|
|
|
var jsonAttribute = property.GetCustomAttribute<JsonPropertyNameAttribute>();
|
2021-03-03 06:35:20 +10:00
|
|
|
if (jsonAttribute == null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-02-12 18:07:00 +13:00
|
|
|
|
2021-12-13 07:31:59 -08:00
|
|
|
var yamlAttribute = new YamlMemberAttribute { Alias = jsonAttribute.Name, ApplyNamingConventions = false };
|
2021-03-03 06:35:20 +10:00
|
|
|
builder.WithAttributeOverride(type, property.Name, yamlAttribute);
|
2019-02-12 18:07:00 +13:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-03 06:35:20 +10:00
|
|
|
return builder;
|
2019-02-12 18:07:00 +13:00
|
|
|
}
|
2018-03-12 14:55:35 -07:00
|
|
|
}
|
|
|
|
|
}
|