2018-01-29 14:58:03 +01:00
package configuration
import (
"encoding/json"
"github.com/containous/traefik/log"
"github.com/containous/traefik/provider"
"github.com/containous/traefik/safe"
"github.com/containous/traefik/types"
)
2018-04-24 22:40:04 +02:00
// ProviderAggregator aggregate providers
type ProviderAggregator struct {
2018-07-11 09:08:03 +02:00
providers [ ] provider . Provider
constraints types . Constraints
2018-01-29 14:58:03 +01:00
}
// NewProviderAggregator return an aggregate of all the providers configured in GlobalConfiguration
2018-04-24 22:40:04 +02:00
func NewProviderAggregator ( gc * GlobalConfiguration ) ProviderAggregator {
2018-07-11 09:08:03 +02:00
provider := ProviderAggregator {
constraints : gc . Constraints ,
}
2018-01-29 14:58:03 +01:00
if gc . Docker != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Docker )
2018-01-29 14:58:03 +01:00
}
if gc . Marathon != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Marathon )
2018-01-29 14:58:03 +01:00
}
if gc . File != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . File )
2018-01-29 14:58:03 +01:00
}
if gc . Rest != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Rest )
2018-01-29 14:58:03 +01:00
}
if gc . Consul != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Consul )
2018-01-29 14:58:03 +01:00
}
if gc . ConsulCatalog != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . ConsulCatalog )
2018-01-29 14:58:03 +01:00
}
if gc . Etcd != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Etcd )
2018-01-29 14:58:03 +01:00
}
if gc . Zookeeper != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Zookeeper )
2018-01-29 14:58:03 +01:00
}
if gc . Boltdb != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Boltdb )
2018-01-29 14:58:03 +01:00
}
if gc . Kubernetes != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Kubernetes )
2018-01-29 14:58:03 +01:00
}
if gc . Mesos != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Mesos )
2018-01-29 14:58:03 +01:00
}
if gc . Eureka != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Eureka )
2018-01-29 14:58:03 +01:00
}
if gc . ECS != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . ECS )
2018-01-29 14:58:03 +01:00
}
if gc . Rancher != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . Rancher )
2018-01-29 14:58:03 +01:00
}
if gc . DynamoDB != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . DynamoDB )
2018-01-29 14:58:03 +01:00
}
if gc . ServiceFabric != nil {
2018-07-11 09:08:03 +02:00
provider . quietAddProvider ( gc . ServiceFabric )
2018-01-29 14:58:03 +01:00
}
return provider
}
2018-07-11 09:08:03 +02:00
func ( p * ProviderAggregator ) quietAddProvider ( provider provider . Provider ) {
err := p . AddProvider ( provider )
if err != nil {
log . Errorf ( "Error initializing provider %T: %v" , provider , err )
}
}
2018-04-24 22:40:04 +02:00
// AddProvider add a provider in the providers map
2018-07-11 09:08:03 +02:00
func ( p * ProviderAggregator ) AddProvider ( provider provider . Provider ) error {
err := provider . Init ( p . constraints )
if err != nil {
return err
}
2018-04-24 22:40:04 +02:00
p . providers = append ( p . providers , provider )
2018-07-11 09:08:03 +02:00
return nil
}
// Init the provider
func ( p ProviderAggregator ) Init ( _ types . Constraints ) error {
return nil
2018-04-24 22:40:04 +02:00
}
// Provide call the provide method of every providers
2018-07-11 09:08:03 +02:00
func ( p ProviderAggregator ) Provide ( configurationChan chan <- types . ConfigMessage , pool * safe . Pool ) error {
2018-01-29 14:58:03 +01:00
for _ , p := range p . providers {
jsonConf , err := json . Marshal ( p )
if err != nil {
2018-07-11 09:08:03 +02:00
log . Debugf ( "Unable to marshal provider conf %T with error: %v" , p , err )
2018-01-29 14:58:03 +01:00
}
2018-07-11 09:08:03 +02:00
log . Infof ( "Starting provider %T %s" , p , jsonConf )
2018-01-29 14:58:03 +01:00
currentProvider := p
safe . Go ( func ( ) {
2018-07-11 09:08:03 +02:00
err := currentProvider . Provide ( configurationChan , pool )
2018-01-29 14:58:03 +01:00
if err != nil {
2018-07-11 09:08:03 +02:00
log . Errorf ( "Error starting provider %T: %v" , p , err )
2018-01-29 14:58:03 +01:00
}
} )
}
return nil
}