-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix name resolution in Systemd systems #220
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,12 +42,12 @@ var fileNotFound = errors.New("file not found") | |
type dockerContainerHandler struct { | ||
client *docker.Client | ||
name string | ||
parent string | ||
id string | ||
aliases []string | ||
machineInfoFactory info.MachineInfoFactory | ||
useSystemd bool | ||
libcontainerStateDir string | ||
cgroup cgroups.Cgroup | ||
} | ||
|
||
func newDockerContainerHandler( | ||
|
@@ -63,12 +63,15 @@ func newDockerContainerHandler( | |
machineInfoFactory: machineInfoFactory, | ||
useSystemd: useSystemd, | ||
libcontainerStateDir: path.Join(dockerRootDir, pathToLibcontainerState), | ||
cgroup: cgroups.Cgroup{ | ||
Parent: "/", | ||
Name: name, | ||
}, | ||
} | ||
if handler.isDockerRoot() { | ||
return handler, nil | ||
} | ||
id := path.Base(name) | ||
handler.parent = path.Dir(name) | ||
id := containerNameToDockerId(name, useSystemd) | ||
handler.id = id | ||
ctnr, err := client.InspectContainer(id) | ||
// We assume that if Inspect fails then the container is not known to docker. | ||
|
@@ -79,6 +82,25 @@ func newDockerContainerHandler( | |
return handler, nil | ||
} | ||
|
||
func containerNameToDockerId(name string, useSystemd bool) string { | ||
id := path.Base(name) | ||
|
||
// Turn systemd cgroup name into Docker ID. | ||
if useSystemd { | ||
const systemdDockerPrefix = "docker-" | ||
if strings.HasPrefix(id, systemdDockerPrefix) { | ||
id = id[len(systemdDockerPrefix):] | ||
} | ||
|
||
const systemdScopeSuffix = ".scope" | ||
if strings.HasSuffix(id, systemdScopeSuffix) { | ||
id = id[:len(id)-len(systemdScopeSuffix)] | ||
} | ||
} | ||
|
||
return id | ||
} | ||
|
||
func (self *dockerContainerHandler) ContainerReference() (info.ContainerReference, error) { | ||
return info.ContainerReference{ | ||
Name: self.name, | ||
|
@@ -112,8 +134,7 @@ func (self *dockerContainerHandler) readLibcontainerConfig() (config *libcontain | |
config = retConfig | ||
|
||
// Replace cgroup parent and name with our own since we may be running in a different context. | ||
config.Cgroups.Parent = self.parent | ||
config.Cgroups.Name = self.id | ||
*config.Cgroups = self.cgroup | ||
|
||
return | ||
} | ||
|
@@ -199,14 +220,6 @@ func (self *dockerContainerHandler) GetStats() (stats *info.ContainerStats, err | |
if self.isDockerRoot() { | ||
return &info.ContainerStats{}, nil | ||
} | ||
config, err := self.readLibcontainerConfig() | ||
if err != nil { | ||
if err == fileNotFound { | ||
glog.Errorf("Libcontainer config not found for container %q", self.name) | ||
return &info.ContainerStats{}, nil | ||
} | ||
return | ||
} | ||
state, err := self.readLibcontainerState() | ||
if err != nil { | ||
if err == fileNotFound { | ||
|
@@ -216,7 +229,7 @@ func (self *dockerContainerHandler) GetStats() (stats *info.ContainerStats, err | |
return | ||
} | ||
|
||
return containerLibcontainer.GetStats(config, state) | ||
return containerLibcontainer.GetStats(&self.cgroup, state) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this mean we will loose network stats? If so, why not make this change only on systemd systems? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we still have the state so GetStats fetches network stats. I checked just in case :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh ok :) |
||
} | ||
|
||
func (self *dockerContainerHandler) ListContainers(listType container.ListType) ([]info.ContainerReference, error) { | ||
|
@@ -258,11 +271,7 @@ func (self *dockerContainerHandler) ListThreads(listType container.ListType) ([] | |
} | ||
|
||
func (self *dockerContainerHandler) ListProcesses(listType container.ListType) ([]int, error) { | ||
c := &cgroups.Cgroup{ | ||
Parent: self.parent, | ||
Name: self.id, | ||
} | ||
return fs.GetPids(c) | ||
return fs.GetPids(&self.cgroup) | ||
} | ||
|
||
func (self *dockerContainerHandler) WatchSubcontainers(events chan container.SubcontainerEvent) error { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we instead export "docker-" prefix from libcontainer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libcontainer as in
container/libcontainer/helpers.go
?I think the right place for this is here since it is a Docker concept and not a libcontainer one (as in, Docker prefixes it's containers with that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant docker/libcontainer. If libcontainer is not the source of the prefix, then the current code is fine as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
libcontainer does some sad mangling of container names I'm trying to remove in a PR :( once that goes in, Docker will be the one doing the prefixing.