[lxc-devel] [lxd/master] Honor "features.profiles" when expanding instance configs

freeekanayaka on Github lxc-bot at linuxcontainers.org
Wed Feb 26 16:11:48 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 587 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200226/0a274268/attachment.bin>
-------------- next part --------------
From c0a344b2152783641f6d2a134394cf26dfddce90 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Tue, 25 Feb 2020 11:12:03 +0000
Subject: [PATCH 1/2] lxd/db: Rename ContainerListExpanded to
 instanceListExpanded

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/db/containers.go             | 6 +++---
 lxd/db/containers_export_test.go | 5 +++++
 lxd/db/containers_test.go        | 2 +-
 lxd/db/storage_volumes.go        | 2 +-
 4 files changed, 10 insertions(+), 5 deletions(-)
 create mode 100644 lxd/db/containers_export_test.go

diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index 6e855dfda6..6ede82234f 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -332,9 +332,9 @@ SELECT instances.name, nodes.id, nodes.address, nodes.heartbeat
 	return result, nil
 }
 
-// ContainerListExpanded loads all containers across all projects and expands
-// their config and devices using the profiles they are associated to.
-func (c *ClusterTx) ContainerListExpanded() ([]Instance, error) {
+// Load all instances across all projects and expands their config and devices
+// using the profiles they are associated to.
+func (c *ClusterTx) instanceListExpanded() ([]Instance, error) {
 	instances, err := c.InstanceList(InstanceFilter{})
 	if err != nil {
 		return nil, errors.Wrap(err, "Load containers")
diff --git a/lxd/db/containers_export_test.go b/lxd/db/containers_export_test.go
new file mode 100644
index 0000000000..898b8b02e5
--- /dev/null
+++ b/lxd/db/containers_export_test.go
@@ -0,0 +1,5 @@
+package db
+
+func (c *ClusterTx) InstanceListExpanded() ([]Instance, error) {
+	return c.instanceListExpanded()
+}
diff --git a/lxd/db/containers_test.go b/lxd/db/containers_test.go
index ce10ef4b10..57666f571a 100644
--- a/lxd/db/containers_test.go
+++ b/lxd/db/containers_test.go
@@ -187,7 +187,7 @@ func TestInstanceListExpanded(t *testing.T) {
 	_, err = tx.InstanceCreate(container)
 	require.NoError(t, err)
 
-	containers, err := tx.ContainerListExpanded()
+	containers, err := tx.InstanceListExpanded()
 	require.NoError(t, err)
 
 	assert.Len(t, containers, 1)
diff --git a/lxd/db/storage_volumes.go b/lxd/db/storage_volumes.go
index 33cc9ffd83..9bb2323bf1 100644
--- a/lxd/db/storage_volumes.go
+++ b/lxd/db/storage_volumes.go
@@ -228,7 +228,7 @@ func (c *Cluster) StorageVolumeIsAvailable(pool, volume string) (bool, error) {
 			return errors.Wrapf(err, "Fetch node name")
 		}
 
-		containers, err := tx.ContainerListExpanded()
+		containers, err := tx.instanceListExpanded()
 		if err != nil {
 			return errors.Wrapf(err, "Fetch containers")
 		}

From e1939aacf369221287ea11f900b369bc716c2af3 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Wed, 26 Feb 2020 11:46:02 +0000
Subject: [PATCH 2/2] lxd/db: Make instanceListExpanded account for projects
 without "features.profiles" enabled

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/db/containers.go | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index 6ede82234f..29652a84c1 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -340,6 +340,17 @@ func (c *ClusterTx) instanceListExpanded() ([]Instance, error) {
 		return nil, errors.Wrap(err, "Load containers")
 	}
 
+	projects, err := c.ProjectList(ProjectFilter{})
+	if err != nil {
+		return nil, errors.Wrap(err, "Load projects")
+	}
+
+	// Map to check which projects have the profiles features on.
+	projectHasProfiles := map[string]bool{}
+	for _, project := range projects {
+		projectHasProfiles[project.Name] = project.Config["features.profiles"] == "true"
+	}
+
 	profiles, err := c.ProfileList(ProfileFilter{})
 	if err != nil {
 		return nil, errors.Wrap(err, "Load profiles")
@@ -358,8 +369,17 @@ func (c *ClusterTx) instanceListExpanded() ([]Instance, error) {
 
 	for i, instance := range instances {
 		profiles := make([]api.Profile, len(instance.Profiles))
+
+		profilesProject := instance.Project
+
+		// If the instance's project does not have the profiles feature
+		// enable, we fall back to the default project.
+		if !projectHasProfiles[profilesProject] {
+			profilesProject = "default"
+		}
+
 		for j, name := range instance.Profiles {
-			profile := profilesByProjectAndName[instance.Project][name]
+			profile := profilesByProjectAndName[profilesProject][name]
 			profiles[j] = *ProfileToAPI(&profile)
 		}
 


More information about the lxc-devel mailing list