33
44package oracle .kubernetes .operator .helpers ;
55
6+ import java .time .OffsetDateTime ;
67import java .util .ArrayList ;
78import java .util .Collections ;
89import java .util .List ;
2122import io .kubernetes .client .openapi .models .V1PodSpec ;
2223import io .kubernetes .client .openapi .models .V1Volume ;
2324import io .kubernetes .client .openapi .models .V1VolumeMount ;
25+ import oracle .kubernetes .operator .DomainProcessorImpl ;
2426import oracle .kubernetes .operator .DomainStatusUpdater ;
2527import oracle .kubernetes .operator .IntrospectorConfigMapConstants ;
2628import oracle .kubernetes .operator .JobWatcher ;
4951import oracle .kubernetes .weblogic .domain .model .ManagedServer ;
5052import oracle .kubernetes .weblogic .domain .model .ServerEnvVars ;
5153
54+ import static java .time .temporal .ChronoUnit .SECONDS ;
5255import static oracle .kubernetes .operator .DomainSourceType .FromModel ;
5356import static oracle .kubernetes .operator .DomainStatusUpdater .INSPECTING_DOMAIN_PROGRESS_REASON ;
5457import static oracle .kubernetes .operator .DomainStatusUpdater .createProgressingStartedEventStep ;
58+ import static oracle .kubernetes .operator .LabelConstants .INTROSPECTION_DOMAIN_SPEC_GENERATION ;
59+ import static oracle .kubernetes .operator .LabelConstants .INTROSPECTION_STATE_LABEL ;
5560import static oracle .kubernetes .operator .logging .MessageKeys .INTROSPECTOR_JOB_FAILED ;
5661import static oracle .kubernetes .operator .logging .MessageKeys .INTROSPECTOR_JOB_FAILED_DETAIL ;
5762
@@ -90,18 +95,40 @@ private static boolean runIntrospector(Packet packet, DomainPresenceInfo info) {
9095 LOGGER .fine ("runIntrospector topology: " + topology );
9196 LOGGER .fine ("runningServersCount: " + runningServersCount (info ));
9297 LOGGER .fine ("creatingServers: " + creatingServers (info ));
98+ LOGGER .fine ("isModelInImageUpdate: " + isModelInImageUpdate (packet , info ));
9399 return topology == null
94- || isBringingUpNewDomain (info )
95- || introspectionRequested (packet )
96- || isModelInImageUpdate (packet , info );
100+ || isBringingUpNewDomain (packet , info )
101+ || isIntrospectionRequestedAndRemove (packet )
102+ || isModelInImageUpdate (packet , info )
103+ || isIntrospectVersionChanged (packet , info );
97104 }
98105
99- private static boolean isBringingUpNewDomain (DomainPresenceInfo info ) {
100- return runningServersCount (info ) == 0 && creatingServers (info );
106+ private static boolean isBringingUpNewDomain (Packet packet , DomainPresenceInfo info ) {
107+ return runningServersCount (info ) == 0 && creatingServers (info ) && isGenerationChanged ( packet , info ) ;
101108 }
102109
103- private static boolean introspectionRequested (Packet packet ) {
104- return packet .containsKey (ProcessingConstants .DOMAIN_INTROSPECT_REQUESTED );
110+ private static boolean isIntrospectionRequestedAndRemove (Packet packet ) {
111+ return packet .remove (ProcessingConstants .DOMAIN_INTROSPECT_REQUESTED ) != null ;
112+ }
113+
114+ private static boolean isIntrospectVersionChanged (Packet packet , DomainPresenceInfo info ) {
115+ return Optional .ofNullable (packet .get (INTROSPECTION_STATE_LABEL ))
116+ .map (introspectVersionLabel -> !introspectVersionLabel .equals (getIntrospectVersion (info ))).orElse (false );
117+ }
118+
119+ private static boolean isGenerationChanged (Packet packet , DomainPresenceInfo info ) {
120+ return Optional .ofNullable (packet .get (INTROSPECTION_DOMAIN_SPEC_GENERATION ))
121+ .map (gen -> !gen .equals (getGeneration (info ))).orElse (true );
122+ }
123+
124+ private static String getIntrospectVersion (DomainPresenceInfo info ) {
125+ return Optional .ofNullable (info .getDomain ()).map (Domain ::getSpec ).map (s -> s .getIntrospectVersion ())
126+ .orElse ("" );
127+ }
128+
129+ private static String getGeneration (DomainPresenceInfo info ) {
130+ return Optional .ofNullable (info .getDomain ()).map (Domain ::getMetadata ).map (m -> m .getGeneration ().toString ())
131+ .orElse ("" );
105132 }
106133
107134 private static boolean isModelInImageUpdate (Packet packet , DomainPresenceInfo info ) {
@@ -178,13 +205,13 @@ static boolean creatingServers(DomainPresenceInfo info) {
178205 }
179206
180207 /**
181- * Factory for {@link Step} that deletes WebLogic domain introspector job.
208+ * Factory for {@link Step} that replaces or creates WebLogic domain introspector job.
182209 *
183210 * @param next Next processing step
184- * @return Step for deleting the domain introsepctor jod
211+ * @return Step for replacing or creating the domain introsepctor jod
185212 */
186- public static Step deleteDomainIntrospectorJobStep (Step next ) {
187- return new DeleteIntrospectorJobStep (next );
213+ public static Step replaceOrCreateDomainIntrospectorJobStep (Step next ) {
214+ return new ReplaceOrCreateIntrospectorJobStep (next );
188215 }
189216
190217 private static Step createWatchDomainIntrospectorJobReadyStep (Step next ) {
@@ -374,7 +401,7 @@ public NextAction apply(Packet packet) {
374401 if (runIntrospector (packet , info )) {
375402 JobStepContext context = new DomainIntrospectorJobStepContext (packet );
376403
377- packet .putIfAbsent (START_TIME , System . currentTimeMillis ());
404+ packet .putIfAbsent (START_TIME , OffsetDateTime . now ());
378405
379406 return doNext (
380407 Step .chain (
@@ -392,50 +419,60 @@ public NextAction apply(Packet packet) {
392419 }
393420 }
394421
395- private static class DeleteIntrospectorJobStep extends Step {
422+ private static class ReplaceOrCreateIntrospectorJobStep extends Step {
396423
397424 static final int JOB_DELETE_TIMEOUT_SECONDS = 1 ;
398425
399- DeleteIntrospectorJobStep (Step next ) {
426+ ReplaceOrCreateIntrospectorJobStep (Step next ) {
400427 super (next );
401428 }
402429
403430 @ Override
404431 public NextAction apply (Packet packet ) {
405- return doNext (deleteJob (packet , getNext ()), packet );
432+ return doNext (replaceOrCreateJob (packet , getNext ()), packet );
406433 }
407434
408- String getJobDeletedMessageKey () {
409- return MessageKeys .JOB_DELETED ;
435+
436+ private Step replaceOrCreateJob (Packet packet , Step next ) {
437+ DomainPresenceInfo info = packet .getSpi (DomainPresenceInfo .class );
438+ return new CallBuilder ().readJobAsync (JobHelper .createJobName (info .getDomain ().getDomainUid ()),
439+ info .getNamespace (), info .getDomain ().getDomainUid (),
440+ new ReplaceOrCreateStep (next ));
410441 }
411442
412- void logJobDeleted (String domainUid , String namespace , String jobName , Packet packet ) {
413- V1Job domainIntrospectorJob =
414- (V1Job ) packet .remove (ProcessingConstants .DOMAIN_INTROSPECTOR_JOB );
443+ private class ReplaceOrCreateStep extends DefaultResponseStep {
415444
416- packet .remove (ProcessingConstants .INTROSPECTOR_JOB_FAILURE_LOGGED );
417- if (domainIntrospectorJob != null
418- && !JobWatcher .isComplete (domainIntrospectorJob )) {
419- logIntrospectorFailure (packet , domainIntrospectorJob );
445+ ReplaceOrCreateStep (Step next ) {
446+ super (next );
420447 }
421- packet .remove (ProcessingConstants .JOB_POD_NAME );
422448
423- LOGGER .fine (getJobDeletedMessageKey (), domainUid , namespace , jobName );
424- }
449+ @ Override
450+ public NextAction onSuccess (Packet packet , CallResponse callResponse ) {
451+ DomainPresenceInfo info = packet .getSpi (DomainPresenceInfo .class );
452+ String namespace = info .getNamespace ();
453+ V1Job job = (V1Job ) callResponse .getResult ();
454+ if ((job != null ) && (packet .get (ProcessingConstants .DOMAIN_INTROSPECTOR_JOB ) == null )) {
455+ packet .put (ProcessingConstants .DOMAIN_INTROSPECTOR_JOB , job );
456+ }
425457
426- private Step deleteJob (Packet packet , Step next ) {
427- DomainPresenceInfo info = packet .getSpi (DomainPresenceInfo .class );
428- java .lang .String domainUid = info .getDomain ().getDomainUid ();
429- java .lang .String namespace = info .getNamespace ();
430- String jobName = JobHelper .createJobName (domainUid );
431- logJobDeleted (domainUid , namespace , jobName , packet );
432- return new CallBuilder ().withTimeoutSeconds (JOB_DELETE_TIMEOUT_SECONDS )
433- .deleteJobAsync (
434- jobName ,
435- namespace ,
436- domainUid ,
437- new V1DeleteOptions ().propagationPolicy ("Foreground" ),
438- new DefaultResponseStep <>(next ));
458+ if (job != null ) {
459+ packet .putIfAbsent (START_TIME , Optional .ofNullable (job .getMetadata ())
460+ .map (m -> m .getCreationTimestamp ()).orElse (OffsetDateTime .now ()));
461+ return doNext (Step .chain (
462+ createProgressingStartedEventStep (info , INSPECTING_DOMAIN_PROGRESS_REASON , true , null ),
463+ readDomainIntrospectorPodLogStep (null ),
464+ deleteDomainIntrospectorJobStep (null ),
465+ ConfigMapHelper .createIntrospectorConfigMapStep (null ),
466+ ConfigMapHelper .readExistingIntrospectorConfigMap (namespace , info .getDomainUid ()),
467+ new DomainProcessorImpl .IntrospectionRequestStep (info ),
468+ createDomainIntrospectorJobStep (getNext ())), packet );
469+ } else {
470+ packet .putIfAbsent (START_TIME , OffsetDateTime .now ());
471+ return doNext (Step .chain (
472+ ConfigMapHelper .readExistingIntrospectorConfigMap (namespace , info .getDomainUid ()),
473+ createDomainIntrospectorJobStep (getNext ())), packet );
474+ }
475+ }
439476 }
440477 }
441478
@@ -507,17 +544,32 @@ public NextAction onSuccess(Packet packet, CallResponse<String> callResponse) {
507544 jobConditionsReason .add (DomainStatusUpdater .ERR_INTROSPECTOR );
508545 }
509546 //Introspector job is incomplete, update domain status and terminate processing
547+ Step nextStep = null ;
548+ int retryIntervalSeconds = TuningParameters .getInstance ().getMainTuning ().domainPresenceRecheckIntervalSeconds ;
549+
550+ if (OffsetDateTime .now ().isAfter (
551+ getJobCreationTime (domainIntrospectorJob ).plus (retryIntervalSeconds , SECONDS ))) {
552+ //Introspector job is incomplete and current time is greater than the lazy deletion time for the job,
553+ //update the domain status and execute the next step
554+ nextStep = getNext ();
555+ }
556+
510557 return doNext (
511558 DomainStatusUpdater .createFailureRelatedSteps (
512559 onSeparateLines (jobConditionsReason ),
513560 onSeparateLines (severeStatuses ),
514- null ),
561+ nextStep ),
515562 packet );
516563 }
517564
518565 return doNext (packet );
519566 }
520567
568+ private OffsetDateTime getJobCreationTime (V1Job domainIntrospectorJob ) {
569+ return Optional .ofNullable (domainIntrospectorJob .getMetadata ())
570+ .map (m -> m .getCreationTimestamp ()).orElse (OffsetDateTime .now ());
571+ }
572+
521573 private boolean isNotComplete (V1Job domainIntrospectorJob ) {
522574 return !JobWatcher .isComplete (domainIntrospectorJob );
523575 }
@@ -611,6 +663,49 @@ private static void logIntrospectorFailure(Packet packet, V1Job domainIntrospect
611663 }
612664 }
613665
666+ static class DeleteDomainIntrospectorJobStep extends Step {
667+
668+ DeleteDomainIntrospectorJobStep (Step next ) {
669+ super (next );
670+ }
671+
672+ @ Override
673+ public NextAction apply (Packet packet ) {
674+ DomainPresenceInfo info = packet .getSpi (DomainPresenceInfo .class );
675+ String jobName = JobHelper .createJobName (info .getDomainUid ());
676+ logJobDeleted (info .getDomainUid (), info .getNamespace (), jobName , packet );
677+ return doNext (new CallBuilder ().withTimeoutSeconds (ReplaceOrCreateIntrospectorJobStep .JOB_DELETE_TIMEOUT_SECONDS )
678+ .deleteJobAsync (
679+ jobName ,
680+ info .getNamespace (),
681+ info .getDomainUid (),
682+ new V1DeleteOptions ().propagationPolicy ("Foreground" ),
683+ new DefaultResponseStep <>(getNext ())), packet );
684+ }
685+ }
686+
687+ public static Step deleteDomainIntrospectorJobStep (Step next ) {
688+ return new DeleteDomainIntrospectorJobStep (next );
689+ }
690+
691+ static void logJobDeleted (String domainUid , String namespace , String jobName , Packet packet ) {
692+ V1Job domainIntrospectorJob =
693+ (V1Job ) packet .remove (ProcessingConstants .DOMAIN_INTROSPECTOR_JOB );
694+
695+ packet .remove (ProcessingConstants .INTROSPECTOR_JOB_FAILURE_LOGGED );
696+ if (domainIntrospectorJob != null
697+ && !JobWatcher .isComplete (domainIntrospectorJob )) {
698+ logIntrospectorFailure (packet , domainIntrospectorJob );
699+ }
700+ packet .remove (ProcessingConstants .JOB_POD_NAME );
701+
702+ LOGGER .fine (getJobDeletedMessageKey (), domainUid , namespace , jobName );
703+ }
704+
705+ static String getJobDeletedMessageKey () {
706+ return MessageKeys .JOB_DELETED ;
707+ }
708+
614709 private static class ReadDomainIntrospectorPodStep extends Step {
615710
616711 ReadDomainIntrospectorPodStep (Step next ) {
0 commit comments