11package io .javaoperatorsdk .operator .processing .dependent ;
22
3+ import java .util .ArrayList ;
4+ import java .util .List ;
35import java .util .Optional ;
46
57import org .slf4j .Logger ;
@@ -18,15 +20,15 @@ public abstract class AbstractDependentResource<R, P extends HasMetadata>
1820 implements DependentResource <R , P > {
1921 private static final Logger log = LoggerFactory .getLogger (AbstractDependentResource .class );
2022
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 ;
2426
2527 protected Creator <R , P > creator ;
2628 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 ) ;
3032
3133 @ SuppressWarnings ("unchecked" )
3234 public AbstractDependentResource () {
@@ -40,33 +42,48 @@ public AbstractDependentResource() {
4042 public ReconcileResult <R > reconcile (P primary , Context <P > context ) {
4143 if (bulk ) {
4244 final var count = bulkDependentResource .count (primary , context );
43- deleteBulkResourcesIfRequired (count , primary , context );
45+ deleteBulkResourcesIfRequired (count , lastKnownBulkSize (), primary , context );
46+ adjustDiscriminators (count );
4447 @ SuppressWarnings ("unchecked" )
4548 final ReconcileResult <R >[] results = new ReconcileResult [count ];
4649 for (int i = 0 ; i < count ; i ++) {
4750 results [i ] = reconcileIndexAware (primary , i , context );
4851 }
49- currentCount = count ;
5052 return ReconcileResult .aggregatedResult (results );
5153 } else {
5254 return reconcileIndexAware (primary , 0 , context );
5355 }
5456 }
5557
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 ) {
5861 return ;
5962 }
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 );
6265 var index = i ;
6366 resource .ifPresent (
6467 r -> bulkDependentResource .deleteBulkResourceWithIndex (primary , r , index , context ));
6568 }
6669 }
6770
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+
6885 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 )
7087 : getSecondaryResource (primary , context );
7188 if (creatable || updatable ) {
7289 if (maybeActual .isEmpty ()) {
@@ -82,7 +99,7 @@ protected ReconcileResult<R> reconcileIndexAware(P primary, int i, Context<P> co
8299 if (updatable ) {
83100 final Matcher .Result <R > match ;
84101 if (bulk ) {
85- match = bulkDependentResource .match (actual , primary , i , context );
102+ match = updater .match (actual , primary , i , context );
86103 } else {
87104 match = updater .match (actual , primary , context );
88105 }
@@ -107,12 +124,17 @@ protected ReconcileResult<R> reconcileIndexAware(P primary, int i, Context<P> co
107124 }
108125
109126 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 );
111129 }
112130
113131 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 ));
116138 }
117139
118140 private void throwIfNull (R desired , P primary , String descriptor ) {
@@ -173,35 +195,28 @@ protected R desired(P primary, Context<P> context) {
173195 }
174196
175197 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" );
189200 }
190201
191- public void setResourceDiscriminator (
202+ public AbstractDependentResource < R , P > setResourceDiscriminator (
192203 ResourceDiscriminator <R , P > resourceDiscriminator ) {
193- this .resourceDiscriminator = resourceDiscriminator ;
204+ if (resourceDiscriminator != null ) {
205+ this .resourceDiscriminator .add (resourceDiscriminator );
206+ }
207+ return this ;
194208 }
195209
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+ }
198216 }
199217
200- protected boolean isUpdatable () {
201- return updatable ;
218+ protected int lastKnownBulkSize () {
219+ return resourceDiscriminator . size () ;
202220 }
203221
204- protected boolean isBulk () {
205- return bulk ;
206- }
207222}
0 commit comments