forked from AVENTER-UG/traefik-mesos
/
mesos.go
333 lines (282 loc) · 9.65 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
package mesos
import (
"context"
cTls "crypto/tls"
"encoding/json"
"fmt"
"hash/fnv"
"io"
"net/http"
"strconv"
"strings"
"text/template"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/traefik/traefik/v3/pkg/config/dynamic"
"github.com/traefik/traefik/v3/pkg/job"
"github.com/traefik/traefik/v3/pkg/logs"
"github.com/traefik/traefik/v3/pkg/provider"
"github.com/traefik/traefik/v3/pkg/safe"
// Register mesos zoo the detector
_ "github.com/mesos/mesos-go/api/v0/detector/zoo"
)
// DefaultTemplateRule The default template for the default rule.
const DefaultTemplateRule = "Host(`{{ normalize .Name }}`)"
var (
_ provider.Provider = (*Provider)(nil)
)
// Provider holds configuration of the provider.
type Provider struct {
Endpoint string `Description:"Mesos server endpoint. You can also specify multiple endpoint for Mesos"`
SSL bool `Description:"Enable Endpoint SSL"`
Principal string `Description:"Principal to authorize agains Mesos Manager"`
Secret string `Description:"Secret authorize agains Mesos Manager"`
PollInterval time.Duration `Description:"Polling interval for endpoint." json:"pollInt"`
PollTimeout time.Duration `Description:"Polling timeout for endpoint." json:"pollTime"`
DefaultRule string `Description:"Default rule." json:"defaultRule,omitempty" toml:"defaultRule,omitempty" yaml:"defaultRule,omitempty"`
ForceUpdateInterval time.Duration `Description:"Interval to force an update."`
logger zerolog.Logger
mesosConfig map[string]*MesosTasks
defaultRuleTpl *template.Template
lastConfigurationHash uint64
lastUpdate time.Time
}
// SetDefaults sets the default values.
func (p *Provider) SetDefaults() {
p.Endpoint = "127.0.0.1:5050"
p.SSL = false
p.PollInterval = time.Duration(10 * time.Second)
p.PollTimeout = time.Duration(10 * time.Second)
p.DefaultRule = DefaultTemplateRule
p.ForceUpdateInterval = time.Duration(10 * time.Minute)
p.lastUpdate = time.Now()
}
// Init the provider.
func (p *Provider) Init() error {
defaultRuleTpl, err := provider.MakeDefaultRuleTemplate(p.DefaultRule, nil)
if err != nil {
return fmt.Errorf("error while parsing default rule: %w", err)
}
p.defaultRuleTpl = defaultRuleTpl
p.mesosConfig = make(map[string]*MesosTasks)
return nil
}
// Provide allows the mesos provider to provide configurations to traefik
// using the given configuration channel.
func (p *Provider) Provide(configurationChan chan<- dynamic.Message, pool *safe.Pool) error {
pool.GoCtx(func(routineCtx context.Context) {
p.logger = log.Ctx(routineCtx).With().Str(logs.ProviderName, "mesos").Logger()
ctxLog := p.logger.WithContext(routineCtx)
// Add protocoll to the endpoint depends if SSL is enabled
protocol := "http://" + p.Endpoint
if p.SSL {
protocol = "https://" + p.Endpoint
}
p.Endpoint = protocol
p.logger.Info().Msgf("Connect Mesos Provider to: ", p.Endpoint)
operation := func() error {
ctx, cancel := context.WithCancel(ctxLog)
defer cancel()
// load initial configuration
if err := p.loadConfiguration(ctx, configurationChan); err != nil {
return fmt.Errorf("failed to refresh mesos tasks: %w", err)
}
ticker := time.NewTicker(time.Duration(p.PollInterval))
defer ticker.Stop()
for {
select {
case <-routineCtx.Done():
return nil
case <-ticker.C:
}
if err := p.loadConfiguration(ctx, configurationChan); err != nil {
return fmt.Errorf("failed to refresh mesos tasks: %w", err)
}
}
}
notify := func(err error, time time.Duration) {
p.logger.Error().Msgf("Provider connection error %+v, retrying in %s", err, time)
}
err := backoff.RetryNotify(safe.OperationWithRecover(operation), job.NewBackOff(backoff.NewExponentialBackOff()), notify)
if err != nil {
p.logger.Error().Msgf("Cannot connect to Provider server: %+v", err)
}
})
return nil
}
func (p *Provider) loadConfiguration(ctx context.Context, configurationChan chan<- dynamic.Message) error {
tasks := p.getTasks()
// build hash to find out if the config changes
fnvHasher := fnv.New64()
tasksString, _ := json.Marshal(&tasks)
_, err := fnvHasher.Write(tasksString)
if err != nil {
p.logger.Error().Msgf("cannot hash mesos tasks data: ", err.Error())
return err
}
// check if the configuration has changed or the last update is 10 minutes ago
timeNow := time.Now()
timeDiff := timeNow.Sub(p.lastUpdate).Minutes()
hash := fnvHasher.Sum64()
if timeDiff >= p.ForceUpdateInterval.Minutes() {
p.logger.Info().Msgf("Force Update Traefik Config", timeDiff)
} else {
if hash == p.lastConfigurationHash {
p.logger.Debug().Msg("nothing to update.")
return nil
}
}
p.lastUpdate = timeNow
p.lastConfigurationHash = hash
p.mesosConfig = make(map[string]*MesosTasks)
// collect all mesos tasks and combine the belong one.
for _, task := range tasks.Tasks {
if task.State == "TASK_RUNNING" {
if task.Labels != nil {
if p.checkTraefikLabels(task) {
if p.checkContainer(task) {
containerName := task.ID
if p.mesosConfig[containerName] == nil {
p.mesosConfig[containerName] = &MesosTasks{}
}
p.mesosConfig[containerName].Tasks = append(p.mesosConfig[containerName].Tasks, task)
}
}
}
}
}
// build the treafik configuration
if len(p.mesosConfig) > 0 {
configuration := p.buildConfiguration(ctx)
if configuration != nil {
p.logger.Info().Msg("Update Traefik Config")
configurationChan <- dynamic.Message{
ProviderName: "mesos",
Configuration: configuration,
}
} else {
p.logger.Error().Msg("Build traefik config error")
}
}
// cleanup old data
p.mesosConfig = make(map[string]*MesosTasks)
return nil
}
func (p *Provider) checkTraefikLabels(task MesosTask) bool {
for _, label := range task.Labels {
if strings.Contains(label.Key, "traefik.") {
return true
}
}
return false
}
func (p *Provider) getTasks() MesosTasks {
client := &http.Client{}
client.Transport = &http.Transport{
TLSClientConfig: &cTls.Config{InsecureSkipVerify: true},
}
req, _ := http.NewRequest("GET", p.Endpoint+"/tasks?order=asc&limit=-1", nil)
req.Close = true
req.SetBasicAuth(p.Principal, p.Secret)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
p.logger.Error().Msgf("Error during get tasks: ", err.Error())
return MesosTasks{}
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
fmt.Errorf("received non-ok response code: %d", res.StatusCode)
return MesosTasks{}
}
p.logger.Debug().Msg("Get Data from Mesos")
var tasks MesosTasks
err = json.NewDecoder(res.Body).Decode(&tasks)
if err != nil {
p.logger.Error().Msg("Error in Data from Mesos: " + err.Error())
return MesosTasks{}
}
return tasks
}
func (p *Provider) checkContainer(task MesosTask) bool {
agentHostname, agentPort, err := p.getAgent(task.SlaveID)
if err != nil {
p.logger.Error().Msg("CheckContainer: Error in get AgentData from Mesos: " + err.Error())
return false
}
p.logger.Debug().Msg("CheckContainer: " + task.Name + " on agent (" + task.SlaveID + ")" + agentHostname + " with task.ID " + task.ID)
if agentHostname != "" {
containers, _ := p.getContainersOfAgent(agentHostname, agentPort)
for _, a := range containers {
p.logger.Debug().Msg(task.ID + " --CONTAINER-- " + a.ExecutorID)
if a.ExecutorID == task.ID {
return true
}
}
}
return false
}
func (p *Provider) getAgent(slaveID string) (string, int, error) {
client := &http.Client{}
client.Transport = &http.Transport{
TLSClientConfig: &cTls.Config{InsecureSkipVerify: true},
}
req, _ := http.NewRequest("GET", p.Endpoint+"/slaves/", nil)
req.Close = true
req.SetBasicAuth(p.Principal, p.Secret)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
p.logger.Error().Msgf("Error during get agent: ", err.Error())
return "", 0, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return "", 0, fmt.Errorf("received non-ok response code: %d", res.StatusCode)
}
data, err := io.ReadAll(res.Body)
var agents MesosAgent
if err := json.Unmarshal(data, &agents); err != nil {
p.logger.Error().Msg("getAgent: Error in AgentData from Mesos " + p.Endpoint + " with error: " + err.Error())
return "", 0, err
}
for _, a := range agents.Slaves {
if a.ID == slaveID {
return a.Hostname, a.Port, nil
}
}
return "", 0, nil
}
func (p *Provider) getContainersOfAgent(agentHostname string, agentPort int) (MesosAgentContainers, error) {
// Add protocoll to the endpoint depends if SSL is enabled
protocol := "http://"
if p.SSL {
protocol = "https://"
}
client := &http.Client{}
client.Transport = &http.Transport{
TLSClientConfig: &cTls.Config{InsecureSkipVerify: true},
}
req, _ := http.NewRequest("GET", protocol+agentHostname+":"+strconv.Itoa(agentPort)+"/containers/", nil)
req.Close = true
req.SetBasicAuth(p.Principal, p.Secret)
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
p.logger.Error().Msgf("Error during get container: ", err.Error())
return MesosAgentContainers{}, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return MesosAgentContainers{}, fmt.Errorf("received non-ok response code: %d", res.StatusCode)
}
data, err := io.ReadAll(res.Body)
var containers MesosAgentContainers
if err := json.Unmarshal(data, &containers); err != nil {
p.logger.Error().Msg("getContainersOfAgent: Error in ContainerAgentData from " + agentHostname + " " + err.Error())
return MesosAgentContainers{}, err
}
return containers, nil
}