1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Linq ;
4
3
using System . Reflection ;
5
4
using DotnetKubernetesClient ;
6
5
using k8s ;
14
13
using KubeOps . Operator . Leadership ;
15
14
using KubeOps . Operator . Serialization ;
16
15
using KubeOps . Operator . Services ;
17
- using KubeOps . Operator . Webhooks ;
18
16
using Microsoft . Extensions . DependencyInjection ;
19
17
using Microsoft . Extensions . DependencyInjection . Extensions ;
20
18
using Microsoft . Extensions . Diagnostics . HealthChecks ;
@@ -31,18 +29,16 @@ internal class OperatorBuilder : IOperatorBuilder
31
29
internal const string LivenessTag = "liveness" ;
32
30
internal const string ReadinessTag = "readiness" ;
33
31
34
- internal static readonly Assembly [ ] Assemblies =
35
- {
36
- Assembly . GetEntryAssembly ( ) ?? throw new Exception ( "No Entry Assembly found." ) ,
37
- Assembly . GetExecutingAssembly ( ) ,
38
- } ;
39
-
40
- private readonly IResourceTypeService _resourceTypeService ;
32
+ private readonly ResourceLocator _resourceLocator = new (
33
+ new [ ]
34
+ {
35
+ Assembly . GetEntryAssembly ( ) ?? throw new Exception ( "No Entry Assembly found." ) ,
36
+ Assembly . GetExecutingAssembly ( ) ,
37
+ } ) ;
41
38
42
39
public OperatorBuilder ( IServiceCollection services )
43
40
{
44
41
Services = services ;
45
- _resourceTypeService = new ResourceTypeService ( Assemblies ) ;
46
42
}
47
43
48
44
public IServiceCollection Services { get ; }
@@ -85,43 +81,10 @@ public IOperatorBuilder AddLivenessCheck<TLivenessCheck>(string? name = default)
85
81
86
82
public IOperatorBuilder AddResourceAssembly ( Assembly assembly )
87
83
{
88
- _resourceTypeService . AddAssembly ( assembly ) ;
89
-
84
+ _resourceLocator . Add ( assembly ) ;
90
85
return this ;
91
86
}
92
87
93
- public IOperatorBuilder AddValidationWebhook < TWebhook > ( )
94
- where TWebhook : class , IValidationWebhook
95
- {
96
- Services . AddTransient ( typeof ( IValidationWebhook ) , typeof ( TWebhook ) ) ;
97
- Services . AddTransient ( typeof ( TWebhook ) ) ;
98
-
99
- return this ;
100
- }
101
-
102
- internal static IEnumerable < ( Type ControllerType , Type EntityType ) > GetControllers ( ) => Assemblies
103
- . SelectMany ( a => a . GetTypes ( ) )
104
- . Where (
105
- t => t . IsClass &&
106
- ! t . IsAbstract &&
107
- t . GetInterfaces ( )
108
- . Any (
109
- i => i . IsGenericType && i . GetGenericTypeDefinition ( ) == typeof ( IResourceController < > ) ) )
110
- . Select (
111
- t => ( t ,
112
- t . GetInterfaces ( )
113
- . First ( i => i . IsGenericType && i . GetGenericTypeDefinition ( ) == typeof ( IResourceController < > ) )
114
- . GenericTypeArguments [ 0 ] ) ) ;
115
-
116
- internal static IEnumerable < Type > GetFinalizers ( ) => Assemblies
117
- . SelectMany ( a => a . GetTypes ( ) )
118
- . Where (
119
- t => t . IsClass &&
120
- ! t . IsAbstract &&
121
- t . GetInterfaces ( )
122
- . Any (
123
- i => i . IsGenericType && i . GetGenericTypeDefinition ( ) == typeof ( IResourceFinalizer < > ) ) ) ;
124
-
125
88
internal IOperatorBuilder AddOperatorBase ( OperatorSettings settings )
126
89
{
127
90
Services . AddSingleton ( settings ) ;
@@ -156,7 +119,7 @@ internal IOperatorBuilder AddOperatorBase(OperatorSettings settings)
156
119
Services . AddTransient < IKubernetesClient , KubernetesClient > ( ) ;
157
120
Services . AddTransient < IEventManager , EventManager > ( ) ;
158
121
159
- Services . AddSingleton ( _resourceTypeService ) ;
122
+ Services . AddSingleton ( _resourceLocator ) ;
160
123
161
124
Services . AddTransient ( typeof ( ResourceCache < > ) ) ;
162
125
Services . AddTransient ( typeof ( ResourceWatcher < > ) ) ;
@@ -183,18 +146,24 @@ internal IOperatorBuilder AddOperatorBase(OperatorSettings settings)
183
146
// and all found controller types.
184
147
Services . AddHostedService < ResourceControllerManager > ( ) ;
185
148
Services . TryAddSingleton ( sp => sp ) ;
186
- foreach ( var ( controllerType , _) in GetControllers ( ) )
149
+ foreach ( var ( controllerType , _) in _resourceLocator . ControllerTypes )
187
150
{
188
151
Services . TryAddScoped ( controllerType ) ;
189
152
}
190
153
191
154
// Register all found finalizer for the finalize manager
192
155
Services . AddTransient ( typeof ( IFinalizerManager < > ) , typeof ( FinalizerManager < > ) ) ;
193
- foreach ( var finalizerType in GetFinalizers ( ) )
156
+ foreach ( var finalizerType in _resourceLocator . FinalizerTypes )
194
157
{
195
158
Services . TryAddScoped ( finalizerType ) ;
196
159
}
197
160
161
+ // Register all found validation webhooks
162
+ foreach ( var ( validatorType , _) in _resourceLocator . ValidatorTypes )
163
+ {
164
+ Services . TryAddScoped ( validatorType ) ;
165
+ }
166
+
198
167
return this ;
199
168
}
200
169
}
0 commit comments