Skip to content

Commit ae96986

Browse files
author
Christoph Bühler
committed
refactor: replace extensions and separate them into classes
1 parent 9d204d4 commit ae96986

File tree

12 files changed

+84
-61
lines changed

12 files changed

+84
-61
lines changed

src/KubeOps/Operator/Caching/ResourceCache.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using k8s.Models;
55
using KellermanSoftware.CompareNetObjects;
66
using KubeOps.Operator.Entities;
7+
using KubeOps.Operator.Entities.Extensions;
78

89
namespace KubeOps.Operator.Caching
910
{

src/KubeOps/Operator/Client/KubernetesClient.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using k8s.Models;
99
using KubeOps.Operator.Client.LabelSelectors;
1010
using KubeOps.Operator.Entities;
11+
using KubeOps.Operator.Entities.Extensions;
1112
using Microsoft.Rest;
1213
using Newtonsoft.Json.Linq;
1314

@@ -27,7 +28,7 @@ public KubernetesClient(IKubernetes apiClient)
2728
string? @namespace = null)
2829
where TResource : class, IKubernetesObject<V1ObjectMeta>
2930
{
30-
var crd = EntityExtensions.CreateResourceDefinition<TResource>();
31+
var crd = CustomEntityDefinitionExtensions.CreateResourceDefinition<TResource>();
3132
try
3233
{
3334
var result = await (string.IsNullOrWhiteSpace(@namespace)
@@ -51,7 +52,7 @@ public async Task<IList<TResource>> List<TResource>(
5152
string? labelSelector = null)
5253
where TResource : IKubernetesObject<V1ObjectMeta>
5354
{
54-
var crd = EntityExtensions.CreateResourceDefinition<TResource>();
55+
var crd = CustomEntityDefinitionExtensions.CreateResourceDefinition<TResource>();
5556
var result = await (string.IsNullOrWhiteSpace(@namespace)
5657
? ApiClient.ListClusterCustomObjectAsync(
5758
crd.Group,
@@ -194,7 +195,7 @@ public Task Delete<TResource>(params TResource[] resources)
194195
public async Task Delete<TResource>(string name, string? @namespace = null)
195196
where TResource : IKubernetesObject<V1ObjectMeta>
196197
{
197-
var crd = EntityExtensions.CreateResourceDefinition<TResource>();
198+
var crd = CustomEntityDefinitionExtensions.CreateResourceDefinition<TResource>();
198199
await (string.IsNullOrWhiteSpace(@namespace)
199200
? ApiClient.DeleteClusterCustomObjectAsync(
200201
crd.Group,
@@ -218,7 +219,7 @@ public Task<Watcher<TResource>> Watch<TResource>(
218219
CancellationToken cancellationToken = default)
219220
where TResource : IKubernetesObject<V1ObjectMeta>
220221
{
221-
var crd = EntityExtensions.CreateResourceDefinition<TResource>();
222+
var crd = CustomEntityDefinitionExtensions.CreateResourceDefinition<TResource>();
222223
var result = string.IsNullOrWhiteSpace(@namespace)
223224
? ApiClient.ListClusterCustomObjectWithHttpMessagesAsync(
224225
crd.Group,

src/KubeOps/Operator/Commands/Generators/CrdGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
using System.Threading.Tasks;
99
using k8s.Models;
1010
using KubeOps.Operator.Entities;
11+
using KubeOps.Operator.Entities.Extensions;
1112
using KubeOps.Operator.Entities.Kustomize;
12-
using KubeOps.Operator.KubernetesEntities;
1313
using KubeOps.Operator.Serialization;
1414
using McMaster.Extensions.CommandLineUtils;
1515

@@ -93,7 +93,7 @@ public static IEnumerable<V1CustomResourceDefinition> GenerateCrds()
9393
var result = new List<V1CustomResourceDefinition>();
9494
foreach (var entityType in GetTypesWithAttribute<KubernetesEntityAttribute>(assembly))
9595
{
96-
var entityDefinition = EntityExtensions.CreateResourceDefinition(entityType);
96+
var entityDefinition = CustomEntityDefinitionExtensions.CreateResourceDefinition(entityType);
9797

9898
var crd = new V1CustomResourceDefinition(
9999
new V1CustomResourceDefinitionSpec(),

src/KubeOps/Operator/Commands/Management/Install.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
using System.Threading.Tasks;
55
using KubeOps.Operator.Client;
66
using KubeOps.Operator.Commands.Generators;
7-
using KubeOps.Operator.KubernetesEntities;
7+
using KubeOps.Operator.Entities;
8+
using KubeOps.Operator.Entities.Extensions;
89
using McMaster.Extensions.CommandLineUtils;
910
using Microsoft.Rest;
1011

src/KubeOps/Operator/Commands/Management/Uninstall.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
using System.Threading.Tasks;
55
using KubeOps.Operator.Client;
66
using KubeOps.Operator.Commands.Generators;
7-
using KubeOps.Operator.KubernetesEntities;
7+
using KubeOps.Operator.Entities;
8+
using KubeOps.Operator.Entities.Extensions;
89
using McMaster.Extensions.CommandLineUtils;
910
using Microsoft.Rest;
1011

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Reflection;
3+
using k8s;
4+
using k8s.Models;
5+
6+
namespace KubeOps.Operator.Entities.Extensions
7+
{
8+
internal static class CustomEntityDefinitionExtensions
9+
{
10+
internal static CustomEntityDefinition CreateResourceDefinition(
11+
this IKubernetesObject<V1ObjectMeta> kubernetesEntity) =>
12+
CreateResourceDefinition(kubernetesEntity.GetType());
13+
14+
internal static CustomEntityDefinition CreateResourceDefinition<TResource>()
15+
where TResource : IKubernetesObject<V1ObjectMeta> =>
16+
CreateResourceDefinition(typeof(TResource));
17+
18+
internal static CustomEntityDefinition CreateResourceDefinition(Type resourceType)
19+
{
20+
var attribute = resourceType.GetCustomAttribute<KubernetesEntityAttribute>();
21+
if (attribute == null)
22+
{
23+
throw new ArgumentException($"The Type {resourceType} does not have the kubernetes entity attribute.");
24+
}
25+
26+
var scopeAttribute = resourceType.GetCustomAttribute<EntityScopeAttribute>();
27+
var kind = string.IsNullOrWhiteSpace(attribute.Kind) ? resourceType.Name : attribute.Kind;
28+
29+
return new CustomEntityDefinition(
30+
kind,
31+
$"{kind}List",
32+
attribute.Group,
33+
attribute.ApiVersion,
34+
kind.ToLower(),
35+
string.IsNullOrWhiteSpace(attribute.PluralName) ? $"{kind.ToLower()}s" : attribute.PluralName,
36+
scopeAttribute?.Scope ?? default);
37+
}
38+
}
39+
}

src/KubeOps/Operator/Entities/EntityExtensions.cs renamed to src/KubeOps/Operator/Entities/Extensions/DeepCloneExtensions.cs

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,20 @@
55
using k8s.Models;
66
using KubeOps.Operator.Comparing;
77

8-
namespace KubeOps.Operator.Entities
8+
namespace KubeOps.Operator.Entities.Extensions
99
{
10-
internal static class EntityExtensions
10+
internal static class DeepCloneExtensions
1111
{
12-
internal static CustomEntityDefinition CreateResourceDefinition(
13-
this IKubernetesObject<V1ObjectMeta> kubernetesEntity) =>
14-
CreateResourceDefinition(kubernetesEntity.GetType());
15-
16-
internal static CustomEntityDefinition CreateResourceDefinition<TResource>()
17-
where TResource : IKubernetesObject<V1ObjectMeta> =>
18-
CreateResourceDefinition(typeof(TResource));
19-
20-
internal static CustomEntityDefinition CreateResourceDefinition(Type resourceType)
12+
/// <summary>
13+
/// Returns a Deep Clone / Deep Copy of an object of type T using a recursive call to the CloneMethod specified above.
14+
/// </summary>
15+
internal static TResource DeepClone<TResource>(this TResource obj)
16+
where TResource : IKubernetesObject<V1ObjectMeta>
2117
{
22-
var attribute = resourceType.GetCustomAttribute<KubernetesEntityAttribute>();
23-
if (attribute == null)
24-
{
25-
throw new ArgumentException($"The Type {resourceType} does not have the kubernetes entity attribute.");
26-
}
27-
28-
var scopeAttribute = resourceType.GetCustomAttribute<EntityScopeAttribute>();
29-
var kind = string.IsNullOrWhiteSpace(attribute.Kind) ? resourceType.Name : attribute.Kind;
30-
31-
return new CustomEntityDefinition(
32-
kind,
33-
$"{kind}List",
34-
attribute.Group,
35-
attribute.ApiVersion,
36-
kind.ToLower(),
37-
string.IsNullOrWhiteSpace(attribute.PluralName) ? $"{kind.ToLower()}s" : attribute.PluralName,
38-
scopeAttribute?.Scope ?? default);
18+
return (TResource) (DeepClone_Internal(
19+
obj,
20+
new Dictionary<object, object>(new ReferenceEqualityComparer())) ??
21+
throw new InvalidCastException());
3922
}
4023

4124
/// <summary>
@@ -55,18 +38,6 @@ private static bool IsPrimitive(this Type type)
5538
return (type.IsValueType & type.IsPrimitive);
5639
}
5740

58-
/// <summary>
59-
/// Returns a Deep Clone / Deep Copy of an object of type T using a recursive call to the CloneMethod specified above.
60-
/// </summary>
61-
internal static TResource DeepClone<TResource>(this TResource obj)
62-
where TResource : IKubernetesObject<V1ObjectMeta>
63-
{
64-
return (TResource) (DeepClone_Internal(
65-
obj,
66-
new Dictionary<object, object>(new ReferenceEqualityComparer())) ??
67-
throw new InvalidCastException());
68-
}
69-
7041
private static object? DeepClone_Internal(object? obj, IDictionary<object, object> visited)
7142
{
7243
if (obj == null) return null;

src/KubeOps/Operator/Entities/ResourceExtensions.cs renamed to src/KubeOps/Operator/Entities/Extensions/ResourceExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using KubeOps.Operator.Finalizer;
77
using Microsoft.Extensions.DependencyInjection;
88

9-
namespace KubeOps.Operator.Entities
9+
namespace KubeOps.Operator.Entities.Extensions
1010
{
1111
public static class ResourceExtensions
1212
{

src/KubeOps/Operator/KubernetesEntities/KubernetesEntityExtensions.cs renamed to src/KubeOps/Operator/Entities/Extensions/V1CustomResourceDefinitionExtensions.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
using System.Collections.Generic;
22
using System.Linq;
3-
using System.Text;
43
using k8s.Models;
54

6-
namespace KubeOps.Operator.KubernetesEntities
5+
namespace KubeOps.Operator.Entities.Extensions
76
{
8-
public static class KubernetesEntityExtensions
7+
public static class V1CustomResourceDefinitionExtensions
98
{
10-
public static string ReadData(this V1Secret secret, string key)
11-
=> Encoding.UTF8.GetString(secret.Data[key]);
12-
13-
public static void WriteData(this V1Secret secret, string key, string value)
14-
=> secret.Data[key] = Encoding.UTF8.GetBytes(value);
15-
169
internal static V1beta1CustomResourceDefinition Convert(this V1CustomResourceDefinition crd)
1710
{
1811
var crdVersion = crd.Spec.Versions.First();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Text;
2+
using k8s.Models;
3+
4+
namespace KubeOps.Operator.Entities.Extensions
5+
{
6+
public static class V1SecretExtensions
7+
{
8+
public static string ReadData(this V1Secret secret, string key)
9+
=> Encoding.UTF8.GetString(secret.Data[key]);
10+
11+
public static void WriteData(this V1Secret secret, string key, string value)
12+
=> secret.Data[key] = Encoding.UTF8.GetBytes(value);
13+
}
14+
}

0 commit comments

Comments
 (0)