2018-11-14 10:18:03 +01:00
|
|
|
package aggregator
|
|
|
|
|
|
|
|
import (
|
2020-09-16 15:46:04 +02:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/config/static"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/provider"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/provider/file"
|
2021-01-28 16:16:05 +01:00
|
|
|
"github.com/traefik/traefik/v2/pkg/provider/traefik"
|
2022-01-24 05:08:05 -05:00
|
|
|
"github.com/traefik/traefik/v2/pkg/redactor"
|
2020-09-16 15:46:04 +02:00
|
|
|
"github.com/traefik/traefik/v2/pkg/safe"
|
2018-11-14 10:18:03 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ProviderAggregator aggregates providers.
|
|
|
|
type ProviderAggregator struct {
|
2021-01-28 16:16:05 +01:00
|
|
|
internalProvider provider.Provider
|
|
|
|
fileProvider provider.Provider
|
|
|
|
providers []provider.Provider
|
2018-11-14 10:18:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewProviderAggregator returns an aggregate of all the providers configured in the static configuration.
|
2018-11-27 17:42:04 +01:00
|
|
|
func NewProviderAggregator(conf static.Providers) ProviderAggregator {
|
|
|
|
p := ProviderAggregator{}
|
2018-11-14 10:18:03 +01:00
|
|
|
|
|
|
|
if conf.File != nil {
|
|
|
|
p.quietAddProvider(conf.File)
|
|
|
|
}
|
|
|
|
|
2019-01-18 15:18:04 +01:00
|
|
|
if conf.Docker != nil {
|
|
|
|
p.quietAddProvider(conf.Docker)
|
|
|
|
}
|
|
|
|
|
2019-01-29 17:54:05 +01:00
|
|
|
if conf.Marathon != nil {
|
|
|
|
p.quietAddProvider(conf.Marathon)
|
|
|
|
}
|
|
|
|
|
2018-12-03 11:32:05 +01:00
|
|
|
if conf.Rest != nil {
|
|
|
|
p.quietAddProvider(conf.Rest)
|
|
|
|
}
|
|
|
|
|
2019-07-08 21:36:03 +02:00
|
|
|
if conf.KubernetesIngress != nil {
|
|
|
|
p.quietAddProvider(conf.KubernetesIngress)
|
2019-02-21 23:08:05 +01:00
|
|
|
}
|
|
|
|
|
2019-03-14 15:56:06 +01:00
|
|
|
if conf.KubernetesCRD != nil {
|
|
|
|
p.quietAddProvider(conf.KubernetesCRD)
|
|
|
|
}
|
2019-06-11 15:12:04 +02:00
|
|
|
|
2020-12-15 16:40:05 +01:00
|
|
|
if conf.KubernetesGateway != nil {
|
|
|
|
p.quietAddProvider(conf.KubernetesGateway)
|
|
|
|
}
|
|
|
|
|
2019-04-05 12:22:04 +02:00
|
|
|
if conf.Rancher != nil {
|
|
|
|
p.quietAddProvider(conf.Rancher)
|
|
|
|
}
|
2019-03-14 15:56:06 +01:00
|
|
|
|
2020-07-15 16:28:04 +02:00
|
|
|
if conf.Ecs != nil {
|
|
|
|
p.quietAddProvider(conf.Ecs)
|
|
|
|
}
|
|
|
|
|
2019-10-15 18:34:08 +03:00
|
|
|
if conf.ConsulCatalog != nil {
|
|
|
|
p.quietAddProvider(conf.ConsulCatalog)
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:56:04 +01:00
|
|
|
if conf.Consul != nil {
|
|
|
|
p.quietAddProvider(conf.Consul)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Etcd != nil {
|
|
|
|
p.quietAddProvider(conf.Etcd)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.ZooKeeper != nil {
|
|
|
|
p.quietAddProvider(conf.ZooKeeper)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Redis != nil {
|
|
|
|
p.quietAddProvider(conf.Redis)
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:56:03 +02:00
|
|
|
if conf.HTTP != nil {
|
|
|
|
p.quietAddProvider(conf.HTTP)
|
|
|
|
}
|
|
|
|
|
2018-11-14 10:18:03 +01:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProviderAggregator) quietAddProvider(provider provider.Provider) {
|
|
|
|
err := p.AddProvider(provider)
|
|
|
|
if err != nil {
|
|
|
|
log.WithoutContext().Errorf("Error while initializing provider %T: %v", provider, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddProvider adds a provider in the providers map.
|
|
|
|
func (p *ProviderAggregator) AddProvider(provider provider.Provider) error {
|
2018-11-27 17:42:04 +01:00
|
|
|
err := provider.Init()
|
2018-11-14 10:18:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-14 09:30:04 +01:00
|
|
|
|
2021-01-28 16:16:05 +01:00
|
|
|
switch provider.(type) {
|
|
|
|
case *file.Provider:
|
|
|
|
p.fileProvider = provider
|
|
|
|
case *traefik.Provider:
|
|
|
|
p.internalProvider = provider
|
|
|
|
default:
|
2019-03-14 09:30:04 +01:00
|
|
|
p.providers = append(p.providers, provider)
|
|
|
|
}
|
2021-01-28 16:16:05 +01:00
|
|
|
|
2018-11-14 10:18:03 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:06:07 +02:00
|
|
|
// Init the provider.
|
2018-11-27 17:42:04 +01:00
|
|
|
func (p ProviderAggregator) Init() error {
|
2018-11-14 10:18:03 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 12:06:07 +02:00
|
|
|
// Provide calls the provide method of every providers.
|
2019-07-10 09:26:04 +02:00
|
|
|
func (p ProviderAggregator) Provide(configurationChan chan<- dynamic.Message, pool *safe.Pool) error {
|
2021-02-01 12:36:03 +01:00
|
|
|
if p.fileProvider != nil {
|
|
|
|
launchProvider(configurationChan, pool, p.fileProvider)
|
|
|
|
}
|
|
|
|
|
2018-11-14 10:18:03 +01:00
|
|
|
for _, prd := range p.providers {
|
2019-03-19 10:04:04 +01:00
|
|
|
prd := prd
|
2018-11-14 10:18:03 +01:00
|
|
|
safe.Go(func() {
|
2019-03-14 09:30:04 +01:00
|
|
|
launchProvider(configurationChan, pool, prd)
|
2018-11-14 10:18:03 +01:00
|
|
|
})
|
|
|
|
}
|
2021-01-28 16:16:05 +01:00
|
|
|
|
2021-02-25 17:20:04 +01:00
|
|
|
// internal provider must be the last because we use it to know if all the providers are loaded.
|
|
|
|
// ConfigurationWatcher will wait for this requiredProvider before applying configurations.
|
|
|
|
if p.internalProvider != nil {
|
|
|
|
launchProvider(configurationChan, pool, p.internalProvider)
|
|
|
|
}
|
|
|
|
|
2018-11-14 10:18:03 +01:00
|
|
|
return nil
|
|
|
|
}
|
2019-03-14 09:30:04 +01:00
|
|
|
|
2019-07-10 09:26:04 +02:00
|
|
|
func launchProvider(configurationChan chan<- dynamic.Message, pool *safe.Pool, prd provider.Provider) {
|
2022-01-24 05:08:05 -05:00
|
|
|
jsonConf, err := redactor.RemoveCredentials(prd)
|
2019-03-14 09:30:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.WithoutContext().Debugf("Cannot marshal the provider configuration %T: %v", prd, err)
|
|
|
|
}
|
|
|
|
|
2022-01-24 05:08:05 -05:00
|
|
|
log.WithoutContext().Infof("Starting provider %T", prd)
|
|
|
|
log.WithoutContext().Debugf("%T provider configuration: %s", prd, jsonConf)
|
2019-03-14 09:30:04 +01:00
|
|
|
|
|
|
|
currentProvider := prd
|
|
|
|
err = currentProvider.Provide(configurationChan, pool)
|
|
|
|
if err != nil {
|
|
|
|
log.WithoutContext().Errorf("Cannot start the provider %T: %v", prd, err)
|
|
|
|
}
|
|
|
|
}
|