This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
mesos.go
270 lines (242 loc) · 6.67 KB
/
mesos.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package mesos
import (
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"sort"
"strings"
"time"
"github.com/nemosupremo/vault-gatekeeper/scheduler"
"github.com/mesos/mesos-go/api/v0/upid"
"github.com/samuel/go-zookeeper/zk"
"github.com/sirupsen/logrus"
)
type defaultLogger struct{}
func (d defaultLogger) Printf(s string, a ...interface{}) {
logrus.Debugf(s, a...)
}
type mesosMaster struct {
Address struct {
Hostname string `json:"hostname"`
Ip string `json:"ip"`
Port int `json:"port"`
} `json:"address"`
Hostname string `json:"hostname"`
Id string `json:"id"`
Ip int64 `json:"ip"`
Pid string `json:"pid"`
Port int `json:"port"`
Version string `json:"version"`
}
type mesosTask struct {
Id string `json:"id"`
Name string `json:"name"`
State string `json:"state"`
SlaveId string `json:"slave_id"`
Resources struct {
Cpus float64 `json:"cpus"`
Disk float64 `json:"disk"`
Mem float64 `json:"mem"`
Ports string `json:"ports"`
} `json:"resources"`
Statuses []struct {
State string `json:"state"`
Timestamp float64 `json:"timestamp"`
} `json:"statuses"`
Container struct {
Type string `json:"type"`
Docker struct {
Image string `json:"image"`
} `json:"docker"`
} `json:"container"`
}
type mesosState struct {
GitSha string `json:"git_sha"`
GitTag string `json:"git_tag"`
Leader string `json:"leader"`
Pid string `json:"pid"`
Slaves []struct {
Id string `json:"id"`
PID string `json:"pid"`
} `json:"slaves"`
Frameworks []struct {
Id string `json:"id"`
Name string `json:"name"`
Active bool `json:"active"`
Tasks []mesosTask `json:"tasks"`
} `json:"frameworks"`
}
var errMesosNoPath = errors.New("No path specified for mesos zk lookup.")
var errMesosParseError = errors.New("Error parsing mesos master data in zk.")
var errMesosNoMaster = errors.New("Error finding mesos master.")
var errUnknownScheme = errors.New("Unknown mesos scheme.")
var errMesosUnreachable = errors.New("No reachable mesos masters.")
type mesosScheduler struct {
Master string
}
type task struct {
id string
name string
group string
image string
ip net.IP
startTime time.Time
}
func (t task) Id() string {
return t.id
}
func (t task) Group() string {
return t.group
}
func (t task) Image() string {
return t.image
}
func (t task) Name() string {
return t.name
}
func (t task) IP() net.IP {
return t.ip
}
func (t task) StartTime() time.Time {
return t.startTime
}
func NewMesosScheduler(master string) (scheduler.Scheduler, error) {
m := &mesosScheduler{master}
if _, _, err := m.getMesosMaster(); err != nil {
return nil, err
}
return m, nil
}
func (m *mesosScheduler) LookupTask(taskId string) (scheduler.Task, error) {
mesosTask, framework, slaveHost, err := m.getMesosTask(taskId)
for i := time.Duration(0); i < 3 && err == nil && len(mesosTask.Statuses) == 0; i++ {
time.Sleep((500 + 250*i) * time.Millisecond)
mesosTask, framework, slaveHost, err = m.getMesosTask(taskId)
}
runningTime := time.Unix(0, 0)
if len(mesosTask.Statuses) > 0 {
// https://github.com/apache/mesos/blob/a61074586d778d432ba991701c9c4de9459db897/src/webui/master/static/js/controllers.js#L148
runningTime = time.Unix(0, int64(mesosTask.Statuses[0].Timestamp*1000000000))
}
var ip net.IP
if err == nil {
if ips, err := net.LookupIP(slaveHost); err == nil {
ip = ips[0]
}
}
return &task{
id: mesosTask.Id,
name: mesosTask.Name,
image: mesosTask.Container.Docker.Image,
group: framework,
startTime: runningTime,
ip: ip,
}, err
}
func (m *mesosScheduler) getMesosMaster() ([]string, string, error) {
var masterHosts []string
protocol := "http"
if path, err := url.Parse(m.Master); err == nil {
switch path.Scheme {
case "zks":
protocol = "https"
fallthrough
case "zk":
if path.Path == "" || path.Path == "/" {
return nil, protocol, errMesosNoPath
}
zookeeperPath := path.Path
if zookeeperPath[0] != '/' {
zookeeperPath = "/" + zookeeperPath
}
if zoo, _, err := zk.Connect(zk.FormatServers(strings.Split(path.Host, ",")), 10*time.Second, zk.WithLogger(&defaultLogger{})); err == nil {
defer zoo.Close()
if children, _, err := zoo.Children(zookeeperPath); err == nil {
sort.Strings(children)
for _, child := range children {
if strings.HasPrefix(child, "json.info_") {
if data, _, err := zoo.Get(zookeeperPath + "/" + child); err == nil {
var masterInfo mesosMaster
if err := json.Unmarshal(data, &masterInfo); err == nil {
masterHosts = []string{fmt.Sprintf("%s:%d", masterInfo.Address.Hostname, masterInfo.Address.Port)}
break
} else {
return nil, protocol, errMesosParseError
}
}
}
}
} else {
return nil, protocol, errMesosNoMaster
}
}
case "https":
protocol = "https"
fallthrough
case "http":
masterHosts = strings.Split(path.Host, ",")
default:
return nil, protocol, errUnknownScheme
}
} else {
masterHosts = strings.Split(m.Master, ",")
}
if len(masterHosts) == 0 {
return nil, protocol, errMesosUnreachable
}
return masterHosts, protocol, nil
}
func (m *mesosScheduler) getMesosTask(taskId string) (mesosTask, string, string, error) {
var state mesosState
var masterErr error
if masterHosts, protocol, err := m.getMesosMaster(); err == nil {
for _, host := range masterHosts {
if resp, err := http.Get(protocol + "://" + host + "/state"); err == nil {
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(&state); err == nil {
if state.Pid == state.Leader {
masterErr = nil
break
}
} else {
masterErr = err
}
} else {
masterErr = err
}
}
if masterErr != nil {
return mesosTask{}, "", "", masterErr
}
if state.Pid != state.Leader {
return mesosTask{}, "", "", errMesosUnreachable
}
slaves := make(map[string]string)
for _, slave := range state.Slaves {
slaves[slave.Id] = slave.PID
}
for _, framework := range state.Frameworks {
for _, task := range framework.Tasks {
if task.Id == taskId {
slaveHost := ""
if slavePid, ok := slaves[task.SlaveId]; ok {
if pid, err := upid.Parse(slavePid); err == nil {
slaveHost = pid.Host
} else {
logrus.Warnf("Mesos: Failed to parse PID %v.", slavePid)
}
} else {
logrus.Warnf("Mesos: Task ID %v was running on Slave %v, but no information about that slave was found.", task.Id, task.SlaveId)
}
return task, framework.Name, slaveHost, nil
}
}
}
return mesosTask{}, "", "", scheduler.ErrTaskNotFound
} else {
return mesosTask{}, "", "", err
}
}