Skip to content

Commit 436a5cf

Browse files
ocdibuehler
authored andcommitted
feat(Kubernetes Entities): Add some utility extensions to assist creating Kubernetes objects (#41)
1 parent d5799be commit 436a5cf

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ bld/
2727
# Testing results
2828
coverage.json
2929
coverage.info
30+
.vs
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using k8s;
2+
using k8s.Models;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace KubeOps.Operator.Entities.Extensions
9+
{
10+
public static class KubernetesObjectExtensions
11+
{
12+
public static TResource WithOwnerReference<TResource>(this TResource resource, IKubernetesObject<V1ObjectMeta> owner)
13+
where TResource : IKubernetesObject<V1ObjectMeta>
14+
{
15+
resource.Metadata.EnsureOwnerReferences().Add(owner.MakeOwnerReference());
16+
return resource;
17+
}
18+
19+
public static IList<V1OwnerReference> EnsureOwnerReferences(this V1ObjectMeta meta)
20+
{
21+
if (meta.OwnerReferences == null)
22+
{
23+
meta.OwnerReferences = new List<V1OwnerReference>();
24+
}
25+
26+
return meta.OwnerReferences;
27+
}
28+
29+
public static V1OwnerReference MakeOwnerReference(this IKubernetesObject<V1ObjectMeta> kubernetesObject)
30+
=> new V1OwnerReference(kubernetesObject.ApiVersion, kubernetesObject.Kind, kubernetesObject.Metadata.Name, kubernetesObject.Metadata.Uid);
31+
32+
public static V1ObjectReference MakeObjectReference(this IKubernetesObject<V1ObjectMeta> kubernetesObject)
33+
=> new V1ObjectReference()
34+
{
35+
ApiVersion = kubernetesObject.ApiVersion,
36+
Kind = kubernetesObject.Kind,
37+
Name = kubernetesObject.Metadata.Name,
38+
NamespaceProperty = kubernetesObject.Metadata.NamespaceProperty,
39+
ResourceVersion = kubernetesObject.Metadata.ResourceVersion,
40+
Uid = kubernetesObject.Metadata.Uid
41+
};
42+
43+
/* commented pending fleshing this out and improving after confirming event best practices
44+
/// <summary>
45+
/// Create an event for a kubernetesObject that can be applied to the cluster using the Create on <see cref="Client.IKubernetesClient" />
46+
/// </summary>
47+
/// <param name="kubernetesObject">The object to create an event for</param>
48+
/// <param name="reason">A short reason tag, like Created, Updated, Reconciled</param>
49+
/// <param name="message">A human readable message to go with the event</param>
50+
/// <param name="type">A string corresponding to the event type, Normal or Warning</param>
51+
/// <param name="component">The component generating the event, it should be the name of the operator</param>
52+
/// <returns></returns>
53+
public static V1Event Event(this IKubernetesObject<V1ObjectMeta> kubernetesObject, string reason, string message, string type = "Normal", string? component = null) =>
54+
new V1Event
55+
{
56+
Metadata = new V1ObjectMeta { Name = $"{kubernetesObject.Name()}.{DateTimeOffset.UtcNow.ToFileTime()}", NamespaceProperty = kubernetesObject.Namespace() },
57+
InvolvedObject = kubernetesObject.MakeObjectReference(),
58+
Reason = reason,
59+
Source = new V1EventSource { Component = component },
60+
Count = 1,
61+
Message = message,
62+
FirstTimestamp = DateTime.UtcNow,
63+
LastTimestamp = DateTime.UtcNow,
64+
Type = type
65+
};
66+
*/
67+
}
68+
}

0 commit comments

Comments
 (0)