2019-11-14 15:40:05 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-02-03 16:56:04 +00:00
|
|
|
"context"
|
2019-11-14 15:40:05 +00:00
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/logs"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/provider"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/safe"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/tls"
|
2024-01-11 16:06:06 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/types"
|
2019-11-14 15:40:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ConfigurationWatcher watches configuration changes.
|
|
|
|
type ConfigurationWatcher struct {
|
2022-02-07 10:58:04 +00:00
|
|
|
providerAggregator provider.Provider
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2020-03-09 10:12:05 +00:00
|
|
|
defaultEntryPoints []string
|
2020-03-05 11:46:05 +00:00
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
allProvidersConfigs chan dynamic.Message
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
newConfigs chan dynamic.Configurations
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2021-02-25 16:20:04 +00:00
|
|
|
requiredProvider string
|
2019-11-14 15:40:05 +00:00
|
|
|
configurationListeners []func(dynamic.Configuration)
|
|
|
|
|
|
|
|
routinesPool *safe.Pool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfigurationWatcher creates a new ConfigurationWatcher.
|
2020-03-05 11:46:05 +00:00
|
|
|
func NewConfigurationWatcher(
|
|
|
|
routinesPool *safe.Pool,
|
|
|
|
pvd provider.Provider,
|
2020-03-09 10:12:05 +00:00
|
|
|
defaultEntryPoints []string,
|
2021-02-25 16:20:04 +00:00
|
|
|
requiredProvider string,
|
2020-03-05 11:46:05 +00:00
|
|
|
) *ConfigurationWatcher {
|
2022-02-07 10:58:04 +00:00
|
|
|
return &ConfigurationWatcher{
|
|
|
|
providerAggregator: pvd,
|
|
|
|
allProvidersConfigs: make(chan dynamic.Message, 100),
|
|
|
|
newConfigs: make(chan dynamic.Configurations),
|
|
|
|
routinesPool: routinesPool,
|
|
|
|
defaultEntryPoints: defaultEntryPoints,
|
|
|
|
requiredProvider: requiredProvider,
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the configuration watcher.
|
|
|
|
func (c *ConfigurationWatcher) Start() {
|
2022-02-07 10:58:04 +00:00
|
|
|
c.routinesPool.GoCtx(c.receiveConfigurations)
|
|
|
|
c.routinesPool.GoCtx(c.applyConfigurations)
|
|
|
|
c.startProviderAggregator()
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop the configuration watcher.
|
|
|
|
func (c *ConfigurationWatcher) Stop() {
|
2022-02-07 10:58:04 +00:00
|
|
|
close(c.allProvidersConfigs)
|
|
|
|
close(c.newConfigs)
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// AddListener adds a new listener function used when new configuration is provided.
|
2019-11-14 15:40:05 +00:00
|
|
|
func (c *ConfigurationWatcher) AddListener(listener func(dynamic.Configuration)) {
|
|
|
|
if c.configurationListeners == nil {
|
|
|
|
c.configurationListeners = make([]func(dynamic.Configuration), 0)
|
|
|
|
}
|
|
|
|
c.configurationListeners = append(c.configurationListeners, listener)
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
func (c *ConfigurationWatcher) startProviderAggregator() {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Info().Msgf("Starting provider aggregator %T", c.providerAggregator)
|
2019-11-14 15:40:05 +00:00
|
|
|
|
|
|
|
safe.Go(func() {
|
2022-02-07 10:58:04 +00:00
|
|
|
err := c.providerAggregator.Provide(c.allProvidersConfigs, c.routinesPool)
|
2019-11-14 15:40:05 +00:00
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
log.Error().Err(err).Msgf("Error starting provider aggregator %T", c.providerAggregator)
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
// receiveConfigurations receives configuration changes from the providers.
|
|
|
|
// The configuration message then gets passed along a series of check, notably
|
|
|
|
// to verify that, for a given provider, the configuration that was just received
|
|
|
|
// is at least different from the previously received one.
|
|
|
|
// The full set of configurations is then sent to the throttling goroutine,
|
|
|
|
// (throttleAndApplyConfigurations) via a RingChannel, which ensures that we can
|
|
|
|
// constantly send in a non-blocking way to the throttling goroutine the last
|
|
|
|
// global state we are aware of.
|
|
|
|
func (c *ConfigurationWatcher) receiveConfigurations(ctx context.Context) {
|
|
|
|
newConfigurations := make(dynamic.Configurations)
|
|
|
|
var output chan dynamic.Configurations
|
2019-11-14 15:40:05 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-02-03 16:56:04 +00:00
|
|
|
case <-ctx.Done():
|
2019-11-14 15:40:05 +00:00
|
|
|
return
|
2022-02-07 10:58:04 +00:00
|
|
|
// DeepCopy is necessary because newConfigurations gets modified later by the consumer of c.newConfigs
|
|
|
|
case output <- newConfigurations.DeepCopy():
|
|
|
|
output = nil
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
default:
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-11-14 15:40:05 +00:00
|
|
|
return
|
2022-02-07 10:58:04 +00:00
|
|
|
case configMsg, ok := <-c.allProvidersConfigs:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.Ctx(ctx).With().Str(logs.ProviderName, configMsg.ProviderName).Logger()
|
2022-02-07 10:58:04 +00:00
|
|
|
|
|
|
|
if configMsg.Configuration == nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msg("Skipping nil configuration")
|
2022-02-07 10:58:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if isEmptyConfiguration(configMsg.Configuration) {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msg("Skipping empty configuration")
|
2022-02-07 10:58:04 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
logConfiguration(logger, configMsg)
|
|
|
|
|
|
|
|
if reflect.DeepEqual(newConfigurations[configMsg.ProviderName], configMsg.Configuration) {
|
|
|
|
// no change, do nothing
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().Msg("Skipping unchanged configuration")
|
2022-02-07 10:58:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newConfigurations[configMsg.ProviderName] = configMsg.Configuration.DeepCopy()
|
|
|
|
|
|
|
|
output = c.newConfigs
|
|
|
|
|
|
|
|
// DeepCopy is necessary because newConfigurations gets modified later by the consumer of c.newConfigs
|
|
|
|
case output <- newConfigurations.DeepCopy():
|
|
|
|
output = nil
|
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
// applyConfigurations blocks on a RingChannel that receives the new
|
|
|
|
// set of configurations that is compiled and sent by receiveConfigurations as soon
|
|
|
|
// as a provider change occurs. If the new set is different from the previous set
|
|
|
|
// that had been applied, the new set is applied, and we sleep for a while before
|
|
|
|
// listening on the channel again.
|
|
|
|
func (c *ConfigurationWatcher) applyConfigurations(ctx context.Context) {
|
|
|
|
var lastConfigurations dynamic.Configurations
|
2019-11-14 15:40:05 +00:00
|
|
|
for {
|
|
|
|
select {
|
2020-02-03 16:56:04 +00:00
|
|
|
case <-ctx.Done():
|
2019-11-14 15:40:05 +00:00
|
|
|
return
|
2022-02-07 10:58:04 +00:00
|
|
|
case newConfigs, ok := <-c.newConfigs:
|
|
|
|
if !ok {
|
2019-11-14 15:40:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
// We wait for first configuration of the required provider before applying configurations.
|
|
|
|
if _, ok := newConfigs[c.requiredProvider]; c.requiredProvider != "" && !ok {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
if reflect.DeepEqual(newConfigs, lastConfigurations) {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
conf := mergeConfiguration(newConfigs.DeepCopy(), c.defaultEntryPoints)
|
|
|
|
conf = applyModel(conf)
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
for _, listener := range c.configurationListeners {
|
|
|
|
listener(conf)
|
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
lastConfigurations = newConfigs
|
2021-02-25 16:20:04 +00:00
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
func logConfiguration(logger zerolog.Logger, configMsg dynamic.Message) {
|
|
|
|
if logger.GetLevel() > zerolog.DebugLevel {
|
2022-02-07 10:58:04 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-13 12:14:35 +00:00
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
copyConf := configMsg.Configuration.DeepCopy()
|
|
|
|
if copyConf.TLS != nil {
|
|
|
|
copyConf.TLS.Certificates = nil
|
2021-07-13 12:14:35 +00:00
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
if copyConf.TLS.Options != nil {
|
|
|
|
cleanedOptions := make(map[string]tls.Options, len(copyConf.TLS.Options))
|
|
|
|
for name, option := range copyConf.TLS.Options {
|
2024-01-11 16:06:06 +00:00
|
|
|
option.ClientAuth.CAFiles = []types.FileOrContent{}
|
2022-02-07 10:58:04 +00:00
|
|
|
cleanedOptions[name] = option
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
copyConf.TLS.Options = cleanedOptions
|
2021-07-13 12:14:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
for k := range copyConf.TLS.Stores {
|
|
|
|
st := copyConf.TLS.Stores[k]
|
|
|
|
st.DefaultCertificate = nil
|
|
|
|
copyConf.TLS.Stores[k] = st
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
if copyConf.HTTP != nil {
|
|
|
|
for _, transport := range copyConf.HTTP.ServersTransports {
|
|
|
|
transport.Certificates = tls.Certificates{}
|
2024-01-11 16:06:06 +00:00
|
|
|
transport.RootCAs = []types.FileOrContent{}
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
2022-02-07 10:58:04 +00:00
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2022-12-09 08:58:05 +00:00
|
|
|
if copyConf.TCP != nil {
|
|
|
|
for _, transport := range copyConf.TCP.ServersTransports {
|
|
|
|
if transport.TLS != nil {
|
|
|
|
transport.TLS.Certificates = tls.Certificates{}
|
2024-01-11 16:06:06 +00:00
|
|
|
transport.TLS.RootCAs = []types.FileOrContent{}
|
2022-12-09 08:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 10:58:04 +00:00
|
|
|
jsonConf, err := json.Marshal(copyConf)
|
|
|
|
if err != nil {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Error().Err(err).Msg("Could not marshal dynamic configuration")
|
|
|
|
logger.Debug().Msgf("Configuration received: [struct] %#v", copyConf)
|
2022-02-07 10:58:04 +00:00
|
|
|
} else {
|
2022-11-21 17:36:05 +00:00
|
|
|
logger.Debug().RawJSON("config", jsonConf).Msg("Configuration received")
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isEmptyConfiguration(conf *dynamic.Configuration) bool {
|
|
|
|
if conf.TCP == nil {
|
|
|
|
conf.TCP = &dynamic.TCPConfiguration{}
|
|
|
|
}
|
|
|
|
if conf.HTTP == nil {
|
|
|
|
conf.HTTP = &dynamic.HTTPConfiguration{}
|
|
|
|
}
|
2020-04-16 14:18:04 +00:00
|
|
|
if conf.UDP == nil {
|
|
|
|
conf.UDP = &dynamic.UDPConfiguration{}
|
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
|
|
|
|
httpEmpty := conf.HTTP.Routers == nil && conf.HTTP.Services == nil && conf.HTTP.Middlewares == nil
|
|
|
|
tlsEmpty := conf.TLS == nil || conf.TLS.Certificates == nil && conf.TLS.Stores == nil && conf.TLS.Options == nil
|
2021-06-11 13:30:05 +00:00
|
|
|
tcpEmpty := conf.TCP.Routers == nil && conf.TCP.Services == nil && conf.TCP.Middlewares == nil
|
2020-04-16 14:18:04 +00:00
|
|
|
udpEmpty := conf.UDP.Routers == nil && conf.UDP.Services == nil
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2020-04-16 14:18:04 +00:00
|
|
|
return httpEmpty && tlsEmpty && tcpEmpty && udpEmpty
|
2019-11-14 15:40:05 +00:00
|
|
|
}
|