Skip to content

Commit c4b5244

Browse files
author
Christoph Bühler
committed
refactor(core logic): rename some files and use interfaces
Rename some entities to resources to adhere to some kind of consistent naming between resource and entity. Use interfaces and add them to the dependency injection.
1 parent 93ba4ba commit c4b5244

15 files changed

+256
-150
lines changed

DotnetOperatorSdk.sln.DotSettings

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<wpf:ResourceDictionary xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xml:space="preserve">
3+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean>
4+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ACCESSORHOLDER_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
5+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_CONSTRUCTOR_INITIALIZER_ON_SAME_LINE/@EntryValue">False</s:Boolean>
6+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FIELD_ATTRIBUTE_ON_SAME_LINE_EX/@EntryValue">NEVER</s:String>
7+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_TYPE_CONSTRAINTS_ON_SAME_LINE/@EntryValue">False</s:Boolean>
8+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_WHILE_ON_NEW_LINE/@EntryValue">True</s:Boolean>
9+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/SPACE_WITHIN_SINGLE_LINE_ARRAY_INITIALIZER_BRACES/@EntryValue">True</s:Boolean>
10+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_AFTER_DECLARATION_LPAR/@EntryValue">True</s:Boolean>
11+
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_AFTER_INVOCATION_LPAR/@EntryValue">True</s:Boolean>
12+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARGUMENTS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
13+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_ARRAY_INITIALIZER_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
14+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_CHAINED_BINARY_EXPRESSIONS/@EntryValue">CHOP_IF_LONG</s:String>
15+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_CHAINED_METHOD_CALLS/@EntryValue">CHOP_IF_LONG</s:String>
16+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_EXTENDS_LIST_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
17+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_MULTIPLE_TYPE_PARAMEER_CONSTRAINTS_STYLE/@EntryValue">CHOP_ALWAYS</s:String>
18+
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_PARAMETERS_STYLE/@EntryValue">CHOP_IF_LONG</s:String>
19+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
20+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
21+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
22+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
23+
</wpf:ResourceDictionary>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using k8s;
2+
using k8s.Models;
3+
4+
namespace KubeOps.Operator.Caching
5+
{
6+
internal interface IResourceCache<TEntity>
7+
where TEntity : IKubernetesObject<V1ObjectMeta>
8+
{
9+
TEntity Get(string id);
10+
11+
TEntity Upsert(TEntity resource, out CacheComparisonResult result);
12+
13+
void Remove(TEntity resource);
14+
15+
public void Clear();
16+
}
17+
}

src/KubeOps/Operator/Caching/ResourceCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace KubeOps.Operator.Caching
99
{
10-
internal class EntityCache<TEntity>
10+
internal class ResourceCache<TEntity> : IResourceCache<TEntity>
1111
where TEntity : IKubernetesObject<V1ObjectMeta>
1212
{
1313
private const string ResourceVersion = "ResourceVersion";

src/KubeOps/Operator/Client/IKubernetesClient.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ public interface IKubernetesClient
1515
Task<TResource?> Get<TResource>(string name, string? @namespace = null)
1616
where TResource : class, IKubernetesObject<V1ObjectMeta>;
1717

18-
// Task<TResource?> Get<TResource>(string name, string? @namespace = null, TResource? requireClass = null)
19-
// where TResource : class, IKubernetesObject<V1ObjectMeta>;
20-
21-
// Task<TResource> Get<TResource>(string name, string? @namespace = null, TResource? requireStruct = null)
22-
// where TResource : struct, IKubernetesObject<V1ObjectMeta>;
23-
2418
Task<IList<TResource>> List<TResource>(
2519
string? @namespace = null,
2620
string? labelSelector = null)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using k8s;
2+
using k8s.Models;
3+
using Microsoft.Extensions.Hosting;
4+
5+
namespace KubeOps.Operator.Controller
6+
{
7+
public interface IResourceController<TEntity> : IHostedService
8+
where TEntity : IKubernetesObject<V1ObjectMeta>
9+
{
10+
}
11+
}

src/KubeOps/Operator/Controller/ResourceControllerBase.cs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,65 @@
1010
using KubeOps.Operator.Finalizer;
1111
using KubeOps.Operator.Queue;
1212
using Microsoft.Extensions.DependencyInjection;
13-
using Microsoft.Extensions.Hosting;
1413
using Microsoft.Extensions.Logging;
1514

1615
namespace KubeOps.Operator.Controller
1716
{
1817
// TODO: namespaced controller (only watch resource of a specific namespace)
1918
// TODO: Webhooks?
2019

21-
public abstract class ResourceControllerBase<TResource> : IHostedService
22-
where TResource : IKubernetesObject<V1ObjectMeta>
20+
public abstract class ResourceControllerBase<TEntity> : IResourceController<TEntity>
21+
where TEntity : IKubernetesObject<V1ObjectMeta>
2322
{
24-
private readonly IReadOnlyList<EntityEventType> _requeueableEvents = new[]
23+
private readonly IReadOnlyList<ResourceEventType> _requeueableEvents = new[]
2524
{
26-
EntityEventType.Created,
27-
EntityEventType.Updated,
28-
EntityEventType.NotModified,
25+
ResourceEventType.Created,
26+
ResourceEventType.Updated,
27+
ResourceEventType.NotModified,
2928
};
3029

31-
private readonly ILogger<ResourceControllerBase<TResource>> _logger;
32-
private readonly EntityEventQueue<TResource> _eventQueue;
33-
34-
private readonly Lazy<IKubernetesClient> _client =
35-
new Lazy<IKubernetesClient>(() => DependencyInjector.Services.GetRequiredService<IKubernetesClient>());
30+
private readonly ILogger<ResourceControllerBase<TEntity>> _logger;
31+
private readonly IResourceEventQueue<TEntity> _eventQueue;
3632

3733
protected ResourceControllerBase()
34+
: this(
35+
DependencyInjector.Services.GetRequiredService<ILogger<ResourceControllerBase<TEntity>>>(),
36+
DependencyInjector.Services.GetRequiredService<IKubernetesClient>(),
37+
DependencyInjector.Services.GetRequiredService<IResourceEventQueue<TEntity>>())
38+
{
39+
}
40+
41+
protected ResourceControllerBase(
42+
ILogger<ResourceControllerBase<TEntity>> logger,
43+
IKubernetesClient client,
44+
IResourceEventQueue<TEntity> eventQueue)
3845
{
39-
_logger = DependencyInjector.Services.GetRequiredService<ILogger<ResourceControllerBase<TResource>>>();
40-
_eventQueue = new EntityEventQueue<TResource>();
46+
_logger = logger;
47+
_eventQueue = eventQueue;
48+
Client = client;
4149
}
4250

43-
protected IKubernetesClient Client => _client.Value;
51+
protected IKubernetesClient Client { get; }
4452

4553
public async Task StartAsync(CancellationToken cancellationToken)
4654
{
47-
_logger.LogInformation(@"Startup CRD Controller for ""{resource}"".", typeof(TResource));
55+
_logger.LogInformation(@"Startup CRD Controller for ""{resource}"".", typeof(TEntity));
4856

4957
_eventQueue.ResourceEvent += OnResourceEvent;
5058
await _eventQueue.Start();
5159
}
5260

5361
public Task StopAsync(CancellationToken cancellationToken)
5462
{
55-
_logger.LogInformation(@"Shutdown CRD Controller for ""{resource}"".", typeof(TResource));
63+
_logger.LogInformation(@"Shutdown CRD Controller for ""{resource}"".", typeof(TEntity));
5664

5765
_eventQueue.Stop();
5866
_eventQueue.ResourceEvent -= OnResourceEvent;
5967

6068
return Task.CompletedTask;
6169
}
6270

63-
protected virtual Task<TimeSpan?> Created(TResource resource)
71+
protected virtual Task<TimeSpan?> Created(TEntity resource)
6472
{
6573
_logger.LogDebug(
6674
@"Object ""{kind}/{name}"" fired ""created"" event.",
@@ -69,7 +77,7 @@ public Task StopAsync(CancellationToken cancellationToken)
6977
return Task.FromResult(default(TimeSpan?));
7078
}
7179

72-
protected virtual Task<TimeSpan?> Updated(TResource resource)
80+
protected virtual Task<TimeSpan?> Updated(TEntity resource)
7381
{
7482
_logger.LogDebug(
7583
@"Object ""{kind}/{name}"" fired ""updated"" event.",
@@ -78,7 +86,7 @@ public Task StopAsync(CancellationToken cancellationToken)
7886
return Task.FromResult(default(TimeSpan?));
7987
}
8088

81-
protected virtual Task<TimeSpan?> NotModified(TResource resource)
89+
protected virtual Task<TimeSpan?> NotModified(TEntity resource)
8290
{
8391
_logger.LogDebug(
8492
@"Object ""{kind}/{name}"" fired ""not modified"" event.",
@@ -87,7 +95,7 @@ public Task StopAsync(CancellationToken cancellationToken)
8795
return Task.FromResult(default(TimeSpan?));
8896
}
8997

90-
protected virtual Task StatusModified(TResource resource)
98+
protected virtual Task StatusModified(TEntity resource)
9199
{
92100
_logger.LogDebug(
93101
@"Object ""{kind}/{name}"" fired ""status modified"" event.",
@@ -96,7 +104,7 @@ protected virtual Task StatusModified(TResource resource)
96104
return Task.CompletedTask;
97105
}
98106

99-
protected virtual Task Deleted(TResource resource)
107+
protected virtual Task Deleted(TEntity resource)
100108
{
101109
_logger.LogDebug(
102110
@"Object ""{kind}/{name}"" fired ""deleted"" event.",
@@ -105,7 +113,7 @@ protected virtual Task Deleted(TResource resource)
105113
return Task.FromResult(default(TimeSpan?));
106114
}
107115

108-
private async void OnResourceEvent(object? _, (EntityEventType type, TResource resource) args)
116+
private async void OnResourceEvent(object? _, (ResourceEventType type, TEntity resource) args)
109117
{
110118
var (type, resource) = args;
111119

@@ -117,15 +125,15 @@ private async void OnResourceEvent(object? _, (EntityEventType type, TResource r
117125
resource.Kind,
118126
resource.Metadata.Name);
119127

120-
if (type == EntityEventType.Finalizing)
128+
if (type == ResourceEventType.Finalizing)
121129
{
122130
if (resource.Metadata.Finalizers == null || resource.Metadata.Finalizers.Count == 0)
123131
{
124132
return;
125133
}
126134

127135
var finalizer = DependencyInjector.Services
128-
.GetServices<IResourceFinalizer<TResource>>()
136+
.GetServices<IResourceFinalizer<TEntity>>()
129137
.FirstOrDefault(f => f.Identifier == resource.Metadata.Finalizers.First());
130138
if (finalizer == null)
131139
{
@@ -152,10 +160,10 @@ private async void OnResourceEvent(object? _, (EntityEventType type, TResource r
152160
{
153161
switch (type)
154162
{
155-
case EntityEventType.StatusUpdated:
163+
case ResourceEventType.StatusUpdated:
156164
await StatusModified(resource);
157165
break;
158-
case EntityEventType.Deleted:
166+
case ResourceEventType.Deleted:
159167
await Deleted(resource);
160168
break;
161169
}
@@ -172,9 +180,9 @@ private async void OnResourceEvent(object? _, (EntityEventType type, TResource r
172180

173181
var requeue = type switch
174182
{
175-
EntityEventType.Created => await Created(resource),
176-
EntityEventType.Updated => await Updated(resource),
177-
EntityEventType.NotModified => await NotModified(resource),
183+
ResourceEventType.Created => await Created(resource),
184+
ResourceEventType.Updated => await Updated(resource),
185+
ResourceEventType.NotModified => await NotModified(resource),
178186
_ => throw new ArgumentOutOfRangeException(),
179187
};
180188

src/KubeOps/Operator/Finalizer/ResourceFinalizerBase.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@ namespace KubeOps.Operator.Finalizer
1414
public abstract class ResourceFinalizerBase<TResource> : IResourceFinalizer<TResource>
1515
where TResource : IKubernetesObject<V1ObjectMeta>
1616
{
17-
private readonly Lazy<IKubernetesClient> _client =
18-
new Lazy<IKubernetesClient>(() => DependencyInjector.Services.GetRequiredService<IKubernetesClient>());
19-
2017
private readonly ILogger<ResourceFinalizerBase<TResource>> _logger;
2118

2219
protected ResourceFinalizerBase()
20+
: this(
21+
DependencyInjector.Services.GetRequiredService<ILogger<ResourceFinalizerBase<TResource>>>(),
22+
DependencyInjector.Services.GetRequiredService<IKubernetesClient>())
23+
{
24+
}
25+
26+
protected ResourceFinalizerBase(ILogger<ResourceFinalizerBase<TResource>> logger, IKubernetesClient client)
2327
{
24-
_logger = DependencyInjector.Services.GetRequiredService<ILogger<ResourceFinalizerBase<TResource>>>();
28+
_logger = logger;
29+
Client = client;
2530
}
2631

2732
public virtual string Identifier
@@ -33,7 +38,7 @@ public virtual string Identifier
3338
}
3439
}
3540

36-
protected IKubernetesClient Client => _client.Value;
41+
protected IKubernetesClient Client { get; }
3742

3843
public async Task Register(TResource resource)
3944
{

src/KubeOps/Operator/KubernetesEntities/KubernetesEntityExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal static V1beta1CustomResourceDefinition Convert(this V1CustomResourceDef
4747
return betaCrd;
4848
}
4949

50-
private static V1beta1JSONSchemaProps Convert(this k8s.Models.V1JSONSchemaProps props)
50+
private static V1beta1JSONSchemaProps Convert(this V1JSONSchemaProps props)
5151
{
5252
var betaProps = new V1beta1JSONSchemaProps();
5353

0 commit comments

Comments
 (0)