Skip to content

Commit

Permalink
Handle old versions of the libcontainer config.
Browse files Browse the repository at this point in the history
Libcontainer recently changed its config by changing the type of a
field. This commit tries parsing the config as the new and old types.
  • Loading branch information
vmarmol committed Dec 17, 2014
1 parent 5ad9cb5 commit 4a504ff
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions container/docker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package docker
import (
"encoding/json"
"fmt"
"io/ioutil"
"math"
"os"
"path"
Expand Down Expand Up @@ -129,25 +130,41 @@ func (self *dockerContainerHandler) ContainerReference() (info.ContainerReferenc
}

// TODO(vmarmol): Switch to getting this from libcontainer once we have a solid API.
func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontainer.Config, err error) {
f, err := os.Open(self.libcontainerConfigPath)
func (self *dockerContainerHandler) readLibcontainerConfig() (*libcontainer.Config, error) {
out, err := ioutil.ReadFile(self.libcontainerConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to open %s - %s\n", self.libcontainerConfigPath, err)
return nil, fmt.Errorf("failed to read libcontainer config from %q: %v", self.libcontainerConfigPath, err)
}
defer f.Close()
d := json.NewDecoder(f)
retConfig := new(libcontainer.Config)
err = d.Decode(retConfig)
var config libcontainer.Config
err = json.Unmarshal(out, &config)
if err != nil {
return
// Try to parse the old config. The main difference is that namespaces used to be a map, now it is a slice of structs.
// The JSON marshaler will use the non-nested field before the nested one.
type oldLibcontainerConfig struct {
libcontainer.Config
OldNamespaces map[string]bool `json:"namespaces,omitempty"`
}
var oldConfig oldLibcontainerConfig
err2 := json.Unmarshal(out, &oldConfig)
if err2 != nil {
// Use original error.
return nil, fmt.Errorf("failed to parse libcontainer config at %q: %v", self.libcontainerConfigPath, err)
}

// Translate the old config into the new config.
config = oldConfig.Config
for ns, _ := range oldConfig.OldNamespaces {
config.Namespaces = append(config.Namespaces, libcontainer.Namespace{
Name: ns,
})
}
}
config = retConfig

// Replace cgroup parent and name with our own since we may be running in a different context.
config.Cgroups.Name = self.cgroup.Name
config.Cgroups.Parent = self.cgroup.Parent

return
return &config, nil
}

func (self *dockerContainerHandler) readLibcontainerState() (state *libcontainer.State, err error) {
Expand All @@ -168,7 +185,7 @@ func (self *dockerContainerHandler) readLibcontainerState() (state *libcontainer
retState := new(libcontainer.State)
err = d.Decode(retState)
if err != nil {
return
return nil, fmt.Errorf("failed to parse libcontainer state at %q: %v", self.libcontainerStatePath, err)
}
state = retState

Expand Down

0 comments on commit 4a504ff

Please sign in to comment.