88import cz .xtf .core .waiting .SimpleWaiter ;
99import cz .xtf .core .waiting .WaiterException ;
1010import io .fabric8 .kubernetes .api .model .Pod ;
11+ import io .fabric8 .kubernetes .api .model .Status ;
1112import io .fabric8 .kubernetes .client .dsl .ExecListener ;
1213import lombok .extern .slf4j .Slf4j ;
1314
@@ -66,6 +67,36 @@ public PodShellOutput execute(String... commands) {
6667 return new PodShellOutput (baosOutput .toString ().trim (), baosError .toString ().trim ());
6768 }
6869
70+ /**
71+ * Executes command(s) on the pod with retry mechanism in case of failure.
72+ * Up to 3 attempts are made with a 5-second delay between each attempt.
73+ */
74+ public PodShellOutput executeWithRetry (String ... commands ){
75+ final int POD_SHELL_RETRY_COUNT = 3 ;
76+ final int POD_SHELL_RETRY_DELAY_MS = 5000 ;
77+
78+ WaiterException lastException = null ;
79+ for (int attempt = 1 ; attempt <= POD_SHELL_RETRY_COUNT ; attempt ++) {
80+ try {
81+
82+ return this .execute (commands );
83+ } catch (WaiterException e ) {
84+ lastException = e ;
85+ log .warn ("Attempt {}/{} failed for command execution on pod {}: {}" ,
86+ attempt , POD_SHELL_RETRY_COUNT , podName , e .getMessage ());
87+ if (attempt < POD_SHELL_RETRY_COUNT ) {
88+ try {
89+ Thread .sleep (POD_SHELL_RETRY_DELAY_MS );
90+ } catch (InterruptedException ie ) {
91+ Thread .currentThread ().interrupt ();
92+ throw new RuntimeException ("Thread has been interrupted!" );
93+ }
94+ }
95+ }
96+ }
97+ throw new WaiterException ("All attempts to execute command on pod " + podName + " have failed." );
98+ }
99+
69100 public class StateExecListener implements ExecListener {
70101 private final AtomicBoolean executionDone = new AtomicBoolean (false );
71102
@@ -76,14 +107,18 @@ public void onOpen() {
76107
77108 @ Override
78109 public void onFailure (Throwable throwable , Response response ) {
79- // DO NOTHING
110+ log .error ("Execution failed in pod '{}': {}" , podName , throwable .getMessage ());
111+ executionDone .set (true );
80112 }
81113
82114 @ Override
83115 public void onClose (int i , String s ) {
84116 executionDone .set (true );
85117 }
86118
119+ @ Override
120+ public void onExit (int code , Status status ) { executionDone .set (true ); }
121+
87122 public boolean hasExecutionFinished () {
88123 return executionDone .get ();
89124 }
0 commit comments