@@ -233,37 +233,37 @@ func MustSetupDeployment(ctx context.Context, clientset *kubernetes.Clientset, d
233233
234234func MustSetupServiceAccount (ctx context.Context , clientset * kubernetes.Clientset , serviceAccountPath string ) (corev1.ServiceAccount , func ()) { // nolint
235235 sa := mustParseServiceAccount (serviceAccountPath )
236- sas := clientset .CoreV1 ().ServiceAccounts (sa .Namespace )
237- mustCreateServiceAccount (ctx , sas , sa )
236+ saClient := clientset .CoreV1 ().ServiceAccounts (sa .Namespace )
237+ mustCreateServiceAccount (ctx , saClient , sa )
238238 return sa , func () {
239- MustDeleteServiceAccount (ctx , sas , sa )
239+ MustDeleteServiceAccount (ctx , saClient , sa )
240240 }
241241}
242242
243243func MustSetupService (ctx context.Context , clientset * kubernetes.Clientset , servicePath string ) (corev1.Service , func ()) { // nolint
244244 svc := mustParseService (servicePath )
245- svcs := clientset .CoreV1 ().Services (svc .Namespace )
246- mustCreateService (ctx , svcs , svc )
245+ svcClient := clientset .CoreV1 ().Services (svc .Namespace )
246+ mustCreateService (ctx , svcClient , svc )
247247 return svc , func () {
248- MustDeleteService (ctx , svcs , svc )
248+ MustDeleteService (ctx , svcClient , svc )
249249 }
250250}
251251
252252func MustSetupLRP (ctx context.Context , clientset * cilium.Clientset , lrpPath string ) (ciliumv2.CiliumLocalRedirectPolicy , func ()) { // nolint
253253 lrp := mustParseLRP (lrpPath )
254- lrps := clientset .CiliumV2 ().CiliumLocalRedirectPolicies (lrp .Namespace )
255- mustCreateCiliumLocalRedirectPolicy (ctx , lrps , lrp )
254+ lrpClient := clientset .CiliumV2 ().CiliumLocalRedirectPolicies (lrp .Namespace )
255+ mustCreateCiliumLocalRedirectPolicy (ctx , lrpClient , lrp )
256256 return lrp , func () {
257- MustDeleteCiliumLocalRedirectPolicy (ctx , lrps , lrp )
257+ MustDeleteCiliumLocalRedirectPolicy (ctx , lrpClient , lrp )
258258 }
259259}
260260
261261func MustSetupCNP (ctx context.Context , clientset * cilium.Clientset , cnpPath string ) (ciliumv2.CiliumNetworkPolicy , func ()) { // nolint
262262 cnp := mustParseCNP (cnpPath )
263- cnps := clientset .CiliumV2 ().CiliumNetworkPolicies (cnp .Namespace )
264- mustCreateCiliumNetworkPolicy (ctx , cnps , cnp )
263+ cnpClient := clientset .CiliumV2 ().CiliumNetworkPolicies (cnp .Namespace )
264+ mustCreateCiliumNetworkPolicy (ctx , cnpClient , cnp )
265265 return cnp , func () {
266- MustDeleteCiliumNetworkPolicy (ctx , cnps , cnp )
266+ MustDeleteCiliumNetworkPolicy (ctx , cnpClient , cnp )
267267 }
268268}
269269
@@ -488,59 +488,60 @@ func writeToFile(dir, fileName, str string) error {
488488 return errors .Wrap (err , "failed to write string" )
489489}
490490
491- func ExecCmdOnPod (ctx context.Context , clientset * kubernetes.Clientset , namespace , podName , containerName string , cmd []string , config * rest.Config ) ([]byte , error ) {
491+ // ExecCmdOnPod runs the specified command on a particular pod and retries the command on failure if doRetry is set to true
492+ // The function returns the standard output, standard error, and error (if any) in that order
493+ func ExecCmdOnPod (ctx context.Context , clientset * kubernetes.Clientset , namespace , podName , containerName string , cmd []string , config * rest.Config , doRetry bool ) ([]byte , []byte , error ) {
492494 var result []byte
495+ var errResult []byte
493496 execCmdOnPod := func () error {
494- output , _ , err := ExecCmdOnPodOnce (ctx , clientset , namespace , podName , containerName , cmd , config )
495- result = output
496- return err
497- }
498- retrier := retry.Retrier {Attempts : ShortRetryAttempts , Delay : RetryDelay }
499- err := retrier .Do (ctx , execCmdOnPod )
500- return result , errors .Wrapf (err , "could not execute the cmd %s on %s" , cmd , podName )
501- }
502-
503- // ExecCmdOnPodOnce runs a command on the specified pod and returns its standard output, standard error output, and error in that order
504- // The command does not retry when the command fails (ex: due to timeout), unlike ExecCmdOnPod
505- func ExecCmdOnPodOnce (ctx context.Context , clientset * kubernetes.Clientset , namespace , podName , containerName string , cmd []string , config * rest.Config ) ([]byte , []byte , error ) { // nolint
506- req := clientset .CoreV1 ().RESTClient ().Post ().
507- Resource ("pods" ).
508- Name (podName ).
509- Namespace (namespace ).
510- SubResource ("exec" ).
511- VersionedParams (& corev1.PodExecOptions {
512- Command : cmd ,
513- Container : containerName ,
514- Stdin : false ,
515- Stdout : true ,
516- Stderr : true ,
517- TTY : false ,
518- }, scheme .ParameterCodec )
519-
520- exec , err := remotecommand .NewSPDYExecutor (config , "POST" , req .URL ())
521- if err != nil {
522- return nil , nil , errors .Wrapf (err , "error in creating executor for req %s" , req .URL ())
523- }
497+ req := clientset .CoreV1 ().RESTClient ().Post ().
498+ Resource ("pods" ).
499+ Name (podName ).
500+ Namespace (namespace ).
501+ SubResource ("exec" ).
502+ VersionedParams (& corev1.PodExecOptions {
503+ Command : cmd ,
504+ Container : containerName ,
505+ Stdin : false ,
506+ Stdout : true ,
507+ Stderr : true ,
508+ TTY : false ,
509+ }, scheme .ParameterCodec )
510+
511+ exec , err := remotecommand .NewSPDYExecutor (config , "POST" , req .URL ())
512+ if err != nil {
513+ return errors .Wrapf (err , "error in creating executor for req %s" , req .URL ())
514+ }
524515
525- var stdout , stderr bytes.Buffer
526- err = exec .StreamWithContext (ctx , remotecommand.StreamOptions {
527- Stdin : nil ,
528- Stdout : & stdout ,
529- Stderr : & stderr ,
530- Tty : false ,
531- })
516+ var stdout , stderr bytes.Buffer
517+ err = exec .StreamWithContext (ctx , remotecommand.StreamOptions {
518+ Stdin : nil ,
519+ Stdout : & stdout ,
520+ Stderr : & stderr ,
521+ Tty : false ,
522+ })
532523
533- result : = stdout .Bytes ()
534- errResult : = stderr .Bytes ()
524+ result = stdout .Bytes ()
525+ errResult = stderr .Bytes ()
535526
536- if err != nil {
537- log .Printf ("Error: %v had error %v from command - %v" , podName , err , cmd )
538- return result , errResult , errors .Wrapf (err , "error in executing command %s" , cmd )
527+ if err != nil {
528+ log .Printf ("Error: %v had error %v from command - %v" , podName , err , cmd )
529+ return errors .Wrapf (err , "error in executing command %s" , cmd )
530+ }
531+ if len (stdout .Bytes ()) == 0 {
532+ log .Printf ("Warning: %v had 0 bytes returned from command - %v" , podName , cmd )
533+ }
534+ return nil
539535 }
540- if len (stdout .Bytes ()) == 0 {
541- log .Printf ("Warning: %v had 0 bytes returned from command - %v" , podName , cmd )
536+
537+ var err error
538+ if doRetry {
539+ retrier := retry.Retrier {Attempts : ShortRetryAttempts , Delay : RetryDelay }
540+ err = retrier .Do (ctx , execCmdOnPod )
541+ } else {
542+ err = execCmdOnPod ()
542543 }
543- return result , errResult , nil
544+ return result , errResult , errors . Wrapf ( err , "could not execute the cmd %s on %s" , cmd , podName )
544545}
545546
546547func NamespaceExists (ctx context.Context , clientset * kubernetes.Clientset , namespace string ) (bool , error ) {
@@ -655,7 +656,7 @@ func RestartKubeProxyService(ctx context.Context, clientset *kubernetes.Clientse
655656 }
656657 privilegedPod := pod .Items [0 ]
657658 // exec into the pod and restart kubeproxy
658- _ , err = ExecCmdOnPod (ctx , clientset , privilegedNamespace , privilegedPod .Name , "" , restartKubeProxyCmd , config )
659+ _ , _ , err = ExecCmdOnPod (ctx , clientset , privilegedNamespace , privilegedPod .Name , "" , restartKubeProxyCmd , config , true )
659660 if err != nil {
660661 return errors .Wrapf (err , "failed to exec into privileged pod %s on node %s" , privilegedPod .Name , node .Name )
661662 }
0 commit comments