traefik/traefik.go

371 lines
12 KiB
Go
Raw Normal View History

2015-09-11 14:37:13 +00:00
package main
import (
2015-10-19 21:29:25 +00:00
"crypto/tls"
"errors"
fmtlog "log"
"net/http"
"net/url"
"os"
"os/signal"
"reflect"
"runtime"
"strings"
"syscall"
"time"
2015-09-12 13:10:03 +00:00
"github.com/BurntSushi/toml"
log "github.com/Sirupsen/logrus"
2015-09-12 13:10:03 +00:00
"github.com/codegangsta/negroni"
"github.com/emilevauge/traefik/middlewares"
"github.com/emilevauge/traefik/provider"
"github.com/emilevauge/traefik/types"
2015-09-11 14:37:13 +00:00
"github.com/gorilla/mux"
2015-09-25 22:20:45 +00:00
"github.com/mailgun/manners"
"github.com/mailgun/oxy/cbreaker"
2015-09-11 14:37:13 +00:00
"github.com/mailgun/oxy/forward"
"github.com/mailgun/oxy/roundrobin"
2015-09-12 13:10:03 +00:00
"github.com/thoas/stats"
"gopkg.in/alecthomas/kingpin.v2"
2015-09-11 14:37:13 +00:00
)
2015-09-11 23:55:10 +00:00
var (
globalConfigFile = kingpin.Arg("conf", "Main configration file.").Default("traefik.toml").String()
2015-10-14 20:04:56 +00:00
version = kingpin.Flag("version", "Get Version.").Short('v').Bool()
currentConfigurations = make(configs)
metrics = stats.New()
oxyLogger = &OxyLogger{}
2015-09-11 23:55:10 +00:00
)
2015-09-11 14:37:13 +00:00
func main() {
2015-09-25 22:20:45 +00:00
runtime.GOMAXPROCS(runtime.NumCPU())
2015-10-14 20:04:56 +00:00
kingpin.Version(Version + " built on the " + BuildDate)
2015-09-11 23:55:10 +00:00
kingpin.Parse()
2015-09-19 11:02:59 +00:00
fmtlog.SetFlags(fmtlog.Lshortfile | fmtlog.LstdFlags)
2015-09-25 22:20:45 +00:00
var srv *manners.GracefulServer
2015-09-11 14:37:13 +00:00
var configurationRouter *mux.Router
var configurationChan = make(chan types.ConfigMessage, 10)
2015-09-16 17:08:01 +00:00
defer close(configurationChan)
var configurationChanValidated = make(chan types.ConfigMessage, 10)
defer close(configurationChanValidated)
2015-09-11 14:37:13 +00:00
var sigs = make(chan os.Signal, 1)
2015-09-25 20:02:55 +00:00
defer close(sigs)
2015-09-25 22:20:45 +00:00
var stopChan = make(chan bool)
defer close(stopChan)
var providers = []provider.Provider{}
2015-09-11 14:37:13 +00:00
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// load global configuration
globalConfiguration := LoadFileConfig(*globalConfigFile)
2015-09-11 14:37:13 +00:00
loggerMiddleware := middlewares.NewLogger(globalConfiguration.AccessLogsFile)
2015-09-12 13:20:56 +00:00
defer loggerMiddleware.Close()
2015-09-11 14:37:13 +00:00
// logging
level, err := log.ParseLevel(strings.ToLower(globalConfiguration.LogLevel))
2015-09-11 14:37:13 +00:00
if err != nil {
log.Fatal("Error getting level", err)
}
log.SetLevel(level)
2015-09-11 14:48:52 +00:00
if len(globalConfiguration.TraefikLogsFile) > 0 {
fi, err := os.OpenFile(globalConfiguration.TraefikLogsFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
2015-09-12 13:20:56 +00:00
defer fi.Close()
2015-09-11 14:37:13 +00:00
if err != nil {
log.Fatal("Error opening file", err)
2015-09-12 13:10:03 +00:00
} else {
log.SetOutput(fi)
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true, DisableSorting: true})
2015-09-11 14:37:13 +00:00
}
} else {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true, DisableSorting: true})
2015-09-11 14:37:13 +00:00
}
log.Debugf("Global configuration loaded %+v", globalConfiguration)
configurationRouter = LoadDefaultConfig(globalConfiguration)
2015-09-11 14:37:13 +00:00
// listen new configurations from providers
go func() {
lastReceivedConfiguration := time.Unix(0, 0)
lastConfigs := make(map[string]*types.ConfigMessage)
2015-09-11 14:37:13 +00:00
for {
configMsg := <-configurationChan
log.Infof("Configuration receveived from provider %s: %#v", configMsg.ProviderName, configMsg.Configuration)
lastConfigs[configMsg.ProviderName] = &configMsg
if time.Now().After(lastReceivedConfiguration.Add(time.Duration(globalConfiguration.ProvidersThrottleDuration))) {
log.Infof("Last %s config received more than %s, OK", configMsg.ProviderName, globalConfiguration.ProvidersThrottleDuration)
// last config received more than n s ago
configurationChanValidated <- configMsg
} else {
log.Infof("Last %s config received less than %s, waiting...", configMsg.ProviderName, globalConfiguration.ProvidersThrottleDuration)
go func() {
<-time.After(globalConfiguration.ProvidersThrottleDuration)
if time.Now().After(lastReceivedConfiguration.Add(time.Duration(globalConfiguration.ProvidersThrottleDuration))) {
log.Infof("Waited for %s config, OK", configMsg.ProviderName)
configurationChanValidated <- *lastConfigs[configMsg.ProviderName]
}
}()
}
lastReceivedConfiguration = time.Now()
}
}()
go func() {
for {
configMsg := <-configurationChanValidated
if configMsg.Configuration == nil {
log.Info("Skipping empty Configuration")
} else if reflect.DeepEqual(currentConfigurations[configMsg.ProviderName], configMsg.Configuration) {
2015-09-11 14:37:13 +00:00
log.Info("Skipping same configuration")
} else {
// Copy configurations to new map so we don't change current if LoadConfig fails
newConfigurations := make(configs)
for k, v := range currentConfigurations {
newConfigurations[k] = v
}
newConfigurations[configMsg.ProviderName] = configMsg.Configuration
newConfigurationRouter, err := LoadConfig(newConfigurations, globalConfiguration)
if err == nil {
currentConfigurations = newConfigurations
2015-09-19 11:02:59 +00:00
configurationRouter = newConfigurationRouter
2015-09-25 20:02:55 +00:00
oldServer := srv
2015-10-19 21:29:25 +00:00
newsrv, err := prepareServer(configurationRouter, globalConfiguration, oldServer, loggerMiddleware, metrics)
if err != nil {
log.Fatal("Error preparing server: ", err)
}
2015-09-25 22:20:45 +00:00
go startServer(newsrv, globalConfiguration)
srv = newsrv
time.Sleep(1 * time.Second)
2015-09-25 22:20:45 +00:00
if oldServer != nil {
log.Info("Stopping old server")
oldServer.Close()
}
} else {
2015-09-19 11:02:59 +00:00
log.Error("Error loading new configuration, aborted ", err)
}
2015-09-11 14:37:13 +00:00
}
}
}()
// configure providers
if globalConfiguration.Docker != nil {
providers = append(providers, globalConfiguration.Docker)
2015-09-11 14:37:13 +00:00
}
if globalConfiguration.Marathon != nil {
providers = append(providers, globalConfiguration.Marathon)
2015-09-11 14:37:13 +00:00
}
if globalConfiguration.File != nil {
if len(globalConfiguration.File.Filename) == 0 {
2015-09-11 14:37:13 +00:00
// no filename, setting to global config file
globalConfiguration.File.Filename = *globalConfigFile
2015-09-11 14:37:13 +00:00
}
providers = append(providers, globalConfiguration.File)
2015-09-11 14:37:13 +00:00
}
if globalConfiguration.Web != nil {
providers = append(providers, globalConfiguration.Web)
2015-09-11 14:37:13 +00:00
}
if globalConfiguration.Consul != nil {
providers = append(providers, globalConfiguration.Consul)
2015-09-21 16:05:56 +00:00
}
if globalConfiguration.Etcd != nil {
providers = append(providers, globalConfiguration.Etcd)
}
if globalConfiguration.Zookeeper != nil {
providers = append(providers, globalConfiguration.Zookeeper)
}
if globalConfiguration.Boltdb != nil {
providers = append(providers, globalConfiguration.Boltdb)
}
2015-09-11 14:37:13 +00:00
// start providers
for _, provider := range providers {
log.Infof("Starting provider %v %+v", reflect.TypeOf(provider), provider)
2015-09-11 14:37:13 +00:00
currentProvider := provider
go func() {
err := currentProvider.Provide(configurationChan)
if err != nil {
log.Errorf("Error starting provider %s", err)
}
2015-09-11 14:37:13 +00:00
}()
}
go func() {
sig := <-sigs
log.Infof("I have to go... %+v", sig)
2015-09-25 22:20:45 +00:00
log.Info("Stopping server")
srv.Close()
stopChan <- true
2015-09-11 14:37:13 +00:00
}()
2015-09-25 20:02:55 +00:00
//negroni.Use(middlewares.NewCircuitBreaker(oxyLogger))
//negroni.Use(middlewares.NewRoutes(configurationRouter))
2015-10-19 21:29:25 +00:00
var er error
srv, er = prepareServer(configurationRouter, globalConfiguration, nil, loggerMiddleware, metrics)
if er != nil {
log.Fatal("Error preparing server: ", er)
}
2015-09-25 22:20:45 +00:00
go startServer(srv, globalConfiguration)
2015-09-12 13:10:03 +00:00
2015-09-25 22:20:45 +00:00
<-stopChan
log.Info("Shutting down")
2015-09-11 14:37:13 +00:00
}
2015-10-19 21:29:25 +00:00
func createTLSConfig(certFile string, keyFile string) (*tls.Config, error) {
config := &tls.Config{}
if config.NextProtos == nil {
config.NextProtos = []string{"http/1.1"}
}
var err error
config.Certificates = make([]tls.Certificate, 1)
if len(certFile) > 0 && len(keyFile) > 0 {
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
} else {
return nil, nil
}
return config, nil
}
2015-09-25 22:20:45 +00:00
func startServer(srv *manners.GracefulServer, globalConfiguration *GlobalConfiguration) {
log.Info("Starting server")
2015-09-25 20:02:55 +00:00
if len(globalConfiguration.CertFile) > 0 && len(globalConfiguration.KeyFile) > 0 {
err := srv.ListenAndServeTLS(globalConfiguration.CertFile, globalConfiguration.KeyFile)
if err != nil {
2015-10-19 21:29:25 +00:00
log.Fatal("Error creating server: ", err)
2015-09-25 20:02:55 +00:00
}
} else {
err := srv.ListenAndServe()
if err != nil {
2015-10-19 21:29:25 +00:00
log.Fatal("Error creating server: ", err)
2015-09-25 20:02:55 +00:00
}
}
2015-09-25 22:20:45 +00:00
log.Info("Server stopped")
2015-09-25 20:02:55 +00:00
}
2015-10-19 21:29:25 +00:00
func prepareServer(router *mux.Router, globalConfiguration *GlobalConfiguration, oldServer *manners.GracefulServer, middlewares ...negroni.Handler) (*manners.GracefulServer, error) {
2015-09-25 22:20:45 +00:00
log.Info("Preparing server")
2015-09-25 20:02:55 +00:00
// middlewares
var negroni = negroni.New()
for _, middleware := range middlewares {
negroni.Use(middleware)
}
negroni.UseHandler(router)
2015-10-19 21:29:25 +00:00
tlsConfig, err := createTLSConfig(globalConfiguration.CertFile, globalConfiguration.KeyFile)
if err != nil {
log.Fatalf("Error creating TLS config %s", err)
return nil, err
}
2015-09-25 20:02:55 +00:00
2015-09-25 22:20:45 +00:00
if oldServer == nil {
return manners.NewWithServer(
&http.Server{
2015-10-19 21:29:25 +00:00
Addr: globalConfiguration.Port,
Handler: negroni,
TLSConfig: tlsConfig,
}), nil
2015-09-25 22:20:45 +00:00
} else {
server, err := oldServer.HijackListener(&http.Server{
2015-09-25 20:02:55 +00:00
Addr: globalConfiguration.Port,
Handler: negroni,
2015-10-19 21:29:25 +00:00
}, tlsConfig)
2015-09-25 22:20:45 +00:00
if err != nil {
log.Fatalf("Error hijacking server %s", err)
2015-10-19 21:29:25 +00:00
return nil, err
2015-09-25 22:20:45 +00:00
} else {
2015-10-19 21:29:25 +00:00
return server, nil
2015-09-25 22:20:45 +00:00
}
2015-09-25 20:02:55 +00:00
}
}
func LoadConfig(configurations configs, globalConfiguration *GlobalConfiguration) (*mux.Router, error) {
2015-09-11 14:37:13 +00:00
router := mux.NewRouter()
2015-09-12 13:10:03 +00:00
router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
2015-09-11 14:37:13 +00:00
backends := map[string]http.Handler{}
for _, configuration := range configurations {
for frontendName, frontend := range configuration.Frontends {
log.Debugf("Creating frontend %s", frontendName)
fwd, _ := forward.New(forward.Logger(oxyLogger), forward.PassHostHeader(frontend.PassHostHeader))
newRoute := router.NewRoute().Name(frontendName)
for routeName, route := range frontend.Routes {
log.Debugf("Creating route %s %s:%s", routeName, route.Rule, route.Value)
newRouteReflect := Invoke(newRoute, route.Rule, route.Value)
newRoute = newRouteReflect[0].Interface().(*mux.Route)
}
if backends[frontend.Backend] == nil {
log.Debugf("Creating backend %s", frontend.Backend)
var lb http.Handler
rr, _ := roundrobin.New(fwd)
if configuration.Backends[frontend.Backend] == nil {
return nil, errors.New("Backend not found: " + frontend.Backend)
}
lbMethod, err := types.NewLoadBalancerMethod(configuration.Backends[frontend.Backend].LoadBalancer)
if err != nil {
configuration.Backends[frontend.Backend].LoadBalancer = &types.LoadBalancer{Method: "wrr"}
}
switch lbMethod {
case types.Drr:
log.Infof("Creating load-balancer drr")
rebalancer, _ := roundrobin.NewRebalancer(rr, roundrobin.RebalancerLogger(oxyLogger))
lb = rebalancer
for serverName, server := range configuration.Backends[frontend.Backend].Servers {
url, err := url.Parse(server.URL)
if err != nil {
return nil, err
}
log.Infof("Creating server %s %s", serverName, url.String())
rebalancer.UpsertServer(url, roundrobin.Weight(server.Weight))
}
case types.Wrr:
log.Infof("Creating load-balancer wrr")
lb = middlewares.NewWebsocketUpgrader(rr)
for serverName, server := range configuration.Backends[frontend.Backend].Servers {
url, err := url.Parse(server.URL)
if err != nil {
return nil, err
}
log.Infof("Creating server %s %s", serverName, url.String())
rr.UpsertServer(url, roundrobin.Weight(server.Weight))
}
2015-09-19 11:02:59 +00:00
}
var negroni = negroni.New()
if configuration.Backends[frontend.Backend].CircuitBreaker != nil {
log.Infof("Creating circuit breaker %s", configuration.Backends[frontend.Backend].CircuitBreaker.Expression)
negroni.Use(middlewares.NewCircuitBreaker(lb, configuration.Backends[frontend.Backend].CircuitBreaker.Expression, cbreaker.Logger(oxyLogger)))
} else {
negroni.UseHandler(lb)
}
backends[frontend.Backend] = negroni
} else {
log.Infof("Reusing backend %s", frontend.Backend)
}
// stream.New(backends[frontend.Backend], stream.Retry("IsNetworkError() && Attempts() <= " + strconv.Itoa(globalConfiguration.Replay)), stream.Logger(oxyLogger))
newRoute.Handler(backends[frontend.Backend])
err := newRoute.GetError()
if err != nil {
log.Error("Error building route: %s", err)
}
2015-09-11 14:37:13 +00:00
}
}
2015-09-19 11:02:59 +00:00
return router, nil
2015-09-11 14:37:13 +00:00
}
func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value {
inputs := make([]reflect.Value, len(args))
for i := range args {
2015-09-11 14:37:13 +00:00
inputs[i] = reflect.ValueOf(args[i])
}
return reflect.ValueOf(any).MethodByName(name).Call(inputs)
}
func LoadFileConfig(file string) *GlobalConfiguration {
configuration := NewGlobalConfiguration()
if _, err := toml.DecodeFile(file, configuration); err != nil {
fmtlog.Fatalf("Error reading file: %s", err)
2015-09-11 14:37:13 +00:00
}
return configuration
}