1
1
package io .javaoperatorsdk .operator ;
2
2
3
- import io .javaoperatorsdk .operator .api .ResourceController ;
4
- import io .javaoperatorsdk .operator .processing .EventDispatcher ;
5
- import io .javaoperatorsdk .operator .processing .EventScheduler ;
6
- import io .javaoperatorsdk .operator .processing .retry .GenericRetry ;
7
- import io .javaoperatorsdk .operator .processing .retry .Retry ;
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+ import java .util .Set ;
6
+
8
7
import io .fabric8 .kubernetes .api .model .apiextensions .v1beta1 .CustomResourceDefinition ;
8
+ import io .fabric8 .kubernetes .client .ConfigBuilder ;
9
9
import io .fabric8 .kubernetes .client .CustomResource ;
10
10
import io .fabric8 .kubernetes .client .CustomResourceDoneable ;
11
11
import io .fabric8 .kubernetes .client .CustomResourceList ;
12
+ import io .fabric8 .kubernetes .client .DefaultKubernetesClient ;
12
13
import io .fabric8 .kubernetes .client .KubernetesClient ;
13
14
import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
14
15
import io .fabric8 .kubernetes .client .dsl .base .CustomResourceDefinitionContext ;
15
16
import io .fabric8 .kubernetes .client .dsl .internal .CustomResourceOperationsImpl ;
16
17
import io .fabric8 .kubernetes .internal .KubernetesDeserializer ;
18
+ import io .fabric8 .openshift .client .DefaultOpenShiftClient ;
19
+ import io .javaoperatorsdk .operator .api .ResourceController ;
20
+ import io .javaoperatorsdk .operator .config .ClientConfiguration ;
21
+ import io .javaoperatorsdk .operator .config .Configuration ;
22
+ import io .javaoperatorsdk .operator .config .OperatorConfiguration ;
23
+ import io .javaoperatorsdk .operator .processing .EventDispatcher ;
24
+ import io .javaoperatorsdk .operator .processing .EventScheduler ;
25
+ import io .javaoperatorsdk .operator .processing .retry .GenericRetry ;
26
+ import io .javaoperatorsdk .operator .processing .retry .Retry ;
27
+ import org .apache .commons .lang3 .StringUtils ;
17
28
import org .slf4j .Logger ;
18
29
import org .slf4j .LoggerFactory ;
19
30
20
- import java .util .Arrays ;
21
- import java .util .HashMap ;
22
- import java .util .Map ;
23
-
24
31
@ SuppressWarnings ("rawtypes" )
25
32
public class Operator {
26
-
33
+
27
34
private final static Logger log = LoggerFactory .getLogger (Operator .class );
28
35
private final KubernetesClient k8sClient ;
36
+ private final Configuration config ;
29
37
private Map <Class <? extends CustomResource >, CustomResourceOperationsImpl > customResourceClients = new HashMap <>();
30
-
38
+
31
39
public Operator (KubernetesClient k8sClient ) {
32
40
this .k8sClient = k8sClient ;
41
+ this .config = Configuration .defaultConfiguration ();
33
42
}
34
-
35
-
36
- public <R extends CustomResource > void registerControllerForAllNamespaces (ResourceController <R > controller ) throws OperatorException {
37
- registerController (controller , true , GenericRetry .defaultLimitedExponentialRetry ());
43
+
44
+ public Operator () {
45
+ this (Configuration .defaultConfiguration ());
38
46
}
39
-
40
- public <R extends CustomResource > void registerControllerForAllNamespaces (ResourceController <R > controller , Retry retry ) throws OperatorException {
41
- registerController (controller , true , retry );
47
+
48
+ public Operator (Configuration config ) {
49
+ this .config = config ;
50
+ ConfigBuilder cb = new ConfigBuilder ();
51
+
52
+ final ClientConfiguration clientCfg = config .getClientConfiguration ();
53
+ cb .withTrustCerts (clientCfg .isTrustSelfSignedCertificates ());
54
+ if (StringUtils .isNotBlank (clientCfg .getUsername ())) {
55
+ cb .withUsername (clientCfg .getUsername ());
56
+ }
57
+ if (StringUtils .isNotBlank (clientCfg .getPassword ())) {
58
+ cb .withUsername (clientCfg .getPassword ());
59
+ }
60
+ if (StringUtils .isNotBlank (clientCfg .getMasterUrl ())) {
61
+ cb .withMasterUrl (clientCfg .getMasterUrl ());
62
+ }
63
+
64
+ config .getOperatorConfiguration ().getWatchedNamespaceIfUnique ().ifPresent (cb ::withNamespace );
65
+ this .k8sClient = clientCfg .isOpenshift () ? new DefaultOpenShiftClient (cb .build ()) : new DefaultKubernetesClient (cb .build ());
42
66
}
43
-
44
- public < R extends CustomResource > void registerController ( ResourceController < R > controller , String ... targetNamespaces ) throws OperatorException {
45
- registerController ( controller , false , GenericRetry . defaultLimitedExponentialRetry (), targetNamespaces ) ;
67
+
68
+ public KubernetesClient getClient () {
69
+ return k8sClient ;
46
70
}
47
-
48
- public <R extends CustomResource > void registerController (ResourceController <R > controller , Retry retry , String ... targetNamespaces ) throws OperatorException {
49
- registerController (controller , false , retry , targetNamespaces );
71
+
72
+ public <R extends CustomResource > void registerController (ResourceController <R > controller ) throws OperatorException {
73
+ registerController (controller , GenericRetry . defaultLimitedExponentialRetry () );
50
74
}
51
-
75
+
52
76
@ SuppressWarnings ("rawtypes" )
53
- private <R extends CustomResource > void registerController (ResourceController <R > controller ,
54
- boolean watchAllNamespaces , Retry retry , String ... targetNamespaces ) throws OperatorException {
77
+ public <R extends CustomResource > void registerController (ResourceController <R > controller , Retry retry ) throws OperatorException {
78
+ controller . setClient ( this . k8sClient );
55
79
Class <R > resClass = ControllerUtils .getCustomResourceClass (controller );
56
80
CustomResourceDefinitionContext crd = getCustomResourceDefinitionForController (controller );
57
81
KubernetesDeserializer .registerCustomKind (crd .getVersion (), crd .getKind (), resClass );
58
82
String finalizer = ControllerUtils .getFinalizer (controller );
59
83
MixedOperation client = k8sClient .customResources (crd , resClass , CustomResourceList .class , ControllerUtils .getCustomResourceDoneableClass (controller ));
60
84
EventDispatcher eventDispatcher = new EventDispatcher (controller ,
61
- finalizer , new EventDispatcher .CustomResourceFacade (client ), ControllerUtils .getGenerationEventProcessing (controller ));
85
+ finalizer , new EventDispatcher .CustomResourceFacade (client ), ControllerUtils .getGenerationEventProcessing (controller ));
62
86
EventScheduler eventScheduler = new EventScheduler (eventDispatcher , retry );
63
- registerWatches (controller , client , resClass , watchAllNamespaces , targetNamespaces , eventScheduler );
87
+ registerWatches (controller , client , resClass , eventScheduler );
64
88
}
65
-
66
-
89
+
90
+
67
91
private <R extends CustomResource > void registerWatches (ResourceController <R > controller , MixedOperation client ,
68
92
Class <R > resClass ,
69
- boolean watchAllNamespaces , String [] targetNamespaces , EventScheduler eventScheduler ) {
70
-
93
+ EventScheduler eventScheduler ) {
94
+
71
95
CustomResourceOperationsImpl crClient = (CustomResourceOperationsImpl ) client ;
72
- if (watchAllNamespaces ) {
96
+ final OperatorConfiguration operatorCfg = config .getOperatorConfiguration ();
97
+ final String namespaces ;
98
+ if (operatorCfg .isWatchingAllNamespaces ()) {
73
99
crClient .inAnyNamespace ().watch (eventScheduler );
74
- } else if (targetNamespaces .length == 0 ) {
100
+ namespaces = "all namespaces" ;
101
+ } else if (operatorCfg .isWatchingCurrentNamespace ()) {
75
102
client .watch (eventScheduler );
103
+ namespaces = "client namespace (" + crClient .getNamespace () + ")" ;
76
104
} else {
77
- for (String targetNamespace : targetNamespaces ) {
105
+ final Set <String > cfgNamespaces = operatorCfg .getNamespaces ();
106
+ for (String targetNamespace : cfgNamespaces ) {
78
107
crClient .inNamespace (targetNamespace ).watch (eventScheduler );
79
108
log .debug ("Registered controller for namespace: {}" , targetNamespace );
80
109
}
110
+ namespaces = String .join (", " , cfgNamespaces );
81
111
}
82
112
customResourceClients .put (resClass , (CustomResourceOperationsImpl ) client );
83
113
log .info ("Registered Controller: '{}' for CRD: '{}' for namespaces: {}" , controller .getClass ().getSimpleName (),
84
- resClass , targetNamespaces . length == 0 ? "[all/client namespace]" : Arrays . toString ( targetNamespaces ) );
114
+ resClass , namespaces );
85
115
}
86
-
116
+
87
117
private CustomResourceDefinitionContext getCustomResourceDefinitionForController (ResourceController controller ) {
88
118
String crdName = ControllerUtils .getCrdName (controller );
89
119
CustomResourceDefinition customResourceDefinition = k8sClient .customResourceDefinitions ().withName (crdName ).get ();
@@ -93,20 +123,20 @@ private CustomResourceDefinitionContext getCustomResourceDefinitionForController
93
123
CustomResourceDefinitionContext context = CustomResourceDefinitionContext .fromCrd (customResourceDefinition );
94
124
return context ;
95
125
}
96
-
126
+
97
127
public Map <Class <? extends CustomResource >, CustomResourceOperationsImpl > getCustomResourceClients () {
98
128
return customResourceClients ;
99
129
}
100
-
130
+
101
131
public <T extends CustomResource , L extends CustomResourceList <T >, D extends CustomResourceDoneable <T >> CustomResourceOperationsImpl <T , L , D >
102
132
getCustomResourceClients (Class <T > customResourceClass ) {
103
133
return customResourceClients .get (customResourceClass );
104
134
}
105
-
135
+
106
136
private String getKind (CustomResourceDefinition crd ) {
107
137
return crd .getSpec ().getNames ().getKind ();
108
138
}
109
-
139
+
110
140
private String getApiVersion (CustomResourceDefinition crd ) {
111
141
return crd .getSpec ().getGroup () + "/" + crd .getSpec ().getVersion ();
112
142
}
0 commit comments