2015-11-12 14:55:17 -05:00
|
|
|
// +build linux freebsd
|
|
|
|
|
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/container"
|
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
|
|
|
"github.com/docker/docker/daemon/links"
|
|
|
|
"github.com/docker/docker/daemon/network"
|
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
|
|
|
"github.com/docker/docker/errors"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
|
|
|
"github.com/docker/docker/pkg/idtools"
|
|
|
|
"github.com/docker/docker/pkg/mount"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/runconfig"
|
2016-01-04 19:05:26 -05:00
|
|
|
containertypes "github.com/docker/engine-api/types/container"
|
|
|
|
networktypes "github.com/docker/engine-api/types/network"
|
2015-12-21 15:10:53 -05:00
|
|
|
"github.com/docker/go-units"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/libnetwork"
|
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/configs"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/devices"
|
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
bridgeSettings := container.NetworkSettings.Networks["bridge"]
|
|
|
|
if bridgeSettings == nil {
|
|
|
|
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
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
childBridgeSettings := child.NetworkSettings.Networks["bridge"]
|
|
|
|
if childBridgeSettings == nil {
|
|
|
|
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
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
for _, envVar := range link.ToEnv() {
|
|
|
|
env = append(env, envVar)
|
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) populateCommand(c *container.Container, env []string) error {
|
|
|
|
var en *execdriver.Network
|
|
|
|
if !c.Config.NetworkDisabled {
|
|
|
|
en = &execdriver.Network{}
|
|
|
|
if !daemon.execDriver.SupportsHooks() || c.HostConfig.NetworkMode.IsHost() {
|
|
|
|
en.NamespacePath = c.NetworkSettings.SandboxKey
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostConfig.NetworkMode.IsContainer() {
|
|
|
|
nc, err := daemon.getNetworkedContainer(c.ID, c.HostConfig.NetworkMode.ConnectedContainer())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
en.ContainerID = nc.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc := &execdriver.Ipc{}
|
|
|
|
var err error
|
|
|
|
c.ShmPath, err = c.ShmResourcePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.HostConfig.IpcMode.IsContainer() {
|
|
|
|
ic, err := daemon.getIpcContainer(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ipc.ContainerID = ic.ID
|
|
|
|
c.ShmPath = ic.ShmPath
|
|
|
|
} else {
|
|
|
|
ipc.HostIpc = c.HostConfig.IpcMode.IsHost()
|
|
|
|
if ipc.HostIpc {
|
|
|
|
if _, err := os.Stat("/dev/shm"); err != nil {
|
|
|
|
return fmt.Errorf("/dev/shm is not mounted, but must be for --ipc=host")
|
|
|
|
}
|
|
|
|
c.ShmPath = "/dev/shm"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pid := &execdriver.Pid{}
|
|
|
|
pid.HostPid = c.HostConfig.PidMode.IsHost()
|
|
|
|
|
|
|
|
uts := &execdriver.UTS{
|
|
|
|
HostUTS: c.HostConfig.UTSMode.IsHost(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build lists of devices allowed and created within the container.
|
|
|
|
var userSpecifiedDevices []*configs.Device
|
|
|
|
for _, deviceMapping := range c.HostConfig.Devices {
|
|
|
|
devs, err := getDevicesFromPath(deviceMapping)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
userSpecifiedDevices = append(userSpecifiedDevices, devs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedDevices := mergeDevices(configs.DefaultAllowedDevices, userSpecifiedDevices)
|
|
|
|
|
|
|
|
autoCreatedDevices := mergeDevices(configs.DefaultAutoCreatedDevices, userSpecifiedDevices)
|
|
|
|
|
2015-12-21 15:10:53 -05:00
|
|
|
var rlimits []*units.Rlimit
|
2015-11-12 14:55:17 -05:00
|
|
|
ulimits := c.HostConfig.Ulimits
|
|
|
|
|
|
|
|
// Merge ulimits with daemon defaults
|
2015-12-21 15:10:53 -05:00
|
|
|
ulIdx := make(map[string]*units.Ulimit)
|
2015-11-12 14:55:17 -05:00
|
|
|
for _, ul := range ulimits {
|
|
|
|
ulIdx[ul.Name] = ul
|
|
|
|
}
|
|
|
|
for name, ul := range daemon.configStore.Ulimits {
|
|
|
|
if _, exists := ulIdx[name]; !exists {
|
|
|
|
ulimits = append(ulimits, ul)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
weightDevices, err := getBlkioWeightDevices(c.HostConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-08 07:06:48 -04:00
|
|
|
readBpsDevice, err := getBlkioReadBpsDevices(c.HostConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
writeBpsDevice, err := getBlkioWriteBpsDevices(c.HostConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-08 07:06:48 -04:00
|
|
|
readIOpsDevice, err := getBlkioReadIOpsDevices(c.HostConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
writeIOpsDevice, err := getBlkioWriteIOpsDevices(c.HostConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
for _, limit := range ulimits {
|
|
|
|
rl, err := limit.GetRlimit()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rlimits = append(rlimits, rl)
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := &execdriver.Resources{
|
|
|
|
CommonResources: execdriver.CommonResources{
|
|
|
|
Memory: c.HostConfig.Memory,
|
|
|
|
MemoryReservation: c.HostConfig.MemoryReservation,
|
|
|
|
CPUShares: c.HostConfig.CPUShares,
|
|
|
|
BlkioWeight: c.HostConfig.BlkioWeight,
|
|
|
|
},
|
2015-07-08 07:06:48 -04:00
|
|
|
MemorySwap: c.HostConfig.MemorySwap,
|
|
|
|
KernelMemory: c.HostConfig.KernelMemory,
|
|
|
|
CpusetCpus: c.HostConfig.CpusetCpus,
|
|
|
|
CpusetMems: c.HostConfig.CpusetMems,
|
|
|
|
CPUPeriod: c.HostConfig.CPUPeriod,
|
|
|
|
CPUQuota: c.HostConfig.CPUQuota,
|
|
|
|
Rlimits: rlimits,
|
|
|
|
BlkioWeightDevice: weightDevices,
|
|
|
|
BlkioThrottleReadBpsDevice: readBpsDevice,
|
|
|
|
BlkioThrottleWriteBpsDevice: writeBpsDevice,
|
|
|
|
BlkioThrottleReadIOpsDevice: readIOpsDevice,
|
|
|
|
BlkioThrottleWriteIOpsDevice: writeIOpsDevice,
|
|
|
|
MemorySwappiness: -1,
|
2015-12-09 15:11:41 -05:00
|
|
|
}
|
|
|
|
|
2016-02-03 04:31:00 -05:00
|
|
|
if c.HostConfig.OomKillDisable != nil {
|
|
|
|
resources.OomKillDisable = *c.HostConfig.OomKillDisable
|
|
|
|
}
|
2015-12-09 15:11:41 -05:00
|
|
|
if c.HostConfig.MemorySwappiness != nil {
|
|
|
|
resources.MemorySwappiness = *c.HostConfig.MemorySwappiness
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
processConfig := execdriver.ProcessConfig{
|
|
|
|
CommonProcessConfig: execdriver.CommonProcessConfig{
|
|
|
|
Entrypoint: c.Path,
|
|
|
|
Arguments: c.Args,
|
|
|
|
Tty: c.Config.Tty,
|
|
|
|
},
|
|
|
|
Privileged: c.HostConfig.Privileged,
|
|
|
|
User: c.Config.User,
|
|
|
|
}
|
|
|
|
|
|
|
|
processConfig.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
|
|
|
|
processConfig.Env = env
|
|
|
|
|
|
|
|
remappedRoot := &execdriver.User{}
|
|
|
|
rootUID, rootGID := daemon.GetRemappedUIDGID()
|
|
|
|
if rootUID != 0 {
|
|
|
|
remappedRoot.UID = rootUID
|
|
|
|
remappedRoot.GID = rootGID
|
|
|
|
}
|
|
|
|
uidMap, gidMap := daemon.GetUIDGIDMaps()
|
|
|
|
|
2016-01-11 14:44:34 -05:00
|
|
|
if !daemon.seccompEnabled {
|
|
|
|
if c.SeccompProfile != "" && c.SeccompProfile != "unconfined" {
|
|
|
|
return fmt.Errorf("Seccomp is not enabled in your kernel, cannot run a custom seccomp profile.")
|
|
|
|
}
|
|
|
|
logrus.Warn("Seccomp is not enabled in your kernel, running container without default profile.")
|
|
|
|
c.SeccompProfile = "unconfined"
|
|
|
|
}
|
|
|
|
|
2016-01-06 16:59:01 -05:00
|
|
|
defaultCgroupParent := "/docker"
|
|
|
|
if daemon.configStore.CgroupParent != "" {
|
|
|
|
defaultCgroupParent = daemon.configStore.CgroupParent
|
2016-01-22 21:15:09 -05:00
|
|
|
} else if daemon.usingSystemd() {
|
|
|
|
defaultCgroupParent = "system.slice"
|
2016-01-06 16:59:01 -05:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
c.Command = &execdriver.Command{
|
|
|
|
CommonCommand: execdriver.CommonCommand{
|
|
|
|
ID: c.ID,
|
|
|
|
MountLabel: c.GetMountLabel(),
|
|
|
|
Network: en,
|
|
|
|
ProcessConfig: processConfig,
|
|
|
|
ProcessLabel: c.GetProcessLabel(),
|
|
|
|
Rootfs: c.BaseFS,
|
|
|
|
Resources: resources,
|
|
|
|
WorkingDir: c.Config.WorkingDir,
|
|
|
|
},
|
|
|
|
AllowedDevices: allowedDevices,
|
|
|
|
AppArmorProfile: c.AppArmorProfile,
|
|
|
|
AutoCreatedDevices: autoCreatedDevices,
|
2016-02-29 06:28:37 -05:00
|
|
|
CapAdd: c.HostConfig.CapAdd,
|
|
|
|
CapDrop: c.HostConfig.CapDrop,
|
2016-01-06 16:59:01 -05:00
|
|
|
CgroupParent: defaultCgroupParent,
|
2015-11-12 14:55:17 -05:00
|
|
|
GIDMapping: gidMap,
|
|
|
|
GroupAdd: c.HostConfig.GroupAdd,
|
|
|
|
Ipc: ipc,
|
|
|
|
OomScoreAdj: c.HostConfig.OomScoreAdj,
|
|
|
|
Pid: pid,
|
|
|
|
ReadonlyRootfs: c.HostConfig.ReadonlyRootfs,
|
|
|
|
RemappedRoot: remappedRoot,
|
2015-11-14 21:02:26 -05:00
|
|
|
SeccompProfile: c.SeccompProfile,
|
2015-11-12 14:55:17 -05:00
|
|
|
UIDMapping: uidMap,
|
|
|
|
UTS: uts,
|
|
|
|
}
|
2015-12-07 12:55:33 -05:00
|
|
|
if c.HostConfig.CgroupParent != "" {
|
|
|
|
c.Command.CgroupParent = c.HostConfig.CgroupParent
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSize returns the real size & virtual size of the container.
|
|
|
|
func (daemon *Daemon) getSize(container *container.Container) (int64, int64) {
|
|
|
|
var (
|
|
|
|
sizeRw, sizeRootfs int64
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := daemon.Mount(container); err != nil {
|
|
|
|
logrus.Errorf("Failed to compute size of container rootfs %s: %s", container.ID, err)
|
|
|
|
return sizeRw, sizeRootfs
|
|
|
|
}
|
|
|
|
defer daemon.Unmount(container)
|
|
|
|
|
|
|
|
sizeRw, err = container.RWLayer.Size()
|
|
|
|
if err != nil {
|
2015-12-16 15:32:16 -05:00
|
|
|
logrus.Errorf("Driver %s couldn't return diff size of container %s: %s",
|
|
|
|
daemon.GraphDriverName(), container.ID, err)
|
2015-11-12 14:55:17 -05:00
|
|
|
// FIXME: GetSize should return an error. Not changing it now in case
|
|
|
|
// there is a side-effect.
|
|
|
|
sizeRw = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent := container.RWLayer.Parent(); parent != nil {
|
|
|
|
sizeRootfs, err = parent.Size()
|
|
|
|
if err != nil {
|
|
|
|
sizeRootfs = -1
|
|
|
|
} else if sizeRw != -1 {
|
|
|
|
sizeRootfs += sizeRw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sizeRw, sizeRootfs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) buildSandboxOptions(container *container.Container, n libnetwork.Network) ([]libnetwork.SandboxOption, error) {
|
|
|
|
var (
|
|
|
|
sboxOptions []libnetwork.SandboxOption
|
|
|
|
err error
|
|
|
|
dns []string
|
|
|
|
dnsSearch []string
|
|
|
|
dnsOptions []string
|
|
|
|
)
|
|
|
|
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionHostname(container.Config.Hostname),
|
|
|
|
libnetwork.OptionDomainname(container.Config.Domainname))
|
|
|
|
|
|
|
|
if container.HostConfig.NetworkMode.IsHost() {
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionUseDefaultSandbox())
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionOriginHostsPath("/etc/hosts"))
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionOriginResolvConfPath("/etc/resolv.conf"))
|
|
|
|
} else if daemon.execDriver.SupportsHooks() {
|
|
|
|
// OptionUseExternalKey is mandatory for userns support.
|
|
|
|
// But optional for non-userns support
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionUseExternalKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
container.HostsPath, err = container.GetRootResourcePath("hosts")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionHostsPath(container.HostsPath))
|
|
|
|
|
|
|
|
container.ResolvConfPath, err = container.GetRootResourcePath("resolv.conf")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionResolvConfPath(container.ResolvConfPath))
|
|
|
|
|
|
|
|
if len(container.HostConfig.DNS) > 0 {
|
|
|
|
dns = container.HostConfig.DNS
|
|
|
|
} else if len(daemon.configStore.DNS) > 0 {
|
|
|
|
dns = daemon.configStore.DNS
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range dns {
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionDNS(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(container.HostConfig.DNSSearch) > 0 {
|
|
|
|
dnsSearch = container.HostConfig.DNSSearch
|
|
|
|
} else if len(daemon.configStore.DNSSearch) > 0 {
|
|
|
|
dnsSearch = daemon.configStore.DNSSearch
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range dnsSearch {
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionDNSSearch(ds))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(container.HostConfig.DNSOptions) > 0 {
|
|
|
|
dnsOptions = container.HostConfig.DNSOptions
|
|
|
|
} else if len(daemon.configStore.DNSOptions) > 0 {
|
|
|
|
dnsOptions = daemon.configStore.DNSOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ds := range dnsOptions {
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionDNSOptions(ds))
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.NetworkSettings.SecondaryIPAddresses != nil {
|
|
|
|
name := container.Config.Hostname
|
|
|
|
if container.Config.Domainname != "" {
|
|
|
|
name = name + "." + container.Config.Domainname
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range container.NetworkSettings.SecondaryIPAddresses {
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(name, a.Addr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, extraHost := range container.HostConfig.ExtraHosts {
|
|
|
|
// allow IPv6 addresses in extra hosts; only split on first ":"
|
|
|
|
parts := strings.SplitN(extraHost, ":", 2)
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(parts[0], parts[1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link feature is supported only for the default bridge network.
|
|
|
|
// return if this call to build join options is not for default bridge network
|
|
|
|
if n.Name() != "bridge" {
|
|
|
|
return sboxOptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ep, _ := container.GetEndpointInNetwork(n)
|
|
|
|
if ep == nil {
|
|
|
|
return sboxOptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var childEndpoints, parentEndpoints []string
|
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
children := daemon.children(container)
|
2015-11-12 14:55:17 -05:00
|
|
|
for linkAlias, child := range children {
|
|
|
|
if !isLinkable(child) {
|
|
|
|
return nil, fmt.Errorf("Cannot link to %s, as it does not belong to the default network", child.Name)
|
|
|
|
}
|
|
|
|
_, alias := path.Split(linkAlias)
|
|
|
|
// allow access to the linked container via the alias, real name, and container hostname
|
|
|
|
aliasList := alias + " " + child.Config.Hostname
|
|
|
|
// only add the name if alias isn't equal to the name
|
|
|
|
if alias != child.Name[1:] {
|
|
|
|
aliasList = aliasList + " " + child.Name[1:]
|
|
|
|
}
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(aliasList, child.NetworkSettings.Networks["bridge"].IPAddress))
|
|
|
|
cEndpoint, _ := child.GetEndpointInNetwork(n)
|
|
|
|
if cEndpoint != nil && cEndpoint.ID() != "" {
|
|
|
|
childEndpoints = append(childEndpoints, cEndpoint.ID())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bridgeSettings := container.NetworkSettings.Networks["bridge"]
|
2015-09-03 20:51:04 -04:00
|
|
|
for alias, parent := range daemon.parents(container) {
|
|
|
|
if daemon.configStore.DisableBridge || !container.HostConfig.NetworkMode.IsPrivate() {
|
2015-11-12 14:55:17 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-03 20:51:04 -04:00
|
|
|
_, alias = path.Split(alias)
|
|
|
|
logrus.Debugf("Update /etc/hosts of %s for alias %s with ip %s", parent.ID, alias, bridgeSettings.IPAddress)
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionParentUpdate(
|
|
|
|
parent.ID,
|
|
|
|
alias,
|
|
|
|
bridgeSettings.IPAddress,
|
|
|
|
))
|
|
|
|
if ep.ID() != "" {
|
|
|
|
parentEndpoints = append(parentEndpoints, ep.ID())
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
linkOptions := options.Generic{
|
|
|
|
netlabel.GenericData: options.Generic{
|
|
|
|
"ParentEndpoints": parentEndpoints,
|
|
|
|
"ChildEndpoints": childEndpoints,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sboxOptions = append(sboxOptions, libnetwork.OptionGeneric(linkOptions))
|
|
|
|
return sboxOptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) updateNetworkSettings(container *container.Container, n libnetwork.Network) error {
|
|
|
|
if container.NetworkSettings == nil {
|
2015-12-09 16:38:39 -05:00
|
|
|
container.NetworkSettings = &network.Settings{Networks: make(map[string]*networktypes.EndpointSettings)}
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
2015-12-18 13:36:17 -05:00
|
|
|
if !container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
|
2015-11-12 14:55:17 -05:00
|
|
|
return runconfig.ErrConflictHostNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
for s := range container.NetworkSettings.Networks {
|
|
|
|
sn, err := daemon.FindNetwork(s)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if sn.Name() == n.Name() {
|
|
|
|
// Avoid duplicate config
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-18 13:36:17 -05:00
|
|
|
if !containertypes.NetworkMode(sn.Type()).IsPrivate() ||
|
|
|
|
!containertypes.NetworkMode(n.Type()).IsPrivate() {
|
2015-11-12 14:55:17 -05:00
|
|
|
return runconfig.ErrConflictSharedNetwork
|
|
|
|
}
|
2015-12-18 13:36:17 -05:00
|
|
|
if containertypes.NetworkMode(sn.Name()).IsNone() ||
|
|
|
|
containertypes.NetworkMode(n.Name()).IsNone() {
|
2015-11-12 14:55:17 -05:00
|
|
|
return runconfig.ErrConflictNoNetwork
|
|
|
|
}
|
|
|
|
}
|
2016-01-07 19:18:34 -05:00
|
|
|
|
|
|
|
if _, ok := container.NetworkSettings.Networks[n.Name()]; !ok {
|
|
|
|
container.NetworkSettings.Networks[n.Name()] = new(networktypes.EndpointSettings)
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) updateEndpointNetworkSettings(container *container.Container, n libnetwork.Network, ep libnetwork.Endpoint) error {
|
|
|
|
if err := container.BuildEndpointInfo(n, ep); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-18 13:36:17 -05:00
|
|
|
if container.HostConfig.NetworkMode == containertypes.NetworkMode("bridge") {
|
2016-01-25 16:30:33 -05:00
|
|
|
container.NetworkSettings.Bridge = daemon.configStore.bridgeConfig.Iface
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateNetwork is used to update the container's network (e.g. when linked containers
|
|
|
|
// get removed/unlinked).
|
|
|
|
func (daemon *Daemon) updateNetwork(container *container.Container) error {
|
|
|
|
ctrl := daemon.netController
|
|
|
|
sid := container.NetworkSettings.SandboxID
|
|
|
|
|
|
|
|
sb, err := ctrl.SandboxByID(sid)
|
|
|
|
if err != nil {
|
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 fmt.Errorf("error locating sandbox id %s: %v", sid, err)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find if container is connected to the default bridge network
|
|
|
|
var n libnetwork.Network
|
|
|
|
for name := range container.NetworkSettings.Networks {
|
|
|
|
sn, err := daemon.FindNetwork(name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sn.Name() == "bridge" {
|
|
|
|
n = sn
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == nil {
|
|
|
|
// Not connected to the default bridge network; Nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
options, err := daemon.buildSandboxOptions(container, n)
|
|
|
|
if err != nil {
|
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 fmt.Errorf("Update network failed: %v", err)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := sb.Refresh(options...); err != nil {
|
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 fmt.Errorf("Update network failed: Failure in refresh sandbox %s: %v", sid, err)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-13 02:33:18 -05:00
|
|
|
// updateContainerNetworkSettings update the network settings
|
2016-01-07 19:18:34 -05:00
|
|
|
func (daemon *Daemon) updateContainerNetworkSettings(container *container.Container, endpointsConfig map[string]*networktypes.EndpointSettings) error {
|
|
|
|
var (
|
|
|
|
n libnetwork.Network
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2015-12-13 02:33:18 -05:00
|
|
|
mode := container.HostConfig.NetworkMode
|
|
|
|
if container.Config.NetworkDisabled || mode.IsContainer() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
networkName := mode.NetworkName()
|
|
|
|
if mode.IsDefault() {
|
|
|
|
networkName = daemon.netController.Config().Daemon.DefaultNetwork
|
|
|
|
}
|
|
|
|
if mode.IsUserDefined() {
|
2016-01-07 19:18:34 -05:00
|
|
|
n, err = daemon.FindNetwork(networkName)
|
2015-12-13 02:33:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
networkName = n.Name()
|
|
|
|
}
|
2016-01-07 19:18:34 -05:00
|
|
|
if container.NetworkSettings == nil {
|
|
|
|
container.NetworkSettings = &network.Settings{}
|
|
|
|
}
|
2016-01-05 14:20:47 -05:00
|
|
|
if len(endpointsConfig) > 0 {
|
2016-01-07 19:18:34 -05:00
|
|
|
container.NetworkSettings.Networks = endpointsConfig
|
|
|
|
}
|
|
|
|
if container.NetworkSettings.Networks == nil {
|
|
|
|
container.NetworkSettings.Networks = make(map[string]*networktypes.EndpointSettings)
|
|
|
|
container.NetworkSettings.Networks[networkName] = new(networktypes.EndpointSettings)
|
|
|
|
}
|
|
|
|
if !mode.IsUserDefined() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Make sure to internally store the per network endpoint config by network name
|
|
|
|
if _, ok := container.NetworkSettings.Networks[networkName]; ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if nwConfig, ok := container.NetworkSettings.Networks[n.ID()]; ok {
|
|
|
|
container.NetworkSettings.Networks[networkName] = nwConfig
|
|
|
|
delete(container.NetworkSettings.Networks, n.ID())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-13 02:33:18 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) allocateNetwork(container *container.Container) error {
|
|
|
|
controller := daemon.netController
|
|
|
|
|
|
|
|
// Cleanup any stale sandbox left over due to ungraceful daemon shutdown
|
|
|
|
if err := controller.SandboxDestroy(container.ID); err != nil {
|
|
|
|
logrus.Errorf("failed to cleanup up stale network sandbox for container %s", container.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSettings := false
|
|
|
|
if len(container.NetworkSettings.Networks) == 0 {
|
2015-12-13 02:33:18 -05:00
|
|
|
if container.Config.NetworkDisabled || container.HostConfig.NetworkMode.IsContainer() {
|
2015-11-12 14:55:17 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:18:34 -05:00
|
|
|
err := daemon.updateContainerNetworkSettings(container, nil)
|
2015-12-13 02:33:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
updateSettings = true
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:18:34 -05:00
|
|
|
for n, nConf := range container.NetworkSettings.Networks {
|
|
|
|
if err := daemon.connectToNetwork(container, n, nConf, updateSettings); err != nil {
|
2015-11-12 14:55:17 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return container.WriteHostConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) getNetworkSandbox(container *container.Container) libnetwork.Sandbox {
|
|
|
|
var sb libnetwork.Sandbox
|
|
|
|
daemon.netController.WalkSandboxes(func(s libnetwork.Sandbox) bool {
|
|
|
|
if s.ContainerID() == container.ID {
|
|
|
|
sb = s
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
return sb
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:18:34 -05:00
|
|
|
// hasUserDefinedIPAddress returns whether the passed endpoint configuration contains IP address configuration
|
|
|
|
func hasUserDefinedIPAddress(epConfig *networktypes.EndpointSettings) bool {
|
|
|
|
return epConfig != nil && epConfig.IPAMConfig != nil && (len(epConfig.IPAMConfig.IPv4Address) > 0 || len(epConfig.IPAMConfig.IPv6Address) > 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// User specified ip address is acceptable only for networks with user specified subnets.
|
|
|
|
func validateNetworkingConfig(n libnetwork.Network, epConfig *networktypes.EndpointSettings) error {
|
2016-01-20 20:51:41 -05:00
|
|
|
if n == nil || epConfig == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-07 19:18:34 -05:00
|
|
|
if !hasUserDefinedIPAddress(epConfig) {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-08 16:38:52 -05:00
|
|
|
_, _, nwIPv4Configs, nwIPv6Configs := n.Info().IpamConfig()
|
2016-01-07 19:18:34 -05:00
|
|
|
for _, s := range []struct {
|
|
|
|
ipConfigured bool
|
|
|
|
subnetConfigs []*libnetwork.IpamConf
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
ipConfigured: len(epConfig.IPAMConfig.IPv4Address) > 0,
|
|
|
|
subnetConfigs: nwIPv4Configs,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ipConfigured: len(epConfig.IPAMConfig.IPv6Address) > 0,
|
|
|
|
subnetConfigs: nwIPv6Configs,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
if s.ipConfigured {
|
|
|
|
foundSubnet := false
|
|
|
|
for _, cfg := range s.subnetConfigs {
|
|
|
|
if len(cfg.PreferredPool) > 0 {
|
|
|
|
foundSubnet = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundSubnet {
|
|
|
|
return runconfig.ErrUnsupportedNetworkNoSubnetAndIP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanOperationalData resets the operational data from the passed endpoint settings
|
|
|
|
func cleanOperationalData(es *networktypes.EndpointSettings) {
|
|
|
|
es.EndpointID = ""
|
|
|
|
es.Gateway = ""
|
|
|
|
es.IPAddress = ""
|
|
|
|
es.IPPrefixLen = 0
|
|
|
|
es.IPv6Gateway = ""
|
|
|
|
es.GlobalIPv6Address = ""
|
|
|
|
es.GlobalIPv6PrefixLen = 0
|
|
|
|
es.MacAddress = ""
|
|
|
|
}
|
|
|
|
|
2016-01-20 20:51:41 -05:00
|
|
|
func (daemon *Daemon) updateNetworkConfig(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (libnetwork.Network, error) {
|
2016-01-11 20:13:39 -05:00
|
|
|
if container.HostConfig.NetworkMode.IsContainer() {
|
|
|
|
return nil, runconfig.ErrConflictSharedNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
if containertypes.NetworkMode(idOrName).IsBridge() &&
|
|
|
|
daemon.configStore.DisableBridge {
|
|
|
|
container.Config.NetworkDisabled = true
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-01-20 20:51:41 -05:00
|
|
|
if !containertypes.NetworkMode(idOrName).IsUserDefined() {
|
|
|
|
if hasUserDefinedIPAddress(endpointConfig) {
|
|
|
|
return nil, runconfig.ErrUnsupportedNetworkAndIP
|
|
|
|
}
|
|
|
|
if endpointConfig != nil && len(endpointConfig.Aliases) > 0 {
|
|
|
|
return nil, runconfig.ErrUnsupportedNetworkAndAlias
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:13:39 -05:00
|
|
|
n, err := daemon.FindNetwork(idOrName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-20 20:51:41 -05:00
|
|
|
if err := validateNetworkingConfig(n, endpointConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:13:39 -05:00
|
|
|
if updateSettings {
|
|
|
|
if err := daemon.updateNetworkSettings(container, n); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// ConnectToNetwork connects a container to a network
|
2016-01-07 19:18:34 -05:00
|
|
|
func (daemon *Daemon) ConnectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings) error {
|
2015-11-12 14:55:17 -05:00
|
|
|
if !container.Running {
|
2016-01-11 20:13:39 -05:00
|
|
|
if container.RemovalInProgress || container.Dead {
|
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 errRemovalContainer(container.ID)
|
2016-01-11 20:13:39 -05:00
|
|
|
}
|
2016-01-20 20:51:41 -05:00
|
|
|
if _, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, true); err != nil {
|
2016-01-11 20:13:39 -05:00
|
|
|
return err
|
|
|
|
}
|
2016-02-02 20:25:28 -05:00
|
|
|
if endpointConfig != nil {
|
|
|
|
container.NetworkSettings.Networks[idOrName] = endpointConfig
|
|
|
|
}
|
2016-01-11 20:13:39 -05:00
|
|
|
} else {
|
|
|
|
if err := daemon.connectToNetwork(container, idOrName, endpointConfig, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
if err := container.ToDiskLocking(); err != nil {
|
|
|
|
return fmt.Errorf("Error saving container to disk: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:18:34 -05:00
|
|
|
func (daemon *Daemon) connectToNetwork(container *container.Container, idOrName string, endpointConfig *networktypes.EndpointSettings, updateSettings bool) (err error) {
|
2016-01-20 20:51:41 -05:00
|
|
|
n, err := daemon.updateNetworkConfig(container, idOrName, endpointConfig, updateSettings)
|
2016-01-11 20:13:39 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if n == nil {
|
|
|
|
return nil
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
controller := daemon.netController
|
|
|
|
|
2016-01-25 22:55:18 -05:00
|
|
|
sb := daemon.getNetworkSandbox(container)
|
|
|
|
createOptions, err := container.BuildCreateEndpointOptions(n, endpointConfig, sb)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
endpointName := strings.TrimPrefix(container.Name, "/")
|
2016-01-11 01:45:43 -05:00
|
|
|
ep, err := n.CreateEndpoint(endpointName, createOptions...)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2016-01-12 14:36:48 -05:00
|
|
|
if e := ep.Delete(false); e != nil {
|
2015-11-12 14:55:17 -05:00
|
|
|
logrus.Warnf("Could not rollback container connection to network %s", idOrName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-01-25 14:14:29 -05:00
|
|
|
if endpointConfig != nil {
|
|
|
|
container.NetworkSettings.Networks[n.Name()] = endpointConfig
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
if err := daemon.updateEndpointNetworkSettings(container, n, ep); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if sb == nil {
|
|
|
|
options, err := daemon.buildSandboxOptions(container, n)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sb, err = controller.NewSandbox(container.ID, options...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container.UpdateSandboxNetworkSettings(sb)
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:20:47 -05:00
|
|
|
joinOptions, err := container.BuildJoinOptions(n)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep.Join(sb, joinOptions...); err != nil {
|
2015-11-12 14:55:17 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.UpdateJoinInfo(n, ep); err != nil {
|
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 fmt.Errorf("Updating join info failed: %v", err)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
2015-12-21 23:35:30 -05:00
|
|
|
daemon.LogNetworkEventWithAttributes(n, "connect", map[string]string{"container": container.ID})
|
2015-11-12 14:55:17 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:56:36 -05:00
|
|
|
// ForceEndpointDelete deletes an endpoing from a network forcefully
|
|
|
|
func (daemon *Daemon) ForceEndpointDelete(name string, n libnetwork.Network) error {
|
|
|
|
ep, err := n.EndpointByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ep.Delete(true)
|
|
|
|
}
|
|
|
|
|
2015-12-03 14:10:27 -05:00
|
|
|
// DisconnectFromNetwork disconnects container from network n.
|
2016-01-12 23:56:36 -05:00
|
|
|
func (daemon *Daemon) DisconnectFromNetwork(container *container.Container, n libnetwork.Network, force bool) error {
|
2015-12-18 13:36:17 -05:00
|
|
|
if container.HostConfig.NetworkMode.IsHost() && containertypes.NetworkMode(n.Type()).IsHost() {
|
2015-12-03 14:10:27 -05:00
|
|
|
return runconfig.ErrConflictHostNetwork
|
|
|
|
}
|
2016-01-11 20:13:39 -05:00
|
|
|
if !container.Running {
|
|
|
|
if container.RemovalInProgress || container.Dead {
|
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 errRemovalContainer(container.ID)
|
2016-01-11 20:13:39 -05:00
|
|
|
}
|
|
|
|
if _, ok := container.NetworkSettings.Networks[n.Name()]; ok {
|
|
|
|
delete(container.NetworkSettings.Networks, n.Name())
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("container %s is not connected to the network %s", container.ID, n.Name())
|
|
|
|
}
|
|
|
|
} else {
|
2016-01-12 23:56:36 -05:00
|
|
|
if err := disconnectFromNetwork(container, n, false); err != nil {
|
2016-01-11 20:13:39 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-12-25 02:09:37 -05:00
|
|
|
}
|
2015-12-03 14:10:27 -05:00
|
|
|
|
|
|
|
if err := container.ToDiskLocking(); err != nil {
|
|
|
|
return fmt.Errorf("Error saving container to disk: %v", err)
|
|
|
|
}
|
2015-12-21 19:45:31 -05:00
|
|
|
|
|
|
|
attributes := map[string]string{
|
|
|
|
"container": container.ID,
|
|
|
|
}
|
|
|
|
daemon.LogNetworkEventWithAttributes(n, "disconnect", attributes)
|
2015-12-25 02:09:37 -05:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-03 14:10:27 -05:00
|
|
|
|
2016-01-12 23:56:36 -05:00
|
|
|
func disconnectFromNetwork(container *container.Container, n libnetwork.Network, force bool) error {
|
2015-12-03 14:10:27 -05:00
|
|
|
var (
|
|
|
|
ep libnetwork.Endpoint
|
|
|
|
sbox libnetwork.Sandbox
|
|
|
|
)
|
|
|
|
|
|
|
|
s := func(current libnetwork.Endpoint) bool {
|
|
|
|
epInfo := current.Info()
|
|
|
|
if epInfo == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if sb := epInfo.Sandbox(); sb != nil {
|
|
|
|
if sb.ContainerID() == container.ID {
|
|
|
|
ep = current
|
|
|
|
sbox = sb
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
n.WalkEndpoints(s)
|
|
|
|
|
2016-01-12 23:56:36 -05:00
|
|
|
if ep == nil && force {
|
|
|
|
epName := strings.TrimPrefix(container.Name, "/")
|
|
|
|
ep, err := n.EndpointByName(epName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ep.Delete(force)
|
|
|
|
}
|
|
|
|
|
2015-12-03 14:10:27 -05:00
|
|
|
if ep == nil {
|
|
|
|
return fmt.Errorf("container %s is not connected to the network", container.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ep.Leave(sbox); err != nil {
|
|
|
|
return fmt.Errorf("container %s failed to leave network %s: %v", container.ID, n.Name(), err)
|
|
|
|
}
|
|
|
|
|
2016-01-12 14:36:48 -05:00
|
|
|
if err := ep.Delete(false); err != nil {
|
2015-12-03 14:10:27 -05:00
|
|
|
return fmt.Errorf("endpoint delete failed for container %s on network %s: %v", container.ID, n.Name(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(container.NetworkSettings.Networks, n.Name())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) initializeNetworking(container *container.Container) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if container.HostConfig.NetworkMode.IsContainer() {
|
|
|
|
// we need to get the hosts files from the container to join
|
|
|
|
nc, err := daemon.getNetworkedContainer(container.ID, container.HostConfig.NetworkMode.ConnectedContainer())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.HostnamePath = nc.HostnamePath
|
|
|
|
container.HostsPath = nc.HostsPath
|
|
|
|
container.ResolvConfPath = nc.ResolvConfPath
|
|
|
|
container.Config.Hostname = nc.Config.Hostname
|
|
|
|
container.Config.Domainname = nc.Config.Domainname
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.HostConfig.NetworkMode.IsHost() {
|
|
|
|
container.Config.Hostname, err = os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(container.Config.Hostname, ".", 2)
|
|
|
|
if len(parts) > 1 {
|
|
|
|
container.Config.Hostname = parts[0]
|
|
|
|
container.Config.Domainname = parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := daemon.allocateNetwork(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return container.BuildHostnameFile()
|
|
|
|
}
|
|
|
|
|
|
|
|
// called from the libcontainer pre-start hook to set the network
|
|
|
|
// namespace configuration linkage to the libnetwork "sandbox" entity
|
|
|
|
func (daemon *Daemon) setNetworkNamespaceKey(containerID string, pid int) error {
|
|
|
|
path := fmt.Sprintf("/proc/%d/ns/net", pid)
|
|
|
|
var sandbox libnetwork.Sandbox
|
|
|
|
search := libnetwork.SandboxContainerWalker(&sandbox, containerID)
|
|
|
|
daemon.netController.WalkSandboxes(search)
|
|
|
|
if sandbox == nil {
|
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 fmt.Errorf("error locating sandbox id %s: no sandbox found", containerID)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return sandbox.SetKey(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) getIpcContainer(container *container.Container) (*container.Container, error) {
|
|
|
|
containerID := container.HostConfig.IpcMode.Container()
|
2015-12-11 12:39:28 -05:00
|
|
|
c, err := daemon.GetContainer(containerID)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !c.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 join IPC of a non running container: %s", containerID)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
2016-02-02 07:36:13 -05:00
|
|
|
if c.IsRestarting() {
|
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, errContainerIsRestarting(container.ID)
|
2016-02-02 07:36:13 -05:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) getNetworkedContainer(containerID, connectedContainerID string) (*container.Container, error) {
|
2015-12-11 12:39:28 -05:00
|
|
|
nc, err := daemon.GetContainer(connectedContainerID)
|
2015-11-12 14:55:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if containerID == nc.ID {
|
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 join own network")
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
if !nc.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
|
|
|
err := fmt.Errorf("cannot join network of a non running container: %s", connectedContainerID)
|
|
|
|
return nil, errors.NewRequestConflictError(err)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
2016-02-02 07:36:13 -05:00
|
|
|
if nc.IsRestarting() {
|
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, errContainerIsRestarting(connectedContainerID)
|
2016-02-02 07:36:13 -05:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
return nc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) releaseNetwork(container *container.Container) {
|
|
|
|
if container.HostConfig.NetworkMode.IsContainer() || container.Config.NetworkDisabled {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sid := container.NetworkSettings.SandboxID
|
2015-12-21 19:45:31 -05:00
|
|
|
settings := container.NetworkSettings.Networks
|
2016-01-20 20:09:11 -05:00
|
|
|
container.NetworkSettings.Ports = nil
|
|
|
|
|
2016-01-07 19:18:34 -05:00
|
|
|
if sid == "" || len(settings) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-21 23:35:30 -05:00
|
|
|
var networks []libnetwork.Network
|
2016-01-07 19:18:34 -05:00
|
|
|
for n, epSettings := range settings {
|
2015-12-21 23:35:30 -05:00
|
|
|
if nw, err := daemon.FindNetwork(n); err == nil {
|
|
|
|
networks = append(networks, nw)
|
|
|
|
}
|
2016-01-07 19:18:34 -05:00
|
|
|
cleanOperationalData(epSettings)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sb, err := daemon.netController.SandboxByID(sid)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("error locating sandbox id %s: %v", sid, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sb.Delete(); err != nil {
|
|
|
|
logrus.Errorf("Error deleting sandbox id %s for container %s: %v", sid, container.ID, err)
|
|
|
|
}
|
2015-12-21 19:45:31 -05:00
|
|
|
|
|
|
|
attributes := map[string]string{
|
|
|
|
"container": container.ID,
|
|
|
|
}
|
2015-12-21 23:35:30 -05:00
|
|
|
for _, nw := range networks {
|
|
|
|
daemon.LogNetworkEventWithAttributes(nw, "disconnect", attributes)
|
2015-12-21 19:45:31 -05:00
|
|
|
}
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
|
|
|
|
rootUID, rootGID := daemon.GetRemappedUIDGID()
|
|
|
|
if !c.HasMountFor("/dev/shm") {
|
|
|
|
shmPath, err := c.ShmResourcePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := idtools.MkdirAllAs(shmPath, 0700, rootUID, rootGID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
shmSize := container.DefaultSHMSize
|
2015-12-29 15:49:17 -05:00
|
|
|
if c.HostConfig.ShmSize != 0 {
|
|
|
|
shmSize = c.HostConfig.ShmSize
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if err := os.Chown(shmPath, rootUID, rootGID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (daemon *Daemon) mountVolumes(container *container.Container) error {
|
|
|
|
mounts, err := daemon.setupMounts(container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range mounts {
|
|
|
|
dest, err := container.GetResourcePath(m.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stat os.FileInfo
|
|
|
|
stat, err = os.Stat(m.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := "rbind,ro"
|
|
|
|
if m.Writable {
|
|
|
|
opts = "rbind,rw"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mount.Mount(m.Source, dest, "bind", opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func killProcessDirectly(container *container.Container) error {
|
|
|
|
if _, err := container.WaitStop(10 * time.Second); err != nil {
|
|
|
|
// Ensure that we don't kill ourselves
|
|
|
|
if pid := container.GetPID(); pid != 0 {
|
|
|
|
logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(container.ID))
|
|
|
|
if err := syscall.Kill(pid, 9); err != nil {
|
|
|
|
if err != syscall.ESRCH {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logrus.Debugf("Cannot kill process (pid=%d) with signal 9: no such process.", pid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-18 13:36:17 -05:00
|
|
|
func getDevicesFromPath(deviceMapping containertypes.DeviceMapping) (devs []*configs.Device, err error) {
|
2016-02-24 20:50:39 -05:00
|
|
|
resolvedPathOnHost := deviceMapping.PathOnHost
|
|
|
|
|
|
|
|
// check if it is a symbolic link
|
|
|
|
if src, e := os.Lstat(deviceMapping.PathOnHost); e == nil && src.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
if linkedPathOnHost, e := os.Readlink(deviceMapping.PathOnHost); e == nil {
|
|
|
|
resolvedPathOnHost = linkedPathOnHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
device, err := devices.DeviceFromPath(resolvedPathOnHost, deviceMapping.CgroupPermissions)
|
2015-11-12 14:55:17 -05:00
|
|
|
// if there was no error, return the device
|
|
|
|
if err == nil {
|
|
|
|
device.Path = deviceMapping.PathInContainer
|
|
|
|
return append(devs, device), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the device is not a device node
|
|
|
|
// try to see if it's a directory holding many devices
|
|
|
|
if err == devices.ErrNotADevice {
|
|
|
|
|
|
|
|
// check if it is a directory
|
2016-02-24 20:50:39 -05:00
|
|
|
if src, e := os.Stat(resolvedPathOnHost); e == nil && src.IsDir() {
|
2015-11-12 14:55:17 -05:00
|
|
|
|
|
|
|
// mount the internal devices recursively
|
2016-02-24 20:50:39 -05:00
|
|
|
filepath.Walk(resolvedPathOnHost, func(dpath string, f os.FileInfo, e error) error {
|
2015-11-12 14:55:17 -05:00
|
|
|
childDevice, e := devices.DeviceFromPath(dpath, deviceMapping.CgroupPermissions)
|
|
|
|
if e != nil {
|
|
|
|
// ignore the device
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the device to userSpecified devices
|
2016-02-24 20:50:39 -05:00
|
|
|
childDevice.Path = strings.Replace(dpath, resolvedPathOnHost, deviceMapping.PathInContainer, 1)
|
2015-11-12 14:55:17 -05:00
|
|
|
devs = append(devs, childDevice)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(devs) > 0 {
|
|
|
|
return devs, nil
|
|
|
|
}
|
|
|
|
|
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 devs, fmt.Errorf("error gathering device information while adding custom device %q: %s", deviceMapping.PathOnHost, err)
|
2015-11-12 14:55:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func mergeDevices(defaultDevices, userDevices []*configs.Device) []*configs.Device {
|
|
|
|
if len(userDevices) == 0 {
|
|
|
|
return defaultDevices
|
|
|
|
}
|
|
|
|
|
|
|
|
paths := map[string]*configs.Device{}
|
|
|
|
for _, d := range userDevices {
|
|
|
|
paths[d.Path] = d
|
|
|
|
}
|
|
|
|
|
|
|
|
var devs []*configs.Device
|
|
|
|
for _, d := range defaultDevices {
|
|
|
|
if _, defined := paths[d.Path]; !defined {
|
|
|
|
devs = append(devs, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(devs, userDevices...)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
_, ok := child.NetworkSettings.Networks["bridge"]
|
|
|
|
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
|
|
|
|
|
|
|
func errRemovalContainer(containerID string) error {
|
|
|
|
return fmt.Errorf("Container %s is marked for removal and cannot be connected or disconnected to the network", containerID)
|
|
|
|
}
|