From 4e3629101d3dce5fd1cb629e011ef18a292f6a67 Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Mon, 5 Jan 2015 13:47:35 -0800 Subject: [PATCH] Cleanup of libcontainer state generation. --- container/raw/handler.go | 59 ++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/container/raw/handler.go b/container/raw/handler.go index 95ea788d..83bb314d 100644 --- a/container/raw/handler.go +++ b/container/raw/handler.go @@ -59,12 +59,23 @@ type rawContainerHandler struct { // (e.g.: "cpu" -> "/sys/fs/cgroup/cpu/test") cgroupPaths map[string]string - fsInfo fs.FsInfo - networkInterface *networkInterface - externalMounts []mount + // Equivalent libcontainer state for this container. + libcontainerState dockerlibcontainer.State + + // Whether this container has network isolation enabled. + hasNetwork bool + + fsInfo fs.FsInfo + externalMounts []mount } func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSubsystems, machineInfoFactory info.MachineInfoFactory) (container.ContainerHandler, error) { + // Create the cgroup paths. + cgroupPaths := make(map[string]string, len(cgroupSubsystems.MountPoints)) + for key, val := range cgroupSubsystems.MountPoints { + cgroupPaths[key] = path.Join(val, name) + } + fsInfo, err := fs.NewFsInfo() if err != nil { return nil, err @@ -73,22 +84,26 @@ func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSu if err != nil { return nil, err } - var networkInterface *networkInterface + + // Generate the equivalent libcontainer state for this container. + libcontainerState := dockerlibcontainer.State{ + CgroupPaths: cgroupPaths, + } + + hasNetwork := false var externalMounts []mount for _, container := range cHints.AllHosts { if name == container.FullName { - networkInterface = container.NetworkInterface + libcontainerState.NetworkState = network.NetworkState{ + VethHost: container.NetworkInterface.VethHost, + VethChild: container.NetworkInterface.VethChild, + } + hasNetwork = true externalMounts = container.Mounts break } } - // Create the cgroup paths. - cgroupPaths := make(map[string]string, len(cgroupSubsystems.MountPoints)) - for key, val := range cgroupSubsystems.MountPoints { - cgroupPaths[key] = path.Join(val, name) - } - return &rawContainerHandler{ name: name, cgroup: &cgroups.Cgroup{ @@ -101,8 +116,9 @@ func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSu watches: make(map[string]struct{}), cgroupWatches: make(map[string]struct{}), cgroupPaths: cgroupPaths, + libcontainerState: libcontainerState, fsInfo: fsInfo, - networkInterface: networkInterface, + hasNetwork: hasNetwork, externalMounts: externalMounts, }, nil } @@ -207,9 +223,7 @@ func (self *rawContainerHandler) GetSpec() (info.ContainerSpec, error) { } //Network - if self.networkInterface != nil { - spec.HasNetwork = true - } + spec.HasNetwork = self.hasNetwork // Check physical network devices for root container. nd, err := self.GetRootNetworkDevices() @@ -282,20 +296,7 @@ func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error { } func (self *rawContainerHandler) GetStats() (*info.ContainerStats, error) { - // TODO(vmarmol): Don't re-create this every time. - state := dockerlibcontainer.State{ - CgroupPaths: self.cgroupPaths, - } - if self.networkInterface != nil { - state = dockerlibcontainer.State{ - NetworkState: network.NetworkState{ - VethHost: self.networkInterface.VethHost, - VethChild: self.networkInterface.VethChild, - }, - } - } - - stats, err := libcontainer.GetStats(self.cgroupPaths, &state) + stats, err := libcontainer.GetStats(self.cgroupPaths, &self.libcontainerState) if err != nil { return nil, err }