12
12
using KubeOps . Operator . Queue ;
13
13
using KubeOps . Operator . Serialization ;
14
14
using KubeOps . Operator . Watcher ;
15
+ using KubeOps . Testing ;
15
16
using McMaster . Extensions . CommandLineUtils ;
16
17
using Microsoft . Extensions . DependencyInjection ;
17
18
using Microsoft . Extensions . Hosting ;
23
24
24
25
namespace KubeOps . Operator
25
26
{
26
- public sealed class KubernetesOperator
27
+ public class KubernetesOperator
27
28
{
28
29
internal const string NoStructuredLogs = "--no-structured-logs" ;
29
30
private const string DefaultOperatorName = "KubernetesOperator" ;
30
- private readonly OperatorSettings _operatorSettings ;
31
31
32
- private readonly IHostBuilder _builder = Host
32
+ protected readonly OperatorSettings OperatorSettings ;
33
+
34
+ protected readonly IList < Action < IServiceCollection > > ServiceConfigurations =
35
+ new List < Action < IServiceCollection > > ( ) ;
36
+
37
+ protected readonly IHostBuilder Builder = Host
33
38
. CreateDefaultBuilder ( )
34
39
. UseConsoleLifetime ( ) ;
35
40
36
- public KubernetesOperator ( string ? operatorName = null )
41
+ public KubernetesOperator ( )
42
+ : this ( ( Assembly . GetEntryAssembly ( ) ? . GetName ( ) . Name ?? DefaultOperatorName ) . ToLowerInvariant ( ) )
43
+ {
44
+ }
45
+
46
+ public KubernetesOperator ( string operatorName )
47
+ : this (
48
+ new OperatorSettings
49
+ {
50
+ Name = operatorName ,
51
+ } )
37
52
{
38
- operatorName ??= ( operatorName ?? Assembly . GetEntryAssembly ( ) ? . GetName ( ) . Name ?? DefaultOperatorName )
39
- . ToLowerInvariant ( ) ;
40
- _operatorSettings = new OperatorSettings
41
- {
42
- Name = operatorName ,
43
- } ;
44
53
}
45
54
46
55
public KubernetesOperator ( OperatorSettings settings )
47
56
{
48
- _operatorSettings = settings ;
57
+ OperatorSettings = settings ;
49
58
}
50
59
51
- public Task < int > Run ( string [ ] args )
60
+ public KubernetesTestOperator ToKubernetesTestOperator ( )
52
61
{
53
- ConfigureRequiredServices ( ) ;
62
+ var op = new KubernetesTestOperator
63
+ {
64
+ OperatorSettings = { Name = OperatorSettings . Name }
65
+ } ;
66
+
67
+ foreach ( var config in ServiceConfigurations )
68
+ {
69
+ op . ConfigureServices ( config ) ;
70
+ }
71
+
72
+ return op ;
73
+ }
74
+
75
+ public Task < int > Run ( ) => Run ( new string [ 0 ] ) ;
76
+
77
+ public virtual Task < int > Run ( string [ ] args )
78
+ {
79
+ ConfigureOperatorServices ( ) ;
54
80
55
81
var app = new CommandLineApplication < RunOperator > ( ) ;
56
82
57
- _builder . ConfigureLogging (
83
+ ConfigureOperatorLogging ( args ) ;
84
+
85
+ var host = Builder . Build ( ) ;
86
+
87
+ app
88
+ . Conventions
89
+ . UseDefaultConventions ( )
90
+ . UseConstructorInjection ( host . Services ) ;
91
+
92
+ DependencyInjector . Services = host . Services ;
93
+ JsonConvert . DefaultSettings = ( ) => host . Services . GetRequiredService < JsonSerializerSettings > ( ) ;
94
+
95
+ return app . ExecuteAsync ( args ) ;
96
+ }
97
+
98
+ public KubernetesOperator ConfigureServices ( Action < IServiceCollection > configuration )
99
+ {
100
+ ServiceConfigurations . Add ( configuration ) ;
101
+ return this ;
102
+ }
103
+
104
+ protected virtual void ConfigureOperatorServices ( )
105
+ {
106
+ ConfigureRequiredServices ( ) ;
107
+ foreach ( var config in ServiceConfigurations )
108
+ {
109
+ Builder . ConfigureServices ( config ) ;
110
+ }
111
+ }
112
+
113
+ protected virtual void ConfigureOperatorLogging ( IEnumerable < string > args ) =>
114
+ Builder . ConfigureLogging (
58
115
( hostContext , logging ) =>
59
116
{
60
117
logging . ClearProviders ( ) ;
@@ -63,11 +120,12 @@ public Task<int> Run(string[] args)
63
120
{
64
121
if ( args . Contains ( NoStructuredLogs ) )
65
122
{
66
- logging . AddConsole ( options =>
67
- {
68
- options . TimestampFormat = @"[dd.MM.yyyy - HH:mm:ss] " ;
69
- options . DisableColors = true ;
70
- } ) ;
123
+ logging . AddConsole (
124
+ options =>
125
+ {
126
+ options . TimestampFormat = @"[dd.MM.yyyy - HH:mm:ss] " ;
127
+ options . DisableColors = true ;
128
+ } ) ;
71
129
}
72
130
else
73
131
{
@@ -80,37 +138,18 @@ public Task<int> Run(string[] args)
80
138
}
81
139
} ) ;
82
140
83
- var host = _builder . Build ( ) ;
84
-
85
- app
86
- . Conventions
87
- . UseDefaultConventions ( )
88
- . UseConstructorInjection ( host . Services ) ;
89
-
90
- DependencyInjector . Services = host . Services ;
91
- JsonConvert . DefaultSettings = ( ) => host . Services . GetRequiredService < JsonSerializerSettings > ( ) ;
92
-
93
- return app . ExecuteAsync ( args ) ;
94
- }
95
-
96
- public KubernetesOperator ConfigureServices ( Action < IServiceCollection > configuration )
97
- {
98
- _builder . ConfigureServices ( configuration ) ;
99
- return this ;
100
- }
101
-
102
141
private void ConfigureRequiredServices ( ) =>
103
- _builder . ConfigureServices (
142
+ Builder . ConfigureServices (
104
143
services =>
105
144
{
106
- services . AddSingleton ( _operatorSettings ) ;
145
+ services . AddSingleton ( OperatorSettings ) ;
107
146
108
147
services . AddTransient (
109
148
_ => new JsonSerializerSettings
110
149
{
111
150
ContractResolver = new NamingConvention ( ) ,
112
151
Converters = new List < JsonConverter >
113
- { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy ( ) } } ,
152
+ { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy ( ) } } ,
114
153
} ) ;
115
154
services . AddTransient (
116
155
_ => new SerializerBuilder ( )
@@ -131,13 +170,13 @@ private void ConfigureRequiredServices() =>
131
170
{
132
171
ContractResolver = new NamingConvention ( ) ,
133
172
Converters = new List < JsonConverter >
134
- { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy ( ) } }
173
+ { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy ( ) } }
135
174
} ,
136
175
DeserializationSettings =
137
176
{
138
177
ContractResolver = new NamingConvention ( ) ,
139
178
Converters = new List < JsonConverter >
140
- { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy ( ) } }
179
+ { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy ( ) } }
141
180
}
142
181
} ;
143
182
} ) ;
0 commit comments