1
1
package io .javaoperatorsdk .operator .processing .dependent ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .List ;
5
3
import java .util .Optional ;
6
4
7
5
import org .slf4j .Logger ;
@@ -22,16 +20,16 @@ public abstract class AbstractDependentResource<R, P extends HasMetadata>
22
20
implements DependentResource <R , P > {
23
21
private static final Logger log = LoggerFactory .getLogger (AbstractDependentResource .class );
24
22
25
- protected final boolean creatable = this instanceof Creator ;
26
- protected final boolean updatable = this instanceof Updater ;
27
- protected final boolean bulk = this instanceof BulkDependentResource ;
23
+ private final boolean creatable = this instanceof Creator ;
24
+ private final boolean updatable = this instanceof Updater ;
25
+ private final boolean bulk = this instanceof BulkDependentResource ;
28
26
29
27
protected Creator <R , P > creator ;
30
28
protected Updater <R , P > updater ;
31
29
protected BulkDependentResource <R , P > bulkDependentResource ;
32
30
private boolean returnEventSource = true ;
33
-
34
- protected List < ResourceDiscriminator < R , P >> resourceDiscriminator = new ArrayList <>( 1 ) ;
31
+ private ResourceDiscriminator < R , P > resourceDiscriminator ;
32
+ private int currentCount ;
35
33
36
34
@ SuppressWarnings ("unchecked" )
37
35
public AbstractDependentResource () {
@@ -62,48 +60,33 @@ protected abstract ResourceEventSource<R, P> provideEventSource(
62
60
public ReconcileResult <R > reconcile (P primary , Context <P > context ) {
63
61
if (bulk ) {
64
62
final var count = bulkDependentResource .count (primary , context );
65
- deleteBulkResourcesIfRequired (count , lastKnownBulkSize (), primary , context );
66
- adjustDiscriminators (count );
63
+ deleteBulkResourcesIfRequired (count , primary , context );
67
64
@ SuppressWarnings ("unchecked" )
68
65
final ReconcileResult <R >[] results = new ReconcileResult [count ];
69
66
for (int i = 0 ; i < count ; i ++) {
70
67
results [i ] = reconcileIndexAware (primary , i , context );
71
68
}
69
+ currentCount = count ;
72
70
return ReconcileResult .aggregatedResult (results );
73
71
} else {
74
72
return reconcileIndexAware (primary , 0 , context );
75
73
}
76
74
}
77
75
78
- protected void deleteBulkResourcesIfRequired (int targetCount , int actualCount , P primary ,
79
- Context <P > context ) {
80
- if (targetCount >= actualCount ) {
76
+ protected void deleteBulkResourcesIfRequired (int targetCount , P primary , Context <P > context ) {
77
+ if (targetCount >= currentCount ) {
81
78
return ;
82
79
}
83
- for (int i = targetCount ; i < actualCount ; i ++) {
84
- var resource = getSecondaryResourceIndexAware (primary , i , context );
80
+ for (int i = targetCount ; i < currentCount ; i ++) {
81
+ var resource = bulkDependentResource . getSecondaryResource (primary , i , context );
85
82
var index = i ;
86
83
resource .ifPresent (
87
84
r -> bulkDependentResource .deleteBulkResourceWithIndex (primary , r , index , context ));
88
85
}
89
86
}
90
87
91
- private void adjustDiscriminators (int count ) {
92
- if (resourceDiscriminator .size () == count ) {
93
- return ;
94
- }
95
- if (resourceDiscriminator .size () < count ) {
96
- for (int i = resourceDiscriminator .size (); i < count ; i ++) {
97
- resourceDiscriminator .add (bulkDependentResource .getResourceDiscriminator (i ));
98
- }
99
- }
100
- if (resourceDiscriminator .size () > count ) {
101
- resourceDiscriminator .subList (count , resourceDiscriminator .size ()).clear ();
102
- }
103
- }
104
-
105
88
protected ReconcileResult <R > reconcileIndexAware (P primary , int i , Context <P > context ) {
106
- Optional <R > maybeActual = bulk ? getSecondaryResourceIndexAware (primary , i , context )
89
+ Optional <R > maybeActual = bulk ? bulkDependentResource . getSecondaryResource (primary , i , context )
107
90
: getSecondaryResource (primary , context );
108
91
if (creatable || updatable ) {
109
92
if (maybeActual .isEmpty ()) {
@@ -119,7 +102,7 @@ protected ReconcileResult<R> reconcileIndexAware(P primary, int i, Context<P> co
119
102
if (updatable ) {
120
103
final Matcher .Result <R > match ;
121
104
if (bulk ) {
122
- match = updater .match (actual , primary , i , context );
105
+ match = bulkDependentResource .match (actual , primary , i , context );
123
106
} else {
124
107
match = updater .match (actual , primary , context );
125
108
}
@@ -144,17 +127,12 @@ protected ReconcileResult<R> reconcileIndexAware(P primary, int i, Context<P> co
144
127
}
145
128
146
129
private R desiredIndexAware (P primary , int i , Context <P > context ) {
147
- return bulk ? desired (primary , i , context )
148
- : desired (primary , context );
130
+ return bulk ? desired (primary , i , context ) : desired (primary , context );
149
131
}
150
132
151
133
public Optional <R > getSecondaryResource (P primary , Context <P > context ) {
152
- return resourceDiscriminator .isEmpty () ? context .getSecondaryResource (resourceType ())
153
- : resourceDiscriminator .get (0 ).distinguish (resourceType (), primary , context );
154
- }
155
-
156
- protected Optional <R > getSecondaryResourceIndexAware (P primary , int index , Context <P > context ) {
157
- return context .getSecondaryResource (resourceType (), resourceDiscriminator .get (index ));
134
+ return resourceDiscriminator == null ? context .getSecondaryResource (resourceType ())
135
+ : resourceDiscriminator .distinguish (resourceType (), primary , context );
158
136
}
159
137
160
138
private void throwIfNull (R desired , P primary , String descriptor ) {
@@ -192,7 +170,7 @@ protected R handleCreate(R desired, P primary, Context<P> context) {
192
170
protected abstract void onCreated (ResourceID primaryResourceId , R created );
193
171
194
172
/**
195
- * Allows subclasses to perform additional processing on the updated resource if needed.
173
+ * Allows sub-classes to perform additional processing on the updated resource if needed.
196
174
*
197
175
* @param primaryResourceId the {@link ResourceID} of the primary resource associated with the
198
176
* newly updated resource
@@ -215,28 +193,36 @@ protected R desired(P primary, Context<P> context) {
215
193
}
216
194
217
195
protected R desired (P primary , int index , Context <P > context ) {
218
- throw new IllegalStateException (
219
- "Must be implemented for bulk DependentResource creation" );
196
+ throw new IllegalStateException ("Must be implemented for bulk DependentResource creation" );
220
197
}
221
198
222
- public AbstractDependentResource <R , P > setResourceDiscriminator (
223
- ResourceDiscriminator <R , P > resourceDiscriminator ) {
224
- if (resourceDiscriminator != null ) {
225
- this .resourceDiscriminator .add (resourceDiscriminator );
199
+ public void delete (P primary , Context <P > context ) {
200
+ if (bulk ) {
201
+ deleteBulkResourcesIfRequired (0 , primary , context );
202
+ } else {
203
+ handleDelete (primary , context );
226
204
}
227
- return this ;
228
205
}
229
206
230
- public ResourceDiscriminator <R , P > getResourceDiscriminator () {
231
- if (this .resourceDiscriminator .isEmpty ()) {
232
- return null ;
233
- } else {
234
- return this .resourceDiscriminator .get (0 );
235
- }
207
+ protected void handleDelete (P primary , Context <P > context ) {
208
+ throw new IllegalStateException ("delete method be implemented if Deleter trait is supported" );
209
+ }
210
+
211
+ public void setResourceDiscriminator (
212
+ ResourceDiscriminator <R , P > resourceDiscriminator ) {
213
+ this .resourceDiscriminator = resourceDiscriminator ;
214
+ }
215
+
216
+ protected boolean isCreatable () {
217
+ return creatable ;
218
+ }
219
+
220
+ protected boolean isUpdatable () {
221
+ return updatable ;
236
222
}
237
223
238
- protected int lastKnownBulkSize () {
239
- return resourceDiscriminator . size () ;
224
+ protected boolean isBulk () {
225
+ return bulk ;
240
226
}
241
227
242
228
protected boolean getReturnEventSource () {
0 commit comments