[lxc-devel] [lxd/master] Instance: Centralise logic for volatile key copying

tomponline on Github lxc-bot at linuxcontainers.org
Tue Nov 24 16:53:06 UTC 2020


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/20201124/2c664e93/attachment.bin>
-------------- next part --------------
From 2c9fdc5afd27c2d90dd9dfa176e2ebab8295c7e0 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 24 Nov 2020 16:46:40 +0000
Subject: [PATCH 1/8] shared/instance: golint fixes

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 shared/instance.go | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/shared/instance.go b/shared/instance.go
index f4ac70993b..0b4ddff8e9 100644
--- a/shared/instance.go
+++ b/shared/instance.go
@@ -14,8 +14,10 @@ import (
 	"github.com/lxc/lxd/shared/validate"
 )
 
+// InstanceAction indicates the type of action being performed.
 type InstanceAction string
 
+// InstanceAction types.
 const (
 	Stop     InstanceAction = "stop"
 	Start    InstanceAction = "start"

From 79941f59a92d0734d4273c75fb197c85a964163c Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 24 Nov 2020 16:46:53 +0000
Subject: [PATCH 2/8] shared/instance: Adds ConfigVolatilePrefix constant

To avoid inconsistent use of "volatile" and "volatile." prefix checks.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 shared/instance.go | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/shared/instance.go b/shared/instance.go
index 0b4ddff8e9..2a0986f9b8 100644
--- a/shared/instance.go
+++ b/shared/instance.go
@@ -26,6 +26,9 @@ const (
 	Unfreeze InstanceAction = "unfreeze"
 )
 
+// ConfigVolatilePrefix indicates the prefix used for volatile config keys.
+const ConfigVolatilePrefix = "volatile."
+
 // IsRootDiskDevice returns true if the given device representation is configured as root disk for
 // an instance. It typically get passed a specific entry of api.Instance.Devices.
 func IsRootDiskDevice(device map[string]string) bool {

From 1e7d93cc87128b919c2d6dbe5ae280bb9d5d5476 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 24 Nov 2020 16:47:21 +0000
Subject: [PATCH 3/8] shared/instance: ConfigVolatilePrefix usage

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 shared/instance.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/shared/instance.go b/shared/instance.go
index 2a0986f9b8..000825c247 100644
--- a/shared/instance.go
+++ b/shared/instance.go
@@ -275,7 +275,7 @@ func ConfigKeyChecker(key string) (func(value string) error, error) {
 		return f, nil
 	}
 
-	if strings.HasPrefix(key, "volatile.") {
+	if strings.HasPrefix(key, ConfigVolatilePrefix) {
 		if strings.HasSuffix(key, ".hwaddr") {
 			return validate.IsAny, nil
 		}

From 91277c1649fdf97fd888d3fc9465330226292280 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 24 Nov 2020 16:47:29 +0000
Subject: [PATCH 4/8] shared/instance: Adds InstanceIncludeWhenCopying function

Brings together the volatile inclusion/exclusion logic that was spread over lxd and lxc.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 shared/instance.go | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/shared/instance.go b/shared/instance.go
index 000825c247..c1f1483776 100644
--- a/shared/instance.go
+++ b/shared/instance.go
@@ -355,3 +355,21 @@ func InstanceGetParentAndSnapshotName(name string) (string, string, bool) {
 
 	return fields[0], fields[1], true
 }
+
+// InstanceIncludeWhenCopying is used to decide whether to include a config item or not when copying an instance.
+// The remoteCopy argument indicates if the copy is remote (i.e between LXD nodes) as this affects the keys kept.
+func InstanceIncludeWhenCopying(configKey string, remoteCopy bool) bool {
+	if configKey == "volatile.base_image" {
+		return true // Include volatile.base_image always as it can help optimize copies.
+	}
+
+	if configKey == "volatile.last_state.idmap" && !remoteCopy {
+		return true // Include volatile.last_state.idmap when doing local copy to avoid needless remapping.
+	}
+
+	if strings.HasPrefix(configKey, ConfigVolatilePrefix) {
+		return false // Exclude all other volatile keys.
+	}
+
+	return true // Keep all other keys.
+}

From 76dcd882dd1bef3bb9713bd223f76630b24a2ba1 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 24 Nov 2020 16:48:13 +0000
Subject: [PATCH 5/8] lxd/copy: shared.InstanceIncludeWhenCopying usage in
 copyInstance

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxc/copy.go | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)

diff --git a/lxc/copy.go b/lxc/copy.go
index 7b88270ee4..128c52230b 100644
--- a/lxc/copy.go
+++ b/lxc/copy.go
@@ -224,13 +224,8 @@ func (c *cmdCopy) copyInstance(conf *config.Config, sourceResource string, destR
 			delete(entry.Config, "volatile.last_state.power")
 
 			if !keepVolatile {
-				// Strip all volatile keys
 				for k := range entry.Config {
-					if k == "volatile.base_image" {
-						continue
-					}
-
-					if strings.HasPrefix(k, "volatile") {
+					if !shared.InstanceIncludeWhenCopying(k, true) {
 						delete(entry.Config, k)
 					}
 				}
@@ -319,11 +314,7 @@ func (c *cmdCopy) copyInstance(conf *config.Config, sourceResource string, destR
 		// Strip the volatile keys if requested
 		if !keepVolatile {
 			for k := range entry.Config {
-				if k == "volatile.base_image" {
-					continue
-				}
-
-				if strings.HasPrefix(k, "volatile") {
+				if !shared.InstanceIncludeWhenCopying(k, true) {
 					delete(entry.Config, k)
 				}
 			}

From fd15eb80b127ccc6ba6308872e7931eab98bcf49 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 24 Nov 2020 16:48:25 +0000
Subject: [PATCH 6/8] lxc: shared.ConfigVolatilePrefix usage

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxc/list_test.go | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lxc/list_test.go b/lxc/list_test.go
index 5bb084e044..9bb986961b 100644
--- a/lxc/list_test.go
+++ b/lxc/list_test.go
@@ -97,11 +97,11 @@ func TestColumns(t *testing.T) {
 			randString(buffer)
 		case 3:
 			if rand.Intn(2) == 0 {
-				buffer.WriteString("volatile.")
+				buffer.WriteString(shared.ConfigVolatilePrefix)
 				randString(buffer)
 				buffer.WriteString(".hwaddr")
 			} else {
-				buffer.WriteString("volatile.")
+				buffer.WriteString(shared.ConfigVolatilePrefix)
 				randString(buffer)
 				buffer.WriteString(".name")
 			}

From ecbb32591a01af930bf93fa85011a65d6cb8b469 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 24 Nov 2020 16:48:42 +0000
Subject: [PATCH 7/8] lxd: shared.ConfigVolatilePrefix usage

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/api_cluster.go                    | 4 ++--
 lxd/instance/drivers/driver_common.go | 2 +-
 lxd/instance/drivers/driver_lxc.go    | 2 +-
 lxd/instance/instance_utils.go        | 2 +-
 lxd/project/permissions.go            | 2 +-
 5 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/lxd/api_cluster.go b/lxd/api_cluster.go
index 7e88bcce2f..4261f90ae2 100644
--- a/lxd/api_cluster.go
+++ b/lxd/api_cluster.go
@@ -144,7 +144,7 @@ func clusterGetMemberConfig(cluster *db.Cluster) ([]api.ClusterMemberConfigKey,
 
 	for pool, config := range pools {
 		for key := range config {
-			if strings.HasPrefix(key, "volatile.") {
+			if strings.HasPrefix(key, shared.ConfigVolatilePrefix) {
 				continue
 			}
 
@@ -160,7 +160,7 @@ func clusterGetMemberConfig(cluster *db.Cluster) ([]api.ClusterMemberConfigKey,
 
 	for network, config := range networks {
 		for key := range config {
-			if strings.HasPrefix(key, "volatile.") {
+			if strings.HasPrefix(key, shared.ConfigVolatilePrefix) {
 				continue
 			}
 
diff --git a/lxd/instance/drivers/driver_common.go b/lxd/instance/drivers/driver_common.go
index 161cb1416a..395a200cf7 100644
--- a/lxd/instance/drivers/driver_common.go
+++ b/lxd/instance/drivers/driver_common.go
@@ -236,7 +236,7 @@ func (d *common) Snapshots() ([]instance.Instance, error) {
 func (d *common) VolatileSet(changes map[string]string) error {
 	// Sanity check.
 	for key := range changes {
-		if !strings.HasPrefix(key, "volatile.") {
+		if !strings.HasPrefix(key, shared.ConfigVolatilePrefix) {
 			return fmt.Errorf("Only volatile keys can be modified with VolatileSet")
 		}
 	}
diff --git a/lxd/instance/drivers/driver_lxc.go b/lxd/instance/drivers/driver_lxc.go
index e731beba16..ab7eeba24c 100644
--- a/lxd/instance/drivers/driver_lxc.go
+++ b/lxd/instance/drivers/driver_lxc.go
@@ -6058,7 +6058,7 @@ func (d *lxc) FillNetworkDevice(name string, m deviceConfig.Device) (deviceConfi
 
 		// Include all currently allocated interface names
 		for k, v := range d.expandedConfig {
-			if !strings.HasPrefix(k, "volatile.") {
+			if !strings.HasPrefix(k, shared.ConfigVolatilePrefix) {
 				continue
 			}
 
diff --git a/lxd/instance/instance_utils.go b/lxd/instance/instance_utils.go
index 589ab7b567..3ea6b4d6ae 100644
--- a/lxd/instance/instance_utils.go
+++ b/lxd/instance/instance_utils.go
@@ -127,7 +127,7 @@ func ValidConfig(sysOS *sys.OS, config map[string]string, profile bool, expanded
 	}
 
 	for k, v := range config {
-		if profile && strings.HasPrefix(k, "volatile.") {
+		if profile && strings.HasPrefix(k, shared.ConfigVolatilePrefix) {
 			return fmt.Errorf("Volatile keys can only be set on instances")
 		}
 
diff --git a/lxd/project/permissions.go b/lxd/project/permissions.go
index 3950396bb6..cc3ba5d5b4 100644
--- a/lxd/project/permissions.go
+++ b/lxd/project/permissions.go
@@ -125,7 +125,7 @@ func checkRestrictionsOnVolatileConfig(project *api.Project, instanceType instan
 	}
 
 	for key, value := range config {
-		if !strings.HasPrefix(key, "volatile.") {
+		if !strings.HasPrefix(key, shared.ConfigVolatilePrefix) {
 			continue
 		}
 

From f98ed83b30211bce54ebcc23f233c406e4c1b115 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 24 Nov 2020 16:49:08 +0000
Subject: [PATCH 8/8] lxd/instances/post: shared.InstanceIncludeWhenCopying
 usage in createFromCopy

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/instances_post.go | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/lxd/instances_post.go b/lxd/instances_post.go
index 3a7028a1de..369f44230b 100644
--- a/lxd/instances_post.go
+++ b/lxd/instances_post.go
@@ -438,9 +438,8 @@ func createFromCopy(d *Daemon, project string, req *api.InstancesPost) response.
 	}
 
 	for key, value := range sourceConfig {
-		if len(key) > 8 && key[0:8] == "volatile" && !shared.StringInSlice(key[9:], []string{"base_image", "last_state.idmap"}) {
-			logger.Debug("Skipping volatile key from copy source",
-				log.Ctx{"key": key})
+		if !shared.InstanceIncludeWhenCopying(key, false) {
+			logger.Debug("Skipping key from copy source", log.Ctx{"key": key, "sourceProject": source.Project(), "sourceInstance": source.Name(), "project": targetProject, "instance": req.Name})
 			continue
 		}
 


More information about the lxc-devel mailing list