1515package kube
1616
1717import (
18+ "bytes"
1819 "io"
19- "io/ioutil"
2020 "net/url"
21+ "strings"
2122
22- log "github.com/sirupsen/logrus"
2323 "k8s.io/api/core/v1"
2424 "k8s.io/client-go/kubernetes"
2525 "k8s.io/client-go/kubernetes/scheme"
@@ -59,26 +59,6 @@ func Exec(cli kubernetes.Interface, namespace, pod, container string, command []
5959// returning stdout, stderr and error. `options` allowed for
6060// additional parameters to be passed.
6161func ExecWithOptions (kubeCli kubernetes.Interface , options ExecOptions ) (string , string , error ) {
62- config , err := LoadConfig ()
63- if err != nil {
64- return "" , "" , err
65- }
66- o , e , errCh := execStream (kubeCli , config , options )
67- defer func () { _ = o .Close () }()
68- defer func () { _ = e .Close () }()
69- stdout , oerr := ioutil .ReadAll (o )
70- if oerr != nil {
71- log .Info ("Failed to read stdout:" , oerr .Error ())
72- }
73- stderr , eerr := ioutil .ReadAll (e )
74- if eerr != nil {
75- log .Info ("Failed to read stderr:" , eerr .Error ())
76- }
77-
78- return string (stdout ), string (stderr ), <- errCh
79- }
80-
81- func execStream (kubeCli kubernetes.Interface , config * restclient.Config , options ExecOptions ) (io.ReadCloser , io.ReadCloser , chan error ) {
8262 const tty = false
8363 req := kubeCli .CoreV1 ().RESTClient ().Post ().
8464 Resource ("pods" ).
@@ -98,16 +78,14 @@ func execStream(kubeCli kubernetes.Interface, config *restclient.Config, options
9878 TTY : tty ,
9979 }, scheme .ParameterCodec )
10080
101- pro , pwo := io .Pipe ()
102- pre , pwe := io .Pipe ()
103- errCh := make (chan error , 1 )
104- go func () {
105- err := execute ("POST" , req .URL (), config , options .Stdin , pwo , pwe , tty )
106- errCh <- err
107- _ = pwo .Close ()
108- _ = pwe .Close ()
109- }()
110- return pro , pre , errCh
81+ config , err := LoadConfig ()
82+ if err != nil {
83+ return "" , "" , err
84+ }
85+
86+ var stdout , stderr bytes.Buffer
87+ err = execute ("POST" , req .URL (), config , options .Stdin , & stdout , & stderr , tty )
88+ return strings .TrimSpace (stdout .String ()), strings .TrimSpace (stderr .String ()), err
11189}
11290
11391func execute (method string , url * url.URL , config * restclient.Config , stdin io.Reader , stdout , stderr io.Writer , tty bool ) error {
0 commit comments