[lxc-devel] [lxd/master] Add db.ContainersNodeList

freeekanayaka on Github lxc-bot at linuxcontainers.org
Thu Apr 12 10:51:41 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 361 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180412/79e63ac7/attachment.bin>
-------------- next part --------------
From 129eb84c6a678ab5aea81b84fa59373e9afc701b Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Thu, 12 Apr 2018 10:50:10 +0000
Subject: [PATCH] Add db.ContainersNodeList

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/db/containers.go      | 20 ++++++++++++++++++++
 lxd/db/containers_test.go | 24 ++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index 867f5971c9..3b93a0d51a 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -663,6 +663,26 @@ func (c *Cluster) ContainersList(cType ContainerType) ([]string, error) {
 	return ret, nil
 }
 
+// ContainersNodeList returns the names of all the containers of the given type
+// running on the local node.
+func (c *Cluster) ContainersNodeList(cType ContainerType) ([]string, error) {
+	q := fmt.Sprintf("SELECT name FROM containers WHERE type=? AND node_id=? ORDER BY name")
+	inargs := []interface{}{cType, c.nodeID}
+	var container string
+	outfmt := []interface{}{container}
+	result, err := queryScan(c.db, q, inargs, outfmt)
+	if err != nil {
+		return nil, err
+	}
+
+	var ret []string
+	for _, container := range result {
+		ret = append(ret, container[0].(string))
+	}
+
+	return ret, nil
+}
+
 // ContainersResetState resets the power state of all containers.
 func (c *Cluster) ContainersResetState() error {
 	// Reset all container states
diff --git a/lxd/db/containers_test.go b/lxd/db/containers_test.go
index 291a58992e..f82d079c82 100644
--- a/lxd/db/containers_test.go
+++ b/lxd/db/containers_test.go
@@ -89,6 +89,30 @@ func TestContainerPool(t *testing.T) {
 	assert.Equal(t, "default", poolName)
 }
 
+// Only containers running on the local node are returned.
+func TestContainersNodeList(t *testing.T) {
+	cluster, cleanup := db.NewTestCluster(t)
+	defer cleanup()
+
+	nodeID1 := int64(1) // This is the default local node
+
+	// Add another node
+	var nodeID2 int64
+	err := cluster.Transaction(func(tx *db.ClusterTx) error {
+		var err error
+		nodeID2, err = tx.NodeAdd("node2", "1.2.3.4:666")
+		require.NoError(t, err)
+		addContainer(t, tx, nodeID1, "c1")
+		addContainer(t, tx, nodeID2, "c2")
+		return nil
+	})
+	require.NoError(t, err)
+
+	names, err := cluster.ContainersNodeList(db.CTypeRegular)
+	require.NoError(t, err)
+	assert.Equal(t, names, []string{"c1"})
+}
+
 func addContainer(t *testing.T, tx *db.ClusterTx, nodeID int64, name string) {
 	stmt := `
 INSERT INTO containers(node_id, name, architecture, type) VALUES (?, ?, 1, ?)


More information about the lxc-devel mailing list