11package io .javaoperatorsdk .operator .processing .dependent ;
22
3- import java .util .ArrayList ;
4- import java .util .List ;
53import java .util .Optional ;
64
75import org .slf4j .Logger ;
@@ -20,15 +18,15 @@ public abstract class AbstractDependentResource<R, P extends HasMetadata>
2018 implements DependentResource <R , P > {
2119 private static final Logger log = LoggerFactory .getLogger (AbstractDependentResource .class );
2220
23- protected final boolean creatable = this instanceof Creator ;
24- protected final boolean updatable = this instanceof Updater ;
25- protected final boolean bulk = this instanceof BulkDependentResource ;
21+ private final boolean creatable = this instanceof Creator ;
22+ private final boolean updatable = this instanceof Updater ;
23+ private final boolean bulk = this instanceof BulkDependentResource ;
2624
2725 protected Creator <R , P > creator ;
2826 protected Updater <R , P > updater ;
2927 protected BulkDependentResource <R , P > bulkDependentResource ;
30-
31- protected List < ResourceDiscriminator < R , P >> resourceDiscriminator = new ArrayList <>( 1 ) ;
28+ private ResourceDiscriminator < R , P > resourceDiscriminator ;
29+ private int currentCount ;
3230
3331 @ SuppressWarnings ("unchecked" )
3432 public AbstractDependentResource () {
@@ -42,48 +40,33 @@ public AbstractDependentResource() {
4240 public ReconcileResult <R > reconcile (P primary , Context <P > context ) {
4341 if (bulk ) {
4442 final var count = bulkDependentResource .count (primary , context );
45- deleteBulkResourcesIfRequired (count , lastKnownBulkSize (), primary , context );
46- adjustDiscriminators (count );
43+ deleteBulkResourcesIfRequired (count , primary , context );
4744 @ SuppressWarnings ("unchecked" )
4845 final ReconcileResult <R >[] results = new ReconcileResult [count ];
4946 for (int i = 0 ; i < count ; i ++) {
5047 results [i ] = reconcileIndexAware (primary , i , context );
5148 }
49+ currentCount = count ;
5250 return ReconcileResult .aggregatedResult (results );
5351 } else {
5452 return reconcileIndexAware (primary , 0 , context );
5553 }
5654 }
5755
58- protected void deleteBulkResourcesIfRequired (int targetCount , int actualCount , P primary ,
59- Context <P > context ) {
60- if (targetCount >= actualCount ) {
56+ protected void deleteBulkResourcesIfRequired (int targetCount , P primary , Context <P > context ) {
57+ if (targetCount >= currentCount ) {
6158 return ;
6259 }
63- for (int i = targetCount ; i < actualCount ; i ++) {
64- var resource = getSecondaryResourceIndexAware (primary , i , context );
60+ for (int i = targetCount ; i < currentCount ; i ++) {
61+ var resource = bulkDependentResource . getSecondaryResource (primary , i , context );
6562 var index = i ;
6663 resource .ifPresent (
6764 r -> bulkDependentResource .deleteBulkResourceWithIndex (primary , r , index , context ));
6865 }
6966 }
7067
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-
8568 protected ReconcileResult <R > reconcileIndexAware (P primary , int i , Context <P > context ) {
86- Optional <R > maybeActual = bulk ? getSecondaryResourceIndexAware (primary , i , context )
69+ Optional <R > maybeActual = bulk ? bulkDependentResource . getSecondaryResource (primary , i , context )
8770 : getSecondaryResource (primary , context );
8871 if (creatable || updatable ) {
8972 if (maybeActual .isEmpty ()) {
@@ -99,7 +82,7 @@ protected ReconcileResult<R> reconcileIndexAware(P primary, int i, Context<P> co
9982 if (updatable ) {
10083 final Matcher .Result <R > match ;
10184 if (bulk ) {
102- match = updater .match (actual , primary , i , context );
85+ match = bulkDependentResource .match (actual , primary , i , context );
10386 } else {
10487 match = updater .match (actual , primary , context );
10588 }
@@ -124,17 +107,12 @@ protected ReconcileResult<R> reconcileIndexAware(P primary, int i, Context<P> co
124107 }
125108
126109 private R desiredIndexAware (P primary , int i , Context <P > context ) {
127- return bulk ? desired (primary , i , context )
128- : desired (primary , context );
110+ return bulk ? desired (primary , i , context ) : desired (primary , context );
129111 }
130112
131113 public Optional <R > getSecondaryResource (P primary , Context <P > 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 ));
114+ return resourceDiscriminator == null ? context .getSecondaryResource (resourceType ())
115+ : resourceDiscriminator .distinguish (resourceType (), primary , context );
138116 }
139117
140118 private void throwIfNull (R desired , P primary , String descriptor ) {
@@ -195,28 +173,35 @@ protected R desired(P primary, Context<P> context) {
195173 }
196174
197175 protected R desired (P primary , int index , Context <P > context ) {
198- throw new IllegalStateException (
199- "Must be implemented for bulk DependentResource creation" );
176+ throw new IllegalStateException ("Must be implemented for bulk DependentResource creation" );
200177 }
201178
202- public AbstractDependentResource <R , P > setResourceDiscriminator (
203- ResourceDiscriminator <R , P > resourceDiscriminator ) {
204- if (resourceDiscriminator != null ) {
205- this .resourceDiscriminator .add (resourceDiscriminator );
179+ public void delete (P primary , Context <P > context ) {
180+ if (bulk ) {
181+ deleteBulkResourcesIfRequired (0 , primary , context );
182+ } else {
183+ handleDelete (primary , context );
206184 }
207- return this ;
208185 }
209186
210- public ResourceDiscriminator <R , P > getResourceDiscriminator () {
211- if (this .resourceDiscriminator .isEmpty ()) {
212- return null ;
213- } else {
214- return this .resourceDiscriminator .get (0 );
215- }
187+ protected void handleDelete (P primary , Context <P > context ) {
188+ throw new IllegalStateException ("delete method be implemented if Deleter trait is supported" );
216189 }
217190
218- protected int lastKnownBulkSize () {
219- return resourceDiscriminator .size ();
191+ public void setResourceDiscriminator (
192+ ResourceDiscriminator <R , P > resourceDiscriminator ) {
193+ this .resourceDiscriminator = resourceDiscriminator ;
220194 }
221195
196+ protected boolean isCreatable () {
197+ return creatable ;
198+ }
199+
200+ protected boolean isUpdatable () {
201+ return updatable ;
202+ }
203+
204+ protected boolean isBulk () {
205+ return bulk ;
206+ }
222207}
0 commit comments