This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
containerd-shim-kata-v2: add the start service support
Add the Start api support of start a pod or container created before. Signed-off-by: fupan <[email protected]>
- Loading branch information
Showing
5 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2018 HyperHQ Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package containerdshim | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/containerd/containerd/api/types/task" | ||
"github.com/kata-containers/runtime/pkg/katautils" | ||
) | ||
|
||
func startContainer(ctx context.Context, s *service, c *container) error { | ||
//start a container | ||
if c.cType == "" { | ||
err := fmt.Errorf("Bug, the container %s type is empty", c.id) | ||
return err | ||
} | ||
|
||
if s.sandbox == nil { | ||
err := fmt.Errorf("Bug, the sandbox hasn't been created for this container %s", c.id) | ||
return err | ||
} | ||
|
||
if c.cType.IsSandbox() { | ||
err := s.sandbox.Start() | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
_, err := s.sandbox.StartContainer(c.id) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// Run post-start OCI hooks. | ||
err := katautils.EnterNetNS(s.sandbox.GetNetNs(), func() error { | ||
return katautils.PostStartHooks(ctx, *c.spec, s.sandbox.ID(), c.bundle) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.status = task.StatusRunning | ||
|
||
stdin, stdout, stderr, err := s.sandbox.IOStream(c.id, c.id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if c.stdin != "" || c.stdout != "" || c.stderr != "" { | ||
tty, err := newTtyIO(ctx, c.stdin, c.stdout, c.stderr, c.terminal) | ||
if err != nil { | ||
return err | ||
} | ||
c.ttyio = tty | ||
go ioCopy(c.exitIOch, tty, stdin, stdout, stderr) | ||
} else { | ||
//close the io exit channel, since there is no io for this container, | ||
//otherwise the following wait goroutine will hang on this channel. | ||
close(c.exitIOch) | ||
} | ||
|
||
go wait(s, c, "") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2018 HyperHQ Inc. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
package containerdshim | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/containerd/containerd/api/types/task" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func wait(s *service, c *container, execID string) (int32, error) { | ||
var execs *exec | ||
var err error | ||
|
||
processID := c.id | ||
|
||
if execID == "" { | ||
//wait until the io closed, then wait the container | ||
<-c.exitIOch | ||
} | ||
|
||
ret, err := s.sandbox.WaitProcess(c.id, processID) | ||
if err != nil { | ||
logrus.WithError(err).WithFields(logrus.Fields{ | ||
"container": c.id, | ||
"pid": processID, | ||
}).Error("Wait for process failed") | ||
} | ||
|
||
if execID == "" { | ||
c.exitCh <- uint32(ret) | ||
} else { | ||
execs.exitCh <- uint32(ret) | ||
} | ||
|
||
timeStamp := time.Now() | ||
c.mu.Lock() | ||
if execID == "" { | ||
c.status = task.StatusStopped | ||
c.exit = uint32(ret) | ||
c.time = timeStamp | ||
} else { | ||
execs.status = task.StatusStopped | ||
execs.exitCode = ret | ||
execs.exitTime = timeStamp | ||
} | ||
c.mu.Unlock() | ||
|
||
go cReap(s, int(ret), c.id, execID, timeStamp) | ||
|
||
return ret, nil | ||
} |