-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModelExtensions.cs
More file actions
59 lines (55 loc) · 2.74 KB
/
ModelExtensions.cs
File metadata and controls
59 lines (55 loc) · 2.74 KB
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
using System;
using k8s.Models;
namespace k8s.Fluent
{
/// <summary>Adds convenient extensions for Kubernetes objects.</summary>
public static class ModelExtensions
{
/// <summary>Adds an owner reference to the object. No attempt is made to ensure the reference is correct or fits with the
/// other references.
/// </summary>
public static void AddOwnerReference(
this IMetadata<V1ObjectMeta> obj, IKubernetesObject<V1ObjectMeta> owner, bool? controller = null, bool? blockDeletion = null) =>
obj.AddOwnerReference(CreateOwnerReference(owner, controller, blockDeletion));
/// <summary>Clones an <see cref="IKubernetesObject"/> by serializing and deserializing it.</summary>
public static T Clone<T>(this T obj) where T : IKubernetesObject
{
if(obj == null) return default(T);
var buffer = new System.Text.StringBuilder();
using(var sw = new System.IO.StringWriter(buffer)) FluentExtensions.DefaultSerializer.Serialize(sw, obj);
using(var sr = new System.IO.StringReader(buffer.ToString()))
{
return (T)FluentExtensions.DefaultSerializer.Deserialize(sr, obj.GetType());
}
}
/// <summary>Creates a <see cref="V1ObjectReference"/> that refers to the given object.</summary>
public static V1ObjectReference CreateObjectReference(this IKubernetesObject<V1ObjectMeta> obj)
{
if(obj == null) throw new ArgumentNullException(nameof(obj));
string apiVersion = obj.ApiVersion, kind = obj.Kind; // default to using the API version and kind from the object
if(string.IsNullOrEmpty(apiVersion) || string.IsNullOrEmpty(kind)) // but if either of them is missing...
{
KubernetesScheme.Default.GetVK(obj.GetType(), out apiVersion, out kind); // get it from the default scheme
}
return new V1ObjectReference()
{
ApiVersion = apiVersion, Kind = kind, Name = obj.Name(), NamespaceProperty = obj.Namespace(), Uid = obj.Uid(),
ResourceVersion = obj.ResourceVersion()
};
}
/// <summary>Creates a <see cref="V1OwnerReference"/> that refers to the given object.</summary>
public static V1OwnerReference CreateOwnerReference(this IKubernetesObject<V1ObjectMeta> obj, bool? controller = null, bool? blockDeletion = null)
{
if(obj == null) throw new ArgumentNullException(nameof(obj));
string apiVersion = obj.ApiVersion, kind = obj.Kind; // default to using the API version and kind from the object
if(string.IsNullOrEmpty(apiVersion) || string.IsNullOrEmpty(kind)) // but if either of them is missing...
{
KubernetesScheme.Default.GetVK(obj.GetType(), out apiVersion, out kind); // get it from the default scheme
}
return new V1OwnerReference()
{
ApiVersion = apiVersion, Kind = kind, Name = obj.Name(), Uid = obj.Uid(), Controller = controller, BlockOwnerDeletion = blockDeletion
};
}
}
}