Skip to content

Commit 63b715f

Browse files
authored
Merge pull request #3108 from hashicorp/b-3013-stats-tls
Fix TLS support in api pkg / cli
2 parents 9512709 + 52857a6 commit 63b715f

File tree

4 files changed

+58
-125
lines changed

4 files changed

+58
-125
lines changed

api/allocations.go

+5-24
Original file line numberDiff line numberDiff line change
@@ -48,43 +48,24 @@ func (a *Allocations) Info(allocID string, q *QueryOptions) (*Allocation, *Query
4848
}
4949

5050
func (a *Allocations) Stats(alloc *Allocation, q *QueryOptions) (*AllocResourceUsage, error) {
51-
node, _, err := a.client.Nodes().Info(alloc.NodeID, q)
52-
if err != nil {
53-
return nil, err
54-
}
55-
if node.Status == "down" {
56-
return nil, NodeDownErr
57-
}
58-
if node.HTTPAddr == "" {
59-
return nil, fmt.Errorf("http addr of the node where alloc %q is running is not advertised", alloc.ID)
60-
}
61-
client, err := NewClient(a.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled))
51+
nodeClient, err := a.client.GetNodeClient(alloc.NodeID, q)
6252
if err != nil {
6353
return nil, err
6454
}
55+
6556
var resp AllocResourceUsage
66-
_, err = client.query("/v1/client/allocation/"+alloc.ID+"/stats", &resp, nil)
57+
_, err = nodeClient.query("/v1/client/allocation/"+alloc.ID+"/stats", &resp, nil)
6758
return &resp, err
6859
}
6960

7061
func (a *Allocations) GC(alloc *Allocation, q *QueryOptions) error {
71-
node, _, err := a.client.Nodes().Info(alloc.NodeID, q)
72-
if err != nil {
73-
return err
74-
}
75-
if node.Status == "down" {
76-
return NodeDownErr
77-
}
78-
if node.HTTPAddr == "" {
79-
return fmt.Errorf("http addr of the node where alloc %q is running is not advertised", alloc.ID)
80-
}
81-
client, err := NewClient(a.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled))
62+
nodeClient, err := a.client.GetNodeClient(alloc.NodeID, q)
8263
if err != nil {
8364
return err
8465
}
8566

8667
var resp struct{}
87-
_, err = client.query("/v1/client/allocation"+alloc.ID+"/gc", &resp, nil)
68+
_, err = nodeClient.query("/v1/client/allocation/"+alloc.ID+"/gc", &resp, nil)
8869
return err
8970
}
9071

api/api.go

+40-4
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,22 @@ type Config struct {
110110
TLSConfig *TLSConfig
111111
}
112112

113-
// CopyConfig copies the configuration with a new address
114-
func (c *Config) CopyConfig(address string, tlsEnabled bool) *Config {
113+
// ClientConfig copies the configuration with a new client address, region, and
114+
// whether the client has TLS enabled.
115+
func (c *Config) ClientConfig(region, address string, tlsEnabled bool) *Config {
115116
scheme := "http"
116117
if tlsEnabled {
117118
scheme = "https"
118119
}
119120
config := &Config{
120121
Address: fmt.Sprintf("%s://%s", scheme, address),
121-
Region: c.Region,
122+
Region: region,
122123
HttpClient: c.HttpClient,
123124
HttpAuth: c.HttpAuth,
124125
WaitTime: c.WaitTime,
125-
TLSConfig: c.TLSConfig,
126+
TLSConfig: c.TLSConfig.Copy(),
126127
}
128+
config.TLSConfig.TLSServerName = fmt.Sprintf("client.%s.nomad", c.Region)
127129

128130
return config
129131
}
@@ -153,6 +155,16 @@ type TLSConfig struct {
153155
Insecure bool
154156
}
155157

158+
func (t *TLSConfig) Copy() *TLSConfig {
159+
if t == nil {
160+
return nil
161+
}
162+
163+
nt := new(TLSConfig)
164+
*nt = *t
165+
return nt
166+
}
167+
156168
// DefaultConfig returns a default configuration for the client
157169
func DefaultConfig() *Config {
158170
config := &Config{
@@ -285,6 +297,30 @@ func (c *Client) SetRegion(region string) {
285297
c.config.Region = region
286298
}
287299

300+
// GetNodeClient returns a new Client that will dial the specified node. If the
301+
// QueryOptions is set, its region will be used.
302+
func (c *Client) GetNodeClient(nodeID string, q *QueryOptions) (*Client, error) {
303+
node, _, err := c.Nodes().Info(nodeID, q)
304+
if err != nil {
305+
return nil, err
306+
}
307+
if node.Status == "down" {
308+
return nil, NodeDownErr
309+
}
310+
if node.HTTPAddr == "" {
311+
return nil, fmt.Errorf("http addr of node %q (%s) is not advertised", node.Name, nodeID)
312+
}
313+
314+
region := c.config.Region
315+
if q != nil && q.Region != "" {
316+
region = q.Region
317+
}
318+
319+
// Get an API client for the node
320+
conf := c.config.ClientConfig(region, node.HTTPAddr, node.TLSEnabled)
321+
return NewClient(conf)
322+
}
323+
288324
// request is used to help build up a request
289325
type request struct {
290326
config *Config

api/fs.go

+8-78
Original file line numberDiff line numberDiff line change
@@ -49,58 +49,9 @@ func (c *Client) AllocFS() *AllocFS {
4949
return &AllocFS{client: c}
5050
}
5151

52-
// getNodeClient returns a Client that will dial the node. If the QueryOptions
53-
// is set, the function will ensure that it is initialized and that the Params
54-
// field is valid.
55-
func (a *AllocFS) getNodeClient(node *Node, allocID string, q **QueryOptions) (*Client, error) {
56-
if node.HTTPAddr == "" {
57-
return nil, fmt.Errorf("http addr of the node where alloc %q is running is not advertised", allocID)
58-
}
59-
60-
region := ""
61-
if q != nil && *q != nil && (*q).Region != "" {
62-
region = (*q).Region
63-
} else if a.client.config.Region != "" {
64-
// Use the region from the client
65-
region = a.client.config.Region
66-
} else {
67-
// Use the region from the agent
68-
agentRegion, err := a.client.Agent().Region()
69-
if err != nil {
70-
return nil, err
71-
}
72-
region = agentRegion
73-
}
74-
75-
// Get an API client for the node
76-
conf := a.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled)
77-
conf.TLSConfig.TLSServerName = fmt.Sprintf("client.%s.nomad", region)
78-
nodeClient, err := NewClient(conf)
79-
if err != nil {
80-
return nil, err
81-
}
82-
83-
// Set the query params
84-
if q == nil {
85-
return nodeClient, nil
86-
}
87-
88-
if *q == nil {
89-
*q = &QueryOptions{}
90-
}
91-
if actQ := *q; actQ.Params == nil {
92-
actQ.Params = make(map[string]string)
93-
}
94-
return nodeClient, nil
95-
}
96-
9752
// List is used to list the files at a given path of an allocation directory
9853
func (a *AllocFS) List(alloc *Allocation, path string, q *QueryOptions) ([]*AllocFileInfo, *QueryMeta, error) {
99-
node, _, err := a.client.Nodes().Info(alloc.NodeID, &QueryOptions{})
100-
if err != nil {
101-
return nil, nil, err
102-
}
103-
nodeClient, err := a.getNodeClient(node, alloc.ID, &q)
54+
nodeClient, err := a.client.GetNodeClient(alloc.NodeID, q)
10455
if err != nil {
10556
return nil, nil, err
10657
}
@@ -117,11 +68,7 @@ func (a *AllocFS) List(alloc *Allocation, path string, q *QueryOptions) ([]*Allo
11768

11869
// Stat is used to stat a file at a given path of an allocation directory
11970
func (a *AllocFS) Stat(alloc *Allocation, path string, q *QueryOptions) (*AllocFileInfo, *QueryMeta, error) {
120-
node, _, err := a.client.Nodes().Info(alloc.NodeID, &QueryOptions{})
121-
if err != nil {
122-
return nil, nil, err
123-
}
124-
nodeClient, err := a.getNodeClient(node, alloc.ID, &q)
71+
nodeClient, err := a.client.GetNodeClient(alloc.NodeID, q)
12572
if err != nil {
12673
return nil, nil, err
12774
}
@@ -138,12 +85,7 @@ func (a *AllocFS) Stat(alloc *Allocation, path string, q *QueryOptions) (*AllocF
13885
// ReadAt is used to read bytes at a given offset until limit at the given path
13986
// in an allocation directory. If limit is <= 0, there is no limit.
14087
func (a *AllocFS) ReadAt(alloc *Allocation, path string, offset int64, limit int64, q *QueryOptions) (io.ReadCloser, error) {
141-
node, _, err := a.client.Nodes().Info(alloc.NodeID, &QueryOptions{})
142-
if err != nil {
143-
return nil, err
144-
}
145-
146-
nodeClient, err := a.getNodeClient(node, alloc.ID, &q)
88+
nodeClient, err := a.client.GetNodeClient(alloc.NodeID, q)
14789
if err != nil {
14890
return nil, err
14991
}
@@ -161,12 +103,7 @@ func (a *AllocFS) ReadAt(alloc *Allocation, path string, offset int64, limit int
161103
// Cat is used to read contents of a file at the given path in an allocation
162104
// directory
163105
func (a *AllocFS) Cat(alloc *Allocation, path string, q *QueryOptions) (io.ReadCloser, error) {
164-
node, _, err := a.client.Nodes().Info(alloc.NodeID, &QueryOptions{})
165-
if err != nil {
166-
return nil, err
167-
}
168-
169-
nodeClient, err := a.getNodeClient(node, alloc.ID, &q)
106+
nodeClient, err := a.client.GetNodeClient(alloc.NodeID, q)
170107
if err != nil {
171108
return nil, err
172109
}
@@ -190,12 +127,7 @@ func (a *AllocFS) Cat(alloc *Allocation, path string, q *QueryOptions) (io.ReadC
190127
func (a *AllocFS) Stream(alloc *Allocation, path, origin string, offset int64,
191128
cancel <-chan struct{}, q *QueryOptions) (<-chan *StreamFrame, error) {
192129

193-
node, _, err := a.client.Nodes().Info(alloc.NodeID, q)
194-
if err != nil {
195-
return nil, err
196-
}
197-
198-
nodeClient, err := a.getNodeClient(node, alloc.ID, &q)
130+
nodeClient, err := a.client.GetNodeClient(alloc.NodeID, q)
199131
if err != nil {
200132
return nil, err
201133
}
@@ -259,14 +191,12 @@ func (a *AllocFS) Stream(alloc *Allocation, path, origin string, offset int64,
259191
func (a *AllocFS) Logs(alloc *Allocation, follow bool, task, logType, origin string,
260192
offset int64, cancel <-chan struct{}, q *QueryOptions) (<-chan *StreamFrame, error) {
261193

262-
node, _, err := a.client.Nodes().Info(alloc.NodeID, q)
194+
nodeClient, err := a.client.GetNodeClient(alloc.NodeID, q)
263195
if err != nil {
264196
return nil, err
265197
}
266-
267-
nodeClient, err := a.getNodeClient(node, alloc.ID, &q)
268-
if err != nil {
269-
return nil, err
198+
if q == nil {
199+
q = &QueryOptions{}
270200
}
271201
q.Params["follow"] = strconv.FormatBool(follow)
272202
q.Params["task"] = task

api/nodes.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"fmt"
54
"sort"
65
"strconv"
76
)
@@ -73,38 +72,25 @@ func (n *Nodes) ForceEvaluate(nodeID string, q *WriteOptions) (string, *WriteMet
7372
}
7473

7574
func (n *Nodes) Stats(nodeID string, q *QueryOptions) (*HostStats, error) {
76-
node, _, err := n.client.Nodes().Info(nodeID, q)
77-
if err != nil {
78-
return nil, err
79-
}
80-
if node.HTTPAddr == "" {
81-
return nil, fmt.Errorf("http addr of the node %q is running is not advertised", nodeID)
82-
}
83-
client, err := NewClient(n.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled))
75+
nodeClient, err := n.client.GetNodeClient(nodeID, q)
8476
if err != nil {
8577
return nil, err
8678
}
8779
var resp HostStats
88-
if _, err := client.query("/v1/client/stats", &resp, nil); err != nil {
80+
if _, err := nodeClient.query("/v1/client/stats", &resp, nil); err != nil {
8981
return nil, err
9082
}
9183
return &resp, nil
9284
}
9385

9486
func (n *Nodes) GC(nodeID string, q *QueryOptions) error {
95-
node, _, err := n.client.Nodes().Info(nodeID, q)
96-
if err != nil {
97-
return err
98-
}
99-
if node.HTTPAddr == "" {
100-
return fmt.Errorf("http addr of the node %q is running is not advertised", nodeID)
101-
}
102-
client, err := NewClient(n.client.config.CopyConfig(node.HTTPAddr, node.TLSEnabled))
87+
nodeClient, err := n.client.GetNodeClient(nodeID, q)
10388
if err != nil {
10489
return err
10590
}
91+
10692
var resp struct{}
107-
_, err = client.query("/v1/client/gc", &resp, nil)
93+
_, err = nodeClient.query("/v1/client/gc", &resp, nil)
10894
return err
10995
}
11096

0 commit comments

Comments
 (0)