-
Notifications
You must be signed in to change notification settings - Fork 31
/
app.go
171 lines (142 loc) · 4.18 KB
/
app.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
package apps
import (
"encoding/json"
"strings"
log "github.com/Sirupsen/logrus"
)
// Only Marathon apps with this label will be registered in Consul
const MarathonConsulLabel = "consul"
type HealthCheck struct {
Path string `json:"path"`
PortIndex int `json:"portIndex"`
Port int `json:"port"`
Protocol string `json:"protocol"`
GracePeriodSeconds int `json:"gracePeriodSeconds"`
IntervalSeconds int `json:"intervalSeconds"`
TimeoutSeconds int `json:"timeoutSeconds"`
MaxConsecutiveFailures int `json:"maxConsecutiveFailures"`
Command struct {
Value string `json:"value"`
}
}
type PortDefinition struct {
Labels map[string]string `json:"labels"`
}
type appWrapper struct {
App App `json:"app"`
}
type Apps struct {
Apps []*App `json:"apps"`
}
type App struct {
Labels map[string]string `json:"labels"`
HealthChecks []HealthCheck `json:"healthChecks"`
ID AppID `json:"id"`
Tasks []Task `json:"tasks"`
PortDefinitions []PortDefinition `json:"portDefinitions"`
}
// Marathon Application Id (aka PathId)
// Usually in the form of /rootGroup/subGroup/subSubGroup/name
// allowed characters: lowercase letters, digits, hyphens, slash
type AppID string
func (id AppID) String() string {
return string(id)
}
func (app *App) IsConsulApp() bool {
_, ok := app.Labels[MarathonConsulLabel]
return ok
}
func (app *App) labelsToRawName(labels map[string]string) string {
if value, ok := labels[MarathonConsulLabel]; ok && !isSpecialConsulNameValue(value) {
return value
}
return app.ID.String()
}
func isSpecialConsulNameValue(name string) bool {
return name == "true" || name == ""
}
func ParseApps(jsonBlob []byte) ([]*App, error) {
apps := &Apps{}
err := json.Unmarshal(jsonBlob, apps)
return apps.Apps, err
}
func ParseApp(jsonBlob []byte) (*App, error) {
wrapper := &appWrapper{}
err := json.Unmarshal(jsonBlob, wrapper)
return &wrapper.App, err
}
type RegistrationIntent struct {
Name string
Port int
Tags []string
}
func (app *App) RegistrationIntentsNumber() int {
if !app.IsConsulApp() {
return 0
}
definitions := app.findConsulPortDefinitions()
if len(definitions) == 0 {
return 1
}
return len(definitions)
}
func (app *App) RegistrationIntents(task *Task, nameSeparator string) []*RegistrationIntent {
commonTags := labelsToTags(app.Labels)
definitions := app.findConsulPortDefinitions()
if len(definitions) == 0 {
return []*RegistrationIntent{
{
Name: app.labelsToName(app.Labels, nameSeparator),
Port: task.Ports[0],
Tags: commonTags,
},
}
}
var intents []*RegistrationIntent
for _, d := range definitions {
intents = append(intents, &RegistrationIntent{
Name: app.labelsToName(d.Labels, nameSeparator),
Port: task.Ports[d.Index],
Tags: append(commonTags, labelsToTags(d.Labels)...),
})
}
return intents
}
func marathonAppNameToServiceName(name string, nameSeparator string) string {
return strings.Replace(strings.Trim(strings.TrimSpace(name), "/"), "/", nameSeparator, -1)
}
func labelsToTags(labels map[string]string) []string {
tags := []string{}
for key, value := range labels {
if value == "tag" {
tags = append(tags, key)
}
}
return tags
}
func (app *App) labelsToName(labels map[string]string, nameSeparator string) string {
appConsulName := app.labelsToRawName(labels)
serviceName := marathonAppNameToServiceName(appConsulName, nameSeparator)
if serviceName == "" {
log.WithField("AppId", app.ID.String()).WithField("ConsulServiceName", appConsulName).
Warn("Warning! Invalid Consul service name provided for app. Will use default app name instead.")
return marathonAppNameToServiceName(app.ID.String(), nameSeparator)
}
return serviceName
}
type indexedPortDefinition struct {
Index int
Labels map[string]string
}
func (app *App) findConsulPortDefinitions() []indexedPortDefinition {
var definitions []indexedPortDefinition
for i, d := range app.PortDefinitions {
if _, ok := d.Labels[MarathonConsulLabel]; ok {
definitions = append(definitions, indexedPortDefinition{
Index: i,
Labels: d.Labels,
})
}
}
return definitions
}