@@ -21,6 +21,7 @@ import (
2121 corev1 "k8s.io/api/core/v1"
2222 "k8s.io/apimachinery/pkg/api/meta"
2323 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+ "k8s.io/apimachinery/pkg/fields"
2425 "k8s.io/client-go/dynamic"
2526 "k8s.io/client-go/kubernetes"
2627 "k8s.io/client-go/kubernetes/scheme"
@@ -165,33 +166,19 @@ func (c *Kubeclient) Exec(ctx context.Context, namespace, pod string, argv []str
165166 stdout string , stderr string , err error ,
166167) {
167168 c .log .Debug ("executing command in pod" , "namespace" , namespace , "pod" , pod , "argv" , argv )
168- buf := & bytes.Buffer {}
169- errBuf := & bytes.Buffer {}
170- request := c .Client .CoreV1 ().RESTClient ().
171- Post ().
172- Namespace (namespace ).
173- Resource ("pods" ).
174- Name (pod ).
175- SubResource ("exec" ).
176- VersionedParams (& corev1.PodExecOptions {
177- Command : argv ,
178- Stdin : false ,
179- Stdout : true ,
180- Stderr : true ,
181- TTY : false ,
182- }, scheme .ParameterCodec )
183- exec , err := remotecommand .NewSPDYExecutor (c .config , http .MethodPost , request .URL ())
169+ podList , err := c .Client .CoreV1 ().Pods (namespace ).List (ctx , metav1.ListOptions {
170+ FieldSelector : fields .OneTermEqualSelector ("metadata.name" , pod ).String (),
171+ })
184172 if err != nil {
185- return "" , "" , fmt .Errorf ("creating executor : %w" , err )
173+ return "" , "" , fmt .Errorf ("listing pods : %w" , err )
186174 }
187-
188- err = exec .StreamWithContext (ctx , remotecommand.StreamOptions {
189- Stdout : buf ,
190- Stderr : errBuf ,
191- Tty : false ,
192- })
193-
194- return buf .String (), errBuf .String (), err
175+ if len (podList .Items ) == 0 {
176+ return "" , "" , fmt .Errorf ("pod not found: %s/%s" , namespace , pod )
177+ }
178+ if len (podList .Items [0 ].Spec .Containers ) == 0 {
179+ return "" , "" , fmt .Errorf ("pod %s/%s has no containers" , namespace , pod )
180+ }
181+ return c .ExecContainer (ctx , namespace , pod , podList .Items [0 ].Spec .Containers [0 ].Name , argv )
195182}
196183
197184// ExecContainer executes a process in the container of a pod and returns the stdout and stderr.
0 commit comments