[lxc-devel] [lxd/master] Fix container profile in projects

freeekanayaka on Github lxc-bot at linuxcontainers.org
Thu Oct 11 11:16:44 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 313 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181011/fdcf48cb/attachment.bin>
-------------- next part --------------
From 0e3141b38458491afa61020a6aa8dc7ad6baf488 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Thu, 11 Oct 2018 13:00:01 +0200
Subject: [PATCH 1/2] Associate a container with the profile from its own
 project

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/container_lxc.go         |  2 +-
 lxd/db/containers.go         | 23 ++++++++++++++++-------
 lxd/db/containers.mapper.go  |  3 ++-
 shared/generate/db/method.go |  2 +-
 4 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 6dab67109a..02059964ac 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -4867,7 +4867,7 @@ func (c *containerLXC) Update(args db.ContainerArgs, userRequested bool) error {
 			return errors.Wrap(err, "Config insert")
 		}
 
-		err = db.ContainerProfilesInsert(tx, c.id, c.profiles)
+		err = db.ContainerProfilesInsert(tx, c.id, c.project, c.profiles)
 		if err != nil {
 			tx.Rollback()
 			return errors.Wrap(err, "Profiles insert")
diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index 36ca875640..f5612cb23b 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -596,21 +596,30 @@ func (c *Cluster) ContainerSetStateful(id int, stateful bool) error {
 }
 
 // ContainerProfilesInsert associates the container with the given ID with the
-// profiles with the given names.
-func ContainerProfilesInsert(tx *sql.Tx, id int, profiles []string) error {
+// profiles with the given names in the given project.
+func ContainerProfilesInsert(tx *sql.Tx, id int, project string, profiles []string) error {
 	applyOrder := 1
-	str := `INSERT INTO containers_profiles (container_id, profile_id, apply_order) VALUES
-		(?, (SELECT id FROM profiles WHERE name=?), ?);`
+	str := `
+INSERT INTO containers_profiles (container_id, profile_id, apply_order)
+  VALUES (
+    ?,
+    (SELECT profiles.id
+     FROM profiles
+     JOIN projects ON projects.id=profiles.project_id
+     WHERE projects.name=? AND profiles.name=?),
+    ?
+  )
+`
 	stmt, err := tx.Prepare(str)
 	if err != nil {
 		return err
 	}
 	defer stmt.Close()
-	for _, p := range profiles {
-		_, err = stmt.Exec(id, p, applyOrder)
+	for _, profile := range profiles {
+		_, err = stmt.Exec(id, project, profile, applyOrder)
 		if err != nil {
 			logger.Debugf("Error adding profile %s to container: %s",
-				p, err)
+				profile, err)
 			return err
 		}
 		applyOrder = applyOrder + 1
diff --git a/lxd/db/containers.mapper.go b/lxd/db/containers.mapper.go
index f60ec5c284..9aec50d48f 100644
--- a/lxd/db/containers.mapper.go
+++ b/lxd/db/containers.mapper.go
@@ -5,6 +5,7 @@ package db
 import (
 	"database/sql"
 	"fmt"
+
 	"github.com/lxc/lxd/lxd/db/cluster"
 	"github.com/lxc/lxd/lxd/db/query"
 	"github.com/lxc/lxd/shared/api"
@@ -423,7 +424,7 @@ func (c *ClusterTx) ContainerCreate(object Container) (int64, error) {
 	}
 
 	// Insert profiles reference.
-	err = ContainerProfilesInsert(c.tx, int(id), object.Profiles)
+	err = ContainerProfilesInsert(c.tx, int(id), object.Project, object.Profiles)
 	if err != nil {
 		return -1, errors.Wrap(err, "Insert profiles for container")
 	}
diff --git a/shared/generate/db/method.go b/shared/generate/db/method.go
index 0cb25e9fbe..dc0ca3b368 100644
--- a/shared/generate/db/method.go
+++ b/shared/generate/db/method.go
@@ -682,7 +682,7 @@ func (m *Method) create(buf *file.Buffer) error {
 		if field.Name == "Profiles" {
 			// TODO: get rid of the special case
 			buf.L("// Insert profiles reference. ")
-			buf.L("err = ContainerProfilesInsert(c.tx, int(id), object.Profiles)")
+			buf.L("err = ContainerProfilesInsert(c.tx, int(id), object.Project, object.Profiles)")
 			buf.L("if err != nil {")
 			buf.L("        return -1, errors.Wrap(err, \"Insert profiles for %s\")", m.entity)
 			buf.L("}")

From 05d0d73a722494165ada60235e0ab31b291d67c0 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Thu, 11 Oct 2018 13:15:30 +0200
Subject: [PATCH 2/2] Expand container devices and configs from the associated
 project

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/container_lxc.go       |  2 +-
 lxd/db/db_internal_test.go |  4 ++--
 lxd/db/devices.go          | 18 ++++++++++--------
 lxd/profiles_test.go       |  2 +-
 lxd/profiles_utils.go      |  2 +-
 5 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 02059964ac..1d83ce70f7 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -1828,7 +1828,7 @@ func (c *containerLXC) expandDevices(profiles []api.Profile) error {
 		}
 	} else {
 		for _, p := range c.profiles {
-			devices, err := c.state.Cluster.Devices(p, true)
+			devices, err := c.state.Cluster.Devices(c.project, p, true)
 			if err != nil {
 				return err
 			}
diff --git a/lxd/db/db_internal_test.go b/lxd/db/db_internal_test.go
index cb3943ec60..b0372dd41d 100644
--- a/lxd/db/db_internal_test.go
+++ b/lxd/db/db_internal_test.go
@@ -335,7 +335,7 @@ func (s *dbTestSuite) Test_dbDevices_profiles() {
 	var subresult types.Device
 	var expected types.Device
 
-	result, err = s.db.Devices("theprofile", true)
+	result, err = s.db.Devices("default", "theprofile", true)
 	s.Nil(err)
 
 	expected = types.Device{"type": "nic", "devicekey": "devicevalue"}
@@ -353,7 +353,7 @@ func (s *dbTestSuite) Test_dbDevices_containers() {
 	var subresult types.Device
 	var expected types.Device
 
-	result, err = s.db.Devices("thename", false)
+	result, err = s.db.Devices("default", "thename", false)
 	s.Nil(err)
 
 	expected = types.Device{"type": "nic", "configkey": "configvalue"}
diff --git a/lxd/db/devices.go b/lxd/db/devices.go
index 5af6f08280..114813c4c0 100644
--- a/lxd/db/devices.go
+++ b/lxd/db/devices.go
@@ -138,22 +138,24 @@ func dbDeviceConfig(db *sql.DB, id int, isprofile bool) (types.Device, error) {
 }
 
 // Devices returns the devices matching the given filters.
-func (c *Cluster) Devices(qName string, isprofile bool) (types.Devices, error) {
+func (c *Cluster) Devices(project, qName string, isprofile bool) (types.Devices, error) {
 	var q string
 	if isprofile {
 		q = `SELECT profiles_devices.id, profiles_devices.name, profiles_devices.type
-			FROM profiles_devices JOIN profiles
-			ON profiles_devices.profile_id = profiles.id
-   		WHERE profiles.name=?`
+			FROM profiles_devices
+                        JOIN profiles ON profiles_devices.profile_id = profiles.id
+                        JOIN projects ON projects.id=profiles.project_id
+   		WHERE projects.name=? AND profiles.name=?`
 	} else {
 		q = `SELECT containers_devices.id, containers_devices.name, containers_devices.type
-			FROM containers_devices JOIN containers
-			ON containers_devices.container_id = containers.id
-			WHERE containers.name=?`
+			FROM containers_devices
+                        JOIN containers	ON containers_devices.container_id = containers.id
+                        JOIN projects ON projects.id=containers.project_id
+			WHERE projects.name=? AND containers.name=?`
 	}
 	var id, dtype int
 	var name, stype string
-	inargs := []interface{}{qName}
+	inargs := []interface{}{project, qName}
 	outfmt := []interface{}{id, name, dtype}
 	results, err := queryScan(c.db, q, inargs, outfmt)
 	if err != nil {
diff --git a/lxd/profiles_test.go b/lxd/profiles_test.go
index 5e3c1b3de1..dc437d90c5 100644
--- a/lxd/profiles_test.go
+++ b/lxd/profiles_test.go
@@ -42,7 +42,7 @@ func Test_removing_a_profile_deletes_associated_configuration_entries(t *testing
 	}
 
 	// Make sure there are 0 profiles_devices entries left.
-	devices, err := cluster.Devices("theprofile", true)
+	devices, err := cluster.Devices("default", "theprofile", true)
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/lxd/profiles_utils.go b/lxd/profiles_utils.go
index 62d55c735f..2aa5ad9039 100644
--- a/lxd/profiles_utils.go
+++ b/lxd/profiles_utils.go
@@ -215,7 +215,7 @@ func doProfileUpdateContainer(d *Daemon, name string, old api.ProfilePut, nodeNa
 			continue
 		}
 		// Use the config currently in the database.
-		devices, err := d.cluster.Devices(profileName, true)
+		devices, err := d.cluster.Devices(args.Project, profileName, true)
 		if err != nil {
 			return errors.Wrapf(err, "failed to load profile devices for '%s'", profileName)
 		}


More information about the lxc-devel mailing list