Skip to content

Commit

Permalink
Fix deprecated config for engine < 1.7, image pull waits for end of
Browse files Browse the repository at this point in the history
response

Signed-off-by: Nishant Totla <[email protected]>
  • Loading branch information
nishanttotla committed Apr 14, 2016
1 parent e29ff44 commit 8d99298
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
15 changes: 14 additions & 1 deletion api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,23 @@ func postContainersCreate(c *context, w http.ResponseWriter, r *http.Request) {
}
)

if err := json.NewDecoder(r.Body).Decode(&config); err != nil {
oldconfig := cluster.OldContainerConfig{
ContainerConfig: config,
Memory: 0,
MemorySwap: 0,
CPUShares: 0,
CPUSet: "",
}

if err := json.NewDecoder(r.Body).Decode(&oldconfig); err != nil {
httpError(w, err.Error(), http.StatusBadRequest)
return
}

// make sure HostConfig fields are consolidated before creating container
cluster.ConsolidateResourceFields(&oldconfig)
config = oldconfig.ContainerConfig

// Pass auth information along if present
var authConfig *apitypes.AuthConfig
buf, err := base64.URLEncoding.DecodeString(r.Header.Get("X-Registry-Auth"))
Expand Down
37 changes: 37 additions & 0 deletions cluster/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ type ContainerConfig struct {
NetworkingConfig network.NetworkingConfig
}

// OldContainerConfig contains additional fields for backward compatibility
// This should be removed after we stop supporting API versions <= 1.8
type OldContainerConfig struct {
ContainerConfig
Memory int64
MemorySwap int64
CPUShares int64 `json:"CpuShares"`
CPUSet string `json:"Cpuset"`
}

func parseEnv(e string) (bool, string, string) {
parts := strings.SplitN(e, ":", 2)
if len(parts) == 2 {
Expand All @@ -29,6 +39,33 @@ func parseEnv(e string) (bool, string, string) {
return false, "", ""
}

// ConsolidateResourceFields is a temporary fix to handle forward/backward compatibility between Docker <1.6 and >=1.7
func ConsolidateResourceFields(c *OldContainerConfig) {
if c.Memory != c.HostConfig.Memory {
if c.Memory != 0 {
c.HostConfig.Memory = c.Memory
}
}

if c.MemorySwap != c.HostConfig.MemorySwap {
if c.MemorySwap != 0 {
c.HostConfig.MemorySwap = c.MemorySwap
}
}

if c.CPUShares != c.HostConfig.CPUShares {
if c.CPUShares != 0 {
c.HostConfig.CPUShares = c.CPUShares
}
}

if c.CPUSet != c.HostConfig.CpusetCpus {
if c.CPUSet != "" {
c.HostConfig.CpusetCpus = c.CPUSet
}
}
}

// BuildContainerConfig creates a cluster.ContainerConfig from a Config, HostConfig, and NetworkingConfig
func BuildContainerConfig(c container.Config, h container.HostConfig, n network.NetworkingConfig) *ContainerConfig {
var (
Expand Down
16 changes: 6 additions & 10 deletions cluster/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,22 +989,18 @@ func (e *Engine) Pull(image string, authConfig *types.AuthConfig) error {

// wait until the image download is finished
dec := json.NewDecoder(pullResponse)
m := map[string]interface{}{}
for {
m := map[string]interface{}{}
err := dec.Decode(&m)
if err != nil {
if err := dec.Decode(&m); err != nil {
if err == io.EOF {
break
}
return err
}
// if the stream contains an error, return it
if val, ok := m["error"]; ok {
if errmsg, strok := val.(string); strok {
return errors.New(errmsg)
}
return errors.New("Error while downloading image")
}
}
// if the final stream object contained an error, return it
if errMsg, ok := m["error"]; ok {
return fmt.Errorf("%v", errMsg)
}

// force refresh images
Expand Down

0 comments on commit 8d99298

Please sign in to comment.