[lxc-devel] [lxd/master] Replaces ContainerType with instance.Type

tomponline on Github lxc-bot at linuxcontainers.org
Mon Sep 9 10:33:30 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 352 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190909/affacd4f/attachment-0001.bin>
-------------- next part --------------
From b1cefe201c141cfe60efe758ba6d7b88daaed7da Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 6 Sep 2019 14:29:22 +0100
Subject: [PATCH 1/7] lxd/instance/instance: Changes instance types to own int
 type

 - Adds constants for Any and Container types

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

diff --git a/lxd/instance/instance.go b/lxd/instance/instance.go
index 1dff9a626d..10952e4821 100644
--- a/lxd/instance/instance.go
+++ b/lxd/instance/instance.go
@@ -1,4 +1,11 @@
 package instance
 
-// TypeContainer represents a container instance type.
-const TypeContainer = "container"
+// Type indicates the type of instance.
+type Type int
+
+const (
+	// TypeAny represents any type of instance.
+	TypeAny = Type(-1)
+	// TypeContainer represents a container instance type.
+	TypeContainer = Type(0)
+)

From c3f76be0bfb66340292f7e55fbcfc6d5d14052ca Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 6 Sep 2019 14:30:15 +0100
Subject: [PATCH 2/7] lxd/device/device/instance/id: Updates interface to use
 instance.Type

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

diff --git a/lxd/device/device_instance_id.go b/lxd/device/device_instance_id.go
index f1923aa17e..c92bb6ae95 100644
--- a/lxd/device/device_instance_id.go
+++ b/lxd/device/device_instance_id.go
@@ -2,6 +2,7 @@ package device
 
 import (
 	"github.com/lxc/lxd/lxd/device/config"
+	"github.com/lxc/lxd/lxd/instance"
 )
 
 // InstanceIdentifier is an interface that allows us to identify an Instance and its properties.
@@ -9,7 +10,7 @@ import (
 // independent of when they're called in the instance lifecycle.
 type InstanceIdentifier interface {
 	Name() string
-	Type() string
+	Type() instance.Type
 	Project() string
 	DevicesPath() string
 	RootfsPath() string

From edc263bfc1263553d933ff2b10c6140ee505a65e Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 6 Sep 2019 14:32:21 +0100
Subject: [PATCH 3/7] lxd/container/lxc: Updates containerLXC to use
 instance.Type

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

diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 425b5e53da..6ab80d9dcc 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -630,7 +630,7 @@ type containerLXC struct {
 	expiryDate time.Time
 }
 
-func (c *containerLXC) Type() string {
+func (c *containerLXC) Type() instance.Type {
 	return instance.TypeContainer
 }
 

From 35a9559a5fb1eedf047adb84a721a0ccdfd1b25e Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Fri, 6 Sep 2019 14:32:01 +0100
Subject: [PATCH 4/7] lxd/container: Updates container interface to use
 instance.Type

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

diff --git a/lxd/container.go b/lxd/container.go
index dba6aea933..666e0a8fe9 100644
--- a/lxd/container.go
+++ b/lxd/container.go
@@ -20,6 +20,7 @@ import (
 	"github.com/lxc/lxd/lxd/db"
 	"github.com/lxc/lxd/lxd/device"
 	"github.com/lxc/lxd/lxd/device/config"
+	"github.com/lxc/lxd/lxd/instance"
 	"github.com/lxc/lxd/lxd/state"
 	"github.com/lxc/lxd/lxd/sys"
 	"github.com/lxc/lxd/lxd/task"
@@ -292,7 +293,7 @@ type container interface {
 	Location() string
 	Project() string
 	Name() string
-	Type() string
+	Type() instance.Type
 	Description() string
 	Architecture() int
 	CreationDate() time.Time

From 9083a95a1daa2e735292588f879e1f5c9bb81925 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 9 Sep 2019 11:20:03 +0100
Subject: [PATCH 5/7] lxd/db/containers: Replaces ContainerType with
 instance.Type

- Removes ContainerType and associated CTypeRegular, CTypeSnapshot constants.
- Updates containerLXC to change Type field tp instance.Type and add IsSnapshot bool.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/db/containers.go | 43 +++++++++++++++++++------------------------
 1 file changed, 19 insertions(+), 24 deletions(-)

diff --git a/lxd/db/containers.go b/lxd/db/containers.go
index 7bd85e7b88..f5a96136e5 100644
--- a/lxd/db/containers.go
+++ b/lxd/db/containers.go
@@ -10,6 +10,7 @@ import (
 	"github.com/lxc/lxd/lxd/db/query"
 	"github.com/lxc/lxd/lxd/device/config"
 	deviceConfig "github.com/lxc/lxd/lxd/device/config"
+	"github.com/lxc/lxd/lxd/instance"
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/api"
 	"github.com/lxc/lxd/shared/logger"
@@ -69,7 +70,8 @@ type Instance struct {
 	Project      string `db:"primary=yes&join=projects.name"`
 	Name         string `db:"primary=yes"`
 	Node         string `db:"join=nodes.name"`
-	Type         int
+	Type         instance.Type
+	IsSnapshot   bool
 	Architecture int
 	Ephemeral    bool
 	CreationDate time.Time
@@ -87,7 +89,7 @@ type InstanceFilter struct {
 	Project string
 	Name    string
 	Node    string
-	Type    int
+	Type    instance.Type
 }
 
 // ContainerToArgs is a convenience to convert the new Container db struct into
@@ -98,7 +100,8 @@ func ContainerToArgs(container *Instance) ContainerArgs {
 		Project:      container.Project,
 		Name:         container.Name,
 		Node:         container.Node,
-		Ctype:        ContainerType(container.Type),
+		Type:         container.Type,
+		IsSnapshot:   container.IsSnapshot,
 		Architecture: container.Architecture,
 		Ephemeral:    container.Ephemeral,
 		CreationDate: container.CreationDate,
@@ -122,9 +125,10 @@ func ContainerToArgs(container *Instance) ContainerArgs {
 // container.
 type ContainerArgs struct {
 	// Don't set manually
-	ID    int
-	Node  string
-	Ctype ContainerType
+	ID         int
+	Node       string
+	Type       instance.Type
+	IsSnapshot bool
 
 	// Creation only
 	Project      string
@@ -157,15 +161,6 @@ type ContainerBackupArgs struct {
 	OptimizedStorage bool
 }
 
-// ContainerType encodes the type of container (either regular or snapshot).
-type ContainerType int
-
-// Numerical codes for container types.
-const (
-	CTypeRegular  ContainerType = 0
-	CTypeSnapshot ContainerType = 1
-)
-
 // ContainerNames returns the names of all containers the given project.
 func (c *ClusterTx) ContainerNames(project string) ([]string, error) {
 	stmt := `
@@ -173,7 +168,7 @@ SELECT instances.name FROM instances
   JOIN projects ON projects.id = instances.project_id
   WHERE projects.name = ? AND instances.type = ?
 `
-	return query.SelectStrings(c.tx, stmt, project, CTypeRegular)
+	return query.SelectStrings(c.tx, stmt, project, instance.TypeContainer)
 }
 
 // ContainerNodeAddress returns the address of the node hosting the container
@@ -261,7 +256,7 @@ SELECT instances.name, nodes.id, nodes.address, nodes.heartbeat
     AND projects.name = ?
   ORDER BY instances.id
 `
-	rows, err := c.tx.Query(stmt, CTypeRegular, project)
+	rows, err := c.tx.Query(stmt, instance.TypeContainer, project)
 	if err != nil {
 		return nil, err
 	}
@@ -342,7 +337,7 @@ SELECT instances.name, nodes.name
   WHERE instances.type=?
     AND projects.name = ?
 `
-	rows, err := c.tx.Query(stmt, CTypeRegular, project)
+	rows, err := c.tx.Query(stmt, instance.TypeContainer, project)
 	if err != nil {
 		return nil, err
 	}
@@ -488,7 +483,7 @@ func (c *ClusterTx) ContainerNodeList() ([]Instance, error) {
 	}
 	filter := InstanceFilter{
 		Node: node,
-		Type: int(CTypeRegular),
+		Type: instance.TypeContainer,
 	}
 
 	return c.InstanceList(filter)
@@ -503,7 +498,7 @@ func (c *ClusterTx) ContainerNodeProjectList(project string) ([]Instance, error)
 	filter := InstanceFilter{
 		Project: project,
 		Node:    node,
-		Type:    int(CTypeRegular),
+		Type:    instance.TypeContainer,
 	}
 
 	return c.InstanceList(filter)
@@ -795,7 +790,7 @@ func (c *Cluster) ContainerConfig(id int) (map[string]string, error) {
 // use it for new code.
 func (c *Cluster) LegacyContainersList() ([]string, error) {
 	q := fmt.Sprintf("SELECT name FROM instances WHERE type=? ORDER BY name")
-	inargs := []interface{}{CTypeRegular}
+	inargs := []interface{}{instance.TypeContainer}
 	var container string
 	outfmt := []interface{}{container}
 	result, err := queryScan(c.db, q, inargs, outfmt)
@@ -822,7 +817,7 @@ FROM instances_snapshots
 JOIN instances ON instances.id = instances_snapshots.instance_id
 WHERE type=? ORDER BY instances.name, instances_snapshots.name
 `)
-	inargs := []interface{}{CTypeRegular}
+	inargs := []interface{}{instance.TypeContainer}
 	var container string
 	var snapshot string
 	outfmt := []interface{}{container, snapshot}
@@ -841,9 +836,9 @@ WHERE type=? ORDER BY instances.name, instances_snapshots.name
 
 // ContainersNodeList returns the names of all the containers of the given type
 // running on the local node.
-func (c *Cluster) ContainersNodeList(cType ContainerType) ([]string, error) {
+func (c *Cluster) ContainersNodeList(instanceType instance.Type) ([]string, error) {
 	q := fmt.Sprintf("SELECT name FROM instances WHERE type=? AND node_id=? ORDER BY name")
-	inargs := []interface{}{cType, c.nodeID}
+	inargs := []interface{}{instanceType, c.nodeID}
 	var container string
 	outfmt := []interface{}{container}
 	result, err := queryScan(c.db, q, inargs, outfmt)

From dd0fbe8b18835f524b80506f39055dea53d6de28 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 9 Sep 2019 11:30:29 +0100
Subject: [PATCH 6/7] lxd: Replaces ContainerType with instance.Type

- Also adds use of IsSnapshot field where CTypeSnapshot was used.

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/api_internal.go          |  6 +++--
 lxd/container.go             | 17 ++++++++------
 lxd/container_lxc.go         | 11 +++++----
 lxd/container_snapshot.go    |  3 ++-
 lxd/container_test.go        | 43 ++++++++++++++++++------------------
 lxd/containers_post.go       |  9 ++++----
 lxd/db/containers_test.go    | 20 +++++++++--------
 lxd/db/snapshots.go          |  3 ++-
 lxd/db/snapshots_test.go     | 10 +++++----
 lxd/logging.go               |  3 ++-
 lxd/main_activateifneeded.go |  3 ++-
 lxd/patches.go               |  4 ++--
 lxd/storage_lvm_utils.go     |  3 ++-
 lxd/storage_migration.go     |  2 +-
 14 files changed, 78 insertions(+), 59 deletions(-)

diff --git a/lxd/api_internal.go b/lxd/api_internal.go
index 1f48eda280..10c1715f09 100644
--- a/lxd/api_internal.go
+++ b/lxd/api_internal.go
@@ -22,6 +22,7 @@ import (
 	"github.com/lxc/lxd/lxd/db/node"
 	"github.com/lxc/lxd/lxd/db/query"
 	deviceConfig "github.com/lxc/lxd/lxd/device/config"
+	"github.com/lxc/lxd/lxd/instance"
 	"github.com/lxc/lxd/lxd/project"
 	driver "github.com/lxc/lxd/lxd/storage"
 	"github.com/lxc/lxd/shared"
@@ -905,7 +906,7 @@ func internalImport(d *Daemon, r *http.Request) Response {
 		BaseImage:    baseImage,
 		Config:       backup.Container.Config,
 		CreationDate: backup.Container.CreatedAt,
-		Ctype:        db.CTypeRegular,
+		Type:         instance.Type(backup.Container.Type),
 		Description:  backup.Container.Description,
 		Devices:      deviceConfig.NewDevices(backup.Container.Devices),
 		Ephemeral:    backup.Container.Ephemeral,
@@ -1011,7 +1012,8 @@ func internalImport(d *Daemon, r *http.Request) Response {
 			BaseImage:    baseImage,
 			Config:       snap.Config,
 			CreationDate: snap.CreatedAt,
-			Ctype:        db.CTypeSnapshot,
+			Type:         instance.Type(backup.Container.Type),
+			IsSnapshot:   true,
 			Devices:      deviceConfig.NewDevices(snap.Devices),
 			Ephemeral:    snap.Ephemeral,
 			LastUsedDate: snap.LastUsedAt,
diff --git a/lxd/container.go b/lxd/container.go
index 666e0a8fe9..0fe54fbcb2 100644
--- a/lxd/container.go
+++ b/lxd/container.go
@@ -608,7 +608,8 @@ func containerCreateAsCopy(s *state.State, args db.ContainerArgs, sourceContaine
 			csArgs := db.ContainerArgs{
 				Architecture: snap.Architecture(),
 				Config:       snap.LocalConfig(),
-				Ctype:        db.CTypeSnapshot,
+				Type:         ct.Type(),
+				IsSnapshot:   true,
 				Devices:      snapDevices,
 				Description:  snap.Description(),
 				Ephemeral:    snap.IsEphemeral(),
@@ -799,8 +800,9 @@ func containerCreateInternal(s *state.State, args db.ContainerArgs) (container,
 		args.Architecture = s.OS.Architectures[0]
 	}
 
-	// Validate container name
-	if args.Ctype == db.CTypeRegular {
+	// Only validate instance name if not snapshot, as snapshot names contain a / character that
+	// is not normally allowed in instance names.
+	if !args.IsSnapshot {
 		err := containerValidName(args.Name)
 		if err != nil {
 			return nil, err
@@ -876,7 +878,7 @@ func containerCreateInternal(s *state.State, args db.ContainerArgs) (container,
 			return fmt.Errorf("Project %q does not exist", args.Project)
 		}
 
-		if args.Ctype == db.CTypeSnapshot {
+		if args.IsSnapshot == true {
 			parts := strings.SplitN(args.Name, shared.SnapshotDelimiter, 2)
 			instanceName := parts[0]
 			snapshotName := parts[1]
@@ -916,7 +918,7 @@ func containerCreateInternal(s *state.State, args db.ContainerArgs) (container,
 			Project:      args.Project,
 			Name:         args.Name,
 			Node:         node,
-			Type:         int(args.Ctype),
+			Type:         args.Type,
 			Architecture: args.Architecture,
 			Ephemeral:    args.Ephemeral,
 			CreationDate: args.CreationDate,
@@ -1082,7 +1084,7 @@ func containerLoadByProject(s *state.State, project string) ([]container, error)
 	err := s.Cluster.Transaction(func(tx *db.ClusterTx) error {
 		filter := db.InstanceFilter{
 			Project: project,
-			Type:    int(db.CTypeRegular),
+			Type:    instance.TypeContainer,
 		}
 		var err error
 		cts, err = tx.InstanceList(filter)
@@ -1385,7 +1387,8 @@ func autoCreateContainerSnapshots(ctx context.Context, d *Daemon, containers []c
 			args := db.ContainerArgs{
 				Architecture: c.Architecture(),
 				Config:       c.LocalConfig(),
-				Ctype:        db.CTypeSnapshot,
+				Type:         c.Type(),
+				IsSnapshot:   true,
 				Devices:      c.LocalDevices(),
 				Ephemeral:    c.IsEphemeral(),
 				Name:         snapshotName,
diff --git a/lxd/container_lxc.go b/lxd/container_lxc.go
index 6ab80d9dcc..ad6180c220 100644
--- a/lxd/container_lxc.go
+++ b/lxd/container_lxc.go
@@ -328,7 +328,8 @@ func containerLXCCreate(s *state.State, args db.ContainerArgs) (container, error
 		description:  args.Description,
 		ephemeral:    args.Ephemeral,
 		architecture: args.Architecture,
-		cType:        args.Ctype,
+		instanceType: args.Type,
+		isSnapshot:   args.IsSnapshot,
 		stateful:     args.Stateful,
 		creationDate: args.CreationDate,
 		lastUsedDate: args.LastUsedDate,
@@ -562,7 +563,8 @@ func containerLXCInstantiate(s *state.State, args db.ContainerArgs) *containerLX
 		description:  args.Description,
 		ephemeral:    args.Ephemeral,
 		architecture: args.Architecture,
-		cType:        args.Ctype,
+		instanceType: args.Type,
+		isSnapshot:   args.IsSnapshot,
 		creationDate: args.CreationDate,
 		lastUsedDate: args.LastUsedDate,
 		profiles:     args.Profiles,
@@ -593,7 +595,8 @@ func containerLXCInstantiate(s *state.State, args db.ContainerArgs) *containerLX
 type containerLXC struct {
 	// Properties
 	architecture int
-	cType        db.ContainerType
+	instanceType instance.Type
+	isSnapshot   bool
 	creationDate time.Time
 	lastUsedDate time.Time
 	ephemeral    bool
@@ -6681,7 +6684,7 @@ func (c *containerLXC) IsRunning() bool {
 }
 
 func (c *containerLXC) IsSnapshot() bool {
-	return c.cType == db.CTypeSnapshot
+	return c.isSnapshot
 }
 
 // Various property query functions
diff --git a/lxd/container_snapshot.go b/lxd/container_snapshot.go
index 851d23c2d9..50a0741a12 100644
--- a/lxd/container_snapshot.go
+++ b/lxd/container_snapshot.go
@@ -140,7 +140,8 @@ func containerSnapshotsPost(d *Daemon, r *http.Request) Response {
 			Project:      c.Project(),
 			Architecture: c.Architecture(),
 			Config:       c.LocalConfig(),
-			Ctype:        db.CTypeSnapshot,
+			Type:         c.Type(),
+			IsSnapshot:   true,
 			Devices:      c.LocalDevices(),
 			Ephemeral:    c.IsEphemeral(),
 			Name:         fullName,
diff --git a/lxd/container_test.go b/lxd/container_test.go
index 3982e1bd45..55f9fe3443 100644
--- a/lxd/container_test.go
+++ b/lxd/container_test.go
@@ -8,6 +8,7 @@ import (
 
 	"github.com/lxc/lxd/lxd/db"
 	"github.com/lxc/lxd/lxd/device/config"
+	"github.com/lxc/lxd/lxd/instance"
 	driver "github.com/lxc/lxd/lxd/storage"
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/api"
@@ -20,7 +21,7 @@ type containerTestSuite struct {
 
 func (suite *containerTestSuite) TestContainer_ProfilesDefault() {
 	args := db.ContainerArgs{
-		Ctype:     db.CTypeRegular,
+		Type:      instance.TypeContainer,
 		Ephemeral: false,
 		Name:      "testFoo",
 	}
@@ -62,7 +63,7 @@ func (suite *containerTestSuite) TestContainer_ProfilesMulti() {
 	}()
 
 	args := db.ContainerArgs{
-		Ctype:     db.CTypeRegular,
+		Type:      instance.TypeContainer,
 		Ephemeral: false,
 		Profiles:  []string{"default", "unprivileged"},
 		Name:      "testFoo",
@@ -85,7 +86,7 @@ func (suite *containerTestSuite) TestContainer_ProfilesMulti() {
 
 func (suite *containerTestSuite) TestContainer_ProfilesOverwriteDefaultNic() {
 	args := db.ContainerArgs{
-		Ctype:     db.CTypeRegular,
+		Type:      instance.TypeContainer,
 		Ephemeral: false,
 		Config:    map[string]string{"security.privileged": "true"},
 		Devices: config.Devices{
@@ -115,7 +116,7 @@ func (suite *containerTestSuite) TestContainer_ProfilesOverwriteDefaultNic() {
 
 func (suite *containerTestSuite) TestContainer_LoadFromDB() {
 	args := db.ContainerArgs{
-		Ctype:     db.CTypeRegular,
+		Type:      instance.TypeContainer,
 		Ephemeral: false,
 		Config:    map[string]string{"security.privileged": "true"},
 		Devices: config.Devices{
@@ -153,7 +154,7 @@ func (suite *containerTestSuite) TestContainer_LoadFromDB() {
 func (suite *containerTestSuite) TestContainer_Path_Regular() {
 	// Regular
 	args := db.ContainerArgs{
-		Ctype:     db.CTypeRegular,
+		Type:      instance.TypeContainer,
 		Ephemeral: false,
 		Name:      "testFoo",
 	}
@@ -169,7 +170,7 @@ func (suite *containerTestSuite) TestContainer_Path_Regular() {
 
 func (suite *containerTestSuite) TestContainer_LogPath() {
 	args := db.ContainerArgs{
-		Ctype:     db.CTypeRegular,
+		Type:      instance.TypeContainer,
 		Ephemeral: false,
 		Name:      "testFoo",
 	}
@@ -183,7 +184,7 @@ func (suite *containerTestSuite) TestContainer_LogPath() {
 
 func (suite *containerTestSuite) TestContainer_IsPrivileged_Privileged() {
 	args := db.ContainerArgs{
-		Ctype:     db.CTypeRegular,
+		Type:      instance.TypeContainer,
 		Ephemeral: false,
 		Config:    map[string]string{"security.privileged": "true"},
 		Name:      "testFoo",
@@ -198,7 +199,7 @@ func (suite *containerTestSuite) TestContainer_IsPrivileged_Privileged() {
 
 func (suite *containerTestSuite) TestContainer_IsPrivileged_Unprivileged() {
 	args := db.ContainerArgs{
-		Ctype:     db.CTypeRegular,
+		Type:      instance.TypeContainer,
 		Ephemeral: false,
 		Config:    map[string]string{"security.privileged": "false"},
 		Name:      "testFoo",
@@ -213,7 +214,7 @@ func (suite *containerTestSuite) TestContainer_IsPrivileged_Unprivileged() {
 
 func (suite *containerTestSuite) TestContainer_Rename() {
 	args := db.ContainerArgs{
-		Ctype:     db.CTypeRegular,
+		Type:      instance.TypeContainer,
 		Ephemeral: false,
 		Name:      "testFoo",
 	}
@@ -228,8 +229,8 @@ func (suite *containerTestSuite) TestContainer_Rename() {
 
 func (suite *containerTestSuite) TestContainer_findIdmap_isolated() {
 	c1, err := containerCreateInternal(suite.d.State(), db.ContainerArgs{
-		Ctype: db.CTypeRegular,
-		Name:  "isol-1",
+		Type: instance.TypeContainer,
+		Name: "isol-1",
 		Config: map[string]string{
 			"security.idmap.isolated": "true",
 		},
@@ -238,8 +239,8 @@ func (suite *containerTestSuite) TestContainer_findIdmap_isolated() {
 	defer c1.Delete()
 
 	c2, err := containerCreateInternal(suite.d.State(), db.ContainerArgs{
-		Ctype: db.CTypeRegular,
-		Name:  "isol-2",
+		Type: instance.TypeContainer,
+		Name: "isol-2",
 		Config: map[string]string{
 			"security.idmap.isolated": "true",
 		},
@@ -269,8 +270,8 @@ func (suite *containerTestSuite) TestContainer_findIdmap_isolated() {
 
 func (suite *containerTestSuite) TestContainer_findIdmap_mixed() {
 	c1, err := containerCreateInternal(suite.d.State(), db.ContainerArgs{
-		Ctype: db.CTypeRegular,
-		Name:  "isol-1",
+		Type: instance.TypeContainer,
+		Name: "isol-1",
 		Config: map[string]string{
 			"security.idmap.isolated": "false",
 		},
@@ -279,8 +280,8 @@ func (suite *containerTestSuite) TestContainer_findIdmap_mixed() {
 	defer c1.Delete()
 
 	c2, err := containerCreateInternal(suite.d.State(), db.ContainerArgs{
-		Ctype: db.CTypeRegular,
-		Name:  "isol-2",
+		Type: instance.TypeContainer,
+		Name: "isol-2",
 		Config: map[string]string{
 			"security.idmap.isolated": "true",
 		},
@@ -310,8 +311,8 @@ func (suite *containerTestSuite) TestContainer_findIdmap_mixed() {
 
 func (suite *containerTestSuite) TestContainer_findIdmap_raw() {
 	c1, err := containerCreateInternal(suite.d.State(), db.ContainerArgs{
-		Ctype: db.CTypeRegular,
-		Name:  "isol-1",
+		Type: instance.TypeContainer,
+		Name: "isol-1",
 		Config: map[string]string{
 			"security.idmap.isolated": "false",
 			"raw.idmap":               "both 1000 1000",
@@ -349,8 +350,8 @@ func (suite *containerTestSuite) TestContainer_findIdmap_maxed() {
 
 	for i := 0; i < 7; i++ {
 		c, err := containerCreateInternal(suite.d.State(), db.ContainerArgs{
-			Ctype: db.CTypeRegular,
-			Name:  fmt.Sprintf("isol-%d", i),
+			Type: instance.TypeContainer,
+			Name: fmt.Sprintf("isol-%d", i),
 			Config: map[string]string{
 				"security.idmap.isolated": "true",
 			},
diff --git a/lxd/containers_post.go b/lxd/containers_post.go
index fc4a707ce2..106c1e3feb 100644
--- a/lxd/containers_post.go
+++ b/lxd/containers_post.go
@@ -20,6 +20,7 @@ import (
 	"github.com/lxc/lxd/lxd/cluster"
 	"github.com/lxc/lxd/lxd/db"
 	"github.com/lxc/lxd/lxd/device/config"
+	"github.com/lxc/lxd/lxd/instance"
 	"github.com/lxc/lxd/lxd/migration"
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/api"
@@ -96,7 +97,7 @@ func createFromImage(d *Daemon, project string, req *api.ContainersPost) Respons
 		args := db.ContainerArgs{
 			Project:     project,
 			Config:      req.Config,
-			Ctype:       db.CTypeRegular,
+			Type:        instance.TypeContainer,
 			Description: req.Description,
 			Devices:     config.NewDevices(req.Devices),
 			Ephemeral:   req.Ephemeral,
@@ -152,7 +153,7 @@ func createFromNone(d *Daemon, project string, req *api.ContainersPost) Response
 	args := db.ContainerArgs{
 		Project:     project,
 		Config:      req.Config,
-		Ctype:       db.CTypeRegular,
+		Type:        instance.TypeContainer,
 		Description: req.Description,
 		Devices:     config.NewDevices(req.Devices),
 		Ephemeral:   req.Ephemeral,
@@ -209,7 +210,7 @@ func createFromMigration(d *Daemon, project string, req *api.ContainersPost) Res
 		Architecture: architecture,
 		BaseImage:    req.Source.BaseImage,
 		Config:       req.Config,
-		Ctype:        db.CTypeRegular,
+		Type:         instance.TypeContainer,
 		Devices:      config.NewDevices(req.Devices),
 		Description:  req.Description,
 		Ephemeral:    req.Ephemeral,
@@ -555,7 +556,7 @@ func createFromCopy(d *Daemon, project string, req *api.ContainersPost) Response
 		Architecture: source.Architecture(),
 		BaseImage:    req.Source.BaseImage,
 		Config:       req.Config,
-		Ctype:        db.CTypeRegular,
+		Type:         instance.TypeContainer,
 		Description:  req.Description,
 		Devices:      config.NewDevices(req.Devices),
 		Ephemeral:    req.Ephemeral,
diff --git a/lxd/db/containers_test.go b/lxd/db/containers_test.go
index 8a0cd545cc..bc2adf4fa8 100644
--- a/lxd/db/containers_test.go
+++ b/lxd/db/containers_test.go
@@ -4,10 +4,12 @@ import (
 	"testing"
 	"time"
 
-	"github.com/lxc/lxd/lxd/db"
-	"github.com/lxc/lxd/shared/api"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
+
+	"github.com/lxc/lxd/lxd/db"
+	"github.com/lxc/lxd/lxd/instance"
+	"github.com/lxc/lxd/shared/api"
 )
 
 func TestContainerList(t *testing.T) {
@@ -30,7 +32,7 @@ func TestContainerList(t *testing.T) {
 	addContainerDevice(t, tx, "c2", "eth0", "nic", nil)
 	addContainerDevice(t, tx, "c3", "root", "disk", map[string]string{"x": "y"})
 
-	filter := db.InstanceFilter{Type: int(db.CTypeRegular)}
+	filter := db.InstanceFilter{Type: instance.TypeContainer}
 	containers, err := tx.InstanceList(filter)
 	require.NoError(t, err)
 	assert.Len(t, containers, 3)
@@ -72,7 +74,7 @@ func TestContainerList_FilterByNode(t *testing.T) {
 	filter := db.InstanceFilter{
 		Project: "default",
 		Node:    "node2",
-		Type:    int(db.CTypeRegular),
+		Type:    instance.TypeContainer,
 	}
 
 	containers, err := tx.InstanceList(filter)
@@ -117,7 +119,7 @@ func TestInstanceList_ContainerWithSameNameInDifferentProjects(t *testing.T) {
 		Project:      "blah",
 		Name:         "c1",
 		Node:         "none",
-		Type:         int(db.CTypeRegular),
+		Type:         instance.TypeContainer,
 		Architecture: 1,
 		Ephemeral:    false,
 		Stateful:     true,
@@ -132,7 +134,7 @@ func TestInstanceList_ContainerWithSameNameInDifferentProjects(t *testing.T) {
 		Project:      "test",
 		Name:         "c1",
 		Node:         "none",
-		Type:         int(db.CTypeRegular),
+		Type:         instance.TypeContainer,
 		Architecture: 1,
 		Ephemeral:    false,
 		Stateful:     true,
@@ -171,7 +173,7 @@ func TestInstanceListExpanded(t *testing.T) {
 		Project:      "default",
 		Name:         "c1",
 		Node:         "none",
-		Type:         int(db.CTypeRegular),
+		Type:         instance.TypeContainer,
 		Architecture: 1,
 		Ephemeral:    false,
 		Stateful:     true,
@@ -396,7 +398,7 @@ func TestContainersNodeList(t *testing.T) {
 	})
 	require.NoError(t, err)
 
-	names, err := cluster.ContainersNodeList(db.CTypeRegular)
+	names, err := cluster.ContainersNodeList(instance.TypeContainer)
 	require.NoError(t, err)
 	assert.Equal(t, names, []string{"c1"})
 }
@@ -448,7 +450,7 @@ func addContainer(t *testing.T, tx *db.ClusterTx, nodeID int64, name string) {
 	stmt := `
 INSERT INTO instances(node_id, name, architecture, type, project_id) VALUES (?, ?, 1, ?, 1)
 `
-	_, err := tx.Tx().Exec(stmt, nodeID, name, db.CTypeRegular)
+	_, err := tx.Tx().Exec(stmt, nodeID, name, instance.TypeContainer)
 	require.NoError(t, err)
 }
 
diff --git a/lxd/db/snapshots.go b/lxd/db/snapshots.go
index 5c48106218..dcf7183d10 100644
--- a/lxd/db/snapshots.go
+++ b/lxd/db/snapshots.go
@@ -69,7 +69,8 @@ func InstanceSnapshotToInstance(instance *Instance, snapshot *InstanceSnapshot)
 		Project:      snapshot.Project,
 		Name:         instance.Name + shared.SnapshotDelimiter + snapshot.Name,
 		Node:         instance.Node,
-		Type:         int(CTypeSnapshot),
+		Type:         instance.Type,
+		IsSnapshot:   true,
 		Architecture: instance.Architecture,
 		Ephemeral:    false,
 		CreationDate: snapshot.CreationDate,
diff --git a/lxd/db/snapshots_test.go b/lxd/db/snapshots_test.go
index dcc348552a..0ef5121170 100644
--- a/lxd/db/snapshots_test.go
+++ b/lxd/db/snapshots_test.go
@@ -4,10 +4,12 @@ import (
 	"testing"
 	"time"
 
-	"github.com/lxc/lxd/lxd/db"
-	"github.com/lxc/lxd/shared/api"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
+
+	"github.com/lxc/lxd/lxd/db"
+	"github.com/lxc/lxd/lxd/instance"
+	"github.com/lxc/lxd/shared/api"
 )
 
 func TestInstanceSnapshotList(t *testing.T) {
@@ -95,7 +97,7 @@ func TestInstanceSnapshotList_SameNameInDifferentProjects(t *testing.T) {
 		Project:      "default",
 		Name:         "i1",
 		Node:         "none",
-		Type:         int(db.CTypeRegular),
+		Type:         instance.TypeContainer,
 		Architecture: 1,
 		Ephemeral:    false,
 		Stateful:     true,
@@ -108,7 +110,7 @@ func TestInstanceSnapshotList_SameNameInDifferentProjects(t *testing.T) {
 		Project:      "p1",
 		Name:         "i1",
 		Node:         "none",
-		Type:         int(db.CTypeRegular),
+		Type:         instance.TypeContainer,
 		Architecture: 1,
 		Ephemeral:    false,
 		Stateful:     true,
diff --git a/lxd/logging.go b/lxd/logging.go
index e46bad2509..162ad51a3d 100644
--- a/lxd/logging.go
+++ b/lxd/logging.go
@@ -7,6 +7,7 @@ import (
 	"time"
 
 	"github.com/lxc/lxd/lxd/db"
+	"github.com/lxc/lxd/lxd/instance"
 	"github.com/lxc/lxd/lxd/state"
 	"github.com/lxc/lxd/lxd/task"
 	"github.com/lxc/lxd/shared"
@@ -52,7 +53,7 @@ func expireLogs(ctx context.Context, state *state.State) error {
 	var containers []string
 	ch := make(chan struct{})
 	go func() {
-		containers, err = state.Cluster.ContainersNodeList(db.CTypeRegular)
+		containers, err = state.Cluster.ContainersNodeList(instance.TypeContainer)
 		ch <- struct{}{}
 	}()
 	select {
diff --git a/lxd/main_activateifneeded.go b/lxd/main_activateifneeded.go
index 686a3df4fc..16196ef827 100644
--- a/lxd/main_activateifneeded.go
+++ b/lxd/main_activateifneeded.go
@@ -10,6 +10,7 @@ import (
 
 	lxd "github.com/lxc/lxd/client"
 	"github.com/lxc/lxd/lxd/db"
+	"github.com/lxc/lxd/lxd/instance"
 	"github.com/lxc/lxd/lxd/node"
 	"github.com/lxc/lxd/shared"
 	"github.com/lxc/lxd/shared/idmap"
@@ -111,7 +112,7 @@ func (c *cmdActivateifneeded) Run(cmd *cobra.Command, args []string) error {
 
 	var containers []db.Instance
 	err = d.cluster.Transaction(func(tx *db.ClusterTx) error {
-		filter := db.InstanceFilter{Type: int(db.CTypeRegular)}
+		filter := db.InstanceFilter{Type: instance.TypeContainer}
 		var err error
 		containers, err = tx.InstanceList(filter)
 		return err
diff --git a/lxd/patches.go b/lxd/patches.go
index e0606d63c6..6a1c6421be 100644
--- a/lxd/patches.go
+++ b/lxd/patches.go
@@ -1892,9 +1892,9 @@ func updatePoolPropertyForAllObjects(d *Daemon, poolName string, allcontainers [
 		}
 
 		if c.IsSnapshot() {
-			args.Ctype = db.CTypeSnapshot
+			args.IsSnapshot = true
 		} else {
-			args.Ctype = db.CTypeRegular
+			args.IsSnapshot = false
 		}
 
 		// Check if the container already has a valid root device entry (profile or previous upgrade)
diff --git a/lxd/storage_lvm_utils.go b/lxd/storage_lvm_utils.go
index 3c88a36888..7ea61f13f2 100644
--- a/lxd/storage_lvm_utils.go
+++ b/lxd/storage_lvm_utils.go
@@ -11,6 +11,7 @@ import (
 	"github.com/pkg/errors"
 
 	"github.com/lxc/lxd/lxd/db"
+	"github.com/lxc/lxd/lxd/instance"
 	"github.com/lxc/lxd/lxd/project"
 	"github.com/lxc/lxd/lxd/state"
 	driver "github.com/lxc/lxd/lxd/storage"
@@ -721,7 +722,7 @@ func storageLVMThinpoolExists(vgName string, poolName string) (bool, error) {
 func storageLVMGetThinPoolUsers(s *state.State) ([]string, error) {
 	results := []string{}
 
-	cNames, err := s.Cluster.ContainersNodeList(db.CTypeRegular)
+	cNames, err := s.Cluster.ContainersNodeList(instance.TypeContainer)
 	if err != nil {
 		return results, err
 	}
diff --git a/lxd/storage_migration.go b/lxd/storage_migration.go
index b3cca61997..a6144075fe 100644
--- a/lxd/storage_migration.go
+++ b/lxd/storage_migration.go
@@ -198,7 +198,7 @@ func snapshotProtobufToContainerArgs(project string, containerName string, snap
 	args := db.ContainerArgs{
 		Architecture: int(snap.GetArchitecture()),
 		Config:       config,
-		Ctype:        db.CTypeSnapshot,
+		IsSnapshot:   true,
 		Devices:      devices,
 		Ephemeral:    snap.GetEphemeral(),
 		Name:         name,

From e166d38be68d77a6a9874f0b6f0f38e5a294c88b Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Mon, 9 Sep 2019 11:31:26 +0100
Subject: [PATCH 7/7] shared/api/container: Adds Type field to Container

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

diff --git a/shared/api/container.go b/shared/api/container.go
index ed41a6e61e..9203fce3cd 100644
--- a/shared/api/container.go
+++ b/shared/api/container.go
@@ -67,6 +67,7 @@ type Container struct {
 	Name            string                       `json:"name" yaml:"name"`
 	Status          string                       `json:"status" yaml:"status"`
 	StatusCode      StatusCode                   `json:"status_code" yaml:"status_code"`
+	Type            int                          `json:"type" yaml:"type"`
 
 	// API extension: container_last_used_at
 	LastUsedAt time.Time `json:"last_used_at" yaml:"last_used_at"`


More information about the lxc-devel mailing list