Skip to content

Commit 59793e5

Browse files
authored
Add 30 second timeout for ExecCommandContainer (#3248)
1 parent 9ffe712 commit 59793e5

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

internal/clientsholder/command.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"context"
2222
"strings"
23+
"time"
2324

2425
"github.com/redhat-best-practices-for-k8s/certsuite/internal/log"
2526
corev1 "k8s.io/api/core/v1"
@@ -32,6 +33,11 @@ type Command interface {
3233
ExecCommandContainer(Context, string) (string, string, error)
3334
}
3435

36+
const (
37+
// ExecCommandTimeout defines the maximum duration allowed for a single container exec
38+
ExecCommandTimeout = 30 * time.Second
39+
)
40+
3541
// ExecCommand runs command in the pod and returns buffer output.
3642
func (clientsholder *ClientsHolder) ExecCommandContainer(
3743
ctx Context, command string) (stdout, stderr string, err error) {
@@ -60,7 +66,11 @@ func (clientsholder *ClientsHolder) ExecCommandContainer(
6066
log.Error("%v", err)
6167
return stdout, stderr, err
6268
}
63-
err = exec.StreamWithContext(context.TODO(), remotecommand.StreamOptions{
69+
// enforce an execution timeout for the remote command
70+
goCtx, cancel := context.WithTimeout(context.TODO(), ExecCommandTimeout)
71+
defer cancel()
72+
73+
err = exec.StreamWithContext(goCtx, remotecommand.StreamOptions{
6474
Stdout: &buffOut,
6575
Stderr: &buffErr,
6676
})

0 commit comments

Comments
 (0)