Skip to content

Commit c9682a6

Browse files
authored
ci: adding a retry login on exec cmd on failure (#2740)
1 parent cba6ded commit c9682a6

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

test/internal/kubernetes/utils.go

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -428,39 +428,46 @@ func writeToFile(dir, fileName, str string) error {
428428
}
429429

430430
func ExecCmdOnPod(ctx context.Context, clientset *kubernetes.Clientset, namespace, podName string, cmd []string, config *rest.Config) ([]byte, error) {
431-
req := clientset.CoreV1().RESTClient().Post().
432-
Resource("pods").
433-
Name(podName).
434-
Namespace(namespace).
435-
SubResource("exec").
436-
VersionedParams(&corev1.PodExecOptions{
437-
Command: cmd,
438-
Stdin: false,
439-
Stdout: true,
440-
Stderr: true,
441-
TTY: false,
442-
}, scheme.ParameterCodec)
443-
444-
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
445-
if err != nil {
446-
return []byte{}, errors.Wrapf(err, "error in creating executor for req %s", req.URL())
447-
}
431+
var result []byte
432+
execCmdOnPod := func() error {
433+
req := clientset.CoreV1().RESTClient().Post().
434+
Resource("pods").
435+
Name(podName).
436+
Namespace(namespace).
437+
SubResource("exec").
438+
VersionedParams(&corev1.PodExecOptions{
439+
Command: cmd,
440+
Stdin: false,
441+
Stdout: true,
442+
Stderr: true,
443+
TTY: false,
444+
}, scheme.ParameterCodec)
445+
446+
exec, err := remotecommand.NewSPDYExecutor(config, "POST", req.URL())
447+
if err != nil {
448+
return errors.Wrapf(err, "error in creating executor for req %s", req.URL())
449+
}
448450

449-
var stdout, stderr bytes.Buffer
450-
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
451-
Stdin: nil,
452-
Stdout: &stdout,
453-
Stderr: &stderr,
454-
Tty: false,
455-
})
456-
if err != nil {
457-
return []byte{}, errors.Wrapf(err, "error in executing command %s", cmd)
458-
}
459-
if len(stdout.Bytes()) == 0 {
460-
log.Printf("Warning: %v had 0 bytes returned from command - %v", podName, cmd)
451+
var stdout, stderr bytes.Buffer
452+
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
453+
Stdin: nil,
454+
Stdout: &stdout,
455+
Stderr: &stderr,
456+
Tty: false,
457+
})
458+
if err != nil {
459+
log.Printf("Error: %v had error %v from command - %v, will retry", podName, err, cmd)
460+
return errors.Wrapf(err, "error in executing command %s", cmd)
461+
}
462+
if len(stdout.Bytes()) == 0 {
463+
log.Printf("Warning: %v had 0 bytes returned from command - %v", podName, cmd)
464+
}
465+
result = stdout.Bytes()
466+
return nil
461467
}
462-
463-
return stdout.Bytes(), nil
468+
retrier := retry.Retrier{Attempts: ShortRetryAttempts, Delay: RetryDelay}
469+
err := retrier.Do(ctx, execCmdOnPod)
470+
return result, errors.Wrapf(err, "could not execute the cmd %s on %s", cmd, podName)
464471
}
465472

466473
func NamespaceExists(ctx context.Context, clientset *kubernetes.Clientset, namespace string) (bool, error) {

0 commit comments

Comments
 (0)