@@ -258,6 +258,15 @@ func MustSetupLRP(ctx context.Context, clientset *cilium.Clientset, lrpPath stri
258258 }
259259}
260260
261+ func MustSetupCNP (ctx context.Context , clientset * cilium.Clientset , cnpPath string ) (ciliumv2.CiliumNetworkPolicy , func ()) { // nolint
262+ cnp := mustParseCNP (cnpPath )
263+ cnps := clientset .CiliumV2 ().CiliumNetworkPolicies (cnp .Namespace )
264+ mustCreateCiliumNetworkPolicy (ctx , cnps , cnp )
265+ return cnp , func () {
266+ MustDeleteCiliumNetworkPolicy (ctx , cnps , cnp )
267+ }
268+ }
269+
261270func Int32ToPtr (i int32 ) * int32 { return & i }
262271
263272func WaitForPodsRunning (ctx context.Context , clientset * kubernetes.Clientset , namespace , labelselector string ) error {
@@ -482,47 +491,56 @@ func writeToFile(dir, fileName, str string) error {
482491func ExecCmdOnPod (ctx context.Context , clientset * kubernetes.Clientset , namespace , podName , containerName string , cmd []string , config * rest.Config ) ([]byte , error ) {
483492 var result []byte
484493 execCmdOnPod := func () error {
485- req := clientset .CoreV1 ().RESTClient ().Post ().
486- Resource ("pods" ).
487- Name (podName ).
488- Namespace (namespace ).
489- SubResource ("exec" ).
490- VersionedParams (& corev1.PodExecOptions {
491- Command : cmd ,
492- Container : containerName ,
493- Stdin : false ,
494- Stdout : true ,
495- Stderr : true ,
496- TTY : false ,
497- }, scheme .ParameterCodec )
498-
499- exec , err := remotecommand .NewSPDYExecutor (config , "POST" , req .URL ())
500- if err != nil {
501- return errors .Wrapf (err , "error in creating executor for req %s" , req .URL ())
502- }
503-
504- var stdout , stderr bytes.Buffer
505- err = exec .StreamWithContext (ctx , remotecommand.StreamOptions {
506- Stdin : nil ,
507- Stdout : & stdout ,
508- Stderr : & stderr ,
509- Tty : false ,
510- })
511- if err != nil {
512- log .Printf ("Error: %v had error %v from command - %v, will retry" , podName , err , cmd )
513- return errors .Wrapf (err , "error in executing command %s" , cmd )
514- }
515- if len (stdout .Bytes ()) == 0 {
516- log .Printf ("Warning: %v had 0 bytes returned from command - %v" , podName , cmd )
517- }
518- result = stdout .Bytes ()
519- return nil
494+ output , _ , err := ExecCmdOnPodOnce (ctx , clientset , namespace , podName , containerName , cmd , config )
495+ result = output
496+ return err
520497 }
521498 retrier := retry.Retrier {Attempts : ShortRetryAttempts , Delay : RetryDelay }
522499 err := retrier .Do (ctx , execCmdOnPod )
523500 return result , errors .Wrapf (err , "could not execute the cmd %s on %s" , cmd , podName )
524501}
525502
503+ func ExecCmdOnPodOnce (ctx context.Context , clientset * kubernetes.Clientset , namespace , podName , containerName string , cmd []string , config * rest.Config ) ([]byte , []byte , error ) {
504+ req := clientset .CoreV1 ().RESTClient ().Post ().
505+ Resource ("pods" ).
506+ Name (podName ).
507+ Namespace (namespace ).
508+ SubResource ("exec" ).
509+ VersionedParams (& corev1.PodExecOptions {
510+ Command : cmd ,
511+ Container : containerName ,
512+ Stdin : false ,
513+ Stdout : true ,
514+ Stderr : true ,
515+ TTY : false ,
516+ }, scheme .ParameterCodec )
517+
518+ exec , err := remotecommand .NewSPDYExecutor (config , "POST" , req .URL ())
519+ if err != nil {
520+ return nil , nil , errors .Wrapf (err , "error in creating executor for req %s" , req .URL ())
521+ }
522+
523+ var stdout , stderr bytes.Buffer
524+ err = exec .StreamWithContext (ctx , remotecommand.StreamOptions {
525+ Stdin : nil ,
526+ Stdout : & stdout ,
527+ Stderr : & stderr ,
528+ Tty : false ,
529+ })
530+
531+ result := stdout .Bytes ()
532+ errResult := stderr .Bytes ()
533+
534+ if err != nil {
535+ log .Printf ("Error: %v had error %v from command - %v" , podName , err , cmd )
536+ return result , errResult , errors .Wrapf (err , "error in executing command %s" , cmd )
537+ }
538+ if len (stdout .Bytes ()) == 0 {
539+ log .Printf ("Warning: %v had 0 bytes returned from command - %v" , podName , cmd )
540+ }
541+ return result , errResult , nil
542+ }
543+
526544func NamespaceExists (ctx context.Context , clientset * kubernetes.Clientset , namespace string ) (bool , error ) {
527545 _ , err := clientset .CoreV1 ().Namespaces ().Get (ctx , namespace , metav1.GetOptions {})
528546 if err != nil {
0 commit comments