/
executor_utils.go
235 lines (193 loc) · 6.17 KB
/
executor_utils.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/Nitro/sidecar-executor/container"
"github.com/fsouza/go-dockerclient"
log "github.com/sirupsen/logrus"
)
// copyLogs will copy the Docker container logs to stdout and stderr so we can
// capture some failure information in the Mesos logs. Then tooling can fetch
// crash info from the Mesos API.
func (exec *sidecarExecutor) copyLogs(containerId string) {
startTimeEpoch := time.Now().UTC().Add(0 - exec.config.LogsSince).Unix()
container.GetLogs(
exec.client, containerId, startTimeEpoch, os.Stdout, os.Stderr,
)
}
// handleContainerLogs will, if configured to do it, watch and relay container
// logs to syslog.
func (exec *sidecarExecutor) handleContainerLogs(containerId string,
labels map[string]string) {
if exec.config.RelaySyslog || exec.config.RelaySyslogStartupOnly {
var output io.Writer
if exec.config.ContainerLogsStdout {
output = os.Stdout
} else {
output = ioutil.Discard
}
exec.logsQuitChan = make(chan struct{})
go exec.relayLogs(exec.logsQuitChan, containerId, labels, output)
}
}
// getMasterHostname talks to the local worker endpoint and discovers the
// Mesos master hostname.
func (exec *sidecarExecutor) getMasterHostname() (string, error) {
envEndpoint := os.Getenv("MESOS_AGENT_ENDPOINT")
if len(envEndpoint) < 1 { // Did we get anything in the env var?
return "", fmt.Errorf("Can't get MESOS_AGENT_ENDPOINT from env! Won't provide Sidecar seeds.")
}
localEndpoint := "http://" + envEndpoint + "/state"
localStruct := struct {
MasterHostname string `json:"master_hostname"`
}{}
// Let's find out the Mesos master's hostname
resp, err := exec.fetcher.Get(localEndpoint)
if err != nil {
return "", fmt.Errorf("Unable to fetch Mesos master info from worker endpoint: %s", err)
}
if resp.Body != nil {
defer resp.Body.Close()
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("Error reading response body from Mesos worker! '%s'", err)
}
err = json.Unmarshal(body, &localStruct)
if err != nil {
return "", fmt.Errorf("Error parsing response body from Mesos worker! '%s'", err)
}
return localStruct.MasterHostname, nil
}
// getWorkerHostnames returns a slice of all the current worker hostnames
func (exec *sidecarExecutor) getWorkerHostnames(masterHostname string) ([]string, error) {
masterAddr := masterHostname
if exec.config.MesosMasterPort != "" {
masterAddr += ":" + exec.config.MesosMasterPort
}
masterEndpoint := "http://" + masterAddr + "/slaves"
type workersStruct struct {
Hostname string `json:"hostname"`
}
masterStruct := struct {
Slaves []workersStruct `json:"slaves"`
}{}
// Let's find out the Mesos master's hostname
resp, err := exec.fetcher.Get(masterEndpoint)
if err != nil {
return nil, fmt.Errorf("Unable to fetch info from master endpoint: %s", err)
}
if resp.Body != nil {
defer resp.Body.Close()
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Error reading response body from Mesos master! '%s'", err)
}
err = json.Unmarshal(body, &masterStruct)
if err != nil {
return nil, fmt.Errorf("Error parsing response body from Mesos master! '%s'", err)
}
var workers []string
for _, worker := range masterStruct.Slaves {
workers = append(workers, worker.Hostname)
}
return workers, nil
}
// addSidecarSeeds mutates the passed slice and inserts an env var formatted
// string (FOO=BAR_1) containing the list of Sidecar seeds that should be
// used to bootstrap a Sidecar instance.
func (exec *sidecarExecutor) addSidecarSeeds(envVars []string) []string {
masterHostname, err := exec.getMasterHostname()
if err != nil {
log.Error(err.Error())
return envVars
}
workerNames, err := exec.getWorkerHostnames(masterHostname)
if err != nil {
log.Error(err.Error())
return envVars
}
return append(envVars, "SIDECAR_SEEDS="+strings.Join(workerNames, ","))
}
// notifyDrain instructs Sidecar to set the current service's status to DRAINING
func (exec *sidecarExecutor) notifyDrain() {
// Check if draining is required
if !shouldCheckSidecar(exec.containerConfig) ||
exec.config.SidecarDrainingDuration == 0 {
return
}
// NB: Unfortunately, since exec.config.SidecarUrl points to
// `state.json`, we need to extract the Host from it first.
sidecarUrl, err := url.Parse(exec.config.SidecarUrl)
if err != nil {
log.Errorf("Error parsing Sidercar URL: %s", err)
return
}
if exec.containerID == "" {
log.Error("Attempted to drain service with empty container ID")
return
}
// URL.Host contains the port as well, if present
sidecarDrainServiceUrl := url.URL{
Scheme: sidecarUrl.Scheme,
Host: sidecarUrl.Host,
Path: fmt.Sprintf("/api/services/%s/drain", exec.containerID[:12]),
}
drainer := func() (int, error) {
resp, err := exec.fetcher.Post(sidecarDrainServiceUrl.String(), "", nil)
if err != nil {
return 0, err
}
defer resp.Body.Close()
return resp.StatusCode, nil
}
log.Warnf("Setting service ID %q status to DRAINING in Sidecar", exec.containerID[:12])
// Bridge the watcher waitgroup to a channel
watcherDoneChan := make(chan struct{})
go func() {
exec.watcherWg.Wait()
close(watcherDoneChan)
}()
RETRIES:
// Try several times to instruct Sidecar to set this service to DRAINING
for i := 0; i <= exec.config.SidecarRetryCount; i++ {
status, err := drainer()
if err == nil && status == 202 {
break
}
log.Warnf("Failed %d attempts to set service to DRAINING in Sidecar!", i+1)
select {
case <-watcherDoneChan:
break RETRIES
default:
}
time.Sleep(exec.config.SidecarRetryDelay)
}
ticker := time.NewTicker(exec.config.SidecarDrainingDuration)
defer ticker.Stop()
select {
case <-ticker.C:
// Finished waiting SidecarDrainingDuration
case <-watcherDoneChan:
// Bail out early if the watcher exits in the mean time
}
}
// Check if it should check Sidecar status, assuming enabled by default
func shouldCheckSidecar(containerConfig *docker.CreateContainerOptions) bool {
value, ok := containerConfig.Config.Labels["SidecarDiscover"]
if !ok {
return true
}
if enabled, err := strconv.ParseBool(value); err == nil {
return enabled
}
return true
}