2014-07-31 20:36:42 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
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 15:53:35 +00:00
|
|
|
"fmt"
|
2016-08-23 04:52:56 +00:00
|
|
|
"net"
|
2016-06-07 07:45:21 +00:00
|
|
|
"runtime"
|
2016-05-27 09:32:26 +00:00
|
|
|
"strings"
|
2016-07-20 23:11:28 +00:00
|
|
|
"time"
|
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 15:53:35 +00:00
|
|
|
|
2016-06-07 07:45:21 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2015-07-02 10:24:35 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-06-07 07:45:21 +00:00
|
|
|
apierrors "github.com/docker/docker/api/errors"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2015-07-20 17:57:15 +00:00
|
|
|
"github.com/docker/docker/image"
|
2015-12-16 22:13:50 +00:00
|
|
|
"github.com/docker/docker/layer"
|
2015-11-18 22:20:54 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2015-06-12 13:25:32 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-05-24 08:13:54 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-09-23 20:29:14 +00:00
|
|
|
volumestore "github.com/docker/docker/volume/store"
|
2015-07-16 23:00:55 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2014-07-31 20:36:42 +00:00
|
|
|
)
|
|
|
|
|
2016-06-14 02:52:49 +00:00
|
|
|
// CreateManagedContainer creates a container that is managed by a Service
|
2016-10-14 20:28:47 +00:00
|
|
|
func (daemon *Daemon) CreateManagedContainer(params types.ContainerCreateConfig, validateHostname bool) (containertypes.ContainerCreateCreatedBody, error) {
|
2016-07-06 07:13:59 +00:00
|
|
|
return daemon.containerCreate(params, true, validateHostname)
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContainerCreate creates a regular container
|
2016-10-14 20:28:47 +00:00
|
|
|
func (daemon *Daemon) ContainerCreate(params types.ContainerCreateConfig, validateHostname bool) (containertypes.ContainerCreateCreatedBody, error) {
|
2016-07-06 07:13:59 +00:00
|
|
|
return daemon.containerCreate(params, false, validateHostname)
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 20:28:47 +00:00
|
|
|
func (daemon *Daemon) containerCreate(params types.ContainerCreateConfig, managed bool, validateHostname bool) (containertypes.ContainerCreateCreatedBody, error) {
|
2016-07-20 23:11:28 +00:00
|
|
|
start := time.Now()
|
2015-09-10 02:23:06 +00:00
|
|
|
if params.Config == nil {
|
2016-10-14 20:28:47 +00:00
|
|
|
return containertypes.ContainerCreateCreatedBody{}, fmt.Errorf("Config cannot be empty in order to create a container")
|
2015-06-06 16:41:42 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 07:13:59 +00:00
|
|
|
warnings, err := daemon.verifyContainerSettings(params.HostConfig, params.Config, false, validateHostname)
|
2015-04-16 06:31:52 +00:00
|
|
|
if err != nil {
|
2016-10-14 20:28:47 +00:00
|
|
|
return containertypes.ContainerCreateCreatedBody{Warnings: warnings}, err
|
2015-01-23 03:29:21 +00:00
|
|
|
}
|
2014-09-25 21:23:59 +00:00
|
|
|
|
2016-01-21 22:24:35 +00:00
|
|
|
err = daemon.verifyNetworkingConfig(params.NetworkingConfig)
|
|
|
|
if err != nil {
|
2016-10-14 20:28:47 +00:00
|
|
|
return containertypes.ContainerCreateCreatedBody{Warnings: warnings}, err
|
2016-01-21 22:24:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 05:10:18 +00:00
|
|
|
if params.HostConfig == nil {
|
2015-12-18 18:36:17 +00:00
|
|
|
params.HostConfig = &containertypes.HostConfig{}
|
2015-11-30 05:10:18 +00:00
|
|
|
}
|
|
|
|
err = daemon.adaptContainerSettings(params.HostConfig, params.AdjustCPUShares)
|
|
|
|
if err != nil {
|
2016-10-14 20:28:47 +00:00
|
|
|
return containertypes.ContainerCreateCreatedBody{Warnings: warnings}, err
|
2015-11-30 05:10:18 +00:00
|
|
|
}
|
2015-09-07 03:07:12 +00:00
|
|
|
|
2016-06-14 02:52:49 +00:00
|
|
|
container, err := daemon.create(params, managed)
|
2014-07-31 20:36:42 +00:00
|
|
|
if err != nil {
|
2016-10-14 20:28:47 +00:00
|
|
|
return containertypes.ContainerCreateCreatedBody{Warnings: warnings}, daemon.imageNotExistToErrcode(err)
|
2014-07-31 20:36:42 +00:00
|
|
|
}
|
2016-07-20 23:11:28 +00:00
|
|
|
containerActions.WithValues("create").UpdateSince(start)
|
2016-10-14 20:28:47 +00:00
|
|
|
|
|
|
|
return containertypes.ContainerCreateCreatedBody{ID: container.ID, Warnings: warnings}, nil
|
2014-07-31 20:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a new container from the given configuration with a given name.
|
2016-06-14 02:52:49 +00:00
|
|
|
func (daemon *Daemon) create(params types.ContainerCreateConfig, managed bool) (retC *container.Container, retErr error) {
|
2014-07-31 20:36:42 +00:00
|
|
|
var (
|
2015-11-12 19:55:17 +00:00
|
|
|
container *container.Container
|
2015-07-20 17:57:15 +00:00
|
|
|
img *image.Image
|
2015-11-18 22:20:54 +00:00
|
|
|
imgID image.ID
|
2014-10-28 21:06:23 +00:00
|
|
|
err error
|
2014-07-31 20:36:42 +00:00
|
|
|
)
|
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
if params.Config.Image != "" {
|
2015-11-18 22:20:54 +00:00
|
|
|
img, err = daemon.GetImage(params.Config.Image)
|
2014-10-28 21:06:23 +00:00
|
|
|
if err != nil {
|
2015-09-06 17:26:40 +00:00
|
|
|
return nil, err
|
2014-10-28 21:06:23 +00:00
|
|
|
}
|
2016-06-07 07:45:21 +00:00
|
|
|
|
|
|
|
if runtime.GOOS == "solaris" && img.OS != "solaris " {
|
|
|
|
return nil, errors.New("Platform on which parent image was created is not Solaris")
|
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
imgID = img.ID()
|
2014-07-31 20:36:42 +00:00
|
|
|
}
|
2014-10-28 21:06:23 +00:00
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
if err := daemon.mergeAndVerifyConfig(params.Config, img); err != nil {
|
2015-09-06 17:26:40 +00:00
|
|
|
return nil, err
|
2014-07-31 20:36:42 +00:00
|
|
|
}
|
2015-08-12 02:27:33 +00:00
|
|
|
|
2016-03-12 12:50:37 +00:00
|
|
|
if err := daemon.mergeAndVerifyLogConfig(¶ms.HostConfig.LogConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-14 02:52:49 +00:00
|
|
|
if container, err = daemon.newContainer(params.Name, params.Config, imgID, managed); err != nil {
|
2015-09-06 17:26:40 +00:00
|
|
|
return nil, err
|
2014-07-31 20:36:42 +00:00
|
|
|
}
|
2015-08-11 14:21:52 +00:00
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
2016-08-17 03:02:16 +00:00
|
|
|
if err := daemon.cleanupContainer(container, true, true); err != nil {
|
2016-03-29 20:52:18 +00:00
|
|
|
logrus.Errorf("failed to cleanup container on create error: %v", err)
|
2015-08-11 14:21:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-12-21 19:23:20 +00:00
|
|
|
if err := daemon.setSecurityOptions(container, params.HostConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-20 04:42:58 +00:00
|
|
|
container.HostConfig.StorageOpt = params.HostConfig.StorageOpt
|
|
|
|
|
2015-12-21 19:23:20 +00:00
|
|
|
// Set RWLayer for container after mount labels have been set
|
|
|
|
if err := daemon.setRWLayer(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:20:54 +00:00
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(daemon.uidMaps, daemon.gidMaps)
|
|
|
|
if err != nil {
|
2015-10-28 13:19:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-12 19:55:17 +00:00
|
|
|
if err := idtools.MkdirAs(container.Root, 0700, rootUID, rootGID); err != nil {
|
2015-09-06 17:26:40 +00:00
|
|
|
return nil, err
|
2014-07-31 20:36:42 +00:00
|
|
|
}
|
2016-05-12 14:52:00 +00:00
|
|
|
if err := idtools.MkdirAs(container.CheckpointDir(), 0700, rootUID, rootGID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
if err := daemon.setHostConfig(container, params.HostConfig); err != nil {
|
2015-09-06 17:26:40 +00:00
|
|
|
return nil, err
|
2014-09-25 21:23:59 +00:00
|
|
|
}
|
2015-05-19 20:05:25 +00:00
|
|
|
|
2016-01-08 18:19:25 +00:00
|
|
|
if err := daemon.createContainerPlatformSpecificSettings(container, params.Config, params.HostConfig); err != nil {
|
2015-09-06 17:26:40 +00:00
|
|
|
return nil, err
|
2014-11-11 16:17:33 +00:00
|
|
|
}
|
2015-07-16 21:14:58 +00:00
|
|
|
|
2016-01-08 00:18:34 +00:00
|
|
|
var endpointsConfigs map[string]*networktypes.EndpointSettings
|
|
|
|
if params.NetworkingConfig != nil {
|
|
|
|
endpointsConfigs = params.NetworkingConfig.EndpointsConfig
|
|
|
|
}
|
2016-05-24 08:13:54 +00:00
|
|
|
// Make sure NetworkMode has an acceptable value. We do this to ensure
|
|
|
|
// backwards API compatibility.
|
|
|
|
container.HostConfig = runconfig.SetDefaultNetModeIfBlank(container.HostConfig)
|
2016-01-08 00:18:34 +00:00
|
|
|
|
2016-09-23 05:06:30 +00:00
|
|
|
daemon.updateContainerNetworkSettings(container, endpointsConfigs)
|
2015-12-13 07:33:18 +00:00
|
|
|
|
2016-03-29 20:52:18 +00:00
|
|
|
if err := container.ToDisk(); err != nil {
|
2015-07-02 10:24:35 +00:00
|
|
|
logrus.Errorf("Error saving new container to disk: %v", err)
|
2015-09-06 17:26:40 +00:00
|
|
|
return nil, err
|
2014-07-31 20:36:42 +00:00
|
|
|
}
|
2016-03-29 20:52:18 +00:00
|
|
|
if err := daemon.Register(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-03 17:33:13 +00:00
|
|
|
daemon.LogContainerEvent(container, "create")
|
2015-09-06 17:26:40 +00:00
|
|
|
return container, nil
|
2014-07-31 20:36:42 +00:00
|
|
|
}
|
2014-12-01 16:44:13 +00:00
|
|
|
|
2016-05-25 19:59:55 +00:00
|
|
|
func (daemon *Daemon) generateSecurityOpt(ipcMode containertypes.IpcMode, pidMode containertypes.PidMode, privileged bool) ([]string, error) {
|
|
|
|
if ipcMode.IsHost() || pidMode.IsHost() || privileged {
|
2014-11-10 21:14:17 +00:00
|
|
|
return label.DisableSecOpt(), nil
|
|
|
|
}
|
2016-05-06 18:56:03 +00:00
|
|
|
|
|
|
|
var ipcLabel []string
|
|
|
|
var pidLabel []string
|
|
|
|
ipcContainer := ipcMode.Container()
|
|
|
|
pidContainer := pidMode.Container()
|
|
|
|
if ipcContainer != "" {
|
2015-12-11 17:39:28 +00:00
|
|
|
c, err := daemon.GetContainer(ipcContainer)
|
2014-12-16 23:06:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-11-10 21:14:17 +00:00
|
|
|
}
|
2016-05-06 18:56:03 +00:00
|
|
|
ipcLabel = label.DupSecOpt(c.ProcessLabel)
|
|
|
|
if pidContainer == "" {
|
|
|
|
return ipcLabel, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pidContainer != "" {
|
|
|
|
c, err := daemon.GetContainer(pidContainer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-10 21:14:17 +00:00
|
|
|
|
2016-05-06 18:56:03 +00:00
|
|
|
pidLabel = label.DupSecOpt(c.ProcessLabel)
|
|
|
|
if ipcContainer == "" {
|
|
|
|
return pidLabel, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if pidLabel != nil && ipcLabel != nil {
|
|
|
|
for i := 0; i < len(pidLabel); i++ {
|
|
|
|
if pidLabel[i] != ipcLabel[i] {
|
|
|
|
return nil, fmt.Errorf("--ipc and --pid containers SELinux labels aren't the same")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pidLabel, nil
|
2014-11-10 21:14:17 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-06-12 13:25:32 +00:00
|
|
|
|
2015-12-16 22:13:50 +00:00
|
|
|
func (daemon *Daemon) setRWLayer(container *container.Container) error {
|
|
|
|
var layerID layer.ChainID
|
|
|
|
if container.ImageID != "" {
|
|
|
|
img, err := daemon.imageStore.Get(container.ImageID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
layerID = img.RootFS.ChainID()
|
|
|
|
}
|
2016-09-21 18:45:25 +00:00
|
|
|
|
2016-11-16 21:31:23 +00:00
|
|
|
rwLayerOpts := &layer.CreateRWLayerOpts{
|
|
|
|
MountLabel: container.MountLabel,
|
|
|
|
InitFunc: daemon.getLayerInit(),
|
|
|
|
StorageOpt: container.HostConfig.StorageOpt,
|
|
|
|
}
|
2016-09-21 18:45:25 +00:00
|
|
|
|
2016-11-16 21:31:23 +00:00
|
|
|
rwLayer, err := daemon.layerStore.CreateRWLayer(container.ID, layerID, rwLayerOpts)
|
2015-12-16 22:13:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.RWLayer = rwLayer
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-12 13:25:32 +00:00
|
|
|
// VolumeCreate creates a volume with the specified name, driver, and opts
|
2016-11-15 19:45:20 +00:00
|
|
|
// This is called directly from the Engine API
|
2016-03-16 21:52:34 +00:00
|
|
|
func (daemon *Daemon) VolumeCreate(name, driverName string, opts, labels map[string]string) (*types.Volume, error) {
|
2015-06-12 13:25:32 +00:00
|
|
|
if name == "" {
|
|
|
|
name = stringid.GenerateNonCryptoID()
|
|
|
|
}
|
|
|
|
|
2016-03-16 21:52:34 +00:00
|
|
|
v, err := daemon.volumes.Create(name, driverName, opts, labels)
|
2015-06-12 13:25:32 +00:00
|
|
|
if err != nil {
|
2015-09-23 20:29:14 +00:00
|
|
|
if volumestore.IsNameConflict(err) {
|
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 15:53:35 +00:00
|
|
|
return nil, fmt.Errorf("A volume named %s already exists. Choose a different volume name.", name)
|
2015-09-23 20:29:14 +00:00
|
|
|
}
|
2015-06-12 13:25:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-04 12:09:51 +00:00
|
|
|
|
2015-09-23 20:29:14 +00:00
|
|
|
daemon.LogVolumeEvent(v.Name(), "create", map[string]string{"driver": v.DriverName()})
|
2016-04-12 21:09:55 +00:00
|
|
|
apiV := volumeToAPIType(v)
|
|
|
|
apiV.Mountpoint = v.Path()
|
|
|
|
return apiV, nil
|
2015-06-12 13:25:32 +00:00
|
|
|
}
|
2016-05-24 15:49:26 +00:00
|
|
|
|
|
|
|
func (daemon *Daemon) mergeAndVerifyConfig(config *containertypes.Config, img *image.Image) error {
|
|
|
|
if img != nil && img.Config != nil {
|
|
|
|
if err := merge(config, img.Config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-06-18 21:16:05 +00:00
|
|
|
// Reset the Entrypoint if it is [""]
|
|
|
|
if len(config.Entrypoint) == 1 && config.Entrypoint[0] == "" {
|
|
|
|
config.Entrypoint = nil
|
|
|
|
}
|
2016-05-24 15:49:26 +00:00
|
|
|
if len(config.Entrypoint) == 0 && len(config.Cmd) == 0 {
|
|
|
|
return fmt.Errorf("No command specified")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-05-27 09:32:26 +00:00
|
|
|
|
|
|
|
// Checks if the client set configurations for more than one network while creating a container
|
2016-08-23 04:52:56 +00:00
|
|
|
// Also checks if the IPAMConfig is valid
|
2016-05-27 09:32:26 +00:00
|
|
|
func (daemon *Daemon) verifyNetworkingConfig(nwConfig *networktypes.NetworkingConfig) error {
|
2016-08-23 04:52:56 +00:00
|
|
|
if nwConfig == nil || len(nwConfig.EndpointsConfig) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(nwConfig.EndpointsConfig) == 1 {
|
|
|
|
for _, v := range nwConfig.EndpointsConfig {
|
2016-08-23 23:50:15 +00:00
|
|
|
if v != nil && v.IPAMConfig != nil {
|
2016-08-23 04:52:56 +00:00
|
|
|
if v.IPAMConfig.IPv4Address != "" && net.ParseIP(v.IPAMConfig.IPv4Address).To4() == nil {
|
2016-06-07 07:45:21 +00:00
|
|
|
return apierrors.NewBadRequestError(fmt.Errorf("invalid IPv4 address: %s", v.IPAMConfig.IPv4Address))
|
2016-08-23 04:52:56 +00:00
|
|
|
}
|
2016-08-31 02:07:21 +00:00
|
|
|
if v.IPAMConfig.IPv6Address != "" {
|
|
|
|
n := net.ParseIP(v.IPAMConfig.IPv6Address)
|
|
|
|
// if the address is an invalid network address (ParseIP == nil) or if it is
|
|
|
|
// an IPv4 address (To4() != nil), then it is an invalid IPv6 address
|
|
|
|
if n == nil || n.To4() != nil {
|
2016-06-07 07:45:21 +00:00
|
|
|
return apierrors.NewBadRequestError(fmt.Errorf("invalid IPv6 address: %s", v.IPAMConfig.IPv6Address))
|
2016-08-31 02:07:21 +00:00
|
|
|
}
|
2016-08-23 04:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 09:32:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
l := make([]string, 0, len(nwConfig.EndpointsConfig))
|
|
|
|
for k := range nwConfig.EndpointsConfig {
|
|
|
|
l = append(l, k)
|
|
|
|
}
|
|
|
|
err := fmt.Errorf("Container cannot be connected to network endpoints: %s", strings.Join(l, ", "))
|
2016-06-07 07:45:21 +00:00
|
|
|
return apierrors.NewBadRequestError(err)
|
2016-05-27 09:32:26 +00:00
|
|
|
}
|