Skip to content

Commit 6fc80d2

Browse files
authored
Experiment: Replace NewSPDYExecutor (#1217)
1 parent 11b4043 commit 6fc80d2

File tree

2 files changed

+17
-57
lines changed

2 files changed

+17
-57
lines changed

tests/globalhelper/pod.go

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"os"
87
"strings"
98
"time"
109

@@ -13,44 +12,25 @@ import (
1312
corev1 "k8s.io/api/core/v1"
1413
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1514
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16-
"k8s.io/client-go/kubernetes/scheme"
1715
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
18-
"k8s.io/client-go/tools/remotecommand"
1916
klog "k8s.io/klog/v2"
17+
18+
egiPod "github.com/openshift-kni/eco-goinfra/pkg/pod"
2019
)
2120

2221
// ExecCommand runs command in the pod and returns buffer output.
2322
func ExecCommand(pod corev1.Pod, command []string) (bytes.Buffer, error) {
2423
var buf bytes.Buffer
2524

26-
req := GetAPIClient().CoreV1Interface.RESTClient().
27-
Post().
28-
Namespace(pod.Namespace).
29-
Resource("pods").
30-
Name(pod.Name).
31-
SubResource("exec").
32-
VersionedParams(&corev1.PodExecOptions{
33-
Container: pod.Spec.Containers[0].Name,
34-
Command: command,
35-
Stdin: true,
36-
Stdout: true,
37-
Stderr: true,
38-
TTY: true,
39-
}, scheme.ParameterCodec)
40-
41-
exec, err := remotecommand.NewSPDYExecutor(GetAPIClient().Config, "POST", req.URL())
25+
builder, err := egiPod.Pull(GetEcoGoinfraClient(), pod.Name, pod.Namespace)
4226
if err != nil {
4327
return buf, err
4428
}
4529

46-
err = exec.StreamWithContext(context.TODO(), remotecommand.StreamOptions{
47-
Stdin: os.Stdin,
48-
Stdout: &buf,
49-
Stderr: os.Stderr,
50-
Tty: true,
51-
})
30+
// Exec in the first container by default (matches previous behavior)
31+
buf, err = builder.ExecCommand(command)
5232
if err != nil {
53-
return buf, err
33+
return bytes.Buffer{}, err
5434
}
5535

5636
return buf, nil

tests/performance/helper/helper.go

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88

99
"github.com/redhat-best-practices-for-k8s/certsuite-qe/tests/globalhelper"
1010
"github.com/redhat-best-practices-for-k8s/certsuite-qe/tests/utils/pod"
11-
"k8s.io/client-go/kubernetes/scheme"
12-
"k8s.io/client-go/tools/remotecommand"
1311
klog "k8s.io/klog/v2"
1412
"k8s.io/utils/ptr"
1513

@@ -20,6 +18,8 @@ import (
2018
k8serrors "k8s.io/apimachinery/pkg/api/errors"
2119
"k8s.io/apimachinery/pkg/api/resource"
2220
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
22+
egiPod "github.com/openshift-kni/eco-goinfra/pkg/pod"
2323
)
2424

2525
func DefineExclusivePod(podName string, namespace string, image string, label map[string]string) *corev1.Pod {
@@ -140,46 +140,26 @@ func ExecCommandContainer(
140140

141141
var buffOut bytes.Buffer
142142

143-
var buffErr bytes.Buffer
144-
145143
podName := pod.Name
146144
podNamespace := pod.Namespace
147-
container := pod.Spec.Containers[0].Name
148145

146+
containerName := pod.Spec.Containers[0].Name
149147
klog.V(5).Infof("execute command on ns=%s, pod=%s container=%s, cmd: %s",
150-
podNamespace, podName, container, strings.Join(commandStr, " "))
151-
152-
req := globalhelper.GetAPIClient().CoreV1Interface.RESTClient().
153-
Post().
154-
Namespace(podNamespace).
155-
Resource("pods").
156-
Name(podName).
157-
SubResource("exec").
158-
VersionedParams(&corev1.PodExecOptions{
159-
Container: container,
160-
Command: commandStr,
161-
Stdin: false,
162-
Stdout: true,
163-
Stderr: true,
164-
TTY: false,
165-
}, scheme.ParameterCodec)
166-
167-
exec, err := remotecommand.NewSPDYExecutor(globalhelper.GetAPIClient().Config, "POST", req.URL())
148+
podNamespace, podName, containerName, strings.Join(commandStr, " "))
149+
150+
builder, err := egiPod.Pull(globalhelper.GetEcoGoinfraClient(), podName, podNamespace)
168151
if err != nil {
169152
klog.ErrorS(err, "failed to create SPDY executor")
170153

171-
return stdout, stderr, err
154+
return "", "", err
172155
}
173156

174-
err = exec.StreamWithContext(context.TODO(), remotecommand.StreamOptions{
175-
Stdout: &buffOut,
176-
Stderr: &buffErr,
177-
})
178-
179-
stdout, stderr = buffOut.String(), buffErr.String()
157+
buffOut, err = builder.ExecCommand(commandStr)
158+
stdout = buffOut.String()
159+
// stderr is not captured by eco-goinfra ExecCommand; leave empty
180160

181161
if err != nil {
182-
klog.ErrorS(err, "exec stream failed", "url", req.URL(), "command", command, "stderr", stderr, "stdout", stdout)
162+
klog.ErrorS(err, "exec stream failed", "command", command, "stderr", stderr, "stdout", stdout)
183163

184164
return stdout, stderr, err
185165
}

0 commit comments

Comments
 (0)