-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
executable file
·272 lines (226 loc) · 6.68 KB
/
main.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
// Copyright 2016 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"io/ioutil"
"os"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/amalgam8/sidecar/config"
"github.com/amalgam8/sidecar/register"
"github.com/amalgam8/sidecar/router/checker"
"github.com/amalgam8/sidecar/router/clients"
"github.com/amalgam8/sidecar/router/nginx"
"github.com/amalgam8/sidecar/supervisor"
"github.com/codegangsta/cli"
)
func main() {
// Initial logging until we parse the user provided log_level arg
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(os.Stderr)
app := cli.NewApp()
app.Name = "sidecar"
app.Usage = "Amalgam8 Sidecar"
app.Version = "0.1"
app.Flags = config.TenantFlags
app.Action = sidecarCommand
err := app.Run(os.Args)
if err != nil {
logrus.WithError(err).Error("Failure running main")
}
}
func sidecarCommand(context *cli.Context) {
conf := config.New(context)
if err := sidecarMain(*conf); err != nil {
logrus.WithError(err).Error("Setup failed")
}
}
func sidecarMain(conf config.Config) error {
var err error
logrus.SetLevel(conf.LogLevel)
if err = conf.Validate(false); err != nil {
logrus.WithError(err).Error("Validation of config failed")
return err
}
if conf.Log {
//Replace the LOGSTASH_REPLACEME string in filebeat.yml with
//the value provided by the user
//TODO: Make this configurable
filebeatConf := "/etc/filebeat/filebeat.yml"
filebeat, err := ioutil.ReadFile(filebeatConf)
if err != nil {
logrus.WithError(err).Error("Could not read filebeat conf")
return err
}
fileContents := strings.Replace(string(filebeat), "LOGSTASH_REPLACEME", conf.LogstashServer, -1)
err = ioutil.WriteFile("/tmp/filebeat.yml", []byte(fileContents), 0)
if err != nil {
logrus.WithError(err).Error("Could not write filebeat conf")
return err
}
// TODO: Log failure?
go supervisor.DoLogManagement("/tmp/filebeat.yml")
}
if conf.Proxy {
if err = startProxy(&conf); err != nil {
logrus.WithError(err).Error("Could not start proxy")
}
}
if conf.Register {
if err = conf.Validate(true); err != nil {
logrus.WithError(err).Error("Validation of config failed")
return err
}
logrus.Info("Registering")
register.DoServiceRegistrationAndHeartbeat(&conf, true)
}
if conf.Supervise {
supervisor.DoAppSupervision(conf.AppArgs)
} else {
select {}
}
return nil
}
func startProxy(conf *config.Config) error {
var err error
rc := clients.NewController(conf)
nginx := nginx.NewNginx(conf.ServiceName)
err = checkIn(rc, conf)
if err != nil {
logrus.WithError(err).Error("Check in failed")
return err
}
// for Kafka enabled tenants we should do both polling and listening
if len(conf.Kafka.Brokers) != 0 {
go func() {
time.Sleep(time.Second * 10)
logrus.Info("Attempting to connect to Kafka")
var consumer checker.Consumer
for {
consumer, err = checker.NewConsumer(checker.ConsumerConfig{
Brokers: conf.Kafka.Brokers,
Username: conf.Kafka.Username,
Password: conf.Kafka.Password,
ClientID: conf.Kafka.APIKey,
Topic: "NewRules",
SASLEnabled: conf.Kafka.SASL,
})
if err != nil {
logrus.WithError(err).Error("Could not connect to Kafka, trying again . . .")
time.Sleep(time.Second * 5) // TODO: exponential falloff?
} else {
break
}
}
logrus.Info("Successfully connected to Kafka")
listener := checker.NewListener(conf, consumer, rc, nginx)
// listen to Kafka indefinitely
if err := listener.Start(); err != nil {
logrus.WithError(err).Error("Could not listen to Kafka")
}
}()
}
poller := checker.NewPoller(conf, rc, nginx)
go func() {
if err = poller.Start(); err != nil {
logrus.WithError(err).Error("Could not poll Controller")
}
}()
return nil
}
func getCredentials(controller clients.Controller) (clients.TenantCredentials, error) {
for {
creds, err := controller.GetCredentials()
if err != nil {
if isRetryable(err) {
time.Sleep(time.Second * 5)
continue
} else {
return creds, err
}
}
return creds, err
}
}
func registerWithProxy(controller clients.Controller, confNotValidErr error) error {
if confNotValidErr != nil {
// Config not valid, can't register
logrus.WithError(confNotValidErr).Error("Validation of config failed")
return confNotValidErr
}
for {
err := controller.Register()
if err != nil {
if isRetryable(err) {
time.Sleep(time.Second * 5)
continue
} else {
return err
}
}
return err
}
}
func checkIn(controller clients.Controller, conf *config.Config) error {
confNotValidErr := conf.Validate(true)
creds, err := getCredentials(controller)
if err != nil {
// if id not found error
if _, ok := err.(*clients.TenantNotFoundError); ok {
logrus.Info("ID not found, registering with controller")
err = registerWithProxy(controller, confNotValidErr)
if err != nil {
// tenant already exists, possible race condition in container group
if _, ok = err.(*clients.ConflictError); ok {
logrus.Warn("Possible race condition occurred during register")
return nil
}
// unrecoverable error occurred registering with controller
logrus.WithError(err).Error("Could not register with Controller")
return err
}
// register succeeded
return nil
}
// unrecoverable error occurred getting credentials from controller
logrus.WithError(err).Error("Could not retrieve credentials")
return err
}
// if sidecar already has valid config do not need to set anything
if confNotValidErr != nil {
logrus.Info("Updating credentials with those from controller")
conf.Kafka.APIKey = creds.Kafka.APIKey
conf.Kafka.Brokers = creds.Kafka.Brokers
conf.Kafka.Password = creds.Kafka.Password
conf.Kafka.RestURL = creds.Kafka.RestURL
conf.Kafka.SASL = creds.Kafka.SASL
conf.Kafka.Username = creds.Kafka.User
conf.Registry.Token = creds.Registry.Token
conf.Registry.URL = creds.Registry.URL
}
return nil
}
func isRetryable(err error) bool {
if _, ok := err.(*clients.ConnectionError); ok {
return true
}
if _, ok := err.(*clients.NetworkError); ok {
return true
}
if _, ok := err.(*clients.ServiceUnavailable); ok {
return true
}
return false
}