-
Notifications
You must be signed in to change notification settings - Fork 183
/
mesosphere.go
255 lines (217 loc) · 5.94 KB
/
mesosphere.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package mesosphere
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/signalfx/signalfx-agent/internal/core/config"
"github.com/signalfx/signalfx-agent/internal/core/services"
"github.com/signalfx/signalfx-agent/internal/observers"
)
var now = time.Now
const (
observerType = "observers/mesosphere"
// DefaultPort of the task state api
DefaultPort = 5051
// RunningState string for a currently running task
RunningState = "TASK_RUNNING"
)
// Config for the Mesos observer
type Config struct {
config.ObserverConfig
PollIntervalSeconds int `yaml:"pollIntervalSeconds" default:"10"`
HostURL string `yaml:"hostURL"`
}
// Mesosphere observer plugin
type Mesosphere struct {
config *Config
hostURL string
client http.Client
serviceCallbacks *observers.ServiceCallbacks
serviceDiffer *observers.ServiceDiffer
}
// PortMappings for a task
type portMappings []struct {
HostPort uint16 `json:"host_port"`
ContainerPort uint16 `json:"container_port"`
Protocol string
}
type tasks struct {
ID string
Hostname string
Frameworks []struct {
ID string
Name string
Executors []struct {
Container string
Tasks []struct {
ID string
Name string
State string
Labels []struct {
Key string
Value string
}
Discovery struct {
Name string
Ports struct {
Ports []struct {
Name string
Number uint16
Protocol string
Labels struct {
Labels []struct {
Key string
Value string
}
}
}
}
}
Container struct {
Docker struct {
Image string
Network string
PortMappings portMappings `json:"port_mappings"`
}
}
}
}
}
}
func init() {
observers.Register(observerType, func(cbs *observers.ServiceCallbacks) interface{} {
return &Mesosphere{
serviceCallbacks: cbs,
}
}, &Config{})
}
// Configure the mesosphere observer/client
func (mesos *Mesosphere) Configure(config *Config) error {
mesos.config = config
if mesos.config.HostURL == "" {
hostname, err := os.Hostname()
if err == nil {
mesos.config.HostURL = fmt.Sprintf("http://%s:%d", hostname, DefaultPort)
} else {
return errors.Wrapf(err, "Could not set default Mesos host URL")
}
}
mesos.client = http.Client{
Timeout: 10 * time.Second,
}
if mesos.serviceDiffer != nil {
mesos.serviceDiffer.Stop()
}
mesos.serviceDiffer = &observers.ServiceDiffer{
DiscoveryFn: mesos.discover,
IntervalSeconds: config.PollIntervalSeconds,
Callbacks: mesos.serviceCallbacks,
}
mesos.serviceDiffer.Start()
return nil
}
// Read services from mesosphere
func (mesos *Mesosphere) discover() []services.Endpoint {
taskInfo, err := mesos.getTasks()
if err != nil {
log.WithError(err).Error("Failed to get Mesos tasks")
return nil
}
var instances []services.Endpoint
for _, framework := range taskInfo.Frameworks {
for _, executor := range framework.Executors {
for _, task := range executor.Tasks {
// only care about running tasks
if task.State != RunningState {
continue
}
// ports required
if len(task.Discovery.Ports.Ports) == 0 {
continue
}
var serviceContainer services.Container
if len(task.Container.Docker.Image) > 0 {
containerName := fmt.Sprintf("mesos-%s.%s", task.ID, executor.Container)
containerImage := task.Container.Docker.Image
containerLabels := map[string]string{}
for _, label := range task.Labels {
containerLabels[label.Key] = label.Value
}
serviceContainer = services.Container{
ID: executor.Container,
Names: []string{containerName},
Image: containerImage,
Command: "",
State: "running",
Labels: containerLabels,
}
} else {
continue
}
orchestration := services.NewOrchestration("mesosphere", services.MESOS, nil, services.PUBLIC)
for _, port := range task.Discovery.Ports.Ports {
portName := task.Discovery.Name
if len(port.Name) > 0 {
portName = port.Name
}
portIP := taskInfo.Hostname
portType := services.PortType(strings.ToUpper(port.Protocol))
privatePort := uint16(0)
publicPort := uint16(port.Number)
for _, portMapping := range task.Container.Docker.PortMappings {
if portMapping.HostPort == publicPort {
privatePort = uint16(portMapping.ContainerPort)
}
}
id := fmt.Sprintf("%s-%d", taskInfo.ID, publicPort)
endpoint := services.ContainerEndpoint{
AltPort: privatePort,
EndpointCore: *services.NewEndpointCore(id, portName, observerType),
Container: serviceContainer,
Orchestration: *orchestration,
}
endpoint.Host = portIP
endpoint.PortType = portType
endpoint.Port = publicPort
for _, label := range port.Labels.Labels {
endpoint.PortLabels[label.Key] = label.Value
}
endpoint.AddDimension("mesos_agent", taskInfo.ID)
endpoint.AddDimension("mesos_framework_id", framework.ID)
endpoint.AddDimension("mesos_framework_name", framework.Name)
endpoint.AddDimension("mesos_task_id", task.ID)
endpoint.AddDimension("mesos_task_name", task.Name)
instances = append(instances, &endpoint)
}
}
}
}
return instances
}
// getTasks from mesosphere state api
func (mesos *Mesosphere) getTasks() (*tasks, error) {
resp, err := mesos.client.Get(fmt.Sprintf("%s/state", mesos.config.HostURL))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to get task states: (code=%d, body=%s)",
resp.StatusCode, body[:512])
}
tasks := &tasks{}
if err := json.Unmarshal(body, tasks); err != nil {
return nil, err
}
return tasks, nil
}