forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
169 lines (141 loc) · 4.21 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ecs
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/docker/docker/api/types"
)
var (
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v2.html
ecsMetadataPath = "/v2/metadata"
ecsMetaStatsPath = "/v2/stats"
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v3.html
ecsMetadataPathV3 = "/task"
ecsMetaStatsPathV3 = "/task/stats"
)
// Client is the ECS client contract
type Client interface {
Task() (*Task, error)
ContainerStats() (map[string]types.StatsJSON, error)
}
type httpClient interface {
Do(req *http.Request) (*http.Response, error)
}
// NewClient constructs an ECS client with the passed configuration params
func NewClient(timeout time.Duration, endpoint string, version int) (*EcsClient, error) {
if version != 2 && version != 3 {
const msg = "expected metadata version 2 or 3, got %d"
return nil, fmt.Errorf(msg, version)
}
baseURL, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
c := &http.Client{
Timeout: timeout,
}
return &EcsClient{
client: c,
baseURL: baseURL,
taskURL: resolveTaskURL(baseURL, version),
statsURL: resolveStatsURL(baseURL, version),
version: version,
}, nil
}
func resolveTaskURL(base *url.URL, version int) string {
var path string
switch version {
case 2:
path = ecsMetadataPath
case 3:
path = ecsMetadataPathV3
default:
// Should never happen.
const msg = "resolveTaskURL: unexpected version %d"
panic(fmt.Errorf(msg, version))
}
return resolveURL(base, path)
}
func resolveStatsURL(base *url.URL, version int) string {
var path string
switch version {
case 2:
path = ecsMetaStatsPath
case 3:
path = ecsMetaStatsPathV3
default:
// Should never happen.
const msg = "resolveStatsURL: unexpected version %d"
panic(fmt.Errorf(msg, version))
}
return resolveURL(base, path)
}
// resolveURL returns a URL string by concatenating the string representation of base
// and path. This is consistent with AWS metadata documentation:
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v3.html#task-metadata-endpoint-v3-paths
func resolveURL(base *url.URL, path string) string {
return base.String() + path
}
// EcsClient contains ECS connection config
type EcsClient struct {
client httpClient
version int
baseURL *url.URL
taskURL string
statsURL string
}
// Task calls the ECS metadata endpoint and returns a populated Task
func (c *EcsClient) Task() (*Task, error) {
req, _ := http.NewRequest("GET", c.taskURL, nil)
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 200))
return nil, fmt.Errorf("%s returned HTTP status %s: %q", c.taskURL, resp.Status, body)
}
task, err := unmarshalTask(resp.Body)
if err != nil {
return nil, err
}
return task, nil
}
// ContainerStats calls the ECS stats endpoint and returns a populated container stats map
func (c *EcsClient) ContainerStats() (map[string]types.StatsJSON, error) {
req, _ := http.NewRequest("GET", c.statsURL, nil)
resp, err := c.client.Do(req)
if err != nil {
return map[string]types.StatsJSON{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 200))
return nil, fmt.Errorf("%s returned HTTP status %s: %q", c.statsURL, resp.Status, body)
}
statsMap, err := unmarshalStats(resp.Body)
if err != nil {
return map[string]types.StatsJSON{}, err
}
return statsMap, nil
}
// PollSync executes Task and ContainerStats in parallel. If both succeed, both structs are returned.
// If either errors, a single error is returned.
func PollSync(c Client) (*Task, map[string]types.StatsJSON, error) {
var task *Task
var stats map[string]types.StatsJSON
var err error
if stats, err = c.ContainerStats(); err != nil {
return nil, nil, err
}
if task, err = c.Task(); err != nil {
return nil, nil, err
}
return task, stats, nil
}