k3s/vendor/github.com/containerd/cri/pkg/server/container_execsync.go

212 lines
7.2 KiB
Go
Raw Normal View History

2019-01-12 04:58:27 +00:00
/*
2020-08-10 17:43:49 +00:00
Copyright The containerd Authors.
2019-01-12 04:58:27 +00:00
2020-08-10 17:43:49 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
2019-01-12 04:58:27 +00:00
2020-08-10 17:43:49 +00:00
http://www.apache.org/licenses/LICENSE-2.0
2019-01-12 04:58:27 +00:00
2020-08-10 17:43:49 +00:00
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2019-01-12 04:58:27 +00:00
*/
package server
import (
"bytes"
"io"
2019-09-27 21:51:53 +00:00
"syscall"
2019-01-12 04:58:27 +00:00
"time"
"github.com/containerd/containerd"
containerdio "github.com/containerd/containerd/cio"
"github.com/containerd/containerd/errdefs"
2019-09-27 21:51:53 +00:00
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/oci"
2019-01-12 04:58:27 +00:00
"github.com/pkg/errors"
"golang.org/x/net/context"
"k8s.io/client-go/tools/remotecommand"
2019-08-30 18:33:25 +00:00
runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
2019-01-12 04:58:27 +00:00
ctrdutil "github.com/containerd/cri/pkg/containerd/util"
cioutil "github.com/containerd/cri/pkg/ioutil"
cio "github.com/containerd/cri/pkg/server/io"
"github.com/containerd/cri/pkg/util"
)
// ExecSync executes a command in the container, and returns the stdout output.
// If command exits with a non-zero exit code, an error is returned.
func (c *criService) ExecSync(ctx context.Context, r *runtime.ExecSyncRequest) (*runtime.ExecSyncResponse, error) {
var stdout, stderr bytes.Buffer
exitCode, err := c.execInContainer(ctx, r.GetContainerId(), execOptions{
cmd: r.GetCmd(),
stdout: cioutil.NewNopWriteCloser(&stdout),
stderr: cioutil.NewNopWriteCloser(&stderr),
timeout: time.Duration(r.GetTimeout()) * time.Second,
})
if err != nil {
return nil, errors.Wrap(err, "failed to exec in container")
}
return &runtime.ExecSyncResponse{
Stdout: stdout.Bytes(),
Stderr: stderr.Bytes(),
ExitCode: int32(*exitCode),
}, nil
}
// execOptions specifies how to execute command in container.
type execOptions struct {
cmd []string
stdin io.Reader
stdout io.WriteCloser
stderr io.WriteCloser
tty bool
resize <-chan remotecommand.TerminalSize
timeout time.Duration
}
2020-08-10 17:43:49 +00:00
func (c *criService) execInternal(ctx context.Context, container containerd.Container, id string, opts execOptions) (*uint32, error) {
2019-01-12 04:58:27 +00:00
// Cancel the context before returning to ensure goroutines are stopped.
// This is important, because if `Start` returns error, `Wait` will hang
// forever unless we cancel the context.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
spec, err := container.Spec(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to get container spec")
}
task, err := container.Task(ctx, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to load task")
}
2019-09-27 21:51:53 +00:00
pspec := spec.Process
pspec.Terminal = opts.tty
2019-01-12 04:58:27 +00:00
if opts.tty {
2019-09-27 21:51:53 +00:00
if err := oci.WithEnv([]string{"TERM=xterm"})(ctx, nil, nil, spec); err != nil {
return nil, errors.Wrap(err, "add TERM env var to spec")
}
2019-01-12 04:58:27 +00:00
}
2019-09-27 21:51:53 +00:00
2019-01-12 04:58:27 +00:00
pspec.Args = opts.cmd
if opts.stdout == nil {
opts.stdout = cio.NewDiscardLogger()
}
if opts.stderr == nil {
opts.stderr = cio.NewDiscardLogger()
}
execID := util.GenerateID()
2019-09-27 21:51:53 +00:00
log.G(ctx).Debugf("Generated exec id %q for container %q", execID, id)
2019-01-12 04:58:27 +00:00
volatileRootDir := c.getVolatileContainerRootDir(id)
var execIO *cio.ExecIO
process, err := task.Exec(ctx, execID, pspec,
func(id string) (containerdio.IO, error) {
var err error
execIO, err = cio.NewExecIO(id, volatileRootDir, opts.tty, opts.stdin != nil)
return execIO, err
},
)
if err != nil {
return nil, errors.Wrapf(err, "failed to create exec %q", execID)
}
defer func() {
deferCtx, deferCancel := ctrdutil.DeferContext()
defer deferCancel()
2019-07-12 03:16:45 +00:00
if _, err := process.Delete(deferCtx, containerd.WithProcessKill); err != nil {
2019-09-27 21:51:53 +00:00
log.G(ctx).WithError(err).Errorf("Failed to delete exec process %q for container %q", execID, id)
2019-01-12 04:58:27 +00:00
}
}()
exitCh, err := process.Wait(ctx)
if err != nil {
return nil, errors.Wrapf(err, "failed to wait for process %q", execID)
}
if err := process.Start(ctx); err != nil {
return nil, errors.Wrapf(err, "failed to start exec %q", execID)
}
2020-08-10 17:43:49 +00:00
handleResizing(ctx, opts.resize, func(size remotecommand.TerminalSize) {
2019-01-12 04:58:27 +00:00
if err := process.Resize(ctx, uint32(size.Width), uint32(size.Height)); err != nil {
2019-09-27 21:51:53 +00:00
log.G(ctx).WithError(err).Errorf("Failed to resize process %q console for container %q", execID, id)
2019-01-12 04:58:27 +00:00
}
})
attachDone := execIO.Attach(cio.AttachOptions{
Stdin: opts.stdin,
Stdout: opts.stdout,
Stderr: opts.stderr,
Tty: opts.tty,
StdinOnce: true,
CloseStdin: func() error {
return process.CloseIO(ctx, containerd.WithStdinCloser)
},
})
2019-09-27 21:51:53 +00:00
execCtx := ctx
if opts.timeout > 0 {
var execCtxCancel context.CancelFunc
execCtx, execCtxCancel = context.WithTimeout(ctx, opts.timeout)
defer execCtxCancel()
2019-01-12 04:58:27 +00:00
}
2019-09-27 21:51:53 +00:00
2019-01-12 04:58:27 +00:00
select {
2019-09-27 21:51:53 +00:00
case <-execCtx.Done():
2019-01-12 04:58:27 +00:00
// Ignore the not found error because the process may exit itself before killing.
2019-09-27 21:51:53 +00:00
if err := process.Kill(ctx, syscall.SIGKILL); err != nil && !errdefs.IsNotFound(err) {
2019-01-12 04:58:27 +00:00
return nil, errors.Wrapf(err, "failed to kill exec %q", execID)
}
// Wait for the process to be killed.
exitRes := <-exitCh
2019-09-27 21:51:53 +00:00
log.G(ctx).Infof("Timeout received while waiting for exec process kill %q code %d and error %v",
2019-01-12 04:58:27 +00:00
execID, exitRes.ExitCode(), exitRes.Error())
<-attachDone
2019-09-27 21:51:53 +00:00
log.G(ctx).Debugf("Stream pipe for exec process %q done", execID)
return nil, errors.Wrapf(execCtx.Err(), "timeout %v exceeded", opts.timeout)
2019-01-12 04:58:27 +00:00
case exitRes := <-exitCh:
code, _, err := exitRes.Result()
2019-09-27 21:51:53 +00:00
log.G(ctx).Infof("Exec process %q exits with exit code %d and error %v", execID, code, err)
2019-01-12 04:58:27 +00:00
if err != nil {
return nil, errors.Wrapf(err, "failed while waiting for exec %q", execID)
}
<-attachDone
2019-09-27 21:51:53 +00:00
log.G(ctx).Debugf("Stream pipe for exec process %q done", execID)
2019-01-12 04:58:27 +00:00
return &code, nil
}
}
2020-08-10 17:43:49 +00:00
// execInContainer executes a command inside the container synchronously, and
// redirects stdio stream properly.
// This function only returns when the exec process exits, this means that:
// 1) As long as the exec process is running, the goroutine in the cri plugin
// will be running and wait for the exit code;
// 2) `kubectl exec -it` will hang until the exec process exits, even after io
// is detached. This is different from dockershim, which leaves the exec process
// running in background after io is detached.
// https://github.com/kubernetes/kubernetes/blob/v1.15.0/pkg/kubelet/dockershim/exec.go#L127
// For example, if the `kubectl exec -it` process is killed, IO will be closed. In
// this case, the CRI plugin will still have a goroutine waiting for the exec process
// to exit and log the exit code, but dockershim won't.
func (c *criService) execInContainer(ctx context.Context, id string, opts execOptions) (*uint32, error) {
// Get container from our container store.
cntr, err := c.containerStore.Get(id)
if err != nil {
return nil, errors.Wrapf(err, "failed to find container %q in store", id)
}
id = cntr.ID
state := cntr.Status.Get().State()
if state != runtime.ContainerState_CONTAINER_RUNNING {
return nil, errors.Errorf("container is in %s state", criContainerStateToString(state))
}
return c.execInternal(ctx, cntr.Container, id, opts)
}