Skip to content

Commit 8d01e30

Browse files
committed
Merge branch 'master' of https://github.com/alibaba/pouch into cri-compatibility
2 parents 9339a70 + 19c956b commit 8d01e30

30 files changed

+330
-205
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Runrioter Wung <[email protected]>
4343
4444
4545
46+
soarpenguin <[email protected]>
4647
4748
Tao Qingyun <[email protected]>
4849
Tiramisu 1993 <[email protected]>

apis/swagger.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2894,7 +2894,10 @@ definitions:
28942894
AppArmorProfile:
28952895
type: "string"
28962896
ExecIDs:
2897-
type: "string"
2897+
description: "exec ids of container"
2898+
type: "array"
2899+
items:
2900+
type: "string"
28982901
HostConfig:
28992902
$ref: "#/definitions/HostConfig"
29002903
SizeRw:

apis/types/container_json.go

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/exec.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ func (e *ExecCommand) runExec(args []string) error {
138138
}
139139

140140
// execExample shows examples in exec command, and is used in auto-generated cli docs.
141-
// TODO: add example
142141
func execExample() string {
143-
return ""
142+
return `$ pouch exec -it 25bf50 ps
143+
PID USER TIME COMMAND
144+
1 root 0:00 /bin/sh
145+
38 root 0:00 ps
146+
`
144147
}

cli/top.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (top *TopCommand) runTop(args []string) error {
6262
// topExamples shows examples in top command, and is used in auto-generated cli docs.
6363
func topExamples() string {
6464
return `$ pouch top 44f675
65-
UID PID PPID C STIME TTY TIME CMD
66-
root 28725 28714 0 3月14 ? 00:00:00 sh
67-
`
65+
UID PID PPID C STIME TTY TIME CMD
66+
root 28725 28714 0 3月14 ? 00:00:00 sh
67+
`
6868
}

cri/stream/request_cache.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
TokenLen = 8
2020
)
2121

22-
// requestCache caches streaming (exec/attach/port-forward) requests and generates a single-use
22+
// RequestCache caches streaming (exec/attach/port-forward) requests and generates a single-use
2323
// random token for their retrieval. The requestCache is used for building streaming URLs without
2424
// the need to encode every request parameter in the URL.
2525
type RequestCache struct {
@@ -31,15 +31,16 @@ type RequestCache struct {
3131
lock sync.Mutex
3232
}
3333

34-
// Type representing an *ExecRequest, *AttachRequest, or *PortForwardRequest.
35-
type request interface{}
34+
// Request representing an *ExecRequest, *AttachRequest, or *PortForwardRequest Type.
35+
type Request interface{}
3636

3737
type cacheEntry struct {
3838
token string
39-
req request
39+
req Request
4040
expireTime time.Time
4141
}
4242

43+
// NewRequestCache return a RequestCache
4344
func NewRequestCache() *RequestCache {
4445
return &RequestCache{
4546
ll: list.New(),
@@ -48,7 +49,7 @@ func NewRequestCache() *RequestCache {
4849
}
4950

5051
// Insert the given request into the cache and returns the token used for fetching it out.
51-
func (c *RequestCache) Insert(req request) (token string, err error) {
52+
func (c *RequestCache) Insert(req Request) (token string, err error) {
5253
c.lock.Lock()
5354
defer c.lock.Unlock()
5455

@@ -69,7 +70,7 @@ func (c *RequestCache) Insert(req request) (token string, err error) {
6970
}
7071

7172
// Consume the token (remove it from the cache) and return the cached request, if found.
72-
func (c *RequestCache) Consume(token string) (req request, found bool) {
73+
func (c *RequestCache) Consume(token string) (req Request, found bool) {
7374
c.lock.Lock()
7475
defer c.lock.Unlock()
7576
ele, ok := c.tokens[token]

cri/v1alpha1/cri.go

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"io"
78
"os"
89
"path"
910
"path/filepath"
@@ -695,55 +696,74 @@ func (c *CriManager) UpdateContainerResources(ctx context.Context, r *runtime.Up
695696
// ExecSync executes a command in the container, and returns the stdout output.
696697
// If command exits with a non-zero exit code, an error is returned.
697698
func (c *CriManager) ExecSync(ctx context.Context, r *runtime.ExecSyncRequest) (*runtime.ExecSyncResponse, error) {
698-
// TODO: handle timeout.
699699
id := r.GetContainerId()
700700

701+
timeout := time.Duration(r.GetTimeout()) * time.Second
702+
var cancel context.CancelFunc
703+
if timeout == 0 {
704+
ctx, cancel = context.WithCancel(ctx)
705+
} else {
706+
ctx, cancel = context.WithTimeout(ctx, timeout)
707+
}
708+
defer cancel()
709+
701710
createConfig := &apitypes.ExecCreateConfig{
702711
Cmd: r.GetCmd(),
703712
}
704-
705713
execid, err := c.ContainerMgr.CreateExec(ctx, id, createConfig)
706714
if err != nil {
707715
return nil, fmt.Errorf("failed to create exec for container %q: %v", id, err)
708716
}
709717

710-
var output bytes.Buffer
711-
startConfig := &apitypes.ExecStartConfig{}
718+
reader, writer := io.Pipe()
719+
defer writer.Close()
720+
712721
attachConfig := &mgr.AttachConfig{
713722
Stdout: true,
714723
Stderr: true,
715-
MemBuffer: &output,
724+
Pipe: writer,
716725
MuxDisabled: true,
717726
}
718727

728+
startConfig := &apitypes.ExecStartConfig{}
729+
719730
err = c.ContainerMgr.StartExec(ctx, execid, startConfig, attachConfig)
720731
if err != nil {
721732
return nil, fmt.Errorf("failed to start exec for container %q: %v", id, err)
722733
}
723734

724-
var execConfig *mgr.ContainerExecConfig
725-
for {
726-
execConfig, err = c.ContainerMgr.GetExecConfig(ctx, execid)
735+
readWaitCh := make(chan error, 1)
736+
var recv bytes.Buffer
737+
go func() {
738+
defer reader.Close()
739+
_, err = io.Copy(&recv, reader)
740+
readWaitCh <- err
741+
}()
742+
743+
select {
744+
case <-ctx.Done():
745+
//TODO maybe stop the execution?
746+
return nil, fmt.Errorf("timeout %v exceeded", timeout)
747+
case readWaitErr := <-readWaitCh:
748+
if readWaitErr != nil {
749+
return nil, fmt.Errorf("failed to read data from the pipe: %v", err)
750+
}
751+
execConfig, err := c.ContainerMgr.GetExecConfig(ctx, execid)
727752
if err != nil {
728753
return nil, fmt.Errorf("failed to inspect exec for container %q: %v", id, err)
729754
}
730-
// Loop until exec finished.
731-
if !execConfig.Running {
732-
break
755+
756+
var stderr []byte
757+
if execConfig.Error != nil {
758+
stderr = []byte(execConfig.Error.Error())
733759
}
734-
time.Sleep(100 * time.Millisecond)
735-
}
736760

737-
var stderr []byte
738-
if execConfig.Error != nil {
739-
stderr = []byte(execConfig.Error.Error())
761+
return &runtime.ExecSyncResponse{
762+
Stdout: recv.Bytes(),
763+
Stderr: stderr,
764+
ExitCode: int32(execConfig.ExitCode),
765+
}, nil
740766
}
741-
742-
return &runtime.ExecSyncResponse{
743-
Stdout: output.Bytes(),
744-
Stderr: stderr,
745-
ExitCode: int32(execConfig.ExitCode),
746-
}, nil
747767
}
748768

749769
// Exec prepares a streaming endpoint to execute a command in the container, and returns the address.

cri/v1alpha1/cri_utils_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func Test_makeSandboxPouchConfig(t *testing.T) {
282282
want *apitypes.ContainerCreateConfig
283283
wantErr bool
284284
}{
285-
// TODO: Add test cases.
285+
// TODO: Add test cases.
286286
}
287287
for _, tt := range tests {
288288
t.Run(tt.name, func(t *testing.T) {
@@ -342,7 +342,7 @@ func Test_toCriSandbox(t *testing.T) {
342342
want *runtime.PodSandbox
343343
wantErr bool
344344
}{
345-
// TODO: Add test cases.
345+
// TODO: Add test cases.
346346
}
347347
for _, tt := range tests {
348348
t.Run(tt.name, func(t *testing.T) {
@@ -558,7 +558,7 @@ func Test_makeContainerName(t *testing.T) {
558558
args args
559559
want string
560560
}{
561-
// TODO: Add test cases.
561+
// TODO: Add test cases.
562562
}
563563
for _, tt := range tests {
564564
t.Run(tt.name, func(t *testing.T) {
@@ -579,7 +579,7 @@ func Test_modifyContainerNamespaceOptions(t *testing.T) {
579579
name string
580580
args args
581581
}{
582-
// TODO: Add test cases.
582+
// TODO: Add test cases.
583583
}
584584
for _, tt := range tests {
585585
t.Run(tt.name, func(t *testing.T) {
@@ -600,7 +600,7 @@ func Test_applyContainerSecurityContext(t *testing.T) {
600600
args args
601601
wantErr bool
602602
}{
603-
// TODO: Add test cases.
603+
// TODO: Add test cases.
604604
}
605605
for _, tt := range tests {
606606
t.Run(tt.name, func(t *testing.T) {
@@ -628,7 +628,7 @@ func TestCriManager_updateCreateConfig(t *testing.T) {
628628
args args
629629
wantErr bool
630630
}{
631-
// TODO: Add test cases.
631+
// TODO: Add test cases.
632632
}
633633
for _, tt := range tests {
634634
t.Run(tt.name, func(t *testing.T) {
@@ -653,7 +653,7 @@ func Test_toCriContainer(t *testing.T) {
653653
want *runtime.Container
654654
wantErr bool
655655
}{
656-
// TODO: Add test cases.
656+
// TODO: Add test cases.
657657
}
658658
for _, tt := range tests {
659659
t.Run(tt.name, func(t *testing.T) {
@@ -680,7 +680,7 @@ func Test_imageToCriImage(t *testing.T) {
680680
want *runtime.Image
681681
wantErr bool
682682
}{
683-
// TODO: Add test cases.
683+
// TODO: Add test cases.
684684
}
685685
for _, tt := range tests {
686686
t.Run(tt.name, func(t *testing.T) {
@@ -711,7 +711,7 @@ func TestCriManager_ensureSandboxImageExists(t *testing.T) {
711711
args args
712712
wantErr bool
713713
}{
714-
// TODO: Add test cases.
714+
// TODO: Add test cases.
715715
}
716716
for _, tt := range tests {
717717
t.Run(tt.name, func(t *testing.T) {
@@ -736,7 +736,7 @@ func Test_getUserFromImageUser(t *testing.T) {
736736
want *int64
737737
want1 string
738738
}{
739-
// TODO: Add test cases.
739+
// TODO: Add test cases.
740740
}
741741
for _, tt := range tests {
742742
t.Run(tt.name, func(t *testing.T) {
@@ -760,7 +760,7 @@ func Test_parseUserFromImageUser(t *testing.T) {
760760
args args
761761
want string
762762
}{
763-
// TODO: Add test cases.
763+
// TODO: Add test cases.
764764
}
765765
for _, tt := range tests {
766766
t.Run(tt.name, func(t *testing.T) {

cri/v1alpha1/service/cri.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"google.golang.org/grpc"
1212
"k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
13-
1413
)
1514

1615
// Service serves the kubelet runtime grpc api which will be consumed by kubelet.

0 commit comments

Comments
 (0)