Skip to content

Commit b40a356

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

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

pkg/cri/server/container_execsync.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ type cappedWriter struct {
4343
remain int
4444
}
4545

46-
var errNoRemain = errors.New("no more space to write")
47-
4846
func (cw *cappedWriter) Write(p []byte) (int, error) {
4947
if cw.remain <= 0 {
50-
return 0, errNoRemain
48+
return len(p), nil
5149
}
5250

5351
end := cw.remain
@@ -60,26 +58,35 @@ func (cw *cappedWriter) Write(p []byte) (int, error) {
6058
if err != nil {
6159
return written, err
6260
}
63-
if written < len(p) {
64-
return written, errNoRemain
65-
}
66-
return written, nil
61+
return len(p), nil
6762
}
6863

6964
func (cw *cappedWriter) Close() error {
7065
return cw.w.Close()
7166
}
7267

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

7877
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+
7986
exitCode, err := c.execInContainer(ctx, r.GetContainerId(), execOptions{
8087
cmd: r.GetCmd(),
81-
stdout: &cappedWriter{w: cioutil.NewNopWriteCloser(&stdout), remain: maxStreamSize},
82-
stderr: &cappedWriter{w: cioutil.NewNopWriteCloser(&stderr), remain: maxStreamSize},
88+
stdout: cout,
89+
stderr: cerr,
8390
timeout: time.Duration(r.GetTimeout()) * time.Second,
8491
})
8592
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)