1
1
package io .javaoperatorsdk .operator .processing .dependent ;
2
2
3
+ import java .util .ArrayList ;
4
+ import java .util .List ;
3
5
import java .util .Optional ;
4
6
5
7
import org .slf4j .Logger ;
@@ -18,15 +20,15 @@ public abstract class AbstractDependentResource<R, P extends HasMetadata>
18
20
implements DependentResource <R , P > {
19
21
private static final Logger log = LoggerFactory .getLogger (AbstractDependentResource .class );
20
22
21
- private final boolean creatable = this instanceof Creator ;
22
- private final boolean updatable = this instanceof Updater ;
23
- private final boolean bulk = this instanceof BulkDependentResource ;
23
+ protected final boolean creatable = this instanceof Creator ;
24
+ protected final boolean updatable = this instanceof Updater ;
25
+ protected final boolean bulk = this instanceof BulkDependentResource ;
24
26
25
27
protected Creator <R , P > creator ;
26
28
protected Updater <R , P > updater ;
27
- private final BulkDependentResource <R , P > bulkDependentResource ;
28
- private ResourceDiscriminator < R , P > resourceDiscriminator ;
29
- private int currentCount ;
29
+ protected BulkDependentResource <R , P > bulkDependentResource ;
30
+
31
+ private final List < ResourceDiscriminator < R , P >> resourceDiscriminator = new ArrayList <>( 1 ) ;
30
32
31
33
@ SuppressWarnings ("unchecked" )
32
34
public AbstractDependentResource () {
@@ -40,33 +42,48 @@ public AbstractDependentResource() {
40
42
public ReconcileResult <R > reconcile (P primary , Context <P > context ) {
41
43
if (bulk ) {
42
44
final var count = bulkDependentResource .count (primary , context );
43
- deleteBulkResourcesIfRequired (count , primary , context );
45
+ deleteBulkResourcesIfRequired (count , lastKnownBulkSize (), primary , context );
46
+ adjustDiscriminators (count );
44
47
@ SuppressWarnings ("unchecked" )
45
48
final ReconcileResult <R >[] results = new ReconcileResult [count ];
46
49
for (int i = 0 ; i < count ; i ++) {
47
50
results [i ] = reconcileIndexAware (primary , i , context );
48
51
}
49
- currentCount = count ;
50
52
return ReconcileResult .aggregatedResult (results );
51
53
} else {
52
54
return reconcileIndexAware (primary , 0 , context );
53
55
}
54
56
}
55
57
56
- protected void deleteBulkResourcesIfRequired (int targetCount , P primary , Context <P > context ) {
57
- if (targetCount >= currentCount ) {
58
+ protected void deleteBulkResourcesIfRequired (int targetCount , int actualCount , P primary ,
59
+ Context <P > context ) {
60
+ if (targetCount >= actualCount ) {
58
61
return ;
59
62
}
60
- for (int i = targetCount ; i < currentCount ; i ++) {
61
- var resource = bulkDependentResource . getSecondaryResource (primary , i , context );
63
+ for (int i = targetCount ; i < actualCount ; i ++) {
64
+ var resource = getSecondaryResourceIndexAware (primary , i , context );
62
65
var index = i ;
63
66
resource .ifPresent (
64
67
r -> bulkDependentResource .deleteBulkResourceWithIndex (primary , r , index , context ));
65
68
}
66
69
}
67
70
71
+ private void adjustDiscriminators (int count ) {
72
+ if (resourceDiscriminator .size () == count ) {
73
+ return ;
74
+ }
75
+ if (resourceDiscriminator .size () < count ) {
76
+ for (int i = resourceDiscriminator .size (); i < count ; i ++) {
77
+ resourceDiscriminator .add (bulkDependentResource .getResourceDiscriminator (i ));
78
+ }
79
+ }
80
+ if (resourceDiscriminator .size () > count ) {
81
+ resourceDiscriminator .subList (count , resourceDiscriminator .size ()).clear ();
82
+ }
83
+ }
84
+
68
85
protected ReconcileResult <R > reconcileIndexAware (P primary , int i , Context <P > context ) {
69
- Optional <R > maybeActual = bulk ? bulkDependentResource . getSecondaryResource (primary , i , context )
86
+ Optional <R > maybeActual = bulk ? getSecondaryResourceIndexAware (primary , i , context )
70
87
: getSecondaryResource (primary , context );
71
88
if (creatable || updatable ) {
72
89
if (maybeActual .isEmpty ()) {
@@ -82,7 +99,7 @@ protected ReconcileResult<R> reconcileIndexAware(P primary, int i, Context<P> co
82
99
if (updatable ) {
83
100
final Matcher .Result <R > match ;
84
101
if (bulk ) {
85
- match = bulkDependentResource .match (actual , primary , i , context );
102
+ match = updater .match (actual , primary , i , context );
86
103
} else {
87
104
match = updater .match (actual , primary , context );
88
105
}
@@ -107,12 +124,17 @@ protected ReconcileResult<R> reconcileIndexAware(P primary, int i, Context<P> co
107
124
}
108
125
109
126
private R desiredIndexAware (P primary , int i , Context <P > context ) {
110
- return bulk ? desired (primary , i , context ) : desired (primary , context );
127
+ return bulk ? desired (primary , i , context )
128
+ : desired (primary , context );
111
129
}
112
130
113
131
public Optional <R > getSecondaryResource (P primary , Context <P > context ) {
114
- return resourceDiscriminator == null ? context .getSecondaryResource (resourceType ())
115
- : resourceDiscriminator .distinguish (resourceType (), primary , context );
132
+ return resourceDiscriminator .isEmpty () ? context .getSecondaryResource (resourceType ())
133
+ : resourceDiscriminator .get (0 ).distinguish (resourceType (), primary , context );
134
+ }
135
+
136
+ protected Optional <R > getSecondaryResourceIndexAware (P primary , int index , Context <P > context ) {
137
+ return context .getSecondaryResource (resourceType (), resourceDiscriminator .get (index ));
116
138
}
117
139
118
140
private void throwIfNull (R desired , P primary , String descriptor ) {
@@ -173,35 +195,28 @@ protected R desired(P primary, Context<P> context) {
173
195
}
174
196
175
197
protected R desired (P primary , int index , Context <P > context ) {
176
- throw new IllegalStateException ("Must be implemented for bulk DependentResource creation" );
177
- }
178
-
179
- public void delete (P primary , Context <P > context ) {
180
- if (bulk ) {
181
- deleteBulkResourcesIfRequired (0 , primary , context );
182
- } else {
183
- handleDelete (primary , context );
184
- }
185
- }
186
-
187
- protected void handleDelete (P primary , Context <P > context ) {
188
- throw new IllegalStateException ("delete method be implemented if Deleter trait is supported" );
198
+ throw new IllegalStateException (
199
+ "Must be implemented for bulk DependentResource creation" );
189
200
}
190
201
191
- public void setResourceDiscriminator (
202
+ public AbstractDependentResource < R , P > setResourceDiscriminator (
192
203
ResourceDiscriminator <R , P > resourceDiscriminator ) {
193
- this .resourceDiscriminator = resourceDiscriminator ;
204
+ if (resourceDiscriminator != null ) {
205
+ this .resourceDiscriminator .add (resourceDiscriminator );
206
+ }
207
+ return this ;
194
208
}
195
209
196
- protected boolean isCreatable () {
197
- return creatable ;
210
+ public ResourceDiscriminator <R , P > getResourceDiscriminator () {
211
+ if (this .resourceDiscriminator .isEmpty ()) {
212
+ return null ;
213
+ } else {
214
+ return this .resourceDiscriminator .get (0 );
215
+ }
198
216
}
199
217
200
- protected boolean isUpdatable () {
201
- return updatable ;
218
+ protected int lastKnownBulkSize () {
219
+ return resourceDiscriminator . size () ;
202
220
}
203
221
204
- protected boolean isBulk () {
205
- return bulk ;
206
- }
207
222
}
0 commit comments