Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 0024210

Browse files
committed
finally, we could remove the todo line for start API
//TODO: remove the tty stream in StartPod API, now we could support attach after created //Done. Signed-off-by: Wang Xu <gnawux@gmail.com>
1 parent 392fb71 commit 0024210

File tree

9 files changed

+67
-219
lines changed

9 files changed

+67
-219
lines changed

client/api/start.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,17 @@
11
package api
22

33
import (
4-
"fmt"
54
"net/url"
6-
7-
"github.com/hyperhq/hyperd/engine"
8-
"github.com/hyperhq/runv/hypervisor/types"
95
)
106

117
func (cli *Client) StartPod(podId string) error {
128
v := url.Values{}
139
v.Set("podId", podId)
1410

15-
body, _, err := readBody(cli.call("POST", "/pod/start?"+v.Encode(), nil, nil))
16-
if err != nil {
17-
return err
18-
}
19-
out := engine.NewOutput()
20-
remoteInfo, err := out.AddEnv()
11+
_, _, err := readBody(cli.call("POST", "/pod/start?"+v.Encode(), nil, nil))
2112
if err != nil {
2213
return err
2314
}
24-
25-
if _, err := out.Write(body); err != nil {
26-
return fmt.Errorf("Error reading remote info: %s", err)
27-
}
28-
out.Close()
29-
errCode := remoteInfo.GetInt("Code")
30-
if errCode != types.E_OK {
31-
if errCode != types.E_BAD_REQUEST &&
32-
errCode != types.E_FAILED {
33-
return fmt.Errorf("Error code is %d", errCode)
34-
} else {
35-
return fmt.Errorf("Cause is %s", remoteInfo.Get("Cause"))
36-
}
37-
}
3815
return nil
3916
}
4017

daemon/daemonbuilder/pod.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (d Docker) ContainerStart(cId string, hostConfig *containertypes.HostConfig
190190
}
191191
}()
192192

193-
if _, _, err = d.Daemon.StartPod(nil, nil, podId, false); err != nil {
193+
if err = d.Daemon.StartPod(podId); err != nil {
194194
return
195195
}
196196

daemon/run.go

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package daemon
22

33
import (
44
"fmt"
5-
"io"
65

76
"github.com/golang/glog"
87

@@ -43,39 +42,21 @@ func (daemon *Daemon) CreatePod(podId string, podSpec *apitypes.UserPod) (*pod.X
4342
return p, nil
4443
}
4544

46-
//TODO: remove the tty stream in StartPod API, now we could support attach after created
47-
func (daemon *Daemon) StartPod(stdin io.ReadCloser, stdout io.WriteCloser, podId string, attach bool) (int, string, error) {
45+
func (daemon *Daemon) StartPod(podId string) error {
4846
p, ok := daemon.PodList.Get(podId)
4947
if !ok {
50-
return -1, "", fmt.Errorf("The pod(%s) can not be found, please create it first", podId)
51-
}
52-
53-
var waitTty chan error
54-
55-
if attach {
56-
glog.V(1).Info("Run pod with tty attached")
57-
58-
ids := p.ContainerIdsOf(apitypes.UserContainer_REGULAR)
59-
for _, id := range ids {
60-
waitTty = make(chan error, 1)
61-
p.Attach(id, stdin, stdout, waitTty)
62-
break
63-
}
48+
return fmt.Errorf("The pod(%s) can not be found, please create it first", podId)
6449
}
6550

6651
glog.Infof("Starting pod %q in vm: %q", podId, p.SandboxName())
6752

6853
err := p.Start()
6954
if err != nil {
7055
glog.Infof("failed to start pod %s: %v", p.Id(), err)
71-
return -1, err.Error(), err
72-
}
73-
74-
if waitTty != nil {
75-
<-waitTty
56+
return err
7657
}
7758

78-
return 0, "", err
59+
return err
7960
}
8061

8162
func (daemon *Daemon) WaitContainer(cid string, second int) (int, error) {

daemon/server.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,12 @@ func (daemon *Daemon) CmdSetPodLabels(podId string, override bool, labels map[st
239239
}
240240

241241
func (daemon *Daemon) CmdStartPod(podId string) (*engine.Env, error) {
242-
code, cause, err := daemon.StartPod(nil, nil, podId, false)
242+
err := daemon.StartPod(podId)
243243
if err != nil {
244244
return nil, err
245245
}
246246

247247
v := &engine.Env{}
248-
v.SetInt("Code", code)
249-
v.Set("Cause", cause)
250-
251248
return v, nil
252249
}
253250

integration/client.go

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -453,36 +453,13 @@ func (c *HyperClient) ContainerExecStart(containerId, execId string, stdin io.Re
453453
}
454454

455455
// StartPod starts a pod by podID
456-
func (c *HyperClient) StartPod(podID, vmID string, attach bool) error {
457-
stream, err := c.client.PodStart(context.Background())
458-
if err != nil {
459-
return err
460-
}
461-
462-
req := types.PodStartMessage{
463-
PodID: podID,
464-
VmID: vmID,
465-
Attach: attach,
466-
}
467-
if err := stream.Send(&req); err != nil {
468-
return err
469-
}
470-
471-
if attach {
472-
if _, err := stream.Recv(); err != nil {
473-
return err
474-
}
475-
476-
return nil
456+
func (c *HyperClient) StartPod(podID string) error {
457+
req := &types.PodStartRequest{
458+
PodID: podID,
477459
}
478460

479-
cmd := types.PodStartMessage{
480-
Data: []byte("ls\n"),
481-
}
482-
if err := stream.Send(&cmd); err != nil {
483-
return err
484-
}
485-
if _, err := stream.Recv(); err != nil {
461+
_, err := c.client.PodStart(c.ctx, req)
462+
if err != nil {
486463
return err
487464
}
488465

integration/hyper_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (s *TestSuite) TestPostAttach(c *C) {
9898
c.Logf("Pod created: %s", pod)
9999
defer s.client.RemovePod(pod)
100100

101-
err = s.client.StartPod(pod, "", false)
101+
err = s.client.StartPod(pod)
102102
c.Assert(err, IsNil)
103103

104104
podInfo, err := s.client.GetPodInfo(pod)
@@ -154,7 +154,7 @@ func (s *TestSuite) TestCreateAndStartPod(c *C) {
154154
c.Errorf("Can't found pod %s", pod)
155155
}
156156

157-
err = s.client.StartPod(pod, "", false)
157+
err = s.client.StartPod(pod)
158158
c.Assert(err, IsNil)
159159

160160
podInfo, err := s.client.GetPodInfo(pod)
@@ -296,7 +296,7 @@ func (s *TestSuite) TestAddListDeleteService(c *C) {
296296

297297
c.Log(" 2 ===> create pod")
298298

299-
err = s.client.StartPod(pod, "", false)
299+
err = s.client.StartPod(pod)
300300
c.Assert(err, IsNil)
301301

302302
updateService := []*types.UserService{
@@ -368,7 +368,7 @@ func (s *TestSuite) TestStartAndStopPod(c *C) {
368368
c.Assert(err, IsNil)
369369
c.Logf("Pod created: %s", pod)
370370

371-
err = s.client.StartPod(pod, "", false)
371+
err = s.client.StartPod(pod)
372372
c.Assert(err, IsNil)
373373

374374
podInfo, err := s.client.GetPodInfo(pod)
@@ -429,7 +429,7 @@ func (s *TestSuite) TestPauseAndUnpausePod(c *C) {
429429
c.Assert(err, IsNil)
430430
c.Logf("Pod created: %s", pod)
431431

432-
err = s.client.StartPod(pod, "", false)
432+
err = s.client.StartPod(pod)
433433
c.Assert(err, IsNil)
434434

435435
podInfo, err := s.client.GetPodInfo(pod)
@@ -479,7 +479,7 @@ func (s *TestSuite) TestGetPodStats(c *C) {
479479
c.Assert(err, IsNil)
480480
}()
481481

482-
err = s.client.StartPod(podID, "", false)
482+
err = s.client.StartPod(podID)
483483
c.Assert(err, IsNil)
484484

485485
stats, err := s.client.GetPodStats(podID)

serverrpc/pod.go

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,68 +26,17 @@ func (s *ServerRPC) PodCreate(ctx context.Context, req *types.PodCreateRequest)
2626
}
2727

2828
// PodStart starts a pod by podID
29-
func (s *ServerRPC) PodStart(stream types.PublicAPI_PodStartServer) error {
30-
req, err := stream.Recv()
31-
if err == io.EOF {
32-
return nil
33-
}
34-
if err != nil {
35-
return err
36-
}
29+
func (s *ServerRPC) PodStart(ctx context.Context, req *types.PodStartRequest) (*types.PodStartResponse, error) {
3730
glog.V(3).Infof("PodStart with request %s", req.String())
3831

39-
if !req.Attach {
40-
_, _, err := s.daemon.StartPod(nil, nil, req.PodID, req.Attach)
41-
if err != nil {
42-
glog.Errorf("StartPod failed: %v", err)
43-
return err
44-
}
45-
46-
// Send an empty message to client, so client could wait for start complete
47-
if err := stream.Send(&types.PodStartMessage{}); err != nil {
48-
return err
49-
}
50-
51-
return nil
52-
}
53-
54-
ir, iw := io.Pipe()
55-
or, ow := io.Pipe()
32+
err := s.daemon.StartPod(req.PodID)
5633

57-
go func() {
58-
for {
59-
cmd, err := stream.Recv()
60-
if err != nil {
61-
return
62-
}
63-
64-
if _, err := iw.Write(cmd.Data); err != nil {
65-
return
66-
}
67-
}
68-
69-
}()
70-
71-
go func() {
72-
for {
73-
res := make([]byte, 512)
74-
n, err := or.Read(res)
75-
if err != nil {
76-
return
77-
}
78-
79-
if err := stream.Send(&types.PodStartMessage{Data: res[:n]}); err != nil {
80-
return
81-
}
82-
}
83-
}()
84-
85-
if _, _, err := s.daemon.StartPod(ir, ow, req.PodID, req.Attach); err != nil {
34+
if err != nil {
8635
glog.Errorf("StartPod failed: %v", err)
87-
return err
36+
return nil, err
8837
}
8938

90-
return nil
39+
return &types.PodStartResponse{}, nil
9140
}
9241

9342
// PodRemove removes a pod by podID

0 commit comments

Comments
 (0)