fix: logger and context.
This commit is contained in:
parent
b4c7b90c9e
commit
8e18d37b3d
52 changed files with 231 additions and 183 deletions
|
@ -350,17 +350,19 @@ func (p *Provider) watchNewDomains(ctx context.Context) {
|
|||
if route.TLS == nil || route.TLS.CertResolver != p.ResolverName {
|
||||
continue
|
||||
}
|
||||
|
||||
ctxRouter := log.With(ctx, log.Str(log.RouterName, routerName), log.Str(log.Rule, route.Rule))
|
||||
logger := log.FromContext(ctxRouter)
|
||||
|
||||
tlsStore := "default"
|
||||
if len(route.TLS.Domains) > 0 {
|
||||
for _, domain := range route.TLS.Domains {
|
||||
if domain.Main != dns01.UnFqdn(domain.Main) {
|
||||
log.Warnf("FQDN detected, please remove the trailing dot: %s", domain.Main)
|
||||
logger.Warnf("FQDN detected, please remove the trailing dot: %s", domain.Main)
|
||||
}
|
||||
for _, san := range domain.SANs {
|
||||
if san != dns01.UnFqdn(san) {
|
||||
log.Warnf("FQDN detected, please remove the trailing dot: %s", san)
|
||||
logger.Warnf("FQDN detected, please remove the trailing dot: %s", san)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -378,7 +380,7 @@ func (p *Provider) watchNewDomains(ctx context.Context) {
|
|||
} else {
|
||||
domains, err := rules.ParseHostSNI(route.Rule)
|
||||
if err != nil {
|
||||
log.FromContext(ctxRouter).Errorf("Error parsing domains in provider ACME: %v", err)
|
||||
logger.Errorf("Error parsing domains in provider ACME: %v", err)
|
||||
continue
|
||||
}
|
||||
p.resolveDomains(ctxRouter, domains, tlsStore)
|
||||
|
|
|
@ -85,8 +85,9 @@ func (p *Provider) buildTCPServiceConfiguration(ctx context.Context, container d
|
|||
}
|
||||
}
|
||||
|
||||
for _, service := range configuration.Services {
|
||||
err := p.addServerTCP(ctx, container, service.LoadBalancer)
|
||||
for name, service := range configuration.Services {
|
||||
ctxSvc := log.With(ctx, log.Str(log.ServiceName, name))
|
||||
err := p.addServerTCP(ctxSvc, container, service.LoadBalancer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -107,8 +108,9 @@ func (p *Provider) buildServiceConfiguration(ctx context.Context, container dock
|
|||
}
|
||||
}
|
||||
|
||||
for _, service := range configuration.Services {
|
||||
err := p.addServer(ctx, container, service.LoadBalancer)
|
||||
for name, service := range configuration.Services {
|
||||
ctxSvc := log.With(ctx, log.Str(log.ServiceName, name))
|
||||
err := p.addServer(ctxSvc, container, service.LoadBalancer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ func (p *Provider) BuildConfiguration() (*dynamic.Configuration, error) {
|
|||
}
|
||||
|
||||
if len(p.Filename) > 0 {
|
||||
return p.loadFileConfig(p.Filename, true)
|
||||
return p.loadFileConfig(ctx, p.Filename, true)
|
||||
}
|
||||
|
||||
return nil, errors.New("error using file configuration provider, neither filename or directory defined")
|
||||
|
@ -156,11 +156,11 @@ func sendConfigToChannel(configurationChan chan<- dynamic.Message, configuration
|
|||
}
|
||||
}
|
||||
|
||||
func (p *Provider) loadFileConfig(filename string, parseTemplate bool) (*dynamic.Configuration, error) {
|
||||
func (p *Provider) loadFileConfig(ctx context.Context, filename string, parseTemplate bool) (*dynamic.Configuration, error) {
|
||||
var err error
|
||||
var configuration *dynamic.Configuration
|
||||
if parseTemplate {
|
||||
configuration, err = p.CreateConfiguration(filename, template.FuncMap{}, false)
|
||||
configuration, err = p.CreateConfiguration(ctx, filename, template.FuncMap{}, false)
|
||||
} else {
|
||||
configuration, err = p.DecodeConfiguration(filename)
|
||||
}
|
||||
|
@ -169,25 +169,25 @@ func (p *Provider) loadFileConfig(filename string, parseTemplate bool) (*dynamic
|
|||
}
|
||||
|
||||
if configuration.TLS != nil {
|
||||
configuration.TLS.Certificates = flattenCertificates(configuration.TLS)
|
||||
configuration.TLS.Certificates = flattenCertificates(ctx, configuration.TLS)
|
||||
}
|
||||
|
||||
return configuration, nil
|
||||
}
|
||||
|
||||
func flattenCertificates(tlsConfig *dynamic.TLSConfiguration) []*tls.CertAndStores {
|
||||
func flattenCertificates(ctx context.Context, tlsConfig *dynamic.TLSConfiguration) []*tls.CertAndStores {
|
||||
var certs []*tls.CertAndStores
|
||||
for _, cert := range tlsConfig.Certificates {
|
||||
content, err := cert.Certificate.CertFile.Read()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.FromContext(ctx).Error(err)
|
||||
continue
|
||||
}
|
||||
cert.Certificate.CertFile = tls.FileOrContent(string(content))
|
||||
|
||||
content, err = cert.Certificate.KeyFile.Read()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
log.FromContext(ctx).Error(err)
|
||||
continue
|
||||
}
|
||||
cert.Certificate.KeyFile = tls.FileOrContent(string(content))
|
||||
|
@ -243,7 +243,7 @@ func (p *Provider) loadFileConfigFromDirectory(ctx context.Context, directory st
|
|||
}
|
||||
|
||||
var c *dynamic.Configuration
|
||||
c, err = p.loadFileConfig(filepath.Join(directory, item.Name()), true)
|
||||
c, err = p.loadFileConfig(ctx, filepath.Join(directory, item.Name()), true)
|
||||
if err != nil {
|
||||
return configuration, err
|
||||
}
|
||||
|
@ -331,7 +331,7 @@ func (p *Provider) loadFileConfigFromDirectory(ctx context.Context, directory st
|
|||
}
|
||||
|
||||
// CreateConfiguration creates a provider configuration from content using templating.
|
||||
func (p *Provider) CreateConfiguration(filename string, funcMap template.FuncMap, templateObjects interface{}) (*dynamic.Configuration, error) {
|
||||
func (p *Provider) CreateConfiguration(ctx context.Context, filename string, funcMap template.FuncMap, templateObjects interface{}) (*dynamic.Configuration, error) {
|
||||
tmplContent, err := readFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading configuration file: %s - %s", filename, err)
|
||||
|
@ -359,7 +359,7 @@ func (p *Provider) CreateConfiguration(filename string, funcMap template.FuncMap
|
|||
|
||||
var renderedTemplate = buffer.String()
|
||||
if p.DebugLogGeneratedTemplate {
|
||||
logger := log.WithoutContext().WithField(log.ProviderName, providerName)
|
||||
logger := log.FromContext(ctx)
|
||||
logger.Debugf("Template content: %s", tmplContent)
|
||||
logger.Debugf("Rendering results: %s", renderedTemplate)
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ func TestTLSContent(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
provider := &Provider{}
|
||||
configuration, err := provider.loadFileConfig(fileConfig.Name(), true)
|
||||
configuration, err := provider.loadFileConfig(context.Background(), fileConfig.Name(), true)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "CONTENT", configuration.TLS.Certificates[0].Certificate.CertFile.String())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue