2018-02-05 16:05:59 -05:00
|
|
|
package runconfig // import "github.com/docker/docker/runconfig"
|
2014-02-11 23:04:39 -05:00
|
|
|
|
|
|
|
import (
|
2015-04-10 20:05:21 -04:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
2016-06-07 15:05:43 -04:00
|
|
|
"github.com/docker/docker/pkg/sysinfo"
|
2014-02-11 23:04:39 -05:00
|
|
|
)
|
|
|
|
|
2016-03-28 14:22:23 -04:00
|
|
|
// ContainerDecoder implements httputils.ContainerDecoder
|
|
|
|
// calling DecodeContainerConfig.
|
2020-03-10 08:09:25 -04:00
|
|
|
type ContainerDecoder struct {
|
|
|
|
GetSysInfo func() *sysinfo.SysInfo
|
|
|
|
}
|
2016-03-28 14:22:23 -04:00
|
|
|
|
|
|
|
// DecodeConfig makes ContainerDecoder to implement httputils.ContainerDecoder
|
|
|
|
func (r ContainerDecoder) DecodeConfig(src io.Reader) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
|
2020-03-10 08:09:25 -04:00
|
|
|
var si *sysinfo.SysInfo
|
|
|
|
if r.GetSysInfo != nil {
|
|
|
|
si = r.GetSysInfo()
|
|
|
|
} else {
|
2021-07-14 10:45:02 -04:00
|
|
|
si = sysinfo.New()
|
2020-03-10 08:09:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return decodeContainerConfig(src, si)
|
2016-03-28 14:22:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeHostConfig makes ContainerDecoder to implement httputils.ContainerDecoder
|
|
|
|
func (r ContainerDecoder) DecodeHostConfig(src io.Reader) (*container.HostConfig, error) {
|
2017-08-29 14:32:59 -04:00
|
|
|
return decodeHostConfig(src)
|
2016-03-28 14:22:23 -04:00
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
// decodeContainerConfig decodes a json encoded config into a ContainerConfigWrapper
|
2021-08-09 05:11:54 -04:00
|
|
|
// struct and returns both a Config and a HostConfig struct, and performs some
|
|
|
|
// validation. Certain parameters need daemon-side validation that cannot be done
|
|
|
|
// on the client, as only the daemon knows what is valid for the platform.
|
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
|
2020-03-10 08:09:25 -04:00
|
|
|
func decodeContainerConfig(src io.Reader, si *sysinfo.SysInfo) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig, error) {
|
2015-04-10 20:05:21 -04:00
|
|
|
var w ContainerConfigWrapper
|
2022-04-08 17:27:50 -04:00
|
|
|
if err := loadJSON(src, &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()
|
2021-08-09 05:11:54 -04:00
|
|
|
if hc == nil {
|
|
|
|
// We may not be passed a host config, such as in the case of docker commit
|
|
|
|
return w.Config, hc, w.NetworkingConfig, nil
|
2015-09-09 22:23:06 -04:00
|
|
|
}
|
2017-03-10 12:39:22 -05:00
|
|
|
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
|
|
|
}
|
2017-03-10 12:39:22 -05:00
|
|
|
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
|
|
|
}
|
2017-03-10 12:39:22 -05:00
|
|
|
if err := validateQoS(hc); err != nil {
|
2016-02-24 20:51:46 -05:00
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2020-03-10 08:09:25 -04:00
|
|
|
if err := validateResources(hc, si); err != nil {
|
2016-06-07 15:05:43 -04:00
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2017-03-10 12:39:22 -05:00
|
|
|
if err := validatePrivileged(hc); err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2017-05-08 12:29:37 -04:00
|
|
|
if err := validateReadonlyRootfs(hc); err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2021-08-09 05:11:54 -04:00
|
|
|
if w.Config != nil && w.Config.Volumes == nil {
|
|
|
|
w.Config.Volumes = make(map[string]struct{})
|
|
|
|
}
|
2016-01-07 19:18:34 -05:00
|
|
|
return w.Config, hc, w.NetworkingConfig, nil
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
2022-04-08 17:27:50 -04:00
|
|
|
|
|
|
|
// loadJSON is similar to api/server/httputils.ReadJSON()
|
|
|
|
func loadJSON(src io.Reader, out interface{}) error {
|
|
|
|
dec := json.NewDecoder(src)
|
|
|
|
if err := dec.Decode(&out); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return validationError("invalid JSON: got EOF while reading request body")
|
|
|
|
}
|
|
|
|
return validationError("invalid JSON: " + err.Error())
|
|
|
|
}
|
|
|
|
if dec.More() {
|
|
|
|
return validationError("unexpected content after JSON")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|