Skip to content

Commit f920683

Browse files
Kazuyoshi Katodmcgowan
authored andcommitted
Implicitly discard the input to drain the reader
Signed-off-by: Derek McGowan <[email protected]>
1 parent 2eb6721 commit f920683

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

pkg/cri/server/container_execsync.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package server
1818

1919
import (
2020
"bytes"
21-
"errors"
2221
"fmt"
2322
"io"
2423
"syscall"
@@ -44,11 +43,9 @@ type cappedWriter struct {
4443
remain int
4544
}
4645

47-
var errNoRemain = errors.New("no more space to write")
48-
4946
func (cw *cappedWriter) Write(p []byte) (int, error) {
5047
if cw.remain <= 0 {
51-
return 0, errNoRemain
48+
return len(p), nil
5249
}
5350

5451
end := cw.remain
@@ -61,26 +58,35 @@ func (cw *cappedWriter) Write(p []byte) (int, error) {
6158
if err != nil {
6259
return written, err
6360
}
64-
if written < len(p) {
65-
return written, errNoRemain
66-
}
67-
return written, nil
61+
return len(p), nil
6862
}
6963

7064
func (cw *cappedWriter) Close() error {
7165
return cw.w.Close()
7266
}
7367

68+
func (cw *cappedWriter) isFull() bool {
69+
return cw.remain <= 0
70+
}
71+
7472
// ExecSync executes a command in the container, and returns the stdout output.
7573
// If command exits with a non-zero exit code, an error is returned.
7674
func (c *criService) ExecSync(ctx context.Context, r *runtime.ExecSyncRequest) (*runtime.ExecSyncResponse, error) {
7775
const maxStreamSize = 1024 * 1024 * 16
7876

7977
var stdout, stderr bytes.Buffer
78+
79+
// cappedWriter truncates the output. In that case, the size of
80+
// the ExecSyncResponse will hit the CRI plugin's gRPC response limit.
81+
// Thus the callers outside of the containerd process (e.g. Kubelet) never see
82+
// the truncated output.
83+
cout := &cappedWriter{w: cioutil.NewNopWriteCloser(&stdout), remain: maxStreamSize}
84+
cerr := &cappedWriter{w: cioutil.NewNopWriteCloser(&stderr), remain: maxStreamSize}
85+
8086
exitCode, err := c.execInContainer(ctx, r.GetContainerId(), execOptions{
8187
cmd: r.GetCmd(),
82-
stdout: &cappedWriter{w: cioutil.NewNopWriteCloser(&stdout), remain: maxStreamSize},
83-
stderr: &cappedWriter{w: cioutil.NewNopWriteCloser(&stderr), remain: maxStreamSize},
88+
stdout: cout,
89+
stderr: cerr,
8490
timeout: time.Duration(r.GetTimeout()) * time.Second,
8591
})
8692
if err != nil {

pkg/cri/server/container_execsync_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ func TestCWWrite(t *testing.T) {
3333
assert.Equal(t, 5, n)
3434

3535
n, err = cw.Write([]byte("helloworld"))
36-
assert.Equal(t, []byte("hellohello"), buf.Bytes(), "partial write")
37-
assert.Equal(t, 5, n)
38-
assert.ErrorIs(t, err, errNoRemain)
36+
assert.NoError(t, err, "no errors even it hits the cap")
37+
assert.Equal(t, 10, n, "no indication of partial write")
38+
assert.True(t, cw.isFull())
39+
assert.Equal(t, []byte("hellohello"), buf.Bytes(), "the underlying writer is capped")
3940

4041
_, err = cw.Write([]byte("world"))
41-
assert.ErrorIs(t, err, errNoRemain)
42+
assert.NoError(t, err)
43+
assert.True(t, cw.isFull())
44+
assert.Equal(t, []byte("hellohello"), buf.Bytes(), "the underlying writer is capped")
4245
}
4346

4447
func TestCWClose(t *testing.T) {

0 commit comments

Comments
 (0)