From afa7e5dca42226fb0d5b500c652f506f79dd89f5 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Fri, 28 Oct 2016 17:16:56 -0700 Subject: [PATCH] Making the client use tls if the node from which migration has to be made has enabled tls --- api/raw.go | 8 ++++++++ client/client.go | 28 +++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/api/raw.go b/api/raw.go index fc9f5e61b..9369829c5 100644 --- a/api/raw.go +++ b/api/raw.go @@ -1,5 +1,7 @@ package api +import "io" + // Raw can be used to do raw queries against custom endpoints type Raw struct { c *Client @@ -17,6 +19,12 @@ func (raw *Raw) Query(endpoint string, out interface{}, q *QueryOptions) (*Query return raw.c.query(endpoint, out, q) } +// Response is used to make a GET request against an endpoint and returns the +// response body +func (raw *Raw) Response(endpoint string, q *QueryOptions) (io.ReadCloser, error) { + return raw.c.rawQuery(endpoint, q) +} + // Write is used to do a PUT request against an endpoint // and serialize/deserialized using the standard Nomad conventions. func (raw *Raw) Write(endpoint string, in, out interface{}, q *WriteOptions) (*WriteMeta, error) { diff --git a/client/client.go b/client/client.go index 6c10d8455..ba945f17f 100644 --- a/client/client.go +++ b/client/client.go @@ -8,7 +8,6 @@ import ( "io/ioutil" "log" "net" - "net/http" "os" "path/filepath" "strconv" @@ -20,6 +19,7 @@ import ( consulapi "github.com/hashicorp/consul/api" "github.com/hashicorp/consul/lib" "github.com/hashicorp/go-multierror" + nomadapi "github.com/hashicorp/nomad/api" "github.com/hashicorp/nomad/client/allocdir" "github.com/hashicorp/nomad/client/config" "github.com/hashicorp/nomad/client/driver" @@ -1519,15 +1519,33 @@ func (c *Client) migrateRemoteAllocDir(alloc *structs.Allocation, allocID string } // Get the snapshot - url := fmt.Sprintf("http://%v/v1/client/allocation/%v/snapshot", node.HTTPAddr, alloc.ID) - resp, err := http.Get(url) + scheme := "http" + if node.TLSEnabled { + scheme = "https" + } + // Create an API client + apiConfig := nomadapi.DefaultConfig() + apiConfig.Address = fmt.Sprintf("%s://%s", scheme, node.HTTPAddr) + apiConfig.TLSConfig = &nomadapi.TLSConfig{ + CACert: c.config.TLSConfig.CAFile, + ClientCert: c.config.TLSConfig.CertFile, + ClientKey: c.config.TLSConfig.KeyFile, + } + apiClient, err := nomadapi.NewClient(apiConfig) + if err != nil { + return nil, err + } + + url := fmt.Sprintf("/v1/client/allocation/%v/snapshot", alloc.ID) + resp, err := apiClient.Raw().Response(url, nil) if err != nil { os.RemoveAll(pathToAllocDir) c.logger.Printf("[ERR] client: error getting snapshot: %v", err) return nil, fmt.Errorf("error getting snapshot for alloc %v: %v", alloc.ID, err) } - tr := tar.NewReader(resp.Body) - defer resp.Body.Close() + + tr := tar.NewReader(resp) + defer resp.Close() buf := make([]byte, 1024)