2015-11-12 14:55:17 -05:00
|
|
|
// +build linux freebsd
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2017-03-30 16:52:40 -04:00
|
|
|
"context"
|
2015-11-12 14:55:17 -05:00
|
|
|
"fmt"
|
2016-10-19 12:22:02 -04:00
|
|
|
"io/ioutil"
|
2015-11-12 14:55:17 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/container"
|
|
|
|
"github.com/docker/docker/daemon/links"
|
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2016-11-08 11:34:45 -05:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/runconfig"
|
2016-10-13 17:51:10 -04:00
|
|
|
"github.com/docker/libnetwork"
|
2017-04-18 09:26:36 -04:00
|
|
|
"github.com/opencontainers/selinux/go-selinux/label"
|
2016-10-26 16:30:53 -04:00
|
|
|
"github.com/pkg/errors"
|
2015-11-12 14:55:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (daemon *Daemon) setupLinkedContainers(container *container.Container) ([]string, error) {
|
|
|
|
var env []string
|
2015-09-03 20:51:04 -04:00
|
|
|
children := daemon.children(container)
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
bridgeSettings := container.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
|
2016-08-23 19:50:15 -04:00
|
|
|
if bridgeSettings == nil || bridgeSettings.EndpointSettings == nil {
|
2015-11-12 14:55:17 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
for linkAlias, child := range children {
|
|
|
|
if !child.IsRunning() {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
return nil, fmt.Errorf("Cannot link to a non running container: %s AS %s", child.Name, linkAlias)
|
2015-09-03 20:51:04 -04:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
childBridgeSettings := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
|
2016-08-23 19:50:15 -04:00
|
|
|
if childBridgeSettings == nil || childBridgeSettings.EndpointSettings == nil {
|
2015-09-03 20:51:04 -04:00
|
|
|
return nil, fmt.Errorf("container %s not attached to default bridge network", child.ID)
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
link := links.NewLink(
|
|
|
|
bridgeSettings.IPAddress,
|
|
|
|
childBridgeSettings.IPAddress,
|
|
|
|
linkAlias,
|
|
|
|
child.Config.Env,
|
|
|
|
child.Config.ExposedPorts,
|
|
|
|
)
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2016-10-13 12:34:19 -04:00
|
|
|
env = append(env, link.ToEnv()...)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
2015-09-03 20:51:04 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
return env, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) getIpcContainer(container *container.Container) (*container.Container, error) {
|
|
|
|
containerID := container.HostConfig.IpcMode.Container()
|
2017-02-28 05:11:48 -05:00
|
|
|
container, err := daemon.GetContainer(containerID)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
2017-02-28 05:11:48 -05:00
|
|
|
return nil, errors.Wrapf(err, "cannot join IPC of a non running container: %s", container.ID)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
2017-02-28 05:11:48 -05:00
|
|
|
return container, daemon.checkContainer(container, containerIsRunning, containerIsNotRestarting)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
2016-05-06 14:56:03 -04:00
|
|
|
func (daemon *Daemon) getPidContainer(container *container.Container) (*container.Container, error) {
|
|
|
|
containerID := container.HostConfig.PidMode.Container()
|
2017-02-28 05:11:48 -05:00
|
|
|
container, err := daemon.GetContainer(containerID)
|
2016-05-06 14:56:03 -04:00
|
|
|
if err != nil {
|
2017-02-28 05:11:48 -05:00
|
|
|
return nil, errors.Wrapf(err, "cannot join PID of a non running container: %s", container.ID)
|
2016-05-06 14:56:03 -04:00
|
|
|
}
|
2017-02-28 05:11:48 -05:00
|
|
|
return container, daemon.checkContainer(container, containerIsRunning, containerIsNotRestarting)
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerIsRunning(c *container.Container) error {
|
2016-05-06 14:56:03 -04:00
|
|
|
if !c.IsRunning() {
|
2017-02-28 05:11:48 -05:00
|
|
|
return errors.Errorf("container %s is not running", c.ID)
|
2016-05-06 14:56:03 -04:00
|
|
|
}
|
2017-02-28 05:11:48 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerIsNotRestarting(c *container.Container) error {
|
2016-05-06 14:56:03 -04:00
|
|
|
if c.IsRestarting() {
|
2017-02-28 05:11:48 -05:00
|
|
|
return errContainerIsRestarting(c.ID)
|
2016-05-06 14:56:03 -04:00
|
|
|
}
|
2017-02-28 05:11:48 -05:00
|
|
|
return nil
|
2016-05-06 14:56:03 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
|
2016-03-18 14:50:19 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
c.ShmPath, err = c.ShmResourcePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostConfig.IpcMode.IsContainer() {
|
|
|
|
ic, err := daemon.getIpcContainer(c)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
c.ShmPath = ic.ShmPath
|
|
|
|
} else if c.HostConfig.IpcMode.IsHost() {
|
|
|
|
if _, err := os.Stat("/dev/shm"); err != nil {
|
|
|
|
return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
c.ShmPath = "/dev/shm"
|
|
|
|
} else {
|
2017-05-31 17:56:23 -04:00
|
|
|
rootIDs := daemon.idMappings.RootPair()
|
2016-03-18 14:50:19 -04:00
|
|
|
if !c.HasMountFor("/dev/shm") {
|
|
|
|
shmPath, err := c.ShmResourcePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
|
2017-05-19 18:06:46 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(shmPath, 0700, rootIDs); err != nil {
|
2016-03-18 14:50:19 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-25 04:11:12 -05:00
|
|
|
shmSize := int64(daemon.configStore.ShmSize)
|
2016-03-18 14:50:19 -04:00
|
|
|
if c.HostConfig.ShmSize != 0 {
|
|
|
|
shmSize = c.HostConfig.ShmSize
|
|
|
|
}
|
|
|
|
shmproperty := "mode=1777,size=" + strconv.FormatInt(shmSize, 10)
|
|
|
|
if err := syscall.Mount("shm", shmPath, "tmpfs", uintptr(syscall.MS_NOEXEC|syscall.MS_NOSUID|syscall.MS_NODEV), label.FormatMountLabel(shmproperty, c.GetMountLabel())); err != nil {
|
|
|
|
return fmt.Errorf("mounting shm tmpfs: %s", err)
|
|
|
|
}
|
2017-05-19 18:06:46 -04:00
|
|
|
if err := os.Chown(shmPath, rootIDs.UID, rootIDs.GID); err != nil {
|
2016-03-18 14:50:19 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-10-19 12:22:02 -04:00
|
|
|
|
2016-10-27 14:29:51 -04:00
|
|
|
func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
|
2016-11-15 10:04:36 -05:00
|
|
|
if len(c.SecretReferences) == 0 {
|
2016-10-28 20:16:07 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:22:02 -04:00
|
|
|
localMountPath := c.SecretMountPath()
|
|
|
|
logrus.Debugf("secrets: setting up secret dir: %s", localMountPath)
|
|
|
|
|
2017-03-16 17:23:33 -04:00
|
|
|
// retrieve possible remapped range start for root UID, GID
|
2017-05-31 17:56:23 -04:00
|
|
|
rootIDs := daemon.idMappings.RootPair()
|
2017-03-16 17:23:33 -04:00
|
|
|
// create tmpfs
|
2017-05-19 18:06:46 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(localMountPath, 0700, rootIDs); err != nil {
|
2017-03-16 17:23:33 -04:00
|
|
|
return errors.Wrap(err, "error creating secret local mount path")
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:51:02 -04:00
|
|
|
defer func() {
|
|
|
|
if setupErr != nil {
|
2016-10-26 16:30:53 -04:00
|
|
|
// cleanup
|
|
|
|
_ = detachMounted(localMountPath)
|
|
|
|
|
|
|
|
if err := os.RemoveAll(localMountPath); err != nil {
|
2017-01-17 14:20:54 -05:00
|
|
|
logrus.Errorf("error cleaning up secret mount: %s", err)
|
2016-10-26 16:30:53 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-27 18:51:02 -04:00
|
|
|
}()
|
2016-10-26 16:30:53 -04:00
|
|
|
|
2017-05-19 18:06:46 -04:00
|
|
|
tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootIDs.UID, rootIDs.GID)
|
2016-11-09 10:52:38 -05:00
|
|
|
if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "nodev,nosuid,noexec,"+tmpfsOwnership); err != nil {
|
2016-10-27 14:29:51 -04:00
|
|
|
return errors.Wrap(err, "unable to setup secret mount")
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2017-03-16 17:23:33 -04:00
|
|
|
if c.DependencyStore == nil {
|
|
|
|
return fmt.Errorf("secret store is not initialized")
|
|
|
|
}
|
2016-11-15 10:04:36 -05:00
|
|
|
|
2017-03-16 17:23:33 -04:00
|
|
|
for _, s := range c.SecretReferences {
|
2016-11-15 10:04:36 -05:00
|
|
|
// TODO (ehazlett): use type switch when more are supported
|
|
|
|
if s.File == nil {
|
2017-03-16 17:23:33 -04:00
|
|
|
logrus.Error("secret target type is not a file target")
|
|
|
|
continue
|
2016-11-15 10:04:36 -05:00
|
|
|
}
|
|
|
|
|
2017-04-28 14:48:52 -04:00
|
|
|
// secrets are created in the SecretMountPath on the host, at a
|
|
|
|
// single level
|
|
|
|
fPath := c.SecretFilePath(*s)
|
2017-05-19 18:06:46 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(filepath.Dir(fPath), 0700, rootIDs); err != nil {
|
2016-10-27 14:29:51 -04:00
|
|
|
return errors.Wrap(err, "error creating secret mount path")
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2016-10-26 16:30:53 -04:00
|
|
|
logrus.WithFields(logrus.Fields{
|
2016-11-15 10:04:36 -05:00
|
|
|
"name": s.File.Name,
|
2016-10-26 16:30:53 -04:00
|
|
|
"path": fPath,
|
|
|
|
}).Debug("injecting secret")
|
2017-03-16 17:23:33 -04:00
|
|
|
secret := c.DependencyStore.Secrets().Get(s.SecretID)
|
2016-11-15 10:04:36 -05:00
|
|
|
if secret == nil {
|
|
|
|
return fmt.Errorf("unable to get secret from secret store")
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(fPath, secret.Spec.Data, s.File.Mode); err != nil {
|
2016-10-27 14:29:51 -04:00
|
|
|
return errors.Wrap(err, "error injecting secret")
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2016-11-15 10:04:36 -05:00
|
|
|
uid, err := strconv.Atoi(s.File.UID)
|
2016-11-03 14:09:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-15 10:04:36 -05:00
|
|
|
gid, err := strconv.Atoi(s.File.GID)
|
2016-11-03 14:09:13 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-19 18:06:46 -04:00
|
|
|
if err := os.Chown(fPath, rootIDs.UID+uid, rootIDs.GID+gid); err != nil {
|
2016-10-27 14:29:51 -04:00
|
|
|
return errors.Wrap(err, "error setting ownership for secret")
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 11:43:23 -04:00
|
|
|
label.Relabel(localMountPath, c.MountLabel, false)
|
|
|
|
|
2016-10-19 12:22:02 -04:00
|
|
|
// remount secrets ro
|
2016-11-09 10:52:38 -05:00
|
|
|
if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "remount,ro,"+tmpfsOwnership); err != nil {
|
2016-10-27 14:29:51 -04:00
|
|
|
return errors.Wrap(err, "unable to remount secret dir as readonly")
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2016-10-27 14:29:51 -04:00
|
|
|
return nil
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2017-03-16 17:23:33 -04:00
|
|
|
func (daemon *Daemon) setupConfigDir(c *container.Container) (setupErr error) {
|
|
|
|
if len(c.ConfigReferences) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
localPath := c.ConfigsDirPath()
|
|
|
|
logrus.Debugf("configs: setting up config dir: %s", localPath)
|
|
|
|
|
|
|
|
// retrieve possible remapped range start for root UID, GID
|
2017-05-31 17:56:23 -04:00
|
|
|
rootIDs := daemon.idMappings.RootPair()
|
2017-03-16 17:23:33 -04:00
|
|
|
// create tmpfs
|
2017-05-19 18:06:46 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(localPath, 0700, rootIDs); err != nil {
|
2017-03-16 17:23:33 -04:00
|
|
|
return errors.Wrap(err, "error creating config dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if setupErr != nil {
|
|
|
|
if err := os.RemoveAll(localPath); err != nil {
|
|
|
|
logrus.Errorf("error cleaning up config dir: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if c.DependencyStore == nil {
|
|
|
|
return fmt.Errorf("config store is not initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, configRef := range c.ConfigReferences {
|
|
|
|
// TODO (ehazlett): use type switch when more are supported
|
|
|
|
if configRef.File == nil {
|
|
|
|
logrus.Error("config target type is not a file target")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fPath := c.ConfigFilePath(*configRef)
|
|
|
|
|
|
|
|
log := logrus.WithFields(logrus.Fields{"name": configRef.File.Name, "path": fPath})
|
|
|
|
|
2017-05-19 18:06:46 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(filepath.Dir(fPath), 0700, rootIDs); err != nil {
|
2017-03-16 17:23:33 -04:00
|
|
|
return errors.Wrap(err, "error creating config path")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("injecting config")
|
|
|
|
config := c.DependencyStore.Configs().Get(configRef.ConfigID)
|
|
|
|
if config == nil {
|
|
|
|
return fmt.Errorf("unable to get config from config store")
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(fPath, config.Spec.Data, configRef.File.Mode); err != nil {
|
|
|
|
return errors.Wrap(err, "error injecting config")
|
|
|
|
}
|
|
|
|
|
|
|
|
uid, err := strconv.Atoi(configRef.File.UID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gid, err := strconv.Atoi(configRef.File.GID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-19 18:06:46 -04:00
|
|
|
if err := os.Chown(fPath, rootIDs.UID+uid, rootIDs.GID+gid); err != nil {
|
2017-03-16 17:23:33 -04:00
|
|
|
return errors.Wrap(err, "error setting ownership for config")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-30 23:01:41 -04:00
|
|
|
func killProcessDirectly(cntr *container.Container) error {
|
2017-03-30 16:52:40 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Block until the container to stops or timeout.
|
2017-03-30 23:01:41 -04:00
|
|
|
status := <-cntr.Wait(ctx, container.WaitConditionNotRunning)
|
2017-03-30 16:52:40 -04:00
|
|
|
if status.Err() != nil {
|
2015-11-12 14:55:17 -05:00
|
|
|
// Ensure that we don't kill ourselves
|
2017-03-30 23:01:41 -04:00
|
|
|
if pid := cntr.GetPID(); pid != 0 {
|
|
|
|
logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(cntr.ID))
|
2015-11-12 14:55:17 -05:00
|
|
|
if err := syscall.Kill(pid, 9); err != nil {
|
|
|
|
if err != syscall.ESRCH {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-04 15:41:06 -05:00
|
|
|
e := errNoSuchProcess{pid, 9}
|
|
|
|
logrus.Debug(e)
|
|
|
|
return e
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func detachMounted(path string) error {
|
|
|
|
return syscall.Unmount(path, syscall.MNT_DETACH)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLinkable(child *container.Container) bool {
|
|
|
|
// A container is linkable only if it belongs to the default network
|
2016-03-09 23:33:21 -05:00
|
|
|
_, ok := child.NetworkSettings.Networks[runconfig.DefaultDaemonNetworkMode().NetworkName()]
|
2015-11-12 14:55:17 -05:00
|
|
|
return ok
|
|
|
|
}
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 10:53:35 -05:00
|
|
|
|
2016-04-20 18:35:11 -04:00
|
|
|
func enableIPOnPredefinedNetwork() bool {
|
|
|
|
return false
|
|
|
|
}
|
2016-09-21 15:02:20 -04:00
|
|
|
|
|
|
|
func (daemon *Daemon) isNetworkHotPluggable() bool {
|
|
|
|
return true
|
|
|
|
}
|
2016-10-13 17:51:10 -04:00
|
|
|
|
|
|
|
func setupPathsAndSandboxOptions(container *container.Container, sboxOptions *[]libnetwork.SandboxOption) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
container.HostsPath, err = container.GetRootResourcePath("hosts")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*sboxOptions = append(*sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
|
|
|
|
|
|
|
|
container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*sboxOptions = append(*sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func initializeNetworkingPaths(container *container.Container, nc *container.Container) {
|
|
|
|
container.HostnamePath = nc.HostnamePath
|
|
|
|
container.HostsPath = nc.HostsPath
|
|
|
|
container.ResolvConfPath = nc.ResolvConfPath
|
|
|
|
}
|