github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/runconfig/config_unix.go (about)

     1  // +build !windows
     2  
     3  package runconfig
     4  
     5  import (
     6  	"github.com/docker/docker/api/types/container"
     7  	networktypes "github.com/docker/docker/api/types/network"
     8  )
     9  
    10  // ContainerConfigWrapper is a Config wrapper that holds the container Config (portable)
    11  // and the corresponding HostConfig (non-portable).
    12  type ContainerConfigWrapper struct {
    13  	*container.Config
    14  	InnerHostConfig       *container.HostConfig          `json:"HostConfig,omitempty"`
    15  	Cpuset                string                         `json:",omitempty"` // Deprecated. Exported for backwards compatibility.
    16  	NetworkingConfig      *networktypes.NetworkingConfig `json:"NetworkingConfig,omitempty"`
    17  	*container.HostConfig                                // Deprecated. Exported to read attributes from json that are not in the inner host config structure.
    18  }
    19  
    20  // getHostConfig gets the HostConfig of the Config.
    21  // It's mostly there to handle Deprecated fields of the ContainerConfigWrapper
    22  func (w *ContainerConfigWrapper) getHostConfig() *container.HostConfig {
    23  	hc := w.HostConfig
    24  
    25  	if hc == nil && w.InnerHostConfig != nil {
    26  		hc = w.InnerHostConfig
    27  	} else if w.InnerHostConfig != nil {
    28  		if hc.Memory != 0 && w.InnerHostConfig.Memory == 0 {
    29  			w.InnerHostConfig.Memory = hc.Memory
    30  		}
    31  		if hc.MemorySwap != 0 && w.InnerHostConfig.MemorySwap == 0 {
    32  			w.InnerHostConfig.MemorySwap = hc.MemorySwap
    33  		}
    34  		if hc.CPUShares != 0 && w.InnerHostConfig.CPUShares == 0 {
    35  			w.InnerHostConfig.CPUShares = hc.CPUShares
    36  		}
    37  		if hc.CpusetCpus != "" && w.InnerHostConfig.CpusetCpus == "" {
    38  			w.InnerHostConfig.CpusetCpus = hc.CpusetCpus
    39  		}
    40  
    41  		if hc.VolumeDriver != "" && w.InnerHostConfig.VolumeDriver == "" {
    42  			w.InnerHostConfig.VolumeDriver = hc.VolumeDriver
    43  		}
    44  
    45  		hc = w.InnerHostConfig
    46  	}
    47  
    48  	if hc != nil {
    49  		if w.Cpuset != "" && hc.CpusetCpus == "" {
    50  			hc.CpusetCpus = w.Cpuset
    51  		}
    52  	}
    53  
    54  	// Make sure NetworkMode has an acceptable value. We do this to ensure
    55  	// backwards compatible API behavior.
    56  	hc = SetDefaultNetModeIfBlank(hc)
    57  
    58  	return hc
    59  }