1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
3
4
using System . Reflection ;
4
5
using DotnetKubernetesClient ;
6
+ using k8s ;
7
+ using k8s . Models ;
5
8
using KubeOps . Operator . Caching ;
6
9
using KubeOps . Operator . Controller ;
7
10
using KubeOps . Operator . DevOps ;
8
11
using KubeOps . Operator . Events ;
9
12
using KubeOps . Operator . Finalizer ;
13
+ using KubeOps . Operator . Kubernetes ;
10
14
using KubeOps . Operator . Leadership ;
11
- using KubeOps . Operator . Queue ;
12
15
using KubeOps . Operator . Serialization ;
13
16
using KubeOps . Operator . Services ;
14
- using KubeOps . Operator . Watcher ;
15
17
using KubeOps . Operator . Webhooks ;
16
18
using Microsoft . Extensions . DependencyInjection ;
19
+ using Microsoft . Extensions . DependencyInjection . Extensions ;
17
20
using Microsoft . Extensions . Diagnostics . HealthChecks ;
18
21
using Microsoft . Rest . Serialization ;
19
22
using Newtonsoft . Json ;
@@ -28,19 +31,18 @@ internal class OperatorBuilder : IOperatorBuilder
28
31
internal const string LivenessTag = "liveness" ;
29
32
internal const string ReadinessTag = "readiness" ;
30
33
34
+ internal static readonly Assembly [ ] Assemblies =
35
+ {
36
+ Assembly . GetEntryAssembly ( ) ?? throw new Exception ( "No Entry Assembly found." ) ,
37
+ Assembly . GetExecutingAssembly ( ) ,
38
+ } ;
39
+
31
40
private readonly IResourceTypeService _resourceTypeService ;
32
41
33
42
public OperatorBuilder ( IServiceCollection services )
34
43
{
35
44
Services = services ;
36
-
37
- var entryAssembly = Assembly . GetEntryAssembly ( ) ;
38
- if ( entryAssembly == null )
39
- {
40
- throw new Exception ( "No Entry Assembly found." ) ;
41
- }
42
-
43
- _resourceTypeService = new ResourceTypeService ( entryAssembly , Assembly . GetExecutingAssembly ( ) ) ;
45
+ _resourceTypeService = new ResourceTypeService ( Assemblies ) ;
44
46
}
45
47
46
48
public IServiceCollection Services { get ; }
@@ -81,14 +83,6 @@ public IOperatorBuilder AddLivenessCheck<TLivenessCheck>(string? name = default)
81
83
return this ;
82
84
}
83
85
84
- public IOperatorBuilder AddController < TController > ( )
85
- where TController : class , IResourceController
86
- {
87
- Services . AddHostedService < TController > ( ) ;
88
-
89
- return this ;
90
- }
91
-
92
86
public IOperatorBuilder AddFinalizer < TFinalizer > ( )
93
87
where TFinalizer : class , IResourceFinalizer
94
88
{
@@ -113,13 +107,24 @@ public IOperatorBuilder AddValidationWebhook<TWebhook>()
113
107
return this ;
114
108
}
115
109
110
+ internal static IEnumerable < ( Type ControllerType , Type EntityType ) > GetControllers ( ) => Assemblies
111
+ . SelectMany ( a => a . GetTypes ( ) )
112
+ . Where (
113
+ t => t . IsClass &&
114
+ ! t . IsAbstract &&
115
+ t . GetInterfaces ( )
116
+ . Any (
117
+ i => i . IsGenericType && i . GetGenericTypeDefinition ( ) == typeof ( IResourceController < > ) ) )
118
+ . Select (
119
+ t => ( t ,
120
+ t . GetInterfaces ( )
121
+ . First ( i => i . IsGenericType && i . GetGenericTypeDefinition ( ) == typeof ( IResourceController < > ) )
122
+ . GenericTypeArguments [ 0 ] ) ) ;
123
+
116
124
internal IOperatorBuilder AddOperatorBase ( OperatorSettings settings )
117
125
{
118
126
Services . AddSingleton ( settings ) ;
119
127
120
- // support lazy service resolution
121
- Services . AddTransient ( typeof ( Lazy < > ) , typeof ( LazyService < > ) ) ;
122
-
123
128
var jsonSettings = new JsonSerializerSettings
124
129
{
125
130
DateFormatHandling = DateFormatHandling . IsoDateFormat ,
@@ -141,33 +146,43 @@ internal IOperatorBuilder AddOperatorBase(OperatorSettings settings)
141
146
_ => new SerializerBuilder ( )
142
147
. ConfigureDefaultValuesHandling ( DefaultValuesHandling . OmitNull )
143
148
. WithNamingConvention ( new NamingConvention ( ) )
144
- . WithTypeConverter ( new YamlIntOrStrTypeConverter ( ) )
145
- . WithTypeConverter ( new YamlByteArrayTypeConverter ( ) )
149
+ . WithTypeConverter ( new Yaml . ByteArrayStringYamlConverter ( ) )
150
+ . WithTypeConverter ( new IntOrStringYamlConverter ( ) )
146
151
. Build ( ) ) ;
147
152
148
153
Services . AddTransient < EntitySerializer > ( ) ;
149
154
150
155
Services . AddTransient < IKubernetesClient , KubernetesClient > ( ) ;
151
156
Services . AddTransient < IEventManager , EventManager > ( ) ;
152
157
153
- Services . AddSingleton ( typeof ( IResourceCache < > ) , typeof ( ResourceCache < > ) ) ;
154
- Services . AddTransient ( typeof ( IResourceWatcher < > ) , typeof ( ResourceWatcher < > ) ) ;
155
- Services . AddTransient ( typeof ( IResourceEventQueue < > ) , typeof ( ResourceEventQueue < > ) ) ;
156
- Services . AddTransient ( typeof ( IResourceServices < > ) , typeof ( ResourceServices < > ) ) ;
158
+ Services . AddTransient ( typeof ( ResourceCache < > ) ) ;
159
+ Services . AddTransient ( typeof ( ResourceWatcher < > ) ) ;
160
+ Services . AddTransient ( typeof ( ManagedResourceController < > ) ) ;
161
+
162
+ // Support all the metrics
163
+ Services . AddSingleton ( typeof ( ResourceWatcherMetrics < > ) ) ;
164
+ Services . AddSingleton ( typeof ( ResourceCacheMetrics < > ) ) ;
165
+ Services . AddSingleton ( typeof ( ResourceControllerMetrics < > ) ) ;
157
166
158
167
// Support for healthchecks and prometheus.
159
168
Services
160
169
. AddHealthChecks ( )
161
170
. ForwardToPrometheus ( ) ;
162
171
163
- // Add the default controller liveness check.
164
- AddHealthCheck < ControllerLivenessCheck > ( ) ;
165
-
166
172
// Support for leader election via V1Leases.
167
173
Services . AddHostedService < LeaderElector > ( ) ;
168
174
Services . AddSingleton < ILeaderElection , LeaderElection > ( ) ;
169
175
170
176
Services . AddSingleton ( _resourceTypeService ) ;
177
+ Services . AddHostedService < ResourceControllerManager > ( ) ;
178
+
179
+ // Add the service provider (for instantiation)
180
+ // and all found controller types.
181
+ Services . TryAddSingleton ( sp => sp ) ;
182
+ foreach ( var ( controllerType , _) in GetControllers ( ) )
183
+ {
184
+ Services . TryAddScoped ( controllerType ) ;
185
+ }
171
186
172
187
return this ;
173
188
}
0 commit comments