[lxc-devel] [lxd/master] Redirect container/snapshost publish API requests to the relevant node

freeekanayaka on Github lxc-bot at linuxcontainers.org
Mon May 28 09:04:43 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 382 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180528/4f2ca3cd/attachment.bin>
-------------- next part --------------
From dd7cc1b8ed51c6d0bb163e00e7396391d972eba3 Mon Sep 17 00:00:00 2001
From: Free Ekanayaka <free.ekanayaka at canonical.com>
Date: Mon, 28 May 2018 09:02:57 +0000
Subject: [PATCH] Redirect container/snapshost publish API requests to the
 relevant node

Signed-off-by: Free Ekanayaka <free.ekanayaka at canonical.com>
---
 lxd/images.go             | 16 ++++++++++++++++
 test/main.sh              |  1 +
 test/suites/clustering.sh | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+)

diff --git a/lxd/images.go b/lxd/images.go
index dc55b6003..adf3a28b3 100644
--- a/lxd/images.go
+++ b/lxd/images.go
@@ -607,6 +607,22 @@ func imagesPost(d *Daemon, r *http.Request) Response {
 		return InternalError(fmt.Errorf("Invalid images JSON"))
 	}
 
+	/* Forward requests for containers on other nodes */
+	if !imageUpload && shared.StringInSlice(req.Source.Type, []string{"container", "snapshot"}) {
+		name := req.Source.Name
+		if name != "" {
+			post.Seek(0, 0)
+			r.Body = post
+			response, err := ForwardedResponseIfContainerIsRemote(d, r, name)
+			if err != nil {
+				return SmartError(err)
+			}
+			if response != nil {
+				return response
+			}
+		}
+	}
+
 	// Begin background operation
 	run := func(op *operation) error {
 		var err error
diff --git a/test/main.sh b/test/main.sh
index c9cd947ef..dc13c3738 100755
--- a/test/main.sh
+++ b/test/main.sh
@@ -208,6 +208,7 @@ run_test test_clustering_membership "clustering membership"
 run_test test_clustering_containers "clustering containers"
 run_test test_clustering_storage "clustering storage"
 run_test test_clustering_network "clustering network"
+run_test test_clustering_publish "clustering publish"
 #run_test test_clustering_upgrade "clustering upgrade"
 
 # shellcheck disable=SC2034
diff --git a/test/suites/clustering.sh b/test/suites/clustering.sh
index 38583ad56..bd318845b 100644
--- a/test/suites/clustering.sh
+++ b/test/suites/clustering.sh
@@ -750,3 +750,50 @@ test_clustering_upgrade() {
   kill_lxd "${LXD_TWO_DIR}"
   kill_lxd "${LXD_THREE_DIR}"
 }
+
+test_clustering_publish() {
+  setup_clustering_bridge
+  prefix="lxd$$"
+  bridge="${prefix}"
+
+  setup_clustering_netns 1
+  LXD_ONE_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
+  chmod +x "${LXD_ONE_DIR}"
+  ns1="${prefix}1"
+  spawn_lxd_and_bootstrap_cluster "${ns1}" "${bridge}" "${LXD_ONE_DIR}"
+
+  # Add a newline at the end of each line. YAML as weird rules..
+  cert=$(sed ':a;N;$!ba;s/\n/\n\n/g' "${LXD_ONE_DIR}/server.crt")
+
+  # Spawn a second node
+  setup_clustering_netns 2
+  LXD_TWO_DIR=$(mktemp -d -p "${TEST_DIR}" XXX)
+  chmod +x "${LXD_TWO_DIR}"
+  ns2="${prefix}2"
+  spawn_lxd_and_join_cluster "${ns2}" "${bridge}" "${cert}" 2 1 "${LXD_TWO_DIR}"
+
+  # Init a container on node2, using a client connected to node1
+  LXD_DIR="${LXD_TWO_DIR}" ensure_import_testimage
+  LXD_DIR="${LXD_ONE_DIR}" lxc init --target node2 testimage foo
+
+  LXD_DIR="${LXD_ONE_DIR}" lxc publish foo --alias=foo-image
+  LXD_DIR="${LXD_ONE_DIR}" lxc image show foo-image | grep -q "public: false"
+  LXD_DIR="${LXD_TWO_DIR}" lxc image delete foo-image
+
+  LXD_DIR="${LXD_TWO_DIR}" lxc snapshot foo backup
+  LXD_DIR="${LXD_ONE_DIR}" lxc publish foo/backup --alias=foo-backup-image
+  LXD_DIR="${LXD_ONE_DIR}" lxc image show foo-backup-image | grep -q "public: false"
+
+  LXD_DIR="${LXD_TWO_DIR}" lxd shutdown
+  LXD_DIR="${LXD_ONE_DIR}" lxd shutdown
+  sleep 2
+  rm -f "${LXD_TWO_DIR}/unix.socket"
+  rm -f "${LXD_ONE_DIR}/unix.socket"
+
+  teardown_clustering_netns
+  teardown_clustering_bridge
+
+  kill_lxd "${LXD_ONE_DIR}"
+  kill_lxd "${LXD_TWO_DIR}"
+}
+


More information about the lxc-devel mailing list