2014-02-11 23:04:39 -05:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2015-04-10 20:05:21 -04:00
|
|
|
"encoding/json"
|
2015-09-09 22:23:06 -04:00
|
|
|
"fmt"
|
2015-04-10 20:05:21 -04:00
|
|
|
"io"
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types/container"
|
2016-01-07 19:18:34 -05:00
|
|
|
networktypes "github.com/docker/engine-api/types/network"
|
2014-02-11 23:04:39 -05:00
|
|
|
)
|
|
|
|
|
2016-03-28 14:22:23 -04:00
|
|
|
// ContainerDecoder implements httputils.ContainerDecoder
|
|
|
|
// calling DecodeContainerConfig.
|
|
|
|
type ContainerDecoder struct{}
|
|
|
|
|
|
|
|
// DecodeConfig makes ContainerDecoder to implement httputils.ContainerDecoder
|
|
|
|
func (r ContainerDecoder) DecodeConfig(src io.Reader) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
|
|
|
|
return DecodeContainerConfig(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeHostConfig makes ContainerDecoder to implement httputils.ContainerDecoder
|
|
|
|
func (r ContainerDecoder) DecodeHostConfig(src io.Reader) (*container.HostConfig, error) {
|
|
|
|
return DecodeHostConfig(src)
|
|
|
|
}
|
|
|
|
|
2015-06-06 12:41:42 -04:00
|
|
|
// DecodeContainerConfig decodes a json encoded config into a ContainerConfigWrapper
|
2016-04-05 08:53:04 -04:00
|
|
|
// struct and returns both a Config and a HostConfig struct
|
2015-06-06 12:41:42 -04:00
|
|
|
// Be aware this function is not checking whether the resulted structs are nil,
|
|
|
|
// it's your business to do so
|
2016-01-07 19:18:34 -05:00
|
|
|
func DecodeContainerConfig(src io.Reader) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
|
2015-04-10 20:05:21 -04:00
|
|
|
var w ContainerConfigWrapper
|
2015-09-09 22:23:06 -04:00
|
|
|
|
|
|
|
decoder := json.NewDecoder(src)
|
2015-04-10 20:05:21 -04:00
|
|
|
if err := decoder.Decode(&w); err != nil {
|
2016-01-07 19:18:34 -05:00
|
|
|
return nil, nil, nil, err
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
2015-04-10 20:05:21 -04:00
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
hc := w.getHostConfig()
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
// Perform platform-specific processing of Volumes and Binds.
|
|
|
|
if w.Config != nil && hc != nil {
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// Initialize the volumes map if currently nil
|
2015-09-09 22:23:06 -04:00
|
|
|
if w.Config.Volumes == nil {
|
|
|
|
w.Config.Volumes = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now validate all the volumes and binds
|
|
|
|
if err := validateVolumesAndBindSettings(w.Config, hc); err != nil {
|
2016-01-07 19:18:34 -05:00
|
|
|
return nil, nil, nil, err
|
2015-09-09 22:23:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
// Certain parameters need daemon-side validation that cannot be done
|
|
|
|
// on the client, as only the daemon knows what is valid for the platform.
|
|
|
|
if err := ValidateNetMode(w.Config, hc); err != nil {
|
2016-01-07 19:18:34 -05:00
|
|
|
return nil, nil, nil, err
|
2015-07-09 18:12:36 -04:00
|
|
|
}
|
|
|
|
|
2016-02-03 15:07:00 -05:00
|
|
|
// Validate isolation
|
|
|
|
if err := ValidateIsolation(hc); err != nil {
|
2016-01-07 19:18:34 -05:00
|
|
|
return nil, nil, nil, err
|
2015-09-18 21:21:57 -04:00
|
|
|
}
|
2016-01-07 19:18:34 -05:00
|
|
|
return w.Config, hc, w.NetworkingConfig, nil
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
2015-09-09 22:23:06 -04:00
|
|
|
|
|
|
|
// validateVolumesAndBindSettings validates each of the volumes and bind settings
|
|
|
|
// passed by the caller to ensure they are valid.
|
2015-12-18 13:36:17 -05:00
|
|
|
func validateVolumesAndBindSettings(c *container.Config, hc *container.HostConfig) error {
|
2015-09-09 22:23:06 -04:00
|
|
|
|
|
|
|
// Ensure all volumes and binds are valid.
|
|
|
|
for spec := range c.Volumes {
|
|
|
|
if _, err := volume.ParseMountSpec(spec, hc.VolumeDriver); err != nil {
|
|
|
|
return fmt.Errorf("Invalid volume spec %q: %v", spec, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, spec := range hc.Binds {
|
|
|
|
if _, err := volume.ParseMountSpec(spec, hc.VolumeDriver); err != nil {
|
|
|
|
return fmt.Errorf("Invalid bind mount spec %q: %v", spec, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|