[lxc-devel] [lxd/master] client: Simplify ConnectPublicLXD logic

stgraber on Github lxc-bot at linuxcontainers.org
Thu Jul 27 17:45:11 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 354 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170727/a604fd19/attachment.bin>
-------------- next part --------------
From 463703d4db59cb48d7ca6b4e5a36bb6ca61c0147 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 27 Jul 2017 13:44:20 -0400
Subject: [PATCH] client: Simplify ConnectPublicLXD logic
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 client/connection.go | 71 ++++++++++++++++++----------------------------------
 lxc/config/remote.go | 14 +++++++++--
 2 files changed, 37 insertions(+), 48 deletions(-)

diff --git a/client/connection.go b/client/connection.go
index ae13332d3..759ac3d25 100644
--- a/client/connection.go
+++ b/client/connection.go
@@ -44,33 +44,7 @@ type ConnectionArgs struct {
 func ConnectLXD(url string, args *ConnectionArgs) (ContainerServer, error) {
 	logger.Infof("Connecting to a remote LXD over HTTPs")
 
-	// Use empty args if not specified
-	if args == nil {
-		args = &ConnectionArgs{}
-	}
-
-	// Initialize the client struct
-	server := ProtocolLXD{
-		httpCertificate: args.TLSServerCert,
-		httpHost:        url,
-		httpProtocol:    "https",
-		httpUserAgent:   args.UserAgent,
-	}
-
-	// Setup the HTTP client
-	httpClient, err := tlsHTTPClient(args.HTTPClient, args.TLSClientCert, args.TLSClientKey, args.TLSCA, args.TLSServerCert, args.Proxy)
-	if err != nil {
-		return nil, err
-	}
-	server.http = httpClient
-
-	// Test the connection and seed the server information
-	_, _, err = server.GetServer()
-	if err != nil {
-		return nil, err
-	}
-
-	return &server, nil
+	return httpsLXD(url, args)
 }
 
 // ConnectLXDUnix lets you connect to a remote LXD daemon over a local unix socket.
@@ -127,17 +101,25 @@ func ConnectLXDUnix(path string, args *ConnectionArgs) (ContainerServer, error)
 func ConnectPublicLXD(url string, args *ConnectionArgs) (ImageServer, error) {
 	logger.Infof("Connecting to a remote public LXD over HTTPs")
 
+	return httpsLXD(url, args)
+}
+
+// ConnectSimpleStreams lets you connect to a remote SimpleStreams image server over HTTPs.
+//
+// Unless the remote server is trusted by the system CA, the remote certificate must be provided (TLSServerCert).
+func ConnectSimpleStreams(url string, args *ConnectionArgs) (ImageServer, error) {
+	logger.Infof("Connecting to a remote simplestreams server")
+
 	// Use empty args if not specified
 	if args == nil {
 		args = &ConnectionArgs{}
 	}
 
 	// Initialize the client struct
-	server := ProtocolLXD{
-		httpCertificate: args.TLSServerCert,
+	server := ProtocolSimpleStreams{
 		httpHost:        url,
-		httpProtocol:    "https",
 		httpUserAgent:   args.UserAgent,
+		httpCertificate: args.TLSServerCert,
 	}
 
 	// Setup the HTTP client
@@ -147,31 +129,26 @@ func ConnectPublicLXD(url string, args *ConnectionArgs) (ImageServer, error) {
 	}
 	server.http = httpClient
 
-	// Test the connection and seed the server information
-	_, _, err = server.GetServer()
-	if err != nil {
-		return nil, err
-	}
+	// Get simplestreams client
+	ssClient := simplestreams.NewClient(url, *httpClient, args.UserAgent)
+	server.ssClient = ssClient
 
 	return &server, nil
 }
 
-// ConnectSimpleStreams lets you connect to a remote SimpleStreams image server over HTTPs.
-//
-// Unless the remote server is trusted by the system CA, the remote certificate must be provided (TLSServerCert).
-func ConnectSimpleStreams(url string, args *ConnectionArgs) (ImageServer, error) {
-	logger.Infof("Connecting to a remote simplestreams server")
-
+// Internal function called by ConnectLXD and ConnectPublicLXD
+func httpsLXD(url string, args *ConnectionArgs) (ContainerServer, error) {
 	// Use empty args if not specified
 	if args == nil {
 		args = &ConnectionArgs{}
 	}
 
 	// Initialize the client struct
-	server := ProtocolSimpleStreams{
+	server := ProtocolLXD{
+		httpCertificate: args.TLSServerCert,
 		httpHost:        url,
+		httpProtocol:    "https",
 		httpUserAgent:   args.UserAgent,
-		httpCertificate: args.TLSServerCert,
 	}
 
 	// Setup the HTTP client
@@ -181,9 +158,11 @@ func ConnectSimpleStreams(url string, args *ConnectionArgs) (ImageServer, error)
 	}
 	server.http = httpClient
 
-	// Get simplestreams client
-	ssClient := simplestreams.NewClient(url, *httpClient, args.UserAgent)
-	server.ssClient = ssClient
+	// Test the connection and seed the server information
+	_, _, err = server.GetServer()
+	if err != nil {
+		return nil, err
+	}
 
 	return &server, nil
 }
diff --git a/lxc/config/remote.go b/lxc/config/remote.go
index e5d255e64..aaf2095b0 100644
--- a/lxc/config/remote.go
+++ b/lxc/config/remote.go
@@ -113,8 +113,18 @@ func (c *Config) GetImageServer(name string) (lxd.ImageServer, error) {
 		return d, nil
 	}
 
-	// HTTPs (LXD)
-	d, err := lxd.ConnectPublicLXD(remote.Addr, args)
+	// HTTPs (public LXD)
+	if remote.Public {
+		d, err := lxd.ConnectPublicLXD(remote.Addr, args)
+		if err != nil {
+			return nil, err
+		}
+
+		return d, nil
+	}
+
+	// HTTPs (private LXD)
+	d, err := lxd.ConnectLXD(remote.Addr, args)
 	if err != nil {
 		return nil, err
 	}


More information about the lxc-devel mailing list