[lxc-devel] [lxd/master] lxd/db: Fix container/profile updates
stgraber on Github
lxc-bot at linuxcontainers.org
Wed Apr 17 10:08:24 UTC 2019
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190417/8947aa54/attachment.bin>
-------------- next part --------------
From d8e081b779c5562729b9b783e1309cce1fba9c7e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 17 Apr 2019 10:34:08 +0100
Subject: [PATCH 1/3] lxd/db: Properly handle unsetting keys
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
lxd/db/containers.go | 4 ++++
lxd/db/devices.go | 2 +-
lxd/db/profiles.go | 4 ++++
3 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index b657739645..111bb8ff01 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -597,6 +597,10 @@ func ContainerConfigInsert(tx *sql.Tx, id int, config map[string]string) error {
defer stmt.Close()
for k, v := range config {
+ if v == "" {
+ continue
+ }
+
_, err := stmt.Exec(id, k, v)
if err != nil {
logger.Debugf("Error adding configuration item %s = %s to container %d",
diff --git a/lxd/db/devices.go b/lxd/db/devices.go
index 4c5129a8ce..cfadc8ff5e 100644
--- a/lxd/db/devices.go
+++ b/lxd/db/devices.go
@@ -95,7 +95,7 @@ func DevicesAdd(tx *sql.Tx, w string, cID int64, devices types.Devices) error {
for ck, cv := range v {
// The type is stored as int in the parent entry
- if ck == "type" {
+ if ck == "type" || cv == "" {
continue
}
diff --git a/lxd/db/profiles.go b/lxd/db/profiles.go
index 1abede0728..647fabb818 100644
--- a/lxd/db/profiles.go
+++ b/lxd/db/profiles.go
@@ -276,6 +276,10 @@ func ProfileConfigAdd(tx *sql.Tx, id int64, config map[string]string) error {
}
for k, v := range config {
+ if v == "" {
+ continue
+ }
+
_, err = stmt.Exec(id, k, v)
if err != nil {
return err
From 7376413d4b52a78ad035583be9bd26e8e74f5954 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 17 Apr 2019 11:00:01 +0100
Subject: [PATCH 2/3] lxd/db/profiles: Fix cross-project updates
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
lxd/db/profiles.go | 31 +++++++++++++++++++------------
lxd/profiles_utils.go | 26 +++++++++++++++-----------
2 files changed, 34 insertions(+), 23 deletions(-)
diff --git a/lxd/db/profiles.go b/lxd/db/profiles.go
index 647fabb818..d8a27dada6 100644
--- a/lxd/db/profiles.go
+++ b/lxd/db/profiles.go
@@ -291,7 +291,7 @@ func ProfileConfigAdd(tx *sql.Tx, id int64, config map[string]string) error {
// ProfileContainersGet gets the names of the containers associated with the
// profile with the given name.
-func (c *Cluster) ProfileContainersGet(project, profile string) ([]string, error) {
+func (c *Cluster) ProfileContainersGet(project, profile string) (map[string][]string, error) {
err := c.Transaction(func(tx *ClusterTx) error {
enabled, err := tx.ProjectHasProfiles(project)
if err != nil {
@@ -306,24 +306,31 @@ func (c *Cluster) ProfileContainersGet(project, profile string) ([]string, error
return nil, err
}
- q := `SELECT containers.name FROM containers JOIN containers_profiles
- ON containers.id == containers_profiles.container_id
- JOIN profiles ON containers_profiles.profile_id == profiles.id
- JOIN projects ON projects.id == profiles.project_id
- WHERE projects.name == ? AND profiles.name == ? AND containers.type == 0`
-
- results := []string{}
- inargs := []interface{}{project, profile}
+ q := `SELECT containers.name, projects.name FROM containers
+ JOIN containers_profiles ON containers.id == containers_profiles.container_id
+ JOIN projects ON projects.id == containers.project_id
+ WHERE containers_profiles.profile_id ==
+ (SELECT profiles.id FROM profiles
+ JOIN projects ON projects.id == profiles.project_id
+ WHERE profiles.name=? AND projects.name=?)
+ AND containers.type == 0`
+
+ results := map[string][]string{}
+ inargs := []interface{}{profile, project}
var name string
- outfmt := []interface{}{name}
+ outfmt := []interface{}{name, name}
output, err := queryScan(c.db, q, inargs, outfmt)
if err != nil {
- return results, err
+ return nil, err
}
for _, r := range output {
- results = append(results, r[0].(string))
+ if results[r[1].(string)] == nil {
+ results[r[1].(string)] = []string{}
+ }
+
+ results[r[1].(string)] = append(results[r[1].(string)], r[0].(string))
}
return results, nil
diff --git a/lxd/profiles_utils.go b/lxd/profiles_utils.go
index 7487cd7c01..e40e824791 100644
--- a/lxd/profiles_utils.go
+++ b/lxd/profiles_utils.go
@@ -229,18 +229,22 @@ func getProfileContainersInfo(cluster *db.Cluster, project, profile string) ([]d
if err != nil {
return nil, errors.Wrapf(err, "failed to query containers with profile '%s'", profile)
}
- containers := make([]db.ContainerArgs, len(names))
- for i, name := range names {
- var container *db.Container
- err := cluster.Transaction(func(tx *db.ClusterTx) error {
- var err error
- container, err = tx.ContainerGet(project, name)
- return err
- })
- if err != nil {
- return nil, errors.Wrapf(err, "Failed to fetch container '%s'", name)
+
+ containers := []db.ContainerArgs{}
+ for ctProject, ctNames := range names {
+ for _, ctName := range ctNames {
+ var container *db.Container
+ err := cluster.Transaction(func(tx *db.ClusterTx) error {
+ var err error
+ container, err = tx.ContainerGet(ctProject, ctName)
+ return err
+ })
+ if err != nil {
+ return nil, errors.Wrapf(err, "Failed to fetch container '%s'", ctName)
+ }
+
+ containers = append(containers, db.ContainerToArgs(container))
}
- containers[i] = db.ContainerToArgs(container)
}
return containers, nil
From 9aaf7a3fbe60c789c43a36186602122a23402663 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Wed, 17 Apr 2019 11:04:58 +0100
Subject: [PATCH 3/3] lxd/profiles: Optimize container updates
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
lxd/profiles_utils.go | 26 ++++++++++++++------------
1 file changed, 14 insertions(+), 12 deletions(-)
diff --git a/lxd/profiles_utils.go b/lxd/profiles_utils.go
index e40e824791..3a156802b2 100644
--- a/lxd/profiles_utils.go
+++ b/lxd/profiles_utils.go
@@ -231,20 +231,22 @@ func getProfileContainersInfo(cluster *db.Cluster, project, profile string) ([]d
}
containers := []db.ContainerArgs{}
- for ctProject, ctNames := range names {
- for _, ctName := range ctNames {
- var container *db.Container
- err := cluster.Transaction(func(tx *db.ClusterTx) error {
- var err error
- container, err = tx.ContainerGet(ctProject, ctName)
- return err
- })
- if err != nil {
- return nil, errors.Wrapf(err, "Failed to fetch container '%s'", ctName)
- }
+ err = cluster.Transaction(func(tx *db.ClusterTx) error {
+ for ctProject, ctNames := range names {
+ for _, ctName := range ctNames {
+ container, err := tx.ContainerGet(ctProject, ctName)
+ if err != nil {
+ return err
+ }
- containers = append(containers, db.ContainerToArgs(container))
+ containers = append(containers, db.ContainerToArgs(container))
+ }
}
+
+ return nil
+ })
+ if err != nil {
+ return nil, errors.Wrapf(err, "Failed to fetch containers")
}
return containers, nil
More information about the lxc-devel
mailing list