[lxc-devel] [lxd/master] lxd/cluster: Improve error on bad target

stgraber on Github lxc-bot at linuxcontainers.org
Tue Jun 19 04:10:42 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 370 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180619/5112bda7/attachment.bin>
-------------- next part --------------
From 4ec89d51df1c2db89fad58dd3c5ebcc5c7ce15de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Tue, 19 Jun 2018 00:09:47 -0400
Subject: [PATCH] lxd/cluster: Improve error on bad target
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Closes #4633

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/cluster/resolve.go | 18 +++++++++++++++++-
 lxd/response.go        | 18 ++----------------
 2 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/lxd/cluster/resolve.go b/lxd/cluster/resolve.go
index 6aece834b..ff3527681 100644
--- a/lxd/cluster/resolve.go
+++ b/lxd/cluster/resolve.go
@@ -1,6 +1,10 @@
 package cluster
 
-import "github.com/lxc/lxd/lxd/db"
+import (
+	"fmt"
+
+	"github.com/lxc/lxd/lxd/db"
+)
 
 // ResolveTarget is a convenience for handling the value ?targetNode query
 // parameter. It returns the address of the given node, or the empty string if
@@ -12,14 +16,26 @@ func ResolveTarget(cluster *db.Cluster, target string) (string, error) {
 		if err != nil {
 			return err
 		}
+
+		if target == name {
+			return nil
+		}
+
 		node, err := tx.NodeByName(target)
 		if err != nil {
+			if err == db.ErrNoSuchObject {
+				return fmt.Errorf("No cluster member called '%s'", target)
+			}
+
 			return err
 		}
+
 		if node.Name != name {
 			address = node.Address
 		}
+
 		return nil
 	})
+
 	return address, err
 }
diff --git a/lxd/response.go b/lxd/response.go
index 85ac1fd92..9c714660c 100644
--- a/lxd/response.go
+++ b/lxd/response.go
@@ -189,25 +189,11 @@ func ForwardedResponseIfTargetIsRemote(d *Daemon, request *http.Request) Respons
 
 	// Figure out the address of the target node (which is possibly
 	// this very same node).
-	var address string
-	err := d.cluster.Transaction(func(tx *db.ClusterTx) error {
-		localNode, err := tx.NodeName()
-		if err != nil {
-			return err
-		}
-		if targetNode == localNode {
-			return nil
-		}
-		node, err := tx.NodeByName(targetNode)
-		if err != nil {
-			return err
-		}
-		address = node.Address
-		return nil
-	})
+	address, err := cluster.ResolveTarget(d.cluster, targetNode)
 	if err != nil {
 		return SmartError(err)
 	}
+
 	if address != "" {
 		// Forward the response.
 		cert := d.endpoints.NetworkCert()


More information about the lxc-devel mailing list