1
1
package io .javaoperatorsdk .operator .api .reconciler .dependent ;
2
2
3
3
import java .util .Optional ;
4
+ import java .util .Set ;
4
5
5
6
import org .slf4j .Logger ;
6
7
import org .slf4j .LoggerFactory ;
7
8
8
9
import io .fabric8 .kubernetes .api .model .HasMetadata ;
9
10
import io .fabric8 .kubernetes .client .KubernetesClient ;
10
11
import io .javaoperatorsdk .operator .ReconcilerUtils ;
12
+ import io .javaoperatorsdk .operator .api .config .ConfigurationService ;
13
+ import io .javaoperatorsdk .operator .api .config .dependent .KubernetesDependent ;
11
14
import io .javaoperatorsdk .operator .api .config .dependent .KubernetesDependentResourceConfiguration ;
12
15
import io .javaoperatorsdk .operator .api .config .informer .InformerConfiguration ;
13
16
import io .javaoperatorsdk .operator .api .reconciler .Context ;
20
23
import io .javaoperatorsdk .operator .processing .event .source .informer .Mappers ;
21
24
22
25
public abstract class KubernetesDependentResource <R extends HasMetadata , P extends HasMetadata >
23
- extends AbstractDependentResource <R , P > implements KubernetesClientAware {
26
+ extends AbstractDependentResource <R , P , KubernetesDependentResourceConfiguration <R , P >>
27
+ implements KubernetesClientAware {
24
28
25
29
private static final Logger log = LoggerFactory .getLogger (KubernetesDependentResource .class );
26
30
27
31
protected KubernetesClient client ;
28
- private boolean explicitDelete = false ;
29
- private boolean owned = true ;
30
32
private InformerEventSource <R , P > informerEventSource ;
31
- private DesiredSupplier <R , P > desiredSupplier = null ;
32
- private Class <R > resourceType = null ;
33
- private AssociatedSecondaryResourceIdentifier <P > associatedSecondaryResourceIdentifier =
34
- ResourceID ::fromResource ;
35
- private PrimaryResourcesRetriever <R > primaryResourcesRetriever = Mappers .fromOwnerReference ();
36
-
37
- public KubernetesDependentResource () {
38
- this (null );
39
- }
40
-
41
- public KubernetesDependentResource (KubernetesClient client ) {
42
- this .client = client ;
43
- }
44
-
45
- public KubernetesDependentResource (
46
- KubernetesClient client , Class <R > resourceType , DesiredSupplier <R , P > desiredSupplier ) {
47
- this .client = client ;
48
- this .resourceType = resourceType ;
49
- this .desiredSupplier = desiredSupplier ;
50
- }
33
+ private KubernetesDependentResourceConfiguration <R , P > configuration ;
51
34
52
- public KubernetesDependentResource (
53
- Class < R > resourceType , DesiredSupplier < R , P > desiredSupplier ) {
54
- this ( null , resourceType , desiredSupplier );
35
+ public void configureWith ( KubernetesDependentResourceConfiguration < R , P > config ) {
36
+ configureWith ( config . getConfigurationService (), config . getLabelSelector (),
37
+ config . getNamespaces (), config . isOwned () );
55
38
}
56
39
57
- // todo builder and/or factory methods
58
- public void initWithConfiguration (KubernetesDependentResourceConfiguration <R , P > config ) {
59
- this .owned = config .isOwned ();
40
+ @ SuppressWarnings ("unchecked" )
41
+ private void configureWith (ConfigurationService service , String labelSelector ,
42
+ Set <String > namespaces , boolean owned ) {
43
+ final var primaryResourcesRetriever =
44
+ (this instanceof PrimaryResourcesRetriever ) ? (PrimaryResourcesRetriever <R >) this
45
+ : Mappers .fromOwnerReference ();
46
+ final AssociatedSecondaryResourceIdentifier <P > secondaryResourceIdentifier =
47
+ (this instanceof AssociatedSecondaryResourceIdentifier )
48
+ ? (AssociatedSecondaryResourceIdentifier <P >) this
49
+ : ResourceID ::fromResource ;
60
50
InformerConfiguration <R , P > ic =
61
- InformerConfiguration .from (config . getConfigurationService () , resourceType ())
62
- .withLabelSelector (config . getLabelSelector () )
63
- .withNamespaces (config . getNamespaces () )
64
- .withPrimaryResourcesRetriever (getPrimaryResourcesRetriever () )
65
- .withAssociatedSecondaryResourceIdentifier (getAssociatedSecondaryResourceIdentifier () )
51
+ InformerConfiguration .from (service , resourceType ())
52
+ .withLabelSelector (labelSelector )
53
+ .withNamespaces (namespaces )
54
+ .withPrimaryResourcesRetriever (primaryResourcesRetriever )
55
+ .withAssociatedSecondaryResourceIdentifier (secondaryResourceIdentifier )
66
56
.build ();
57
+ configuration = KubernetesDependentResourceConfiguration .from (ic , owned , getClass ());
67
58
informerEventSource = new InformerEventSource <>(ic , client );
68
59
}
69
60
70
61
protected void beforeCreateOrUpdate (R desired , P primary ) {
71
- if (owned ) {
62
+ if (configuration . isOwned () ) {
72
63
desired .addOwnerReference (primary );
73
64
}
74
65
}
@@ -100,38 +91,17 @@ protected R update(R actual, R target, P primary, Context context) {
100
91
.replace (target );
101
92
}
102
93
103
- @ SuppressWarnings ({"unchecked" , "rawtypes" })
104
94
@ Override
105
95
public Optional <EventSource > eventSource (EventSourceContext <P > context ) {
106
- if (informerEventSource != null ) {
107
- return Optional .of (informerEventSource );
96
+ if (informerEventSource == null ) {
97
+ configureWith (context .getConfigurationService (), null , null ,
98
+ KubernetesDependent .OWNED_DEFAULT );
99
+ log .warn ("Using default configuration for " + resourceType ().getSimpleName ()
100
+ + " KubernetesDependentResource, call configureWith to provide configuration" );
108
101
}
109
- var informerConfig = initDefaultInformerConfiguration (context );
110
- informerEventSource = new InformerEventSource (informerConfig , context );
111
102
return Optional .of (informerEventSource );
112
103
}
113
104
114
- @ SuppressWarnings ("unchecked" )
115
- private InformerConfiguration <R , P > initDefaultInformerConfiguration (
116
- EventSourceContext <P > context ) {
117
- return InformerConfiguration .from (context , resourceType ())
118
- .withPrimaryResourcesRetriever (getPrimaryResourcesRetriever ())
119
- .withAssociatedSecondaryResourceIdentifier (getAssociatedSecondaryResourceIdentifier ())
120
- .build ();
121
- }
122
-
123
-
124
- protected PrimaryResourcesRetriever <R > getPrimaryResourcesRetriever () {
125
- return (this instanceof PrimaryResourcesRetriever ) ? (PrimaryResourcesRetriever <R >) this
126
- : primaryResourcesRetriever ;
127
- }
128
-
129
- protected AssociatedSecondaryResourceIdentifier <P > getAssociatedSecondaryResourceIdentifier () {
130
- return (this instanceof AssociatedSecondaryResourceIdentifier )
131
- ? (AssociatedSecondaryResourceIdentifier <P >) this
132
- : associatedSecondaryResourceIdentifier ;
133
- }
134
-
135
105
public KubernetesDependentResource <R , P > setInformerEventSource (
136
106
InformerEventSource <R , P > informerEventSource ) {
137
107
this .informerEventSource = informerEventSource ;
@@ -140,10 +110,9 @@ public KubernetesDependentResource<R, P> setInformerEventSource(
140
110
141
111
@ Override
142
112
public void delete (P primary , Context context ) {
143
- if (explicitDelete ) {
144
- var resource = getResource (primary );
145
- resource .ifPresent (r -> client .resource (r ).delete ());
146
- }
113
+ // todo: do we need explicit delete? If yes, add it to configuration
114
+ var resource = getResource (primary );
115
+ resource .ifPresent (r -> client .resource (r ).delete ());
147
116
}
148
117
149
118
@ Override
@@ -155,59 +124,4 @@ public Optional<R> getResource(P primaryResource) {
155
124
public void setKubernetesClient (KubernetesClient client ) {
156
125
this .client = client ;
157
126
}
158
-
159
- public KubernetesDependentResource <R , P > setExplicitDelete (boolean explicitDelete ) {
160
- this .explicitDelete = explicitDelete ;
161
- return this ;
162
- }
163
-
164
- public boolean isExplicitDelete () {
165
- return explicitDelete ;
166
- }
167
-
168
- public boolean isOwned () {
169
- return owned ;
170
- }
171
-
172
- public KubernetesDependentResource <R , P > setOwned (boolean owned ) {
173
- this .owned = owned ;
174
- return this ;
175
- }
176
-
177
- @ Override
178
- public Class <R > resourceType () {
179
- if (resourceType != null ) {
180
- return resourceType ;
181
- } else {
182
- return super .resourceType ();
183
- }
184
- }
185
-
186
- @ Override
187
- protected R desired (P primary , Context context ) {
188
- return desiredSupplier .getDesired (primary , context );
189
- }
190
-
191
- public KubernetesDependentResource <R , P > setAssociatedSecondaryResourceIdentifier (
192
- AssociatedSecondaryResourceIdentifier <P > associatedSecondaryResourceIdentifier ) {
193
- this .associatedSecondaryResourceIdentifier = associatedSecondaryResourceIdentifier ;
194
- return this ;
195
- }
196
-
197
- public KubernetesDependentResource <R , P > setPrimaryResourcesRetriever (
198
- PrimaryResourcesRetriever <R > primaryResourcesRetriever ) {
199
- this .primaryResourcesRetriever = primaryResourcesRetriever ;
200
- return this ;
201
- }
202
-
203
- public KubernetesDependentResource <R , P > setDesiredSupplier (
204
- DesiredSupplier <R , P > desiredSupplier ) {
205
- this .desiredSupplier = desiredSupplier ;
206
- return this ;
207
- }
208
-
209
- public KubernetesDependentResource <R , P > setResourceType (Class <R > resourceType ) {
210
- this .resourceType = resourceType ;
211
- return this ;
212
- }
213
127
}
0 commit comments