1818import java .time .Duration ;
1919import java .util .List ;
2020import java .util .Map ;
21+ import java .util .concurrent .atomic .AtomicReference ;
2122
2223import io .fabric8 .kubernetes .api .model .ContainerBuilder ;
2324import io .fabric8 .kubernetes .api .model .ContainerPortBuilder ;
@@ -45,17 +46,21 @@ public class ExpectationReconciler implements Reconciler<ExpectationCustomResour
4546 public static final String DEPLOYMENT_READY = "Deployment ready" ;
4647 public static final String DEPLOYMENT_TIMEOUT = "Deployment timeout" ;
4748 public static final String DEPLOYMENT_READY_EXPECTATION_NAME = "deploymentReadyExpectation" ;
49+
4850 private final ExpectationManager <ExpectationCustomResource > expectationManager =
4951 new ExpectationManager <>();
52+ private final AtomicReference <Duration > timeoutRef =
53+ new AtomicReference <>(Duration .ofSeconds (30 ));
5054
51- private volatile Long timeout = 30000l ;
55+ public void setTimeout (Duration timeout ) {
56+ timeoutRef .set (timeout );
57+ }
5258
5359 @ Override
5460 public UpdateControl <ExpectationCustomResource > reconcile (
5561 ExpectationCustomResource primary , Context <ExpectationCustomResource > context ) {
56-
5762 // cleans up expectation manager for the resource on delete event
58- // in case of cleaner interface used, this can done also there.
63+ // in case of cleaner interface used, this can be done also there
5964 if (context .isPrimaryResourceDeleted ()) {
6065 expectationManager .removeExpectation (primary );
6166 }
@@ -70,53 +75,41 @@ public UpdateControl<ExpectationCustomResource> reconcile(
7075 createDeployment (primary , context );
7176 var set =
7277 expectationManager .checkAndSetExpectation (
73- primary , context , Duration . ofSeconds ( timeout ), deploymentReadyExpectation (context ));
78+ primary , context , timeoutRef . get ( ), deploymentReadyExpectation ());
7479 if (set ) {
7580 return UpdateControl .noUpdate ();
7681 }
7782 } else {
78- // checks the expectation if it is fulfilled also removes it,
79- // in your logic you might add a next expectation based on your workflow
80- // Expectations have a name, so you can easily distinguish them if there is more of them .
83+ // Checks the expectation and removes it once it is fulfilled.
84+ // In your logic you might add a next expectation based on your workflow.
85+ // Expectations have a name, so you can easily distinguish multiple expectations .
8186 var res =
8287 expectationManager .checkExpectation (DEPLOYMENT_READY_EXPECTATION_NAME , primary , context );
83- // Note that this happens only once, since if the expectation is fulfilled, it is also removed
84- // from the manager.
88+ // note that this happens only once, since if the expectation is fulfilled, it is also removed
89+ // from the manager
8590 if (res .isFulfilled ()) {
86- return pathchStatusWithMessage (primary , DEPLOYMENT_READY );
91+ return patchStatusWithMessage (primary , DEPLOYMENT_READY );
8792 } else if (res .isTimedOut ()) {
8893 // you might add some other timeout handling here
89- return pathchStatusWithMessage (primary , DEPLOYMENT_TIMEOUT );
94+ return patchStatusWithMessage (primary , DEPLOYMENT_TIMEOUT );
9095 }
9196 }
9297 return UpdateControl .noUpdate ();
9398 }
9499
95- private static UpdateControl <ExpectationCustomResource > pathchStatusWithMessage (
96- ExpectationCustomResource primary , String message ) {
97- primary .setStatus (new ExpectationCustomResourceStatus ());
98- primary .getStatus ().setMessage (message );
99- return UpdateControl .patchStatus (primary );
100- }
101-
102- private static Expectation <ExpectationCustomResource > deploymentReadyExpectation (
103- Context <ExpectationCustomResource > context ) {
104- return Expectation .createExpectation (
105- DEPLOYMENT_READY_EXPECTATION_NAME ,
106- (p , c ) ->
107- context
108- .getSecondaryResource (Deployment .class )
109- .map (
110- ad ->
111- ad .getStatus () != null
112- && ad .getStatus ().getReadyReplicas () != null
113- && ad .getStatus ().getReadyReplicas () == 3 )
114- .orElse (false ));
100+ @ Override
101+ public List <EventSource <?, ExpectationCustomResource >> prepareEventSources (
102+ EventSourceContext <ExpectationCustomResource > context ) {
103+ return List .of (
104+ new InformerEventSource <>(
105+ InformerEventSourceConfiguration .from (Deployment .class , ExpectationCustomResource .class )
106+ .build (),
107+ context ));
115108 }
116109
117- private Deployment createDeployment (
110+ private static void createDeployment (
118111 ExpectationCustomResource primary , Context <ExpectationCustomResource > context ) {
119- var d =
112+ var deployment =
120113 new DeploymentBuilder ()
121114 .withMetadata (
122115 new ObjectMetaBuilder ()
@@ -147,21 +140,28 @@ private Deployment createDeployment(
147140 .build ())
148141 .build ())
149142 .build ();
150- d .addOwnerReference (primary );
151- return context .getClient ().resource (d ).serverSideApply ();
143+ deployment .addOwnerReference (primary );
144+ context .getClient ().resource (deployment ).serverSideApply ();
152145 }
153146
154- @ Override
155- public List <EventSource <?, ExpectationCustomResource >> prepareEventSources (
156- EventSourceContext <ExpectationCustomResource > context ) {
157- return List .of (
158- new InformerEventSource <>(
159- InformerEventSourceConfiguration .from (Deployment .class , ExpectationCustomResource .class )
160- .build (),
161- context ));
147+ private static Expectation <ExpectationCustomResource > deploymentReadyExpectation () {
148+ return Expectation .createExpectation (
149+ DEPLOYMENT_READY_EXPECTATION_NAME ,
150+ (primary , context ) ->
151+ context
152+ .getSecondaryResource (Deployment .class )
153+ .map (
154+ ad ->
155+ ad .getStatus () != null
156+ && ad .getStatus ().getReadyReplicas () != null
157+ && ad .getStatus ().getReadyReplicas () == 3 )
158+ .orElse (false ));
162159 }
163160
164- public void setTimeout (Long timeout ) {
165- this .timeout = timeout ;
161+ private static UpdateControl <ExpectationCustomResource > patchStatusWithMessage (
162+ ExpectationCustomResource primary , String message ) {
163+ primary .setStatus (new ExpectationCustomResourceStatus ());
164+ primary .getStatus ().setMessage (message );
165+ return UpdateControl .patchStatus (primary );
166166 }
167167}
0 commit comments