[lxc-devel] [lxd/master] Associate image with the right project on the joined node

adglkh on Github lxc-bot at linuxcontainers.org
Fri Mar 1 04:44:23 UTC 2019


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/20190228/ba24a46f/attachment.bin>
-------------- next part --------------
From 509cd4a146b3e9003df1320e19058ec734760163 Mon Sep 17 00:00:00 2001
From: gary-wzl77 <gary.wang at canonical.com>
Date: Fri, 1 Mar 2019 11:08:50 +0800
Subject: [PATCH 1/4] tests: avoid needless wait times during image
 synchronization when clustering.

Signed-off-by: gary-wzl77 <gary.wang at canonical.com>
---
 test/suites/clustering.sh | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/test/suites/clustering.sh b/test/suites/clustering.sh
index bee2480296..9dc641f32d 100644
--- a/test/suites/clustering.sh
+++ b/test/suites/clustering.sh
@@ -1179,8 +1179,20 @@ test_clustering_image_replication() {
   spawn_lxd_and_join_cluster "${ns3}" "${bridge}" "${cert}" 3 1 "${LXD_THREE_DIR}"
 
   # Wait for the test image to be synced into the joined node on the background
-  sleep 5
-  [ -f "${LXD_THREE_DIR}/images/${fingerprint}" ] || false
+  retries=10
+  while [ "${retries}" != "0" ]; do
+    if [ ! -f "${LXD_THREE_DIR}/images/${fingerprint}" ]; then
+        sleep 0.5
+        retries=$((retries-1))
+        continue
+    fi
+    break
+  done
+
+  if [ "${retries}" -eq 0 ]; then
+      echo "Images failed to synced into the joined node"
+      return 1
+  fi
 
   # Delete the imported image
   LXD_DIR="${LXD_ONE_DIR}" lxc image delete testimage

From 1f609a4a1fd2190b7f12a1d3b65c670aec94d2e8 Mon Sep 17 00:00:00 2001
From: gary-wzl77 <gary.wang at canonical.com>
Date: Fri, 1 Mar 2019 11:19:50 +0800
Subject: [PATCH 2/4] Only show the image auto-sync log when clustering.

Signed-off-by: gary-wzl77 <gary.wang at canonical.com>
---
 lxd/images.go | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)

diff --git a/lxd/images.go b/lxd/images.go
index c5a2a8b7ad..8f7b36e2c2 100644
--- a/lxd/images.go
+++ b/lxd/images.go
@@ -1998,6 +1998,23 @@ func imageRefresh(d *Daemon, r *http.Request) Response {
 
 func autoSyncImagesTask(d *Daemon) (task.Func, task.Schedule) {
 	f := func(ctx context.Context) {
+		// In order to only have one task operation executed per image when syncing the images
+		// across the cluster, only leader node can launch the task, no others.
+		localAddress, err := node.ClusterAddress(d.db)
+		if err != nil {
+			logger.Errorf("Failed to get current node address: %v", err)
+			return
+		}
+		leader, err := d.gateway.LeaderAddress()
+		if err != nil {
+			logger.Errorf("Failed to get leader node address: %v", err)
+			return
+		}
+		if localAddress != leader {
+			logger.Debug("Skipping image synchronization task since we're not leader")
+			return
+		}
+
 		opRun := func(op *operation) error {
 			return autoSyncImages(ctx, d)
 		}
@@ -2021,21 +2038,6 @@ func autoSyncImagesTask(d *Daemon) (task.Func, task.Schedule) {
 }
 
 func autoSyncImages(ctx context.Context, d *Daemon) error {
-	// In order to only have one task operation executed per image when syncing the images
-	// across the cluster, only leader node can launch the task, no others.
-	localAddress, err := node.ClusterAddress(d.db)
-	if err != nil {
-		return err
-	}
-	leader, err := d.gateway.LeaderAddress()
-	if err != nil {
-		return err
-	}
-	if localAddress != leader {
-		logger.Debug("Skipping image synchronization since we're not leader")
-		return nil
-	}
-
 	// Check how many images the current node owns and automatically sync all
 	// available images to other nodes which don't have yet.
 	fingerprints, err := d.cluster.ImagesGetOnCurrentNode()

From f2ee2a242da9b59b6a32105589e56a67fb33ca4a Mon Sep 17 00:00:00 2001
From: gary-wzl77 <gary.wang at canonical.com>
Date: Fri, 1 Mar 2019 12:03:33 +0800
Subject: [PATCH 3/4] Support to fetch all projects name that the image
 associated with.

Signed-off-by: gary-wzl77 <gary.wang at canonical.com>
---
 lxd/db/images.go | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/lxd/db/images.go b/lxd/db/images.go
index ef23d1d5d7..accf3a6064 100644
--- a/lxd/db/images.go
+++ b/lxd/db/images.go
@@ -912,6 +912,22 @@ func (c *Cluster) ImagesGetByNodeID(id int64) ([]string, error) {
 	return addresses, err
 }
 
+// ImagesGetProjectsWithImage returns all projects name that the image associated with the given fingerprint.
+func (c *Cluster) ImagesGetProjectsWithImage(fingerprint string) ([]string, error) {
+	var projects []string
+	err := c.Transaction(func(tx *ClusterTx) error {
+		var err error
+		stmt := `
+    SELECT projects.name FROM projects
+      LEFT JOIN images ON images.project_id = projects.id
+    WHERE images.fingerprint = ?
+    `
+		projects, err = query.SelectStrings(tx.tx, stmt, fingerprint)
+		return err
+	})
+	return projects, err
+}
+
 // ImageGetNodesWithImage returns the addresses of online nodes which already have the image.
 func (c *Cluster) ImageGetNodesWithImage(fingerprint string) ([]string, error) {
 	q := `

From e1f99d2cbcb2872903c5d6dc71e49158d7b62018 Mon Sep 17 00:00:00 2001
From: gary-wzl77 <gary.wang at canonical.com>
Date: Fri, 1 Mar 2019 12:09:09 +0800
Subject: [PATCH 4/4] Associate image with the right project on the joined
 node.

Signed-off-by: gary-wzl77 <gary.wang at canonical.com>
---
 lxd/api_cluster.go | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/lxd/api_cluster.go b/lxd/api_cluster.go
index 914c4781e2..0cd3069bcd 100644
--- a/lxd/api_cluster.go
+++ b/lxd/api_cluster.go
@@ -553,8 +553,17 @@ func clusterPutJoin(d *Daemon, req api.ClusterPut) Response {
 					return err
 				}
 
-				project := "default"
-				return d.cluster.ImageAssociateNode(project, fingerprint)
+				projects, err := d.cluster.ImagesGetProjectsWithImage(fingerprint)
+				if err != nil {
+					return err
+				}
+				for _, project := range projects {
+					err := d.cluster.ImageAssociateNode(project, fingerprint)
+					if err != nil {
+						return err
+					}
+				}
+				return nil
 			}
 
 			for _, f := range fingerprints {


More information about the lxc-devel mailing list