@@ -428,39 +428,46 @@ func writeToFile(dir, fileName, str string) error {
428428}
429429
430430func 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
466473func NamespaceExists (ctx context.Context , clientset * kubernetes.Clientset , namespace string ) (bool , error ) {
0 commit comments