6393c383 |
package runconfig
import ( |
767df67e |
"encoding/json" |
a7e686a7 |
"fmt" |
767df67e |
"io"
|
91e197d6 |
"github.com/docker/docker/api/types/container"
networktypes "github.com/docker/docker/api/types/network" |
56f77d5a |
"github.com/docker/docker/pkg/sysinfo" |
a7e686a7 |
"github.com/docker/docker/volume" |
6393c383 |
)
|
f0d26e16 |
// 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)
}
|
4ce81779 |
// DecodeContainerConfig decodes a json encoded config into a ContainerConfigWrapper |
34700cc1 |
// struct and returns both a Config and a HostConfig struct |
4ce81779 |
// Be aware this function is not checking whether the resulted structs are nil,
// it's your business to do so |
2bb3fc1b |
func DecodeContainerConfig(src io.Reader) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) { |
767df67e |
var w ContainerConfigWrapper |
a7e686a7 |
decoder := json.NewDecoder(src) |
767df67e |
if err := decoder.Decode(&w); err != nil { |
2bb3fc1b |
return nil, nil, nil, err |
6393c383 |
} |
767df67e |
|
f6ed5905 |
hc := w.getHostConfig()
|
a7e686a7 |
// Perform platform-specific processing of Volumes and Binds.
if w.Config != nil && hc != nil {
|
927b334e |
// Initialize the volumes map if currently nil |
a7e686a7 |
if w.Config.Volumes == nil {
w.Config.Volumes = make(map[string]struct{})
}
// Now validate all the volumes and binds |
18768fdc |
if err := validateMountSettings(w.Config, hc); err != nil { |
2bb3fc1b |
return nil, nil, nil, err |
a7e686a7 |
}
}
|
f6ed5905 |
// Certain parameters need daemon-side validation that cannot be done
// on the client, as only the daemon knows what is valid for the platform. |
4af3389d |
if err := validateNetMode(w.Config, hc); err != nil { |
2bb3fc1b |
return nil, nil, nil, err |
f6ed5905 |
}
|
d4b07324 |
// Validate isolation |
4af3389d |
if err := validateIsolation(hc); err != nil { |
2bb3fc1b |
return nil, nil, nil, err |
15e35c44 |
} |
8df20663 |
// Validate QoS |
4af3389d |
if err := validateQoS(hc); err != nil { |
8df20663 |
return nil, nil, nil, err
} |
e85867cb |
|
56f77d5a |
// Validate Resources |
4af3389d |
if err := validateResources(hc, sysinfo.New(true)); err != nil { |
56f77d5a |
return nil, nil, nil, err
} |
4af3389d |
// Validate Privileged
if err := validatePrivileged(hc); err != nil {
return nil, nil, nil, err
}
|
2bb3fc1b |
return w.Config, hc, w.NetworkingConfig, nil |
6393c383 |
} |
a7e686a7 |
|
18768fdc |
// validateMountSettings validates each of the volumes and bind settings |
a7e686a7 |
// passed by the caller to ensure they are valid. |
18768fdc |
func validateMountSettings(c *container.Config, hc *container.HostConfig) error {
// it is ok to have len(hc.Mounts) > 0 && (len(hc.Binds) > 0 || len (c.Volumes) > 0 || len (hc.Tmpfs) > 0 ) |
a7e686a7 |
// Ensure all volumes and binds are valid.
for spec := range c.Volumes { |
fc7b904d |
if _, err := volume.ParseMountRaw(spec, hc.VolumeDriver); err != nil {
return fmt.Errorf("invalid volume spec %q: %v", spec, err) |
a7e686a7 |
}
}
for _, spec := range hc.Binds { |
fc7b904d |
if _, err := volume.ParseMountRaw(spec, hc.VolumeDriver); err != nil {
return fmt.Errorf("invalid bind mount spec %q: %v", spec, err) |
a7e686a7 |
}
}
return nil
} |