1- // Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved.
1+ // Copyright 2018,2019 Oracle Corporation and/or its affiliates. All rights reserved.
22// Licensed under the Universal Permissive License v 1.0 as shown at
33// http://oss.oracle.com/licenses/upl.
44
55package oracle .kubernetes .operator ;
66
7+ import static oracle .kubernetes .operator .KubernetesConstants .CONTAINER_NAME ;
8+
79import com .google .common .base .Charsets ;
810import com .google .common .io .CharStreams ;
911import io .kubernetes .client .ApiClient ;
1012import io .kubernetes .client .ApiException ;
11- import io .kubernetes .client .Exec ;
1213import io .kubernetes .client .models .V1Pod ;
1314import java .io .IOException ;
1415import java .io .InputStream ;
2021import java .util .concurrent .ConcurrentHashMap ;
2122import java .util .concurrent .ConcurrentMap ;
2223import java .util .concurrent .TimeUnit ;
24+ import java .util .function .Function ;
2325import oracle .kubernetes .operator .helpers .ClientPool ;
2426import oracle .kubernetes .operator .helpers .DomainPresenceInfo ;
2527import oracle .kubernetes .operator .helpers .ServerKubernetesObjects ;
2628import oracle .kubernetes .operator .logging .LoggingFacade ;
2729import oracle .kubernetes .operator .logging .LoggingFactory ;
2830import oracle .kubernetes .operator .logging .MessageKeys ;
2931import oracle .kubernetes .operator .steps .ReadHealthStep ;
32+ import oracle .kubernetes .operator .utils .KubernetesExec ;
33+ import oracle .kubernetes .operator .utils .KubernetesExecFactory ;
34+ import oracle .kubernetes .operator .utils .KubernetesExecFactoryImpl ;
3035import oracle .kubernetes .operator .work .NextAction ;
3136import oracle .kubernetes .operator .work .Packet ;
3237import oracle .kubernetes .operator .work .Step ;
3540/** Creates an asynchronous step to read the WebLogic server state from a particular pod */
3641public class ServerStatusReader {
3742 private static final LoggingFacade LOGGER = LoggingFactory .getLogger ("Operator" , "Operator" );
43+ private static KubernetesExecFactory EXEC_FACTORY = new KubernetesExecFactoryImpl ();
44+ private static Function <Step , Step > STEP_FACTORY = ReadHealthStep ::createReadHealthStep ;
3845
3946 private ServerStatusReader () {}
4047
41- public static Step createDomainStatusReaderStep (
48+ static Step createDomainStatusReaderStep (
4249 DomainPresenceInfo info , long timeoutSeconds , Step next ) {
4350 return new DomainStatusReaderStep (info , timeoutSeconds , next );
4451 }
@@ -47,7 +54,7 @@ private static class DomainStatusReaderStep extends Step {
4754 private final DomainPresenceInfo info ;
4855 private final long timeoutSeconds ;
4956
50- public DomainStatusReaderStep (DomainPresenceInfo info , long timeoutSeconds , Step next ) {
57+ DomainStatusReaderStep (DomainPresenceInfo info , long timeoutSeconds , Step next ) {
5158 super (next );
5259 this .info = info ;
5360 this .timeoutSeconds = timeoutSeconds ;
@@ -65,13 +72,13 @@ public NextAction apply(Packet packet) {
6572 for (Map .Entry <String , ServerKubernetesObjects > entry : info .getServers ().entrySet ()) {
6673 String serverName = entry .getKey ();
6774 ServerKubernetesObjects sko = entry .getValue ();
68- if (sko != null ) {
75+ if (sko != null ) { // !! Impossible to have a null value in a concurrent map
6976 V1Pod pod = sko .getPod ().get ();
7077 if (pod != null ) {
7178 Packet p = packet .clone ();
7279 startDetails .add (
7380 new StepAndPacket (
74- createServerStatusReaderStep (sko , pod , serverName , timeoutSeconds , null ), p ));
81+ createServerStatusReaderStep (sko , pod , serverName , timeoutSeconds ), p ));
7582 }
7683 }
7784 }
@@ -90,13 +97,12 @@ public NextAction apply(Packet packet) {
9097 * @param pod The pod
9198 * @param serverName Server name
9299 * @param timeoutSeconds Timeout in seconds
93- * @param next Next step
94100 * @return Created step
95101 */
96- public static Step createServerStatusReaderStep (
97- ServerKubernetesObjects sko , V1Pod pod , String serverName , long timeoutSeconds , Step next ) {
102+ private static Step createServerStatusReaderStep (
103+ ServerKubernetesObjects sko , V1Pod pod , String serverName , long timeoutSeconds ) {
98104 return new ServerStatusReaderStep (
99- sko , pod , serverName , timeoutSeconds , new ServerHealthStep (serverName , next ));
105+ sko , pod , serverName , timeoutSeconds , new ServerHealthStep (serverName , null ));
100106 }
101107
102108 private static class ServerStatusReaderStep extends Step {
@@ -105,7 +111,7 @@ private static class ServerStatusReaderStep extends Step {
105111 private final String serverName ;
106112 private final long timeoutSeconds ;
107113
108- public ServerStatusReaderStep (
114+ ServerStatusReaderStep (
109115 ServerKubernetesObjects sko , V1Pod pod , String serverName , long timeoutSeconds , Step next ) {
110116 super (next );
111117 this .sko = sko ;
@@ -120,7 +126,7 @@ public NextAction apply(Packet packet) {
120126 ConcurrentMap <String , String > serverStateMap =
121127 (ConcurrentMap <String , String >) packet .get (ProcessingConstants .SERVER_STATE_MAP );
122128
123- if (PodWatcher .isReady (pod , true )) {
129+ if (PodWatcher .getReadyStatus (pod )) {
124130 sko .getLastKnownStatus ().set (WebLogicConstants .RUNNING_STATE );
125131 serverStateMap .put (serverName , WebLogicConstants .RUNNING_STATE );
126132 return doNext (packet );
@@ -145,14 +151,10 @@ public NextAction apply(Packet packet) {
145151 ClientPool helper = ClientPool .getInstance ();
146152 ApiClient client = helper .take ();
147153 try {
148- proc =
149- new Exec (client )
150- .exec (
151- pod ,
152- new String [] {"/weblogic-operator/scripts/readState.sh" },
153- KubernetesConstants .CONTAINER_NAME ,
154- stdin ,
155- tty );
154+ KubernetesExec kubernetesExec = EXEC_FACTORY .create (client , pod , CONTAINER_NAME );
155+ kubernetesExec .setStdin (stdin );
156+ kubernetesExec .setTty (tty );
157+ proc = kubernetesExec .exec ("/weblogic-operator/scripts/readState.sh" );
156158
157159 InputStream in = proc .getInputStream ();
158160 if (proc .waitFor (timeoutSeconds , TimeUnit .SECONDS )) {
@@ -179,7 +181,7 @@ public NextAction apply(Packet packet) {
179181 private static class ServerHealthStep extends Step {
180182 private final String serverName ;
181183
182- public ServerHealthStep (String serverName , Step next ) {
184+ ServerHealthStep (String serverName , Step next ) {
183185 super (next );
184186 this .serverName = serverName ;
185187 }
@@ -193,7 +195,7 @@ public NextAction apply(Packet packet) {
193195
194196 if (WebLogicConstants .STATES_SUPPORTING_REST .contains (state )) {
195197 packet .put (ProcessingConstants .SERVER_NAME , serverName );
196- return doNext (ReadHealthStep . createReadHealthStep (getNext ()), packet );
198+ return doNext (STEP_FACTORY . apply (getNext ()), packet );
197199 }
198200
199201 return doNext (packet );
0 commit comments