[lxc-devel] [lxd/master] Don't allow the state functions to fail

stgraber on Github lxc-bot at linuxcontainers.org
Thu Feb 25 17:18:02 UTC 2016


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 501 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20160225/42e9cb84/attachment.bin>
-------------- next part --------------
From 8660a0d7136ddca307e54eff49cdde99512407a8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 25 Feb 2016 11:33:31 -0500
Subject: [PATCH] Don't allow the state functions to fail
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If we get garbage back from the cgroup subsystem we should just render
the values as empty or -1 rather than fail the entire REST call for it.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/container_lxc.go | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 6a8d42f..7041e20 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -1431,20 +1431,10 @@ func (c *containerLXC) RenderState() (*shared.ContainerState, error) {
 	}
 
 	if c.IsRunning() {
-		memory, err := c.memoryState()
-		if err != nil {
-			return nil, err
-		}
-
-		network, err := c.networkState()
-		if err != nil {
-			return nil, err
-		}
-
 		pid := c.InitPID()
 		status.Disk = c.diskState()
-		status.Memory = *memory
-		status.Network = network
+		status.Memory = c.memoryState()
+		status.Network = c.networkState()
 		status.Pid = int64(pid)
 		status.Processes = c.processesState()
 	}
@@ -2718,18 +2708,18 @@ func (c *containerLXC) diskState() map[string]shared.ContainerStateDisk {
 	return disk
 }
 
-func (c *containerLXC) memoryState() (*shared.ContainerStateMemory, error) {
+func (c *containerLXC) memoryState() shared.ContainerStateMemory {
 	memory := shared.ContainerStateMemory{}
 
 	if !cgMemoryController {
-		return &memory, nil
+		return memory
 	}
 
 	// Memory in bytes
 	value, err := c.CGroupGet("memory.usage_in_bytes")
 	valueInt, err := strconv.ParseInt(value, 10, 64)
 	if err != nil {
-		return nil, err
+		valueInt = -1
 	}
 	memory.Usage = valueInt
 
@@ -2737,7 +2727,7 @@ func (c *containerLXC) memoryState() (*shared.ContainerStateMemory, error) {
 	value, err = c.CGroupGet("memory.max_usage_in_bytes")
 	valueInt, err = strconv.ParseInt(value, 10, 64)
 	if err != nil {
-		return nil, err
+		valueInt = -1
 	}
 
 	memory.UsagePeak = valueInt
@@ -2747,7 +2737,7 @@ func (c *containerLXC) memoryState() (*shared.ContainerStateMemory, error) {
 		value, err := c.CGroupGet("memory.memsw.usage_in_bytes")
 		valueInt, err := strconv.ParseInt(value, 10, 64)
 		if err != nil {
-			return nil, err
+			valueInt = -1
 		}
 
 		memory.SwapUsage = valueInt - memory.Usage
@@ -2756,19 +2746,21 @@ func (c *containerLXC) memoryState() (*shared.ContainerStateMemory, error) {
 		value, err = c.CGroupGet("memory.memsw.max_usage_in_bytes")
 		valueInt, err = strconv.ParseInt(value, 10, 64)
 		if err != nil {
-			return nil, err
+			valueInt = -1
 		}
 
 		memory.SwapUsagePeak = valueInt - memory.UsagePeak
 	}
 
-	return &memory, nil
+	return memory
 }
 
-func (c *containerLXC) networkState() (map[string]shared.ContainerStateNetwork, error) {
+func (c *containerLXC) networkState() map[string]shared.ContainerStateNetwork {
+	result := map[string]shared.ContainerStateNetwork{}
+
 	pid := c.InitPID()
 	if pid < 1 {
-		return nil, fmt.Errorf("Container isn't running")
+		return result
 	}
 
 	// Get the network state from the container
@@ -2779,24 +2771,25 @@ func (c *containerLXC) networkState() (map[string]shared.ContainerStateNetwork,
 
 	// Process forkgetnet response
 	if err != nil {
-		return nil, fmt.Errorf("Error calling 'lxd forkgetnet %d': %s", pid, string(out))
+		shared.Log.Error("Error calling 'lxd forkgetnet", log.Ctx{"container": c.name, "output": string(out), "pid": pid})
+		return result
 	}
 
 	networks := map[string]shared.ContainerStateNetwork{}
 
 	err = json.Unmarshal(out, &networks)
 	if err != nil {
-		return nil, err
+		shared.Log.Error("Failure to read forkgetnet json", log.Ctx{"container": c.name, "err": err})
+		return result
 	}
 
 	// Add HostName field
-	result := map[string]shared.ContainerStateNetwork{}
 	for netName, net := range networks {
 		net.HostName = c.getHostInterface(netName)
 		result[netName] = net
 	}
 
-	return result, nil
+	return result
 }
 
 func (c *containerLXC) processesState() int64 {


More information about the lxc-devel mailing list