[lxc-devel] [lxd/master] Adds instanceInstantiate function

tomponline on Github lxc-bot at linuxcontainers.org
Mon Oct 7 10:05:20 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 553 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20191007/fb36dcb5/attachment-0001.bin>
-------------- next part --------------
From bd849a1ec6422831e6901f0634d5deee7cc3a917 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 17 Sep 2019 10:57:17 +0100
Subject: [PATCH 1/2] lxd/container: Adds instanceInstantiate function

instanceInstantiate function will create the correct underlying struct based on instance type and returns it as an Instance.

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

diff --git a/lxd/container.go b/lxd/container.go
index dce1e8f191..70b3df455f 100644
--- a/lxd/container.go
+++ b/lxd/container.go
@@ -1143,6 +1143,24 @@ func instanceLoadAllInternal(dbInstances []db.Instance, s *state.State) ([]Insta
 	return instances, nil
 }
 
+// instanceInstantiate creates the underlying instance type struct and returns it as an Instance.
+func instanceInstantiate(s *state.State, args db.InstanceArgs, cProfiles []api.Profile) (Instance, error) {
+	var inst Instance
+	var err error
+
+	if args.Type == instancetype.Container {
+		inst, err = containerLXCLoad(s, args, cProfiles)
+	} else {
+		return nil, fmt.Errorf("Invalid instance type for instance %s", args.Name)
+	}
+
+	if err != nil {
+		return nil, err
+	}
+
+	return inst, nil
+}
+
 func containerCompareSnapshots(source Instance, target Instance) ([]Instance, []Instance, error) {
 	// Get the source snapshots
 	sourceSnapshots, err := source.Snapshots()

From 73fd165c973b094da112d7832c7de1bde061c16a Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 17 Sep 2019 10:58:22 +0100
Subject: [PATCH 2/2] lxd: Replaces use of containerLXCLoad with
 instanceInstantiate

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/container.go       | 20 +++++++-------------
 lxd/containers.go      |  4 ++--
 lxd/containers_post.go |  4 ++--
 3 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/lxd/container.go b/lxd/container.go
index 70b3df455f..dfb40fb0b9 100644
--- a/lxd/container.go
+++ b/lxd/container.go
@@ -986,13 +986,12 @@ func instanceLoadByProjectAndName(s *state.State, project, name string) (Instanc
 	}
 
 	args := db.ContainerToArgs(container)
-
-	c, err := containerLXCLoad(s, args, nil)
+	inst, err := instanceInstantiate(s, args, nil)
 	if err != nil {
 		return nil, errors.Wrap(err, "Failed to load container")
 	}
 
-	return c, nil
+	return inst, nil
 }
 
 func instanceLoadByProject(s *state.State, project string) ([]Instance, error) {
@@ -1126,18 +1125,13 @@ func instanceLoadAllInternal(dbInstances []db.Instance, s *state.State) ([]Insta
 			cProfiles = append(cProfiles, profiles[dbInstance.Project][name])
 		}
 
-		if dbInstance.Type == instancetype.Container {
-			args := db.ContainerToArgs(&dbInstance)
-			ct, err := containerLXCLoad(s, args, cProfiles)
-			if err != nil {
-				return nil, err
-			}
-			instances = append(instances, ct)
-		} else {
-			// TODO add virtual machine load here.
-			continue
+		args := db.ContainerToArgs(&dbInstance)
+		inst, err := instanceInstantiate(s, args, cProfiles)
+		if err != nil {
+			return nil, err
 		}
 
+		instances = append(instances, inst)
 	}
 
 	return instances, nil
diff --git a/lxd/containers.go b/lxd/containers.go
index ab403bf639..0d4f588db5 100644
--- a/lxd/containers.go
+++ b/lxd/containers.go
@@ -278,7 +278,7 @@ func containersShutdown(s *state.State) error {
 
 		for project, names := range cnames {
 			for _, name := range names {
-				c, err := containerLXCLoad(s, db.InstanceArgs{
+				inst, err := instanceInstantiate(s, db.InstanceArgs{
 					Project: project,
 					Name:    name,
 					Config:  make(map[string]string),
@@ -287,7 +287,7 @@ func containersShutdown(s *state.State) error {
 					return err
 				}
 
-				instances = append(instances, c)
+				instances = append(instances, inst)
 			}
 		}
 	}
diff --git a/lxd/containers_post.go b/lxd/containers_post.go
index 5c79e675d2..79e403a45d 100644
--- a/lxd/containers_post.go
+++ b/lxd/containers_post.go
@@ -323,12 +323,12 @@ func createFromMigration(d *Daemon, project string, req *api.InstancesPost) resp
 			}
 		} else {
 			// Retrieve the future storage pool
-			cM, err := containerLXCLoad(d.State(), args, nil)
+			inst, err := instanceInstantiate(d.State(), args, nil)
 			if err != nil {
 				return response.InternalError(err)
 			}
 
-			_, rootDiskDevice, err := shared.GetRootDiskDevice(cM.ExpandedDevices().CloneNative())
+			_, rootDiskDevice, err := shared.GetRootDiskDevice(inst.ExpandedDevices().CloneNative())
 			if err != nil {
 				return response.InternalError(err)
 			}


More information about the lxc-devel mailing list