forked from traefik/traefik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprecated_config.go
327 lines (285 loc) · 9.52 KB
/
deprecated_config.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
package mesos
import (
"fmt"
"math"
"strconv"
"strings"
"text/template"
"github.com/BurntSushi/ty/fun"
"github.com/containous/traefik/log"
"github.com/containous/traefik/provider"
"github.com/containous/traefik/provider/label"
"github.com/containous/traefik/types"
"github.com/mesosphere/mesos-dns/records/state"
)
func (p *Provider) buildConfigurationV1(tasks []state.Task) *types.Configuration {
var mesosFuncMap = template.FuncMap{
"getDomain": getFuncStringValueV1(label.TraefikDomain, p.Domain),
"getID": getIDV1,
// Backend functions
"getBackendName": getBackendNameV1,
"getHost": p.getHostV1,
"getProtocol": getFuncApplicationStringValueV1(label.TraefikProtocol, label.DefaultProtocol),
"getWeight": getFuncApplicationIntValueV1(label.TraefikWeight, label.DefaultWeightInt),
"getBackend": getBackendV1,
"getPort": p.getPort,
// Frontend functions
"getFrontendBackend": getBackendNameV1,
"getFrontEndName": getFrontendNameV1,
"getEntryPoints": getFuncSliceStringValueV1(label.TraefikFrontendEntryPoints),
"getBasicAuth": getFuncSliceStringValueV1(label.TraefikFrontendAuthBasic),
"getPriority": getFuncIntValueV1(label.TraefikFrontendPriority, label.DefaultFrontendPriorityInt),
"getPassHostHeader": getFuncBoolValueV1(label.TraefikFrontendPassHostHeader, label.DefaultPassHostHeaderBool),
"getFrontendRule": p.getFrontendRuleV1,
}
// filter tasks
filteredTasks := fun.Filter(func(task state.Task) bool {
return taskFilterV1(task, p.ExposedByDefault)
}, tasks).([]state.Task)
// Deprecated
var filteredApps []state.Task
uniqueApps := make(map[string]struct{})
for _, task := range filteredTasks {
if _, ok := uniqueApps[task.DiscoveryInfo.Name]; !ok {
uniqueApps[task.DiscoveryInfo.Name] = struct{}{}
filteredApps = append(filteredApps, task)
}
}
appsTasks := make(map[string][]state.Task)
for _, task := range filteredTasks {
if _, ok := appsTasks[task.DiscoveryInfo.Name]; !ok {
appsTasks[task.DiscoveryInfo.Name] = []state.Task{task}
} else {
appsTasks[task.DiscoveryInfo.Name] = append(appsTasks[task.DiscoveryInfo.Name], task)
}
}
templateObjects := struct {
ApplicationsTasks map[string][]state.Task
Applications []state.Task // Deprecated
Tasks []state.Task // Deprecated
Domain string
}{
ApplicationsTasks: appsTasks,
Applications: filteredApps, // Deprecated
Tasks: filteredTasks, // Deprecated
Domain: p.Domain,
}
configuration, err := p.GetConfiguration("templates/mesos-v1.tmpl", mesosFuncMap, templateObjects)
if err != nil {
log.Error(err)
}
return configuration
}
// Deprecated
func taskFilterV1(task state.Task, exposedByDefaultFlag bool) bool {
if len(task.DiscoveryInfo.Ports.DiscoveryPorts) == 0 {
log.Debugf("Filtering Mesos task without port %s", task.Name)
return false
}
if !isEnabledV1(task, exposedByDefaultFlag) {
log.Debugf("Filtering disabled Mesos task %s", task.DiscoveryInfo.Name)
return false
}
// filter indeterminable task port
portIndexLabel := getStringValueV1(task, label.TraefikPortIndex, "")
portValueLabel := getStringValueV1(task, label.TraefikPort, "")
if portIndexLabel != "" && portValueLabel != "" {
log.Debugf("Filtering Mesos task %s specifying both %q' and %q labels", task.Name, label.TraefikPortIndex, label.TraefikPort)
return false
}
if portIndexLabel != "" {
index, err := strconv.Atoi(portIndexLabel)
if err != nil || index < 0 || index > len(task.DiscoveryInfo.Ports.DiscoveryPorts)-1 {
log.Debugf("Filtering Mesos task %s with unexpected value for %q label", task.Name, label.TraefikPortIndex)
return false
}
}
if portValueLabel != "" {
port, err := strconv.Atoi(portValueLabel)
if err != nil {
log.Debugf("Filtering Mesos task %s with unexpected value for %q label", task.Name, label.TraefikPort)
return false
}
var foundPort bool
for _, exposedPort := range task.DiscoveryInfo.Ports.DiscoveryPorts {
if port == exposedPort.Number {
foundPort = true
break
}
}
if !foundPort {
log.Debugf("Filtering Mesos task %s without a matching port for %q label", task.Name, label.TraefikPort)
return false
}
}
// filter healthChecks
if task.Statuses != nil && len(task.Statuses) > 0 && task.Statuses[0].Healthy != nil && !*task.Statuses[0].Healthy {
log.Debugf("Filtering Mesos task %s with bad healthCheck", task.DiscoveryInfo.Name)
return false
}
return true
}
// Deprecated
func getIDV1(task state.Task) string {
return provider.Normalize(task.ID)
}
// Deprecated
func getBackendV1(task state.Task, apps []state.Task) string {
_, err := getApplicationV1(task, apps)
if err != nil {
log.Error(err)
return ""
}
return getBackendNameV1(task)
}
// Deprecated
func getBackendNameV1(task state.Task) string {
if value := getStringValueV1(task, label.TraefikBackend, ""); len(value) > 0 {
return value
}
return provider.Normalize(task.DiscoveryInfo.Name)
}
// Deprecated
func getFrontendNameV1(task state.Task) string {
// TODO task.ID -> task.Name + task.ID
return provider.Normalize(task.ID)
}
// Deprecated
func (p *Provider) getPort(task state.Task, applications []state.Task) string {
_, err := getApplicationV1(task, applications)
if err != nil {
log.Error(err)
return ""
}
plv := getIntValueV1(task, label.TraefikPortIndex, math.MinInt32, len(task.DiscoveryInfo.Ports.DiscoveryPorts)-1)
if plv >= 0 {
return strconv.Itoa(task.DiscoveryInfo.Ports.DiscoveryPorts[plv].Number)
}
if pv := getStringValueV1(task, label.TraefikPort, ""); len(pv) > 0 {
return pv
}
for _, port := range task.DiscoveryInfo.Ports.DiscoveryPorts {
return strconv.Itoa(port.Number)
}
return ""
}
// getFrontendRuleV1 returns the frontend rule for the specified application, using
// it's label. It returns a default one (Host) if the label is not present.
// Deprecated
func (p *Provider) getFrontendRuleV1(task state.Task) string {
if v := getStringValueV1(task, label.TraefikFrontendRule, ""); len(v) > 0 {
return v
}
return "Host:" + strings.ToLower(strings.Replace(p.getSubDomain(task.DiscoveryInfo.Name), "_", "-", -1)) + "." + p.Domain
}
// Deprecated
func (p *Provider) getHostV1(task state.Task) string {
return task.IP(strings.Split(p.IPSources, ",")...)
}
// Deprecated
func isEnabledV1(task state.Task, exposedByDefault bool) bool {
return getBoolValueV1(task, label.TraefikEnable, exposedByDefault)
}
// Label functions
// Deprecated
func getFuncApplicationStringValueV1(labelName string, defaultValue string) func(task state.Task, applications []state.Task) string {
return func(task state.Task, applications []state.Task) string {
_, err := getApplicationV1(task, applications)
if err != nil {
log.Error(err)
return defaultValue
}
return getStringValueV1(task, labelName, defaultValue)
}
}
// Deprecated
func getFuncApplicationIntValueV1(labelName string, defaultValue int) func(task state.Task, applications []state.Task) int {
return func(task state.Task, applications []state.Task) int {
_, err := getApplicationV1(task, applications)
if err != nil {
log.Error(err)
return defaultValue
}
return getIntValueV1(task, labelName, defaultValue, math.MaxInt32)
}
}
// Deprecated
func getFuncStringValueV1(labelName string, defaultValue string) func(task state.Task) string {
return func(task state.Task) string {
return getStringValueV1(task, labelName, defaultValue)
}
}
// Deprecated
func getFuncBoolValueV1(labelName string, defaultValue bool) func(task state.Task) bool {
return func(task state.Task) bool {
return getBoolValueV1(task, labelName, defaultValue)
}
}
// Deprecated
func getFuncIntValueV1(labelName string, defaultValue int) func(task state.Task) int {
return func(task state.Task) int {
return getIntValueV1(task, labelName, defaultValue, math.MaxInt32)
}
}
// Deprecated
func getFuncSliceStringValueV1(labelName string) func(task state.Task) []string {
return func(task state.Task) []string {
return getSliceStringValueV1(task, labelName)
}
}
// Deprecated
func getStringValueV1(task state.Task, labelName string, defaultValue string) string {
for _, lbl := range task.Labels {
if lbl.Key == labelName && len(lbl.Value) > 0 {
return lbl.Value
}
}
return defaultValue
}
// Deprecated
func getBoolValueV1(task state.Task, labelName string, defaultValue bool) bool {
for _, lbl := range task.Labels {
if lbl.Key == labelName {
v, err := strconv.ParseBool(lbl.Value)
if err == nil {
return v
}
}
}
return defaultValue
}
// Deprecated
func getIntValueV1(task state.Task, labelName string, defaultValue int, maxValue int) int {
for _, lbl := range task.Labels {
if lbl.Key == labelName {
value, err := strconv.Atoi(lbl.Value)
if err == nil {
if value <= maxValue {
return value
}
log.Warnf("The value %q for %q exceed the max authorized value %q, falling back to %v.", lbl.Value, labelName, maxValue, defaultValue)
} else {
log.Warnf("Unable to parse %q: %q, falling back to %v. %v", labelName, lbl.Value, defaultValue, err)
}
}
}
return defaultValue
}
// Deprecated
func getSliceStringValueV1(task state.Task, labelName string) []string {
for _, lbl := range task.Labels {
if lbl.Key == labelName {
return label.SplitAndTrimString(lbl.Value, ",")
}
}
return nil
}
// Deprecated
func getApplicationV1(task state.Task, apps []state.Task) (state.Task, error) {
for _, app := range apps {
if app.DiscoveryInfo.Name == task.DiscoveryInfo.Name {
return app, nil
}
}
return state.Task{}, fmt.Errorf("unable to get Mesos application from task %s", task.DiscoveryInfo.Name)
}