forked from kubernetes-client/csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKubernetesYaml.cs
315 lines (277 loc) · 12.3 KB
/
KubernetesYaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System.Reflection;
using System.Text;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace k8s
{
/// <summary>
/// This is a utility class that helps you load objects from YAML files.
/// </summary>
public static class KubernetesYaml
{
private static readonly object DeserializerLockObject = new object();
private static readonly object SerializerLockObject = new object();
private static DeserializerBuilder CommonDeserializerBuilder =>
new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.WithTypeConverter(new IntOrStringYamlConverter())
.WithTypeConverter(new ByteArrayStringYamlConverter())
.WithTypeConverter(new ResourceQuantityYamlConverter())
.WithAttemptingUnquotedStringTypeDeserialization()
.WithOverridesFromJsonPropertyAttributes();
private static readonly IDeserializer StrictDeserializer =
CommonDeserializerBuilder
.WithDuplicateKeyChecking()
.Build();
private static readonly IDeserializer Deserializer =
CommonDeserializerBuilder
.IgnoreUnmatchedProperties()
.Build();
private static IDeserializer GetDeserializer(bool strict) => strict ? StrictDeserializer : Deserializer;
private static readonly IValueSerializer Serializer =
new SerializerBuilder()
.DisableAliases()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.WithTypeConverter(new IntOrStringYamlConverter())
.WithTypeConverter(new ByteArrayStringYamlConverter())
.WithTypeConverter(new ResourceQuantityYamlConverter())
.WithEventEmitter(e => new StringQuotingEmitter(e))
.WithEventEmitter(e => new FloatEmitter(e))
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
.WithOverridesFromJsonPropertyAttributes()
.BuildValueSerializer();
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);
private class ByteArrayStringYamlConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return type == typeof(byte[]);
}
public object ReadYaml(IParser parser, Type type)
{
if (parser?.Current is Scalar scalar)
{
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)
{
var obj = (byte[])value;
emitter?.Emit(new Scalar(Encoding.UTF8.GetString(obj)));
}
}
/// <summary>
/// Load a collection of objects from a stream asynchronously
///
/// caller is responsible for closing the stream
/// </summary>
/// <param name="stream">
/// The stream to load the objects from.
/// </param>
/// <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>
/// <param name="strict">true if a strict deserializer should be used (throwing exception on unknown properties), false otherwise</param>
/// <returns>collection of objects</returns>
public static async Task<List<object>> LoadAllFromStreamAsync(Stream stream, IDictionary<string, Type> typeMap = null, bool strict = false)
{
var reader = new StreamReader(stream);
var content = await reader.ReadToEndAsync().ConfigureAwait(false);
return LoadAllFromString(content, typeMap);
}
/// <summary>
/// Load a collection of objects from a file asynchronously
/// </summary>
/// <param name="fileName">The name of the file to load from.</param>
/// <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>
/// <param name="strict">true if a strict deserializer should be used (throwing exception on unknown properties), false otherwise</param>
/// <returns>collection of objects</returns>
public static async Task<List<object>> LoadAllFromFileAsync(string fileName, IDictionary<string, Type> typeMap = null, bool strict = false)
{
using (var fileStream = File.OpenRead(fileName))
{
return await LoadAllFromStreamAsync(fileStream, typeMap).ConfigureAwait(false);
}
}
/// <summary>
/// Load a collection of objects from a string
/// </summary>
/// <param name="content">
/// The string to load the objects from.
/// </param>
/// <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>
/// <param name="strict">true if a strict deserializer should be used (throwing exception on unknown properties), false otherwise</param>
/// <returns>collection of objects</returns>
public static List<object> LoadAllFromString(string content, IDictionary<string, Type> typeMap = null, bool strict = false)
{
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);
var types = new List<Type>();
var parser = new MergingParser(new Parser(new StringReader(content)));
parser.Consume<StreamStart>();
while (parser.Accept<DocumentStart>(out _))
{
lock (DeserializerLockObject)
{
var dict = GetDeserializer(strict).Deserialize<Dictionary<object, object>>(parser);
types.Add(mergedTypeMap[dict["apiVersion"] + "/" + dict["kind"]]);
}
}
parser = new MergingParser(new Parser(new StringReader(content)));
parser.Consume<StreamStart>();
var ix = 0;
var results = new List<object>();
while (parser.Accept<DocumentStart>(out _))
{
var objType = types[ix++];
lock (DeserializerLockObject)
{
var obj = GetDeserializer(strict).Deserialize(parser, objType);
results.Add(obj);
}
}
return results;
}
public static async Task<T> LoadFromStreamAsync<T>(Stream stream, bool strict = false)
{
var reader = new StreamReader(stream);
var content = await reader.ReadToEndAsync().ConfigureAwait(false);
return Deserialize<T>(content, strict);
}
public static async Task<T> LoadFromFileAsync<T>(string file, bool strict = false)
{
using (var fs = File.OpenRead(file))
{
return await LoadFromStreamAsync<T>(fs, strict).ConfigureAwait(false);
}
}
[Obsolete("use Deserialize")]
public static T LoadFromString<T>(string content, bool strict = false)
{
return Deserialize<T>(content, strict);
}
[Obsolete("use Serialize")]
public static string SaveToString<T>(T value)
{
return Serialize(value);
}
public static TValue Deserialize<TValue>(string yaml, bool strict = false)
{
using var reader = new StringReader(yaml);
lock (DeserializerLockObject)
{
return GetDeserializer(strict).Deserialize<TValue>(new MergingParser(new Parser(reader)));
}
}
public static TValue Deserialize<TValue>(Stream yaml, bool strict = false)
{
using var reader = new StreamReader(yaml);
lock (DeserializerLockObject)
{
return GetDeserializer(strict).Deserialize<TValue>(new MergingParser(new Parser(reader)));
}
}
public static string SerializeAll(IEnumerable<object> values)
{
if (values == null)
{
return "";
}
var stringBuilder = new StringBuilder();
var writer = new StringWriter(stringBuilder);
var emitter = new Emitter(writer);
emitter.Emit(new StreamStart());
foreach (var value in values)
{
if (value != null)
{
emitter.Emit(new DocumentStart());
lock (SerializerLockObject)
{
Serializer.SerializeValue(emitter, value, value.GetType());
}
emitter.Emit(new DocumentEnd(true));
}
}
return stringBuilder.ToString();
}
public static string Serialize(object value)
{
if (value == null)
{
return "";
}
var stringBuilder = new StringBuilder();
var writer = new StringWriter(stringBuilder);
var emitter = new Emitter(writer);
emitter.Emit(new StreamStart());
emitter.Emit(new DocumentStart());
lock (SerializerLockObject)
{
Serializer.SerializeValue(emitter, value, value.GetType());
}
return stringBuilder.ToString();
}
private static TBuilder WithOverridesFromJsonPropertyAttributes<TBuilder>(this TBuilder builder)
where TBuilder : BuilderSkeleton<TBuilder>
{
// 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)
{
foreach (var property in type.GetProperties())
{
var jsonAttribute = property.GetCustomAttribute<JsonPropertyNameAttribute>();
if (jsonAttribute == null)
{
continue;
}
var yamlAttribute = new YamlMemberAttribute { Alias = jsonAttribute.Name, ApplyNamingConventions = false };
builder.WithAttributeOverride(type, property.Name, yamlAttribute);
}
}
return builder;
}
}
}