14
14
import io .fabric8 .kubernetes .client .KubernetesClient ;
15
15
import io .fabric8 .kubernetes .client .Version ;
16
16
import io .javaoperatorsdk .operator .api .config .ConfigurationService ;
17
+ import io .javaoperatorsdk .operator .api .config .ConfigurationServiceOverrider ;
18
+ import io .javaoperatorsdk .operator .api .config .ConfigurationServiceProvider ;
17
19
import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
18
20
import io .javaoperatorsdk .operator .api .config .ControllerConfigurationOverrider ;
19
21
import io .javaoperatorsdk .operator .api .config .ExecutorServiceManager ;
25
27
public class Operator implements LifecycleAware {
26
28
private static final Logger log = LoggerFactory .getLogger (Operator .class );
27
29
private final KubernetesClient kubernetesClient ;
28
- private final ConfigurationService configurationService ;
29
30
private final ControllerManager controllers = new ControllerManager ();
30
31
32
+ public Operator () {
33
+ this (new DefaultKubernetesClient (), ConfigurationServiceProvider .instance ());
34
+ }
35
+
36
+ public Operator (KubernetesClient kubernetesClient ) {
37
+ this (kubernetesClient , ConfigurationServiceProvider .instance ());
38
+ }
31
39
40
+ /**
41
+ * @deprecated Use {@link #Operator(Consumer)} instead
42
+ */
43
+ @ Deprecated
32
44
public Operator (ConfigurationService configurationService ) {
33
45
this (new DefaultKubernetesClient (), configurationService );
34
46
}
35
47
48
+ public Operator (Consumer <ConfigurationServiceOverrider > overrider ) {
49
+ this (new DefaultKubernetesClient (), overrider );
50
+ }
51
+
52
+ public Operator (KubernetesClient client , Consumer <ConfigurationServiceOverrider > overrider ) {
53
+ this (client );
54
+ ConfigurationServiceProvider .overrideCurrent (overrider );
55
+ }
56
+
36
57
/**
37
58
* Note that Operator by default closes the client on stop, this can be changed using
38
59
* {@link ConfigurationService}
@@ -42,7 +63,7 @@ public Operator(ConfigurationService configurationService) {
42
63
*/
43
64
public Operator (KubernetesClient kubernetesClient , ConfigurationService configurationService ) {
44
65
this .kubernetesClient = kubernetesClient ;
45
- this . configurationService = configurationService ;
66
+ ConfigurationServiceProvider . set ( configurationService ) ;
46
67
}
47
68
48
69
/** Adds a shutdown hook that automatically calls {@link #stop()} ()} when the app shuts down. */
@@ -54,10 +75,6 @@ public KubernetesClient getKubernetesClient() {
54
75
return kubernetesClient ;
55
76
}
56
77
57
- public ConfigurationService getConfigurationService () {
58
- return configurationService ;
59
- }
60
-
61
78
public List <Controller > getControllers () {
62
79
return new ArrayList <>(controllers .controllers .values ());
63
80
}
@@ -70,7 +87,7 @@ public List<Controller> getControllers() {
70
87
public void start () {
71
88
controllers .shouldStart ();
72
89
73
- final var version = configurationService .getVersion ();
90
+ final var version = ConfigurationServiceProvider . instance () .getVersion ();
74
91
log .info (
75
92
"Operator SDK {} (commit: {}) built on {} starting..." ,
76
93
version .getSdkVersion (),
@@ -80,12 +97,13 @@ public void start() {
80
97
final var clientVersion = Version .clientVersion ();
81
98
log .info ("Client version: {}" , clientVersion );
82
99
83
- ExecutorServiceManager .init (configurationService );
100
+ ExecutorServiceManager .init ();
84
101
controllers .start ();
85
102
}
86
103
87
104
@ Override
88
105
public void stop () throws OperatorException {
106
+ final var configurationService = ConfigurationServiceProvider .instance ();
89
107
log .info (
90
108
"Operator SDK {} is shutting down..." , configurationService .getVersion ().getSdkVersion ());
91
109
@@ -107,7 +125,8 @@ public void stop() throws OperatorException {
107
125
*/
108
126
public <R extends HasMetadata > void register (Reconciler <R > reconciler )
109
127
throws OperatorException {
110
- final var controllerConfiguration = configurationService .getConfigurationFor (reconciler );
128
+ final var controllerConfiguration =
129
+ ConfigurationServiceProvider .instance ().getConfigurationFor (reconciler );
111
130
register (reconciler , controllerConfiguration );
112
131
}
113
132
@@ -132,7 +151,8 @@ public <R extends HasMetadata> void register(Reconciler<R> reconciler,
132
151
"Cannot register reconciler with name " + reconciler .getClass ().getCanonicalName () +
133
152
" reconciler named " + ReconcilerUtils .getNameFor (reconciler )
134
153
+ " because its configuration cannot be found.\n " +
135
- " Known reconcilers are: " + configurationService .getKnownReconcilerNames ());
154
+ " Known reconcilers are: "
155
+ + ConfigurationServiceProvider .instance ().getKnownReconcilerNames ());
136
156
}
137
157
138
158
final var controller = new Controller <>(reconciler , configuration , kubernetesClient );
@@ -158,7 +178,8 @@ public <R extends HasMetadata> void register(Reconciler<R> reconciler,
158
178
*/
159
179
public <R extends HasMetadata > void register (Reconciler <R > reconciler ,
160
180
Consumer <ControllerConfigurationOverrider <R >> configOverrider ) {
161
- final var controllerConfiguration = configurationService .getConfigurationFor (reconciler );
181
+ final var controllerConfiguration =
182
+ ConfigurationServiceProvider .instance ().getConfigurationFor (reconciler );
162
183
var configToOverride = ControllerConfigurationOverrider .override (controllerConfiguration );
163
184
configOverrider .accept (configToOverride );
164
185
register (reconciler , configToOverride .build ());
0 commit comments