44import java .util .ArrayList ;
55import java .util .List ;
66
7- import org .slf4j .Logger ;
8- import org .slf4j .LoggerFactory ;
9-
107import io .fabric8 .kubernetes .api .model .HasMetadata ;
118import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
129import io .javaoperatorsdk .operator .api .config .dependent .DependentResourceConfiguration ;
2623
2724@ SuppressWarnings ({"rawtypes" , "unchecked" })
2825@ Ignore
29- public class DependentResourceManager <R extends HasMetadata > implements EventSourceInitializer <R >,
30- EventSourceContextInjector , Reconciler <R > {
31-
32- private static final Logger log = LoggerFactory .getLogger (DependentResourceManager .class );
33-
34- private final Reconciler <R > reconciler ;
35- private final ControllerConfiguration <R > configuration ;
26+ public class DependentResourceManager <P extends HasMetadata > implements EventSourceInitializer <P >,
27+ EventSourceContextInjector , Reconciler <P > {
28+ private final Reconciler <P > reconciler ;
29+ private final ControllerConfiguration <P > configuration ;
3630 private List <DependentResourceController > dependents ;
3731
3832
39- public DependentResourceManager (Controller <R > controller ) {
33+ public DependentResourceManager (Controller <P > controller ) {
4034 this .reconciler = controller .getReconciler ();
4135 this .configuration = controller .getConfiguration ();
4236 }
4337
4438 @ Override
45- public List <EventSource > prepareEventSources (EventSourceContext <R > context ) {
39+ public List <EventSource > prepareEventSources (EventSourceContext <P > context ) {
4640 final List <DependentResourceConfiguration > configured = configuration .getDependentResources ();
4741 dependents = new ArrayList <>(configured .size ());
4842
@@ -65,71 +59,23 @@ public void injectInto(EventSourceContext context) {
6559 }
6660
6761 @ Override
68- public UpdateControl <R > reconcile (R resource , Context context ) {
62+ public UpdateControl <P > reconcile (P resource , Context context ) {
6963 initContextIfNeeded (resource , context );
70-
71- dependents .forEach (dependent -> {
72- var actual = dependent .getFor (resource , context );
73- if (actual == null || !dependent .match (actual , resource , context )) {
74- final var desired = dependent .desired (resource , context );
75- if (desired != null ) {
76- createOrReplaceDependent (resource , context , dependent , desired );
77- }
78- }
79- });
80-
64+ dependents .forEach (dependent -> dependent .reconcile (resource , context ));
8165 return UpdateControl .noUpdate ();
8266 }
8367
8468 @ Override
85- public DeleteControl cleanup (R resource , Context context ) {
69+ public DeleteControl cleanup (P resource , Context context ) {
8670 initContextIfNeeded (resource , context );
87-
88- dependents .forEach (dependent -> {
89- var dependentResource = dependent .getFor (resource , context );
90- if (dependentResource != null ) {
91- dependent .delete (dependentResource , resource , context );
92- logOperationInfo (resource , dependent , dependentResource , "Deleting" );
93- } else {
94- log .info ("Ignoring already deleted {} for '{}' {}" ,
95- dependent .getResourceType ().getName (),
96- resource .getMetadata ().getName (),
97- configuration .getResourceTypeName ());
98- }
99- });
100-
71+ dependents .forEach (dependent -> dependent .cleanup (resource , context ));
10172 return Reconciler .super .cleanup (resource , context );
10273 }
10374
104- private void createOrReplaceDependent (R primaryResource ,
105- Context context , DependentResourceController dependentController ,
106- Object dependentResource ) {
107- // add owner reference if needed
108- if (dependentResource instanceof HasMetadata
109- && ((KubernetesDependentResourceController ) dependentController ).owned ()) {
110- ((HasMetadata ) dependentResource ).addOwnerReference (primaryResource );
111- }
112-
113- logOperationInfo (primaryResource , dependentController , dependentResource , "Reconciling" );
114-
115- // commit the changes
116- // todo: add metrics timing for dependent resource
117- dependentController .createOrReplace (dependentResource , context );
118- }
119-
120- private void logOperationInfo (R resource , DependentResourceController dependent ,
121- Object dependentResource , String operationDescription ) {
122- if (log .isInfoEnabled ()) {
123- log .info ("{} {} for '{}' {}" , operationDescription ,
124- dependent .descriptionFor (dependentResource ),
125- resource .getMetadata ().getName (),
126- configuration .getResourceTypeName ());
127- }
128- }
12975
130- private void initContextIfNeeded (R resource , Context context ) {
76+ private void initContextIfNeeded (P resource , Context context ) {
13177 if (reconciler instanceof ContextInitializer ) {
132- final var initializer = (ContextInitializer <R >) reconciler ;
78+ final var initializer = (ContextInitializer <P >) reconciler ;
13379 initializer .initContext (resource , context );
13480 }
13581 }
0 commit comments