-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mesos.go
335 lines (288 loc) · 9.28 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package mesos
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
logrus "github.com/AVENTER-UG/mesos-m3s/logger"
mesosproto "github.com/AVENTER-UG/mesos-m3s/proto"
cfg "github.com/AVENTER-UG/mesos-m3s/types"
"github.com/gogo/protobuf/jsonpb"
)
// Mesos include all the current vars and global config
type Mesos struct {
Config *cfg.Config
Framework *cfg.FrameworkConfig
}
// Marshaler to serialize Protobuf Message to JSON
var marshaller = jsonpb.Marshaler{
EnumsAsInts: false,
Indent: " ",
OrigName: true,
}
// New will create a new API object
func New(cfg *cfg.Config, frm *cfg.FrameworkConfig) *Mesos {
e := &Mesos{
Config: cfg,
Framework: frm,
}
return e
}
// Revive will revive the mesos tasks to clean up
func (e *Mesos) Revive() {
logrus.WithField("func", "mesos.Revive").Debug("Revive Tasks")
revive := &mesosproto.Call{
Type: mesosproto.Call_REVIVE,
}
err := e.Call(revive)
if err != nil {
logrus.WithField("func", "mesos.Revive").Error("Call Revive: ", err)
}
}
// SuppressFramework if all Tasks are running, suppress framework offers
func (e *Mesos) SuppressFramework() {
logrus.WithField("func", "mesos.SuppressFramework").Info("Framework Suppress")
suppress := &mesosproto.Call{
Type: mesosproto.Call_SUPPRESS,
}
err := e.Call(suppress)
if err != nil {
logrus.WithField("func", "mesos.SuppressFramework").Error("Suppress Framework Call: ")
}
}
// Kill a Task with the given taskID
func (e *Mesos) Kill(taskID string, agentID string) error {
logrus.WithField("func", "mesos.Kill").Info("Kill task ", taskID)
// tell mesos to shutdonw the given task
err := e.Call(&mesosproto.Call{
Type: mesosproto.Call_KILL,
Kill: &mesosproto.Call_Kill{
TaskID: mesosproto.TaskID{
Value: taskID,
},
AgentID: &mesosproto.AgentID{
Value: agentID,
},
},
})
return err
}
// Call will send messages to mesos
func (e *Mesos) Call(message *mesosproto.Call) error {
message.FrameworkID = e.Framework.FrameworkInfo.ID
body, err := marshaller.MarshalToString(message)
if err != nil {
logrus.WithField("func", "mesos.Call").Debug("Could not Marshal message:", err.Error())
return err
}
client := &http.Client{}
// #nosec G402
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
protocol := "https"
if !e.Framework.MesosSSL {
protocol = "http"
}
req, _ := http.NewRequest("POST", protocol+"://"+e.Framework.MesosMasterServer+"/api/v1/scheduler", bytes.NewBuffer([]byte(body)))
req.Close = true
req.SetBasicAuth(e.Framework.Username, e.Framework.Password)
req.Header.Set("Mesos-Stream-Id", e.Framework.MesosStreamID)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
logrus.WithField("func", "mesos.Call").Error("Call Message: ", err)
return err
}
defer res.Body.Close()
if res.StatusCode != 202 {
body, err := io.ReadAll(res.Body)
if err != nil {
logrus.WithField("func", "mesos.Call").Error("Call Handling (could not read res.Body)")
return fmt.Errorf("Error %d", res.StatusCode)
}
logrus.WithField("func", "mesos.Call").Error("Call Handling: ", string(body))
return fmt.Errorf("Error %d", res.StatusCode)
}
return nil
}
// DecodeTask will decode the key into an mesos command struct
func (e *Mesos) DecodeTask(key string) cfg.Command {
var task cfg.Command
if key != "" {
err := json.NewDecoder(strings.NewReader(key)).Decode(&task)
if err != nil {
logrus.WithField("func", "scheduler.DecodeTask").Error("Could not decode task: ", err.Error())
return cfg.Command{}
}
return task
}
return cfg.Command{}
}
// GetOffer get out the offer for the mesos task
func (e *Mesos) GetOffer(offers *mesosproto.Event_Offers, cmd cfg.Command) (mesosproto.Offer, []mesosproto.OfferID) {
var offerIds []mesosproto.OfferID
var offerret mesosproto.Offer
for n, offer := range offers.Offers {
logrus.WithField("func", "mesos.GetOffer").Debug("Got Offer From:", offer.GetHostname())
offerIds = append(offerIds, offer.ID)
if cmd.TaskName == "" {
continue
}
// if the ressources of this offer does not matched what the command need, the skip
if !e.IsRessourceMatched(offer.Resources, cmd) {
logrus.WithField("func", "mesos.GetOffer").Debug("Could not found any matched ressources, get next offer")
e.Call(e.DeclineOffer(offerIds))
offerIds = nil
continue
}
offerret = offers.Offers[n]
offerIds = e.removeOffer(offerIds, offerret.ID.Value)
}
return offerret, offerIds
}
// remove the offer we took from the list
func (e *Mesos) removeOffer(offers []mesosproto.OfferID, clean string) []mesosproto.OfferID {
var offerIds []mesosproto.OfferID
for _, offer := range offers {
if offer.Value != clean {
offerIds = append(offerIds, offer)
}
}
return offerIds
}
// DeclineOffer will decline the given offers
func (e *Mesos) DeclineOffer(offerIds []mesosproto.OfferID) *mesosproto.Call {
logrus.WithField("func", "scheduler.HandleOffers").Debug("Offer Decline: ", offerIds)
refuseSeconds := 120.0
decline := &mesosproto.Call{
Type: mesosproto.Call_DECLINE,
Decline: &mesosproto.Call_Decline{OfferIDs: offerIds, Filters: &mesosproto.Filters{
RefuseSeconds: &refuseSeconds,
},
},
}
return decline
}
// IsRessourceMatched - check if the ressources of the offer are matching the needs of the cmd
// nolint:gocyclo
func (e *Mesos) IsRessourceMatched(ressource []mesosproto.Resource, cmd cfg.Command) bool {
mem := false
cpu := false
ports := true
for _, v := range ressource {
if v.GetName() == "cpus" && v.Scalar.GetValue() >= cmd.CPU {
logrus.WithField("func", "mesos.IsRessourceMatched").Debug("Matched Offer CPU")
cpu = true
}
if v.GetName() == "mem" && v.Scalar.GetValue() >= cmd.Memory {
logrus.WithField("func", "mesos.IsRessourceMatched").Debug("Matched Offer Memory")
mem = true
}
if len(cmd.DockerPortMappings) > 0 {
if v.GetName() == "ports" {
for _, taskPort := range cmd.DockerPortMappings {
for _, portRange := range v.GetRanges().Range {
if taskPort.HostPort >= uint32(portRange.Begin) && taskPort.HostPort <= uint32(portRange.End) {
logrus.WithField("func", "mesos.IsRessourceMatched").Debug("Matched Offer TaskPort: ", taskPort.HostPort)
logrus.WithField("func", "mesos.IsRessourceMatched").Debug("Matched Offer RangePort: ", portRange)
ports = ports || true
break
}
ports = ports || false
}
}
}
}
}
return mem && cpu && ports
}
// GetAgentInfo get information about the agent
func (e *Mesos) GetAgentInfo(agentID string) cfg.MesosSlaves {
client := &http.Client{}
// #nosec G402
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
protocol := "https"
if !e.Framework.MesosSSL {
protocol = "http"
}
req, _ := http.NewRequest("POST", protocol+"://"+e.Framework.MesosMasterServer+"/slaves/"+agentID, nil)
req.Close = true
req.SetBasicAuth(e.Framework.Username, e.Framework.Password)
req.Header.Set("Mesos-Stream-Id", e.Framework.MesosStreamID)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
logrus.WithField("func", "getAgentInfo").Error("Could not connect to agent: ", err.Error())
return cfg.MesosSlaves{}
}
if res.StatusCode == http.StatusOK {
defer res.Body.Close()
var agent cfg.MesosAgent
err = json.NewDecoder(res.Body).Decode(&agent)
if err != nil {
logrus.WithField("func", "getAgentInfo").Error("Could not encode json result: ", err.Error())
// if there is an error, dump out the res.Body as debug
bodyBytes, err := io.ReadAll(res.Body)
if err == nil {
logrus.WithField("func", "getAgentInfo").Debug("response Body Dump: ", string(bodyBytes))
}
return cfg.MesosSlaves{}
}
// get the used agent info
for _, a := range agent.Slaves {
if a.ID == agentID {
return a
}
}
}
return cfg.MesosSlaves{}
}
// GetNetworkInfo get network info of task
func (e *Mesos) GetNetworkInfo(taskID string) []mesosproto.NetworkInfo {
task := e.GetTaskInfo(taskID)
if len(task.Tasks) > 0 {
for _, status := range task.Tasks[0].Statuses {
if status.State == "TASK_RUNNING" {
var netw []mesosproto.NetworkInfo
netw = append(netw, status.ContainerStatus.NetworkInfos[0])
return netw
}
}
}
return []mesosproto.NetworkInfo{}
}
// GetTaskInfo get the task object to the given ID
func (e *Mesos) GetTaskInfo(taskID string) cfg.MesosTasks {
client := &http.Client{}
// #nosec G402
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
protocol := "https"
if !e.Framework.MesosSSL {
protocol = "http"
}
req, _ := http.NewRequest("POST", protocol+"://"+e.Framework.MesosMasterServer+"/tasks/?task_id="+taskID+"&framework_id="+e.Framework.FrameworkInfo.ID.GetValue(), nil)
req.Close = true
req.SetBasicAuth(e.Framework.Username, e.Framework.Password)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
logrus.WithField("func", "mesos.GetTaskInfo").Error("Could not connect to mesos-master: ", err.Error())
return cfg.MesosTasks{}
}
defer res.Body.Close()
var task cfg.MesosTasks
err = json.NewDecoder(res.Body).Decode(&task)
if err != nil {
logrus.WithField("func", "mesos.GetTaskInfo").Error("Could not encode json result: ", err.Error())
return cfg.MesosTasks{}
}
return task
}