This patch adds the untilRemoved option to the ContainerWait API which allows the client to wait until the container is not only exited but also removed. This patch also adds some more CLI integration tests for waiting for a created container and waiting with the new --until-removed flag. Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Handle detach sequence in CLI Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Update Container Wait Conditions Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Apply container wait changes to API 1.30 The set of changes to the containerWait API missed the cut for the Docker 17.05 release (API version 1.29). This patch bumps the version checks to use 1.30 instead. This patch also makes a minor update to a testfile which was added to the builder/dockerfile package. Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Remove wait changes from CLI Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Address minor nits on wait changes - Changed the name of the tty Proxy wrapper to `escapeProxy` - Removed the unnecessary Error() method on container.State - Fixes a typo in comment (repeated word) Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Use router.WithCancel in the containerWait handler This handler previously added this functionality manually but now uses the existing wrapper which does it for us. Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Add WaitCondition constants to api/types/container Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Address more ContainerWait review comments - Update ContainerWait backend interface to not return pointer values for container.StateStatus type. - Updated container state's Wait() method comments to clarify that a context MUST be used for cancelling the request, setting timeouts, and to avoid goroutine leaks. - Removed unnecessary buffering when making channels in the client's ContainerWait methods. - Renamed result and error channels in client's ContainerWait methods to clarify that only a single result or error value would be sent on the channel. Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Move container.WaitCondition type to separate file ... to avoid conflict with swagger-generated code for API response Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Address more ContainerWait review comments Docker-DCO-1.1-Signed-off-by: Josh Hawn <josh.hawn@docker.com> (github: jlhawn) Upstream-commit: 4921171587c09d0fcd8086a62a25813332f44112 Component: engine
466 lines
12 KiB
Go
466 lines
12 KiB
Go
package container
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/docker/distribution/reference"
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/backend"
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/api/types/events"
|
|
containerpkg "github.com/docker/docker/container"
|
|
"github.com/docker/docker/daemon/cluster/convert"
|
|
executorpkg "github.com/docker/docker/daemon/cluster/executor"
|
|
"github.com/docker/libnetwork"
|
|
"github.com/docker/swarmkit/agent/exec"
|
|
"github.com/docker/swarmkit/api"
|
|
"github.com/docker/swarmkit/log"
|
|
gogotypes "github.com/gogo/protobuf/types"
|
|
"github.com/opencontainers/go-digest"
|
|
"golang.org/x/net/context"
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
// containerAdapter conducts remote operations for a container. All calls
|
|
// are mostly naked calls to the client API, seeded with information from
|
|
// containerConfig.
|
|
type containerAdapter struct {
|
|
backend executorpkg.Backend
|
|
container *containerConfig
|
|
dependencies exec.DependencyGetter
|
|
}
|
|
|
|
func newContainerAdapter(b executorpkg.Backend, task *api.Task, dependencies exec.DependencyGetter) (*containerAdapter, error) {
|
|
ctnr, err := newContainerConfig(task)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &containerAdapter{
|
|
container: ctnr,
|
|
backend: b,
|
|
dependencies: dependencies,
|
|
}, nil
|
|
}
|
|
|
|
func (c *containerAdapter) pullImage(ctx context.Context) error {
|
|
spec := c.container.spec()
|
|
|
|
// Skip pulling if the image is referenced by image ID.
|
|
if _, err := digest.Parse(spec.Image); err == nil {
|
|
return nil
|
|
}
|
|
|
|
// Skip pulling if the image is referenced by digest and already
|
|
// exists locally.
|
|
named, err := reference.ParseNormalizedNamed(spec.Image)
|
|
if err == nil {
|
|
if _, ok := named.(reference.Canonical); ok {
|
|
_, err := c.backend.LookupImage(spec.Image)
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// if the image needs to be pulled, the auth config will be retrieved and updated
|
|
var encodedAuthConfig string
|
|
if spec.PullOptions != nil {
|
|
encodedAuthConfig = spec.PullOptions.RegistryAuth
|
|
}
|
|
|
|
authConfig := &types.AuthConfig{}
|
|
if encodedAuthConfig != "" {
|
|
if err := json.NewDecoder(base64.NewDecoder(base64.URLEncoding, strings.NewReader(encodedAuthConfig))).Decode(authConfig); err != nil {
|
|
logrus.Warnf("invalid authconfig: %v", err)
|
|
}
|
|
}
|
|
|
|
pr, pw := io.Pipe()
|
|
metaHeaders := map[string][]string{}
|
|
go func() {
|
|
err := c.backend.PullImage(ctx, c.container.image(), "", metaHeaders, authConfig, pw)
|
|
pw.CloseWithError(err)
|
|
}()
|
|
|
|
dec := json.NewDecoder(pr)
|
|
dec.UseNumber()
|
|
m := map[string]interface{}{}
|
|
spamLimiter := rate.NewLimiter(rate.Every(time.Second), 1)
|
|
|
|
lastStatus := ""
|
|
for {
|
|
if err := dec.Decode(&m); err != nil {
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
return err
|
|
}
|
|
l := log.G(ctx)
|
|
// limit pull progress logs unless the status changes
|
|
if spamLimiter.Allow() || lastStatus != m["status"] {
|
|
// if we have progress details, we have everything we need
|
|
if progress, ok := m["progressDetail"].(map[string]interface{}); ok {
|
|
// first, log the image and status
|
|
l = l.WithFields(logrus.Fields{
|
|
"image": c.container.image(),
|
|
"status": m["status"],
|
|
})
|
|
// then, if we have progress, log the progress
|
|
if progress["current"] != nil && progress["total"] != nil {
|
|
l = l.WithFields(logrus.Fields{
|
|
"current": progress["current"],
|
|
"total": progress["total"],
|
|
})
|
|
}
|
|
}
|
|
l.Debug("pull in progress")
|
|
}
|
|
// sometimes, we get no useful information at all, and add no fields
|
|
if status, ok := m["status"].(string); ok {
|
|
lastStatus = status
|
|
}
|
|
}
|
|
|
|
// if the final stream object contained an error, return it
|
|
if errMsg, ok := m["error"]; ok {
|
|
return fmt.Errorf("%v", errMsg)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *containerAdapter) createNetworks(ctx context.Context) error {
|
|
for _, network := range c.container.networks() {
|
|
ncr, err := c.container.networkCreateRequest(network)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := c.backend.CreateManagedNetwork(ncr); err != nil { // todo name missing
|
|
if _, ok := err.(libnetwork.NetworkNameError); ok {
|
|
continue
|
|
}
|
|
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *containerAdapter) removeNetworks(ctx context.Context) error {
|
|
for _, nid := range c.container.networks() {
|
|
if err := c.backend.DeleteManagedNetwork(nid); err != nil {
|
|
switch err.(type) {
|
|
case *libnetwork.ActiveEndpointsError:
|
|
continue
|
|
case libnetwork.ErrNoSuchNetwork:
|
|
continue
|
|
default:
|
|
log.G(ctx).Errorf("network %s remove failed: %v", nid, err)
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *containerAdapter) networkAttach(ctx context.Context) error {
|
|
config := c.container.createNetworkingConfig()
|
|
|
|
var (
|
|
networkName string
|
|
networkID string
|
|
)
|
|
|
|
if config != nil {
|
|
for n, epConfig := range config.EndpointsConfig {
|
|
networkName = n
|
|
networkID = epConfig.NetworkID
|
|
break
|
|
}
|
|
}
|
|
|
|
return c.backend.UpdateAttachment(networkName, networkID, c.container.id(), config)
|
|
}
|
|
|
|
func (c *containerAdapter) waitForDetach(ctx context.Context) error {
|
|
config := c.container.createNetworkingConfig()
|
|
|
|
var (
|
|
networkName string
|
|
networkID string
|
|
)
|
|
|
|
if config != nil {
|
|
for n, epConfig := range config.EndpointsConfig {
|
|
networkName = n
|
|
networkID = epConfig.NetworkID
|
|
break
|
|
}
|
|
}
|
|
|
|
return c.backend.WaitForDetachment(ctx, networkName, networkID, c.container.taskID(), c.container.id())
|
|
}
|
|
|
|
func (c *containerAdapter) create(ctx context.Context) error {
|
|
var cr containertypes.ContainerCreateCreatedBody
|
|
var err error
|
|
|
|
if cr, err = c.backend.CreateManagedContainer(types.ContainerCreateConfig{
|
|
Name: c.container.name(),
|
|
Config: c.container.config(),
|
|
HostConfig: c.container.hostConfig(),
|
|
// Use the first network in container create
|
|
NetworkingConfig: c.container.createNetworkingConfig(),
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Docker daemon currently doesn't support multiple networks in container create
|
|
// Connect to all other networks
|
|
nc := c.container.connectNetworkingConfig()
|
|
|
|
if nc != nil {
|
|
for n, ep := range nc.EndpointsConfig {
|
|
if err := c.backend.ConnectContainerToNetwork(cr.ID, n, ep); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
container := c.container.task.Spec.GetContainer()
|
|
if container == nil {
|
|
return errors.New("unable to get container from task spec")
|
|
}
|
|
|
|
if err := c.backend.SetContainerDependencyStore(cr.ID, c.dependencies); err != nil {
|
|
return err
|
|
}
|
|
|
|
// configure secrets
|
|
secretRefs := convert.SecretReferencesFromGRPC(container.Secrets)
|
|
if err := c.backend.SetContainerSecretReferences(cr.ID, secretRefs); err != nil {
|
|
return err
|
|
}
|
|
|
|
configRefs := convert.ConfigReferencesFromGRPC(container.Configs)
|
|
if err := c.backend.SetContainerConfigReferences(cr.ID, configRefs); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := c.backend.UpdateContainerServiceConfig(cr.ID, c.container.serviceConfig()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// checkMounts ensures that the provided mounts won't have any host-specific
|
|
// problems at start up. For example, we disallow bind mounts without an
|
|
// existing path, which slightly different from the container API.
|
|
func (c *containerAdapter) checkMounts() error {
|
|
spec := c.container.spec()
|
|
for _, mount := range spec.Mounts {
|
|
switch mount.Type {
|
|
case api.MountTypeBind:
|
|
if _, err := os.Stat(mount.Source); os.IsNotExist(err) {
|
|
return fmt.Errorf("invalid bind mount source, source path not found: %s", mount.Source)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *containerAdapter) start(ctx context.Context) error {
|
|
if err := c.checkMounts(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.backend.ContainerStart(c.container.name(), nil, "", "")
|
|
}
|
|
|
|
func (c *containerAdapter) inspect(ctx context.Context) (types.ContainerJSON, error) {
|
|
cs, err := c.backend.ContainerInspectCurrent(c.container.name(), false)
|
|
if ctx.Err() != nil {
|
|
return types.ContainerJSON{}, ctx.Err()
|
|
}
|
|
if err != nil {
|
|
return types.ContainerJSON{}, err
|
|
}
|
|
return *cs, nil
|
|
}
|
|
|
|
// events issues a call to the events API and returns a channel with all
|
|
// events. The stream of events can be shutdown by cancelling the context.
|
|
func (c *containerAdapter) events(ctx context.Context) <-chan events.Message {
|
|
log.G(ctx).Debugf("waiting on events")
|
|
buffer, l := c.backend.SubscribeToEvents(time.Time{}, time.Time{}, c.container.eventFilter())
|
|
eventsq := make(chan events.Message, len(buffer))
|
|
|
|
for _, event := range buffer {
|
|
eventsq <- event
|
|
}
|
|
|
|
go func() {
|
|
defer c.backend.UnsubscribeFromEvents(l)
|
|
|
|
for {
|
|
select {
|
|
case ev := <-l:
|
|
jev, ok := ev.(events.Message)
|
|
if !ok {
|
|
log.G(ctx).Warnf("unexpected event message: %q", ev)
|
|
continue
|
|
}
|
|
select {
|
|
case eventsq <- jev:
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
return eventsq
|
|
}
|
|
|
|
func (c *containerAdapter) wait(ctx context.Context) (<-chan containerpkg.StateStatus, error) {
|
|
return c.backend.ContainerWait(ctx, c.container.nameOrID(), containerpkg.WaitConditionNotRunning)
|
|
}
|
|
|
|
func (c *containerAdapter) shutdown(ctx context.Context) error {
|
|
// Default stop grace period to nil (daemon will use the stopTimeout of the container)
|
|
var stopgrace *int
|
|
spec := c.container.spec()
|
|
if spec.StopGracePeriod != nil {
|
|
stopgraceValue := int(spec.StopGracePeriod.Seconds)
|
|
stopgrace = &stopgraceValue
|
|
}
|
|
return c.backend.ContainerStop(c.container.name(), stopgrace)
|
|
}
|
|
|
|
func (c *containerAdapter) terminate(ctx context.Context) error {
|
|
return c.backend.ContainerKill(c.container.name(), uint64(syscall.SIGKILL))
|
|
}
|
|
|
|
func (c *containerAdapter) remove(ctx context.Context) error {
|
|
return c.backend.ContainerRm(c.container.name(), &types.ContainerRmConfig{
|
|
RemoveVolume: true,
|
|
ForceRemove: true,
|
|
})
|
|
}
|
|
|
|
func (c *containerAdapter) createVolumes(ctx context.Context) error {
|
|
// Create plugin volumes that are embedded inside a Mount
|
|
for _, mount := range c.container.task.Spec.GetContainer().Mounts {
|
|
if mount.Type != api.MountTypeVolume {
|
|
continue
|
|
}
|
|
|
|
if mount.VolumeOptions == nil {
|
|
continue
|
|
}
|
|
|
|
if mount.VolumeOptions.DriverConfig == nil {
|
|
continue
|
|
}
|
|
|
|
req := c.container.volumeCreateRequest(&mount)
|
|
|
|
// Check if this volume exists on the engine
|
|
if _, err := c.backend.VolumeCreate(req.Name, req.Driver, req.DriverOpts, req.Labels); err != nil {
|
|
// TODO(amitshukla): Today, volume create through the engine api does not return an error
|
|
// when the named volume with the same parameters already exists.
|
|
// It returns an error if the driver name is different - that is a valid error
|
|
return err
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *containerAdapter) activateServiceBinding() error {
|
|
return c.backend.ActivateContainerServiceBinding(c.container.name())
|
|
}
|
|
|
|
func (c *containerAdapter) deactivateServiceBinding() error {
|
|
return c.backend.DeactivateContainerServiceBinding(c.container.name())
|
|
}
|
|
|
|
func (c *containerAdapter) logs(ctx context.Context, options api.LogSubscriptionOptions) (<-chan *backend.LogMessage, error) {
|
|
apiOptions := &types.ContainerLogsOptions{
|
|
Follow: options.Follow,
|
|
|
|
// Always say yes to Timestamps and Details. we make the decision
|
|
// of whether to return these to the user or not way higher up the
|
|
// stack.
|
|
Timestamps: true,
|
|
Details: true,
|
|
}
|
|
|
|
if options.Since != nil {
|
|
since, err := gogotypes.TimestampFromProto(options.Since)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// print since as this formatted string because the docker container
|
|
// logs interface expects it like this.
|
|
// see github.com/docker/docker/api/types/time.ParseTimestamps
|
|
apiOptions.Since = fmt.Sprintf("%d.%09d", since.Unix(), int64(since.Nanosecond()))
|
|
}
|
|
|
|
if options.Tail < 0 {
|
|
// See protobuf documentation for details of how this works.
|
|
apiOptions.Tail = fmt.Sprint(-options.Tail - 1)
|
|
} else if options.Tail > 0 {
|
|
return nil, errors.New("tail relative to start of logs not supported via docker API")
|
|
}
|
|
|
|
if len(options.Streams) == 0 {
|
|
// empty == all
|
|
apiOptions.ShowStdout, apiOptions.ShowStderr = true, true
|
|
} else {
|
|
for _, stream := range options.Streams {
|
|
switch stream {
|
|
case api.LogStreamStdout:
|
|
apiOptions.ShowStdout = true
|
|
case api.LogStreamStderr:
|
|
apiOptions.ShowStderr = true
|
|
}
|
|
}
|
|
}
|
|
msgs, err := c.backend.ContainerLogs(ctx, c.container.name(), apiOptions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return msgs, nil
|
|
}
|
|
|
|
// todo: typed/wrapped errors
|
|
func isContainerCreateNameConflict(err error) bool {
|
|
return strings.Contains(err.Error(), "Conflict. The name")
|
|
}
|
|
|
|
func isUnknownContainer(err error) bool {
|
|
return strings.Contains(err.Error(), "No such container:")
|
|
}
|
|
|
|
func isStoppedContainer(err error) bool {
|
|
return strings.Contains(err.Error(), "is already stopped")
|
|
}
|