2016-07-29 22:35:10 +00:00
|
|
|
package oci
|
|
|
|
|
|
|
|
import (
|
2016-08-29 18:48:58 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-09-16 23:32:19 +00:00
|
|
|
"io/ioutil"
|
2016-08-01 17:39:42 +00:00
|
|
|
"os"
|
2016-08-29 18:48:58 +00:00
|
|
|
"os/exec"
|
2016-07-29 22:35:10 +00:00
|
|
|
"path/filepath"
|
2016-09-16 23:32:19 +00:00
|
|
|
"strconv"
|
2016-07-29 22:35:10 +00:00
|
|
|
"strings"
|
2016-09-15 23:40:44 +00:00
|
|
|
"syscall"
|
2016-08-31 23:32:17 +00:00
|
|
|
"time"
|
2016-07-29 22:35:10 +00:00
|
|
|
|
2017-08-29 17:09:59 +00:00
|
|
|
"github.com/containerd/cgroups"
|
2016-12-14 11:15:20 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/utils"
|
2017-05-25 09:11:14 +00:00
|
|
|
rspec "github.com/opencontainers/runtime-spec/specs-go"
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-10-15 20:05:41 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-09-27 18:35:40 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-08-29 09:25:51 +00:00
|
|
|
kwait "k8s.io/apimachinery/pkg/util/wait"
|
2016-07-29 22:35:10 +00:00
|
|
|
)
|
|
|
|
|
2016-10-06 22:16:21 +00:00
|
|
|
const (
|
|
|
|
// ContainerStateCreated represents the created state of a container
|
|
|
|
ContainerStateCreated = "created"
|
2017-09-19 12:26:32 +00:00
|
|
|
// ContainerStatePaused represents the paused state of a container
|
|
|
|
ContainerStatePaused = "paused"
|
2016-10-06 22:16:21 +00:00
|
|
|
// ContainerStateRunning represents the running state of a container
|
|
|
|
ContainerStateRunning = "running"
|
|
|
|
// ContainerStateStopped represents the stopped state of a container
|
|
|
|
ContainerStateStopped = "stopped"
|
2016-10-13 06:07:22 +00:00
|
|
|
// ContainerCreateTimeout represents the value of container creating timeout
|
2017-09-28 13:01:46 +00:00
|
|
|
ContainerCreateTimeout = 240 * time.Second
|
2017-08-29 21:11:30 +00:00
|
|
|
|
|
|
|
// CgroupfsCgroupsManager represents cgroupfs native cgroup manager
|
|
|
|
CgroupfsCgroupsManager = "cgroupfs"
|
|
|
|
// SystemdCgroupsManager represents systemd native cgroup manager
|
|
|
|
SystemdCgroupsManager = "systemd"
|
2017-10-02 13:38:55 +00:00
|
|
|
// ContainerExitsDir is the location of container exit dirs
|
|
|
|
ContainerExitsDir = "/var/run/crio/exits"
|
2017-10-23 15:35:38 +00:00
|
|
|
// ContainerAttachSocketDir is the location for container attach sockets
|
|
|
|
ContainerAttachSocketDir = "/var/run/crio"
|
2017-10-15 20:05:41 +00:00
|
|
|
|
|
|
|
// killContainerTimeout is the timeout that we wait for the container to
|
|
|
|
// be SIGKILLed.
|
|
|
|
killContainerTimeout = 2 * time.Minute
|
2016-10-06 22:16:21 +00:00
|
|
|
)
|
|
|
|
|
2016-07-29 22:35:10 +00:00
|
|
|
// New creates a new Runtime with options provided
|
2017-09-22 14:50:48 +00:00
|
|
|
func New(runtimeTrustedPath string,
|
|
|
|
runtimeUntrustedPath string,
|
|
|
|
trustLevel string,
|
|
|
|
conmonPath string,
|
|
|
|
conmonEnv []string,
|
|
|
|
cgroupManager string,
|
|
|
|
containerExitsDir string,
|
|
|
|
logSizeMax int64,
|
|
|
|
noPivot bool) (*Runtime, error) {
|
2016-07-29 22:35:10 +00:00
|
|
|
r := &Runtime{
|
2017-08-13 01:35:55 +00:00
|
|
|
name: filepath.Base(runtimeTrustedPath),
|
|
|
|
trustedPath: runtimeTrustedPath,
|
|
|
|
untrustedPath: runtimeUntrustedPath,
|
|
|
|
trustLevel: trustLevel,
|
|
|
|
conmonPath: conmonPath,
|
|
|
|
conmonEnv: conmonEnv,
|
|
|
|
cgroupManager: cgroupManager,
|
|
|
|
containerExitsDir: containerExitsDir,
|
2017-09-25 22:28:29 +00:00
|
|
|
logSizeMax: logSizeMax,
|
2017-09-22 14:50:48 +00:00
|
|
|
noPivot: noPivot,
|
2016-07-29 22:35:10 +00:00
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:22:34 +00:00
|
|
|
// Runtime stores the information about a oci runtime
|
2016-07-29 22:35:10 +00:00
|
|
|
type Runtime struct {
|
2017-08-13 01:35:55 +00:00
|
|
|
name string
|
|
|
|
trustedPath string
|
|
|
|
untrustedPath string
|
|
|
|
trustLevel string
|
|
|
|
conmonPath string
|
|
|
|
conmonEnv []string
|
|
|
|
cgroupManager string
|
|
|
|
containerExitsDir string
|
2017-09-25 22:28:29 +00:00
|
|
|
logSizeMax int64
|
2017-09-22 14:50:48 +00:00
|
|
|
noPivot bool
|
2016-07-29 22:35:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 23:40:44 +00:00
|
|
|
// syncInfo is used to return data from monitor process to daemon
|
|
|
|
type syncInfo struct {
|
2017-05-12 20:30:30 +00:00
|
|
|
Pid int `json:"pid"`
|
|
|
|
Message string `json:"message,omitempty"`
|
2016-09-15 23:40:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 22:18:14 +00:00
|
|
|
// exitCodeInfo is used to return the monitored process exit code to the daemon
|
|
|
|
type exitCodeInfo struct {
|
2017-06-21 17:39:02 +00:00
|
|
|
ExitCode int32 `json:"exit_code"`
|
|
|
|
Message string `json:"message,omitempty"`
|
2017-01-10 22:18:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-29 22:35:10 +00:00
|
|
|
// Name returns the name of the OCI Runtime
|
|
|
|
func (r *Runtime) Name() string {
|
|
|
|
return r.name
|
|
|
|
}
|
|
|
|
|
2017-02-21 10:51:16 +00:00
|
|
|
// Path returns the full path the OCI Runtime executable.
|
2017-06-02 21:15:19 +00:00
|
|
|
// Depending if the container is privileged and/or trusted,
|
|
|
|
// this will return either the trusted or untrusted runtime path.
|
2017-02-21 10:51:16 +00:00
|
|
|
func (r *Runtime) Path(c *Container) string {
|
2017-06-02 21:15:19 +00:00
|
|
|
if !c.trusted {
|
|
|
|
// We have an explicitly untrusted container.
|
|
|
|
if c.privileged {
|
|
|
|
logrus.Warnf("Running an untrusted but privileged container")
|
|
|
|
return r.trustedPath
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.untrustedPath != "" {
|
|
|
|
return r.untrustedPath
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.trustedPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our container is trusted. Let's look at the configured trust level.
|
|
|
|
if r.trustLevel == "trusted" {
|
|
|
|
return r.trustedPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our container is trusted, but we are running untrusted.
|
|
|
|
// We will use the untrusted container runtime if it's set
|
|
|
|
// and if it's not a privileged container.
|
|
|
|
if c.privileged || r.untrustedPath == "" {
|
|
|
|
return r.trustedPath
|
2017-02-21 10:51:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 21:15:19 +00:00
|
|
|
return r.untrustedPath
|
2016-07-29 22:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Version returns the version of the OCI Runtime
|
|
|
|
func (r *Runtime) Version() (string, error) {
|
2017-06-02 21:15:19 +00:00
|
|
|
runtimeVersion, err := getOCIVersion(r.trustedPath, "-v")
|
2016-07-29 22:35:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return runtimeVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOCIVersion(name string, args ...string) (string, error) {
|
|
|
|
out, err := utils.ExecCmd(name, args...)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
firstLine := out[:strings.Index(out, "\n")]
|
|
|
|
v := firstLine[strings.LastIndex(firstLine, " ")+1:]
|
|
|
|
return v, nil
|
|
|
|
}
|
2016-08-01 17:39:42 +00:00
|
|
|
|
|
|
|
// CreateContainer creates a container.
|
2017-09-27 15:35:20 +00:00
|
|
|
func (r *Runtime) CreateContainer(c *Container, cgroupParent string) (err error) {
|
2017-05-12 20:30:30 +00:00
|
|
|
var stderrBuf bytes.Buffer
|
2016-09-15 23:40:44 +00:00
|
|
|
parentPipe, childPipe, err := newPipe()
|
2017-06-09 14:18:27 +00:00
|
|
|
childStartPipe, parentStartPipe, err := newPipe()
|
2016-09-15 23:40:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating socket pair: %v", err)
|
|
|
|
}
|
|
|
|
defer parentPipe.Close()
|
2017-06-09 14:18:27 +00:00
|
|
|
defer parentStartPipe.Close()
|
2016-09-15 23:40:44 +00:00
|
|
|
|
2016-12-19 23:06:27 +00:00
|
|
|
var args []string
|
2017-08-29 21:11:30 +00:00
|
|
|
if r.cgroupManager == SystemdCgroupsManager {
|
2016-12-19 23:06:27 +00:00
|
|
|
args = append(args, "-s")
|
|
|
|
}
|
2017-06-23 22:01:05 +00:00
|
|
|
args = append(args, "-c", c.id)
|
2017-05-30 21:35:12 +00:00
|
|
|
args = append(args, "-u", c.id)
|
2017-02-21 10:51:16 +00:00
|
|
|
args = append(args, "-r", r.Path(c))
|
2016-11-14 16:52:58 +00:00
|
|
|
args = append(args, "-b", c.bundlePath)
|
|
|
|
args = append(args, "-p", filepath.Join(c.bundlePath, "pidfile"))
|
2016-10-07 15:59:39 +00:00
|
|
|
args = append(args, "-l", c.logPath)
|
2017-08-13 01:35:55 +00:00
|
|
|
args = append(args, "--exit-dir", r.containerExitsDir)
|
2017-10-24 22:28:53 +00:00
|
|
|
args = append(args, "--socket-dir-path", ContainerAttachSocketDir)
|
2017-09-25 22:31:00 +00:00
|
|
|
if r.logSizeMax >= 0 {
|
|
|
|
args = append(args, "--log-size-max", fmt.Sprintf("%v", r.logSizeMax))
|
|
|
|
}
|
2017-09-22 14:50:48 +00:00
|
|
|
if r.noPivot {
|
|
|
|
args = append(args, "--no-pivot")
|
|
|
|
}
|
2016-09-15 23:40:44 +00:00
|
|
|
if c.terminal {
|
|
|
|
args = append(args, "-t")
|
2017-06-08 20:08:29 +00:00
|
|
|
} else if c.stdin {
|
2018-01-06 03:36:10 +00:00
|
|
|
if !c.stdinOnce {
|
|
|
|
args = append(args, "--leave-stdin-open")
|
|
|
|
}
|
2017-06-08 20:08:29 +00:00
|
|
|
args = append(args, "-i")
|
2016-09-15 23:40:44 +00:00
|
|
|
}
|
2016-10-07 15:59:39 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"args": args,
|
|
|
|
}).Debugf("running conmon: %s", r.conmonPath)
|
2016-09-15 23:40:44 +00:00
|
|
|
|
2016-09-28 19:49:46 +00:00
|
|
|
cmd := exec.Command(r.conmonPath, args...)
|
2016-09-15 23:40:44 +00:00
|
|
|
cmd.Dir = c.bundlePath
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{
|
|
|
|
Setpgid: true,
|
|
|
|
}
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2017-05-12 20:30:30 +00:00
|
|
|
if c.terminal {
|
|
|
|
cmd.Stderr = &stderrBuf
|
|
|
|
}
|
2017-06-09 14:18:27 +00:00
|
|
|
cmd.ExtraFiles = append(cmd.ExtraFiles, childPipe, childStartPipe)
|
2016-09-15 23:40:44 +00:00
|
|
|
// 0, 1 and 2 are stdin, stdout and stderr
|
2016-10-17 07:44:27 +00:00
|
|
|
cmd.Env = append(r.conmonEnv, fmt.Sprintf("_OCI_SYNCPIPE=%d", 3))
|
2017-06-09 14:18:27 +00:00
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("_OCI_STARTPIPE=%d", 4))
|
2016-09-15 23:40:44 +00:00
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
childPipe.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need childPipe on the parent side
|
|
|
|
childPipe.Close()
|
2017-06-09 14:18:27 +00:00
|
|
|
childStartPipe.Close()
|
2016-09-15 23:40:44 +00:00
|
|
|
|
2017-03-06 23:08:46 +00:00
|
|
|
// Move conmon to specified cgroup
|
2017-08-29 21:11:30 +00:00
|
|
|
if r.cgroupManager == SystemdCgroupsManager {
|
2017-08-29 17:09:59 +00:00
|
|
|
logrus.Infof("Running conmon under slice %s and unitName %s", cgroupParent, createUnitName("crio-conmon", c.id))
|
|
|
|
if err = utils.RunUnderSystemdScope(cmd.Process.Pid, cgroupParent, createUnitName("crio-conmon", c.id)); err != nil {
|
|
|
|
logrus.Warnf("Failed to add conmon to systemd sandbox cgroup: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
control, err := cgroups.New(cgroups.V1, cgroups.StaticPath(filepath.Join(cgroupParent, "/crio-conmon-"+c.id)), &rspec.LinuxResources{})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Failed to add conmon to cgroupfs sandbox cgroup: %v", err)
|
|
|
|
} else {
|
2017-10-13 09:27:56 +00:00
|
|
|
// Here we should defer a crio-connmon- cgroup hierarchy deletion, but it will
|
|
|
|
// always fail as conmon's pid is still there.
|
|
|
|
// Fortunately, kubelet takes care of deleting this for us, so the leak will
|
|
|
|
// only happens in corner case where one does a manual deletion of the container
|
|
|
|
// through e.g. runc. This should be handled by implementing a conmon monitoring
|
|
|
|
// routine that does the cgroup cleanup once conmon is terminated.
|
2017-08-29 17:09:59 +00:00
|
|
|
if err := control.Add(cgroups.Process{Pid: cmd.Process.Pid}); err != nil {
|
|
|
|
logrus.Warnf("Failed to add conmon to cgroupfs sandbox cgroup: %v", err)
|
2017-03-06 23:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 14:18:27 +00:00
|
|
|
/* We set the cgroup, now the child can start creating children */
|
|
|
|
someData := []byte{0}
|
|
|
|
_, err = parentStartPipe.Write(someData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for initial setup and fork, and reap child */
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-27 15:35:20 +00:00
|
|
|
// We will delete all container resources if creation fails
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
r.DeleteContainer(c)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-09-15 23:40:44 +00:00
|
|
|
// Wait to get container pid from conmon
|
2016-10-13 06:07:22 +00:00
|
|
|
type syncStruct struct {
|
|
|
|
si *syncInfo
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
ch := make(chan syncStruct)
|
|
|
|
go func() {
|
|
|
|
var si *syncInfo
|
|
|
|
if err = json.NewDecoder(parentPipe).Decode(&si); err != nil {
|
|
|
|
ch <- syncStruct{err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- syncStruct{si: si}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ss := <-ch:
|
|
|
|
if ss.err != nil {
|
2017-05-12 20:30:30 +00:00
|
|
|
return fmt.Errorf("error reading container (probably exited) json message: %v", ss.err)
|
2016-10-13 06:07:22 +00:00
|
|
|
}
|
2017-06-14 14:33:07 +00:00
|
|
|
logrus.Debugf("Received container pid: %d", ss.si.Pid)
|
2017-05-12 20:30:30 +00:00
|
|
|
if ss.si.Pid == -1 {
|
2017-06-20 10:18:07 +00:00
|
|
|
if ss.si.Message != "" {
|
2017-09-27 10:06:13 +00:00
|
|
|
logrus.Errorf("Container creation error: %s", ss.si.Message)
|
2017-06-20 10:18:07 +00:00
|
|
|
return fmt.Errorf("container create failed: %s", ss.si.Message)
|
2017-05-12 20:30:30 +00:00
|
|
|
}
|
2017-09-27 10:06:13 +00:00
|
|
|
logrus.Errorf("Container creation failed")
|
2017-05-12 20:30:30 +00:00
|
|
|
return fmt.Errorf("container create failed")
|
|
|
|
}
|
2016-10-13 06:07:22 +00:00
|
|
|
case <-time.After(ContainerCreateTimeout):
|
2017-09-27 10:06:13 +00:00
|
|
|
logrus.Errorf("Container creation timeout (%v)", ContainerCreateTimeout)
|
2016-10-13 06:07:22 +00:00
|
|
|
return fmt.Errorf("create container timeout")
|
2016-09-15 23:40:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
2016-08-15 22:22:34 +00:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:08:46 +00:00
|
|
|
func createUnitName(prefix string, name string) string {
|
|
|
|
return fmt.Sprintf("%s-%s.scope", prefix, name)
|
|
|
|
}
|
|
|
|
|
2016-08-15 22:22:34 +00:00
|
|
|
// StartContainer starts a container.
|
|
|
|
func (r *Runtime) StartContainer(c *Container) error {
|
2016-12-13 20:50:27 +00:00
|
|
|
c.opLock.Lock()
|
|
|
|
defer c.opLock.Unlock()
|
2017-06-23 22:01:05 +00:00
|
|
|
if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.Path(c), "start", c.id); err != nil {
|
2016-09-12 22:43:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.state.Started = time.Now()
|
|
|
|
return nil
|
2016-08-15 22:22:34 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 22:48:04 +00:00
|
|
|
// ExecSyncResponse is returned from ExecSync.
|
|
|
|
type ExecSyncResponse struct {
|
|
|
|
Stdout []byte
|
|
|
|
Stderr []byte
|
|
|
|
ExitCode int32
|
|
|
|
}
|
|
|
|
|
2016-11-24 09:57:10 +00:00
|
|
|
// ExecSyncError wraps command's streams, exit code and error on ExecSync error.
|
|
|
|
type ExecSyncError struct {
|
|
|
|
Stdout bytes.Buffer
|
|
|
|
Stderr bytes.Buffer
|
|
|
|
ExitCode int32
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ExecSyncError) Error() string {
|
|
|
|
return fmt.Sprintf("command error: %+v, stdout: %s, stderr: %s, exit code %d", e.Err, e.Stdout.Bytes(), e.Stderr.Bytes(), e.ExitCode)
|
|
|
|
}
|
|
|
|
|
2017-01-10 22:18:14 +00:00
|
|
|
func prepareExec() (pidFile, parentPipe, childPipe *os.File, err error) {
|
|
|
|
parentPipe, childPipe, err = os.Pipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pidFile, err = ioutil.TempFile("", "pidfile")
|
|
|
|
if err != nil {
|
|
|
|
parentPipe.Close()
|
|
|
|
childPipe.Close()
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-11 18:34:48 +00:00
|
|
|
func parseLog(log []byte) (stdout, stderr []byte) {
|
|
|
|
// Split the log on newlines, which is what separates entries.
|
|
|
|
lines := bytes.SplitAfter(log, []byte{'\n'})
|
|
|
|
for _, line := range lines {
|
|
|
|
// Ignore empty lines.
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-17 18:58:12 +00:00
|
|
|
// The format of log lines is "DATE pipe LogTag REST".
|
|
|
|
parts := bytes.SplitN(line, []byte{' '}, 4)
|
|
|
|
if len(parts) < 4 {
|
2017-04-11 18:34:48 +00:00
|
|
|
// Ignore the line if it's formatted incorrectly, but complain
|
|
|
|
// about it so it can be debugged.
|
|
|
|
logrus.Warnf("hit invalid log format: %q", string(line))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe := string(parts[1])
|
2017-11-17 18:58:12 +00:00
|
|
|
content := parts[3]
|
|
|
|
|
|
|
|
linetype := string(parts[2])
|
|
|
|
if linetype == "P" {
|
|
|
|
contentLen := len(content)
|
|
|
|
if content[contentLen-1] == '\n' {
|
|
|
|
content = content[:contentLen-1]
|
|
|
|
}
|
|
|
|
}
|
2017-04-11 18:34:48 +00:00
|
|
|
|
|
|
|
switch pipe {
|
|
|
|
case "stdout":
|
|
|
|
stdout = append(stdout, content...)
|
|
|
|
case "stderr":
|
|
|
|
stderr = append(stderr, content...)
|
|
|
|
default:
|
|
|
|
// Complain about unknown pipes.
|
|
|
|
logrus.Warnf("hit invalid log format [unknown pipe %s]: %q", pipe, string(line))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, stderr
|
|
|
|
}
|
|
|
|
|
2016-11-18 00:40:52 +00:00
|
|
|
// ExecSync execs a command in a container and returns it's stdout, stderr and return code.
|
2016-11-18 22:48:04 +00:00
|
|
|
func (r *Runtime) ExecSync(c *Container, command []string, timeout int64) (resp *ExecSyncResponse, err error) {
|
2017-01-10 22:18:14 +00:00
|
|
|
pidFile, parentPipe, childPipe, err := prepareExec()
|
|
|
|
if err != nil {
|
|
|
|
return nil, ExecSyncError{
|
|
|
|
ExitCode: -1,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer parentPipe.Close()
|
|
|
|
defer func() {
|
|
|
|
if e := os.Remove(pidFile.Name()); e != nil {
|
|
|
|
logrus.Warnf("could not remove temporary PID file %s", pidFile.Name())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-06-23 22:01:05 +00:00
|
|
|
logFile, err := ioutil.TempFile("", "crio-log-"+c.id)
|
2016-10-07 15:59:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ExecSyncError{
|
|
|
|
ExitCode: -1,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logPath := logFile.Name()
|
|
|
|
defer func() {
|
|
|
|
logFile.Close()
|
|
|
|
os.RemoveAll(logPath)
|
|
|
|
}()
|
|
|
|
|
2017-01-10 22:18:14 +00:00
|
|
|
var args []string
|
2017-06-23 22:01:05 +00:00
|
|
|
args = append(args, "-c", c.id)
|
2017-02-21 10:51:16 +00:00
|
|
|
args = append(args, "-r", r.Path(c))
|
2017-01-10 22:18:14 +00:00
|
|
|
args = append(args, "-p", pidFile.Name())
|
|
|
|
args = append(args, "-e")
|
|
|
|
if c.terminal {
|
|
|
|
args = append(args, "-t")
|
|
|
|
}
|
2017-06-21 17:39:02 +00:00
|
|
|
if timeout > 0 {
|
|
|
|
args = append(args, "-T")
|
|
|
|
args = append(args, fmt.Sprintf("%d", timeout))
|
|
|
|
}
|
2016-10-07 15:59:39 +00:00
|
|
|
args = append(args, "-l", logPath)
|
2017-10-24 22:28:53 +00:00
|
|
|
args = append(args, "--socket-dir-path", ContainerAttachSocketDir)
|
2017-01-10 22:18:14 +00:00
|
|
|
|
2017-12-07 19:33:07 +00:00
|
|
|
processFile, err := PrepareProcessExec(c, command, false)
|
2017-05-25 09:11:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ExecSyncError{
|
|
|
|
ExitCode: -1,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 19:33:07 +00:00
|
|
|
defer os.RemoveAll(processFile.Name())
|
2017-05-25 09:11:14 +00:00
|
|
|
|
2017-12-07 19:33:07 +00:00
|
|
|
args = append(args, "--exec-process-spec", processFile.Name())
|
2017-01-10 22:18:14 +00:00
|
|
|
|
|
|
|
cmd := exec.Command(r.conmonPath, args...)
|
|
|
|
|
2016-11-18 00:40:52 +00:00
|
|
|
var stdoutBuf, stderrBuf bytes.Buffer
|
|
|
|
cmd.Stdout = &stdoutBuf
|
|
|
|
cmd.Stderr = &stderrBuf
|
2017-01-10 22:18:14 +00:00
|
|
|
cmd.ExtraFiles = append(cmd.ExtraFiles, childPipe)
|
|
|
|
// 0, 1 and 2 are stdin, stdout and stderr
|
|
|
|
cmd.Env = append(r.conmonEnv, fmt.Sprintf("_OCI_SYNCPIPE=%d", 3))
|
|
|
|
|
2016-11-18 00:40:52 +00:00
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
2017-01-10 22:18:14 +00:00
|
|
|
childPipe.Close()
|
2016-11-24 09:57:10 +00:00
|
|
|
return nil, ExecSyncError{
|
|
|
|
Stdout: stdoutBuf,
|
|
|
|
Stderr: stderrBuf,
|
2016-11-18 22:48:04 +00:00
|
|
|
ExitCode: -1,
|
2016-11-24 09:57:10 +00:00
|
|
|
Err: err,
|
|
|
|
}
|
2016-11-18 00:40:52 +00:00
|
|
|
}
|
2016-11-18 22:48:04 +00:00
|
|
|
|
2017-01-10 22:18:14 +00:00
|
|
|
// We don't need childPipe on the parent side
|
|
|
|
childPipe.Close()
|
|
|
|
|
2017-06-21 17:39:02 +00:00
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
if exitErr, ok := err.(*exec.ExitError); ok {
|
2017-06-28 15:47:31 +00:00
|
|
|
if status, ok := exitErr.Sys().(unix.WaitStatus); ok {
|
2016-11-24 09:57:10 +00:00
|
|
|
return nil, ExecSyncError{
|
|
|
|
Stdout: stdoutBuf,
|
|
|
|
Stderr: stderrBuf,
|
2017-06-21 17:39:02 +00:00
|
|
|
ExitCode: int32(status.ExitStatus()),
|
|
|
|
Err: err,
|
2016-11-24 09:57:10 +00:00
|
|
|
}
|
2016-11-18 00:40:52 +00:00
|
|
|
}
|
2017-06-21 17:39:02 +00:00
|
|
|
} else {
|
2016-11-24 09:57:10 +00:00
|
|
|
return nil, ExecSyncError{
|
|
|
|
Stdout: stdoutBuf,
|
|
|
|
Stderr: stderrBuf,
|
2016-11-18 22:48:04 +00:00
|
|
|
ExitCode: -1,
|
2017-06-21 17:39:02 +00:00
|
|
|
Err: err,
|
2016-11-18 22:48:04 +00:00
|
|
|
}
|
2017-01-10 22:18:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-18 22:48:04 +00:00
|
|
|
|
2017-01-10 22:18:14 +00:00
|
|
|
var ec *exitCodeInfo
|
|
|
|
if err := json.NewDecoder(parentPipe).Decode(&ec); err != nil {
|
|
|
|
return nil, ExecSyncError{
|
|
|
|
Stdout: stdoutBuf,
|
|
|
|
Stderr: stderrBuf,
|
|
|
|
ExitCode: -1,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 17:39:02 +00:00
|
|
|
logrus.Infof("Received container exit code: %v, message: %s", ec.ExitCode, ec.Message)
|
|
|
|
|
|
|
|
if ec.ExitCode == -1 {
|
|
|
|
return nil, ExecSyncError{
|
|
|
|
Stdout: stdoutBuf,
|
|
|
|
Stderr: stderrBuf,
|
|
|
|
ExitCode: -1,
|
|
|
|
Err: fmt.Errorf(ec.Message),
|
|
|
|
}
|
|
|
|
}
|
2017-01-10 22:18:14 +00:00
|
|
|
|
2016-10-07 15:59:39 +00:00
|
|
|
// The actual logged output is not the same as stdoutBuf and stderrBuf,
|
|
|
|
// which are used for getting error information. For the actual
|
|
|
|
// ExecSyncResponse we have to read the logfile.
|
|
|
|
// XXX: Currently runC dups the same console over both stdout and stderr,
|
|
|
|
// so we can't differentiate between the two.
|
|
|
|
|
2017-04-11 18:34:48 +00:00
|
|
|
logBytes, err := ioutil.ReadFile(logPath)
|
2016-10-07 15:59:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, ExecSyncError{
|
|
|
|
Stdout: stdoutBuf,
|
|
|
|
Stderr: stderrBuf,
|
|
|
|
ExitCode: -1,
|
|
|
|
Err: err,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 18:34:48 +00:00
|
|
|
// We have to parse the log output into {stdout, stderr} buffers.
|
|
|
|
stdoutBytes, stderrBytes := parseLog(logBytes)
|
2016-11-18 22:48:04 +00:00
|
|
|
return &ExecSyncResponse{
|
2017-04-11 18:34:48 +00:00
|
|
|
Stdout: stdoutBytes,
|
|
|
|
Stderr: stderrBytes,
|
2017-04-10 20:59:03 +00:00
|
|
|
ExitCode: ec.ExitCode,
|
2016-11-18 22:48:04 +00:00
|
|
|
}, nil
|
2016-11-18 00:40:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 08:52:56 +00:00
|
|
|
// UpdateContainer updates container resources
|
|
|
|
func (r *Runtime) UpdateContainer(c *Container, res *rspec.LinuxResources) error {
|
|
|
|
cmd := exec.Command(r.Path(c), "update", "--resources", "-", c.id)
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
jsonResources, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cmd.Stdin = bytes.NewReader(jsonResources)
|
|
|
|
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return fmt.Errorf("updating resources for container %q failed: %v %v (%v)", c.id, stderr.String(), stdout.String(), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-15 20:05:41 +00:00
|
|
|
func waitContainerStop(ctx context.Context, c *Container, timeout time.Duration) error {
|
2017-05-15 13:27:03 +00:00
|
|
|
done := make(chan struct{})
|
2018-02-14 18:11:51 +00:00
|
|
|
// we could potentially re-use "done" channel to exit the loop on timeout,
|
|
|
|
// but we use another channel "chControl" so that we never panic
|
|
|
|
// attempting to close an already-closed "done" channel. The panic
|
|
|
|
// would occur in the "default" select case below if we'd closed the
|
|
|
|
// "done" channel (instead of the "chControl" channel) in the timeout
|
|
|
|
// select case.
|
2017-06-14 17:37:44 +00:00
|
|
|
chControl := make(chan struct{})
|
2017-05-15 13:27:03 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2017-06-14 17:37:44 +00:00
|
|
|
select {
|
|
|
|
case <-chControl:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
// Check if the process is still around
|
|
|
|
err := unix.Kill(c.state.Pid, 0)
|
2017-06-28 15:47:31 +00:00
|
|
|
if err == unix.ESRCH {
|
2017-06-14 17:37:44 +00:00
|
|
|
close(done)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2016-09-27 18:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-15 13:27:03 +00:00
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return nil
|
2017-10-15 20:05:41 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
close(chControl)
|
|
|
|
return ctx.Err()
|
|
|
|
case <-time.After(timeout):
|
2017-06-14 17:37:44 +00:00
|
|
|
close(chControl)
|
2017-06-28 15:47:31 +00:00
|
|
|
err := unix.Kill(c.state.Pid, unix.SIGKILL)
|
|
|
|
if err != nil && err != unix.ESRCH {
|
2017-05-15 13:27:03 +00:00
|
|
|
return fmt.Errorf("failed to kill process: %v", err)
|
2016-09-27 18:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-11 09:35:31 +00:00
|
|
|
c.state.Finished = time.Now()
|
2016-09-27 18:35:40 +00:00
|
|
|
return nil
|
2016-08-01 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
2017-10-15 20:05:41 +00:00
|
|
|
// StopContainer stops a container. Timeout is given in seconds.
|
|
|
|
func (r *Runtime) StopContainer(ctx context.Context, c *Container, timeout int64) error {
|
|
|
|
c.opLock.Lock()
|
|
|
|
defer c.opLock.Unlock()
|
|
|
|
|
|
|
|
// Check if the process is around before sending a signal
|
|
|
|
err := unix.Kill(c.state.Pid, 0)
|
|
|
|
if err == unix.ESRCH {
|
|
|
|
c.state.Finished = time.Now()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeout > 0 {
|
|
|
|
if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.Path(c), "kill", c.id, c.GetStopSignal()); err != nil {
|
|
|
|
return fmt.Errorf("failed to stop container %s, %v", c.id, err)
|
|
|
|
}
|
|
|
|
err = waitContainerStop(ctx, c, time.Duration(timeout)*time.Second)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
logrus.Warnf("Stop container %q timed out: %v", c.ID(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, r.Path(c), "kill", "--all", c.id, "KILL"); err != nil {
|
|
|
|
return fmt.Errorf("failed to stop container %s, %v", c.id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return waitContainerStop(ctx, c, killContainerTimeout)
|
|
|
|
}
|
|
|
|
|
2016-08-16 17:16:05 +00:00
|
|
|
// DeleteContainer deletes a container.
|
|
|
|
func (r *Runtime) DeleteContainer(c *Container) error {
|
2016-12-13 20:50:27 +00:00
|
|
|
c.opLock.Lock()
|
|
|
|
defer c.opLock.Unlock()
|
2017-06-23 22:01:05 +00:00
|
|
|
_, err := utils.ExecCmd(r.Path(c), "delete", "--force", c.id)
|
2017-05-11 09:45:31 +00:00
|
|
|
return err
|
2016-08-16 17:16:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-10 16:18:59 +00:00
|
|
|
// SetStartFailed sets the container state appropriately after a start failure
|
|
|
|
func (r *Runtime) SetStartFailed(c *Container, err error) {
|
|
|
|
c.opLock.Lock()
|
|
|
|
defer c.opLock.Unlock()
|
|
|
|
// adjust finished and started times
|
|
|
|
c.state.Finished, c.state.Started = c.state.Created, c.state.Created
|
|
|
|
c.state.Error = err.Error()
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
// UpdateStatus refreshes the status of the container.
|
2016-08-29 18:48:58 +00:00
|
|
|
func (r *Runtime) UpdateStatus(c *Container) error {
|
2016-12-13 20:50:27 +00:00
|
|
|
c.opLock.Lock()
|
|
|
|
defer c.opLock.Unlock()
|
2017-12-06 17:35:20 +00:00
|
|
|
out, err := exec.Command(r.Path(c), "state", c.id).Output()
|
2016-08-29 18:48:58 +00:00
|
|
|
if err != nil {
|
2017-06-01 09:21:20 +00:00
|
|
|
// there are many code paths that could lead to have a bad state in the
|
|
|
|
// underlying runtime.
|
|
|
|
// On any error like a container went away or we rebooted and containers
|
|
|
|
// went away we do not error out stopping kubernetes to recover.
|
|
|
|
// We always populate the fields below so kube can restart/reschedule
|
|
|
|
// containers failing.
|
|
|
|
c.state.Status = ContainerStateStopped
|
|
|
|
c.state.Finished = time.Now()
|
|
|
|
c.state.ExitCode = 255
|
|
|
|
return nil
|
2016-08-29 18:48:58 +00:00
|
|
|
}
|
2016-12-09 20:53:56 +00:00
|
|
|
if err := json.NewDecoder(bytes.NewBuffer(out)).Decode(&c.state); err != nil {
|
2017-06-23 22:01:05 +00:00
|
|
|
return fmt.Errorf("failed to decode container status for %s: %s", c.id, err)
|
2016-08-29 18:48:58 +00:00
|
|
|
}
|
2016-09-16 23:32:19 +00:00
|
|
|
|
2016-10-06 22:16:21 +00:00
|
|
|
if c.state.Status == ContainerStateStopped {
|
2017-08-13 01:35:55 +00:00
|
|
|
exitFilePath := filepath.Join(r.containerExitsDir, c.id)
|
2017-08-29 09:25:51 +00:00
|
|
|
var fi os.FileInfo
|
|
|
|
err = kwait.ExponentialBackoff(
|
|
|
|
kwait.Backoff{
|
|
|
|
Duration: 500 * time.Millisecond,
|
|
|
|
Factor: 1.2,
|
|
|
|
Steps: 6,
|
|
|
|
},
|
|
|
|
func() (bool, error) {
|
|
|
|
var err error
|
|
|
|
fi, err = os.Stat(exitFilePath)
|
|
|
|
if err != nil {
|
|
|
|
// wait longer
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
2016-09-16 23:32:19 +00:00
|
|
|
if err != nil {
|
2017-03-14 20:39:09 +00:00
|
|
|
logrus.Warnf("failed to find container exit file: %v", err)
|
|
|
|
c.state.ExitCode = -1
|
|
|
|
} else {
|
2017-07-17 13:31:12 +00:00
|
|
|
c.state.Finished = getFinishedTime(fi)
|
2017-03-14 20:39:09 +00:00
|
|
|
statusCodeStr, err := ioutil.ReadFile(exitFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to read exit file: %v", err)
|
|
|
|
}
|
|
|
|
statusCode, err := strconv.Atoi(string(statusCodeStr))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("status code conversion failed: %v", err)
|
|
|
|
}
|
|
|
|
c.state.ExitCode = int32(statusCode)
|
2016-09-16 23:32:19 +00:00
|
|
|
}
|
2017-05-23 21:04:10 +00:00
|
|
|
|
|
|
|
oomFilePath := filepath.Join(c.bundlePath, "oom")
|
|
|
|
if _, err = os.Stat(oomFilePath); err == nil {
|
|
|
|
c.state.OOMKilled = true
|
|
|
|
}
|
2016-09-16 23:32:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 18:48:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerStatus returns the state of a container.
|
2016-08-31 23:32:17 +00:00
|
|
|
func (r *Runtime) ContainerStatus(c *Container) *ContainerState {
|
2016-12-13 20:50:27 +00:00
|
|
|
c.opLock.Lock()
|
|
|
|
defer c.opLock.Unlock()
|
2016-08-29 18:48:58 +00:00
|
|
|
return c.state
|
|
|
|
}
|
|
|
|
|
2016-09-15 23:40:44 +00:00
|
|
|
// newPipe creates a unix socket pair for communication
|
|
|
|
func newPipe() (parent *os.File, child *os.File, err error) {
|
2017-06-28 15:47:31 +00:00
|
|
|
fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0)
|
2016-09-15 23:40:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return os.NewFile(uintptr(fds[1]), "parent"), os.NewFile(uintptr(fds[0]), "child"), nil
|
|
|
|
}
|
2016-11-18 15:03:13 +00:00
|
|
|
|
|
|
|
// RuntimeReady checks if the runtime is up and ready to accept
|
|
|
|
// basic containers e.g. container only needs host network.
|
|
|
|
func (r *Runtime) RuntimeReady() (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworkReady checks if the runtime network is up and ready to
|
|
|
|
// accept containers which require container network.
|
|
|
|
func (r *Runtime) NetworkReady() (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
2017-09-19 12:26:32 +00:00
|
|
|
|
|
|
|
// PauseContainer pauses a container.
|
|
|
|
func (r *Runtime) PauseContainer(c *Container) error {
|
|
|
|
c.opLock.Lock()
|
|
|
|
defer c.opLock.Unlock()
|
|
|
|
_, err := utils.ExecCmd(r.Path(c), "pause", c.id)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnpauseContainer unpauses a container.
|
|
|
|
func (r *Runtime) UnpauseContainer(c *Container) error {
|
|
|
|
c.opLock.Lock()
|
|
|
|
defer c.opLock.Unlock()
|
|
|
|
_, err := utils.ExecCmd(r.Path(c), "resume", c.id)
|
|
|
|
return err
|
|
|
|
}
|
2017-12-07 19:33:07 +00:00
|
|
|
|
|
|
|
// PrepareProcessExec returns the path of the process.json used in runc exec -p
|
|
|
|
// caller is responsible to close the returned *os.File if needed.
|
|
|
|
func PrepareProcessExec(c *Container, cmd []string, tty bool) (*os.File, error) {
|
|
|
|
f, err := ioutil.TempFile("", "exec-process-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pspec := c.Spec().Process
|
|
|
|
pspec.Args = cmd
|
|
|
|
if tty {
|
|
|
|
pspec.Terminal = true
|
|
|
|
}
|
|
|
|
processJSON, err := json.Marshal(pspec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(f.Name(), processJSON, 0644); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|