1
1
package io .javaoperatorsdk .operator .processing .dependent ;
2
2
3
- import java .util .Optional ;
3
+ import java .util .* ;
4
4
5
5
import org .slf4j .Logger ;
6
6
import org .slf4j .LoggerFactory ;
@@ -26,90 +26,91 @@ public abstract class AbstractDependentResource<R, P extends HasMetadata>
26
26
protected Updater <R , P > updater ;
27
27
protected BulkDependentResource <R , P > bulkDependentResource ;
28
28
private ResourceDiscriminator <R , P > resourceDiscriminator ;
29
- private int currentCount ;
30
29
31
- @ SuppressWarnings ("unchecked" )
32
- public AbstractDependentResource () {
30
+ @ SuppressWarnings ({ "unchecked" , "rawtypes" } )
31
+ protected AbstractDependentResource () {
33
32
creator = creatable ? (Creator <R , P >) this : null ;
34
33
updater = updatable ? (Updater <R , P >) this : null ;
35
34
36
- bulkDependentResource = bulk ? (BulkDependentResource < R , P > ) this : null ;
35
+ bulkDependentResource = bulk ? (BulkDependentResource ) this : null ;
37
36
}
38
37
38
+
39
39
@ Override
40
40
public ReconcileResult <R > reconcile (P primary , Context <P > context ) {
41
41
if (bulk ) {
42
- final var count = bulkDependentResource .count (primary , context );
43
- deleteBulkResourcesIfRequired (count , primary , context );
44
- @ SuppressWarnings ("unchecked" )
45
- final ReconcileResult <R >[] results = new ReconcileResult [count ];
46
- for (int i = 0 ; i < count ; i ++) {
47
- results [i ] = reconcileIndexAware (primary , i , context );
42
+ final var targetKeys = bulkDependentResource .targetKeys (primary , context );
43
+ Map <String , R > actualResources =
44
+ bulkDependentResource .getSecondaryResources (primary , context );
45
+
46
+ deleteBulkResourcesIfRequired (targetKeys , actualResources , primary , context );
47
+ final List <ReconcileResult <R >> results = new ArrayList <>(targetKeys .size ());
48
+
49
+ for (String key : targetKeys ) {
50
+ results .add (reconcileIndexAware (primary , actualResources .get (key ), key , context ));
48
51
}
49
- currentCount = count ;
50
52
return ReconcileResult .aggregatedResult (results );
51
53
} else {
52
- return reconcileIndexAware (primary , 0 , context );
54
+ var actualResource = getSecondaryResource (primary , context );
55
+ return reconcileIndexAware (primary , actualResource .orElse (null ), null , context );
53
56
}
54
57
}
55
58
56
- protected void deleteBulkResourcesIfRequired (int targetCount , P primary , Context <P > context ) {
57
- if (targetCount >= currentCount ) {
58
- return ;
59
- }
60
- for (int i = targetCount ; i < currentCount ; i ++) {
61
- var resource = bulkDependentResource .getSecondaryResource (primary , i , context );
62
- var index = i ;
63
- resource .ifPresent (
64
- r -> bulkDependentResource .deleteBulkResourceWithIndex (primary , r , index , context ));
65
- }
59
+ @ SuppressWarnings ({"rawtypes" })
60
+ protected void deleteBulkResourcesIfRequired (Set targetKeys , Map <String , R > actualResources ,
61
+ P primary , Context <P > context ) {
62
+ actualResources .forEach ((key , value ) -> {
63
+ if (!targetKeys .contains (key )) {
64
+ bulkDependentResource .deleteBulkResource (primary , value , key , context );
65
+ }
66
+ });
66
67
}
67
68
68
- protected ReconcileResult <R > reconcileIndexAware (P primary , int i , Context <P > context ) {
69
- Optional <R > maybeActual = bulk ? bulkDependentResource .getSecondaryResource (primary , i , context )
70
- : getSecondaryResource (primary , context );
69
+ protected ReconcileResult <R > reconcileIndexAware (P primary , R resource , String key ,
70
+ Context <P > context ) {
71
71
if (creatable || updatable ) {
72
- if (maybeActual . isEmpty () ) {
72
+ if (resource == null ) {
73
73
if (creatable ) {
74
- var desired = desiredIndexAware (primary , i , context );
74
+ var desired = desiredIndexAware (primary , key , context );
75
75
throwIfNull (desired , primary , "Desired" );
76
76
logForOperation ("Creating" , primary , desired );
77
77
var createdResource = handleCreate (desired , primary , context );
78
78
return ReconcileResult .resourceCreated (createdResource );
79
79
}
80
80
} else {
81
- final var actual = maybeActual .get ();
82
81
if (updatable ) {
83
82
final Matcher .Result <R > match ;
84
83
if (bulk ) {
85
- match = bulkDependentResource .match (actual , primary , i , context );
84
+ match = bulkDependentResource .match (resource , primary , key , context );
86
85
} else {
87
- match = updater .match (actual , primary , context );
86
+ match = updater .match (resource , primary , context );
88
87
}
89
88
if (!match .matched ()) {
90
89
final var desired =
91
- match .computedDesired ().orElse (desiredIndexAware (primary , i , context ));
90
+ match .computedDesired ().orElse (desiredIndexAware (primary , key , context ));
92
91
throwIfNull (desired , primary , "Desired" );
93
92
logForOperation ("Updating" , primary , desired );
94
- var updatedResource = handleUpdate (actual , desired , primary , context );
93
+ var updatedResource = handleUpdate (resource , desired , primary , context );
95
94
return ReconcileResult .resourceUpdated (updatedResource );
96
95
}
97
96
} else {
98
- log .debug ("Update skipped for dependent {} as it matched the existing one" , actual );
97
+ log .debug ("Update skipped for dependent {} as it matched the existing one" , resource );
99
98
}
100
99
}
101
100
} else {
102
101
log .debug (
103
102
"Dependent {} is read-only, implement Creator and/or Updater interfaces to modify it" ,
104
103
getClass ().getSimpleName ());
105
104
}
106
- return ReconcileResult .noOperation (maybeActual . orElse ( null ) );
105
+ return ReconcileResult .noOperation (resource );
107
106
}
108
107
109
- private R desiredIndexAware (P primary , int i , Context <P > context ) {
110
- return bulk ? desired (primary , i , context ) : desired (primary , context );
108
+ private R desiredIndexAware (P primary , String key , Context <P > context ) {
109
+ return bulk ? bulkDependentResource .desired (primary , key , context )
110
+ : desired (primary , context );
111
111
}
112
112
113
+ @ Override
113
114
public Optional <R > getSecondaryResource (P primary , Context <P > context ) {
114
115
return resourceDiscriminator == null ? context .getSecondaryResource (resourceType ())
115
116
: resourceDiscriminator .distinguish (resourceType (), primary , context );
@@ -172,13 +173,10 @@ protected R desired(P primary, Context<P> context) {
172
173
"desired method must be implemented if this DependentResource can be created and/or updated" );
173
174
}
174
175
175
- protected R desired (P primary , int index , Context <P > context ) {
176
- throw new IllegalStateException ("Must be implemented for bulk DependentResource creation" );
177
- }
178
-
179
176
public void delete (P primary , Context <P > context ) {
180
177
if (bulk ) {
181
- deleteBulkResourcesIfRequired (0 , primary , context );
178
+ var actualResources = bulkDependentResource .getSecondaryResources (primary , context );
179
+ deleteBulkResourcesIfRequired (Collections .emptySet (), actualResources , primary , context );
182
180
} else {
183
181
handleDelete (primary , context );
184
182
}
0 commit comments