feat: use parser to load dynamic config from file.

This commit is contained in:
Ludovic Fernandez 2020-06-17 16:48:04 +02:00 committed by GitHub
parent 7affeae480
commit cb1d0441e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 187 additions and 88 deletions

View file

@ -38,9 +38,9 @@ type Configuration struct {
CAServer string `description:"CA server to use." json:"caServer,omitempty" toml:"caServer,omitempty" yaml:"caServer,omitempty"`
Storage string `description:"Storage to use." json:"storage,omitempty" toml:"storage,omitempty" yaml:"storage,omitempty"`
KeyType string `description:"KeyType used for generating certificate private key. Allow value 'EC256', 'EC384', 'RSA2048', 'RSA4096', 'RSA8192'." json:"keyType,omitempty" toml:"keyType,omitempty" yaml:"keyType,omitempty"`
DNSChallenge *DNSChallenge `description:"Activate DNS-01 Challenge." json:"dnsChallenge,omitempty" toml:"dnsChallenge,omitempty" yaml:"dnsChallenge,omitempty" label:"allowEmpty"`
HTTPChallenge *HTTPChallenge `description:"Activate HTTP-01 Challenge." json:"httpChallenge,omitempty" toml:"httpChallenge,omitempty" yaml:"httpChallenge,omitempty" label:"allowEmpty"`
TLSChallenge *TLSChallenge `description:"Activate TLS-ALPN-01 Challenge." json:"tlsChallenge,omitempty" toml:"tlsChallenge,omitempty" yaml:"tlsChallenge,omitempty" label:"allowEmpty"`
DNSChallenge *DNSChallenge `description:"Activate DNS-01 Challenge." json:"dnsChallenge,omitempty" toml:"dnsChallenge,omitempty" yaml:"dnsChallenge,omitempty" label:"allowEmpty" file:"allowEmpty"`
HTTPChallenge *HTTPChallenge `description:"Activate HTTP-01 Challenge." json:"httpChallenge,omitempty" toml:"httpChallenge,omitempty" yaml:"httpChallenge,omitempty" label:"allowEmpty" file:"allowEmpty"`
TLSChallenge *TLSChallenge `description:"Activate TLS-ALPN-01 Challenge." json:"tlsChallenge,omitempty" toml:"tlsChallenge,omitempty" yaml:"tlsChallenge,omitempty" label:"allowEmpty" file:"allowEmpty"`
}
// SetDefaults sets the default values.

View file

@ -11,15 +11,14 @@ import (
"strings"
"text/template"
"github.com/BurntSushi/toml"
"github.com/Masterminds/sprig"
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/containous/traefik/v2/pkg/config/file"
"github.com/containous/traefik/v2/pkg/log"
"github.com/containous/traefik/v2/pkg/provider"
"github.com/containous/traefik/v2/pkg/safe"
"github.com/containous/traefik/v2/pkg/tls"
"gopkg.in/fsnotify.v1"
"gopkg.in/yaml.v2"
)
const providerName = "file"
@ -418,22 +417,9 @@ func (p *Provider) decodeConfiguration(filePath string, content string) (*dynami
},
}
switch strings.ToLower(filepath.Ext(filePath)) {
case ".toml":
_, err := toml.Decode(content, configuration)
if err != nil {
return nil, err
}
case ".yml", ".yaml":
var err error
err = yaml.Unmarshal([]byte(content), configuration)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unsupported file extension: %s", filePath)
err := file.DecodeContent(content, strings.ToLower(filepath.Ext(filePath)), configuration)
if err != nil {
return nil, err
}
return configuration, nil

View file

@ -90,11 +90,12 @@ func TestProvideWithoutWatch(t *testing.T) {
timeout := time.After(time.Second)
select {
case conf := <-configChan:
require.NotNil(t, conf.Configuration.HTTP)
numServices := len(conf.Configuration.HTTP.Services) + len(conf.Configuration.TCP.Services) + len(conf.Configuration.UDP.Services)
numRouters := len(conf.Configuration.HTTP.Routers) + len(conf.Configuration.TCP.Routers) + len(conf.Configuration.UDP.Routers)
assert.Equal(t, numServices, test.expectedNumService)
assert.Equal(t, numRouters, test.expectedNumRouter)
assert.Equal(t, test.expectedNumService, numServices)
assert.Equal(t, test.expectedNumRouter, numRouters)
require.NotNil(t, conf.Configuration.TLS)
assert.Len(t, conf.Configuration.TLS.Certificates, test.expectedNumTLSConf)
assert.Len(t, conf.Configuration.TLS.Options, test.expectedNumTLSOptions)

View file

@ -1,5 +1,5 @@
[http.routers]
[http.routers."router1"]
service = "application-1"

View file

@ -66,7 +66,7 @@
[tcp.services.applicationtcp-1.loadBalancer]
[[tcp.services.applicationtcp-1.loadBalancer.servers]]
url = "http://172.17.0.9:80"
address = "http://172.17.0.9:80"
[udp.routers]
@ -77,4 +77,4 @@
[udp.services.applicationudp-1.loadBalancer]
[[udp.services.applicationudp-1.loadBalancer.servers]]
url = "http://172.17.0.10:80"
address = "http://172.17.0.10:80"

View file

@ -1,6 +1,6 @@
[http.services]
{{ range $i, $e := until 20 }}
[http.services.application-{{ $e }}]
[[http.services.application-{{ $e }}.servers]]
url="http://127.0.0.1"
[http.services.application-{{ $e }}.loadBalancer]
[[http.services.application-{{ $e }}.loadBalancer.servers]]
url = "http://127.0.0.1"
{{ end }}

View file

@ -1,6 +1,6 @@
http:
{{ range $i, $e := until 20 }}
routers:
{{ range $i, $e := until 20 }}
router{{ $e }}:
service: application-1
{{ end }}