add flag on ACME

add flag on Retry

set Retry.MaxMem to 2 by default

rm useless import

rm useless structtag

add custom parser on []acme.Domain type

add commants + refactor
This commit is contained in:
Martin 2016-05-25 17:06:34 +02:00
parent 6752b49536
commit f64c2bc065
4 changed files with 65 additions and 26 deletions

View file

@ -16,6 +16,7 @@ import (
fmtlog "log" fmtlog "log"
"os" "os"
"reflect" "reflect"
"strings"
"sync" "sync"
"time" "time"
) )
@ -161,15 +162,47 @@ func (dc *DomainsCertificate) needRenew() bool {
// ACME allows to connect to lets encrypt and retrieve certs // ACME allows to connect to lets encrypt and retrieve certs
type ACME struct { type ACME struct {
Email string Email string `description:"Email address used for registration"`
Domains []Domain Domains []Domain `description:"SANs (alternative domains) to each main domain"`
StorageFile string StorageFile string `description:"File used for certificates storage."`
OnDemand bool OnDemand bool `description:"Enable on demand certificate. This will request a certificate from Let's Encrypt during the first TLS handshake for a hostname that does not yet have a certificate."`
CAServer string CAServer string `description:"CA server to use."`
EntryPoint string EntryPoint string `description:"Entrypoint to proxy acme challenge to."`
storageLock sync.RWMutex storageLock sync.RWMutex
} }
//Domains parse []Domain
type Domains []Domain
//Set []Domain
func (ds *Domains) Set(str string) error {
fargs := func(c rune) bool {
return c == ',' || c == ';'
}
// get function
slice := strings.FieldsFunc(str, fargs)
if len(slice) >= 2 {
return fmt.Errorf("Parse error ACME.Domain. Imposible to parse %s", str)
}
d := Domain{
Main: slice[0],
SANs: slice[1:],
}
*ds = append(*ds, d)
return nil
}
//Get []Domain
func (ds *Domains) Get() interface{} { return []Domain(*ds) }
//String returns []Domain in string
func (ds *Domains) String() string { return fmt.Sprintf("%+v", *ds) }
//SetValue sets []Domain into the parser
func (ds *Domains) SetValue(val interface{}) {
*ds = Domains(val.([]Domain))
}
// Domain holds a domain name with SANs // Domain holds a domain name with SANs
type Domain struct { type Domain struct {
Main string Main string

View file

@ -20,17 +20,17 @@ type TraefikConfiguration struct {
// GlobalConfiguration holds global configuration (with providers, etc.). // GlobalConfiguration holds global configuration (with providers, etc.).
// It's populated from the traefik configuration file passed as an argument to the binary. // It's populated from the traefik configuration file passed as an argument to the binary.
type GlobalConfiguration struct { type GlobalConfiguration struct {
GraceTimeOut int64 `short:"g" description:"Configuration file to use (TOML)."` GraceTimeOut int64 `short:"g" description:"Configuration file to use (TOML)."`
Debug bool Debug bool
AccessLogsFile string `description:"Access logs file"` AccessLogsFile string `description:"Access logs file"`
TraefikLogsFile string `description:"Traefik logs file"` TraefikLogsFile string `description:"Traefik logs file"`
LogLevel string `short:"l" description:"Log level"` LogLevel string `short:"l" description:"Log level"`
EntryPoints EntryPoints `description:"Entrypoints definition using format: --entryPoints='Name:http Address::8000 Redirect.EntryPoint:https' --entryPoints='Name:https Address::4442 TLS:tests/traefik.crt,tests/traefik.key'"` EntryPoints EntryPoints `description:"Entrypoints definition using format: --entryPoints='Name:http Address::8000 Redirect.EntryPoint:https' --entryPoints='Name:https Address::4442 TLS:tests/traefik.crt,tests/traefik.key'"`
ACME *acme.ACME ACME *acme.ACME `description:"Enable ACME (Let's Encrypt): automatic SSL"`
DefaultEntryPoints DefaultEntryPoints `description:"Entrypoints to be used by frontends that do not specify any entrypoint"` DefaultEntryPoints DefaultEntryPoints `description:"Entrypoints to be used by frontends that do not specify any entrypoint"`
ProvidersThrottleDuration time.Duration `description:"Backends throttle duration: minimum duration between 2 events from providers before applying a new configuration. It avoids unnecessary reloads if multiples events are sent in a short amount of time."` ProvidersThrottleDuration time.Duration `description:"Backends throttle duration: minimum duration between 2 events from providers before applying a new configuration. It avoids unnecessary reloads if multiples events are sent in a short amount of time."`
MaxIdleConnsPerHost int `description:"If non-zero, controls the maximum idle (keep-alive) to keep per-host. If zero, DefaultMaxIdleConnsPerHost is used"` MaxIdleConnsPerHost int `description:"If non-zero, controls the maximum idle (keep-alive) to keep per-host. If zero, DefaultMaxIdleConnsPerHost is used"`
Retry *Retry Retry *Retry `description:"Enable retry sending request if network error"`
Docker *provider.Docker `description:"Enable Docker backend"` Docker *provider.Docker `description:"Enable Docker backend"`
File *provider.File `description:"Enable File backend"` File *provider.File `description:"Enable File backend"`
Web *WebProvider `description:"Enable Web backend"` Web *WebProvider `description:"Enable Web backend"`
@ -49,7 +49,7 @@ type DefaultEntryPoints []string
// String is the method to format the flag's value, part of the flag.Value interface. // String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics. // The String method's output will be used in diagnostics.
func (dep *DefaultEntryPoints) String() string { func (dep *DefaultEntryPoints) String() string {
//TODO : //TODO : The string returned should be formatted in such way that the func Set below could parse it.
return fmt.Sprintf("%#v", dep) return fmt.Sprintf("%#v", dep)
} }
@ -86,8 +86,10 @@ type EntryPoints map[string]*EntryPoint
// String is the method to format the flag's value, part of the flag.Value interface. // String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics. // The String method's output will be used in diagnostics.
func (ep *EntryPoints) String() string { func (ep *EntryPoints) String() string {
//TODO : //TODO : The string returned should be formatted in such way that the func Set below could parse it.
return "" //Like this --entryPoints='Name:http Address::8000 Redirect.EntryPoint:https'
//But the Set func parses entrypoint one by one only
return fmt.Sprintf("%+v", *ep)
} }
// Set is the method to set the flag value, part of the flag.Value interface. // Set is the method to set the flag value, part of the flag.Value interface.
@ -208,12 +210,12 @@ type Certificate struct {
// Retry contains request retry config // Retry contains request retry config
type Retry struct { type Retry struct {
Attempts int Attempts int `description:"Number of attempts"`
MaxMem int64 MaxMem int64 `description:"Maximum request body to be stored in memory in Mo"`
} }
// NewTraefikPointersConfiguration creates a TraefikConfiguration with pointers default values // NewTraefikDefaultPointersConfiguration creates a TraefikConfiguration with pointers default values
func NewTraefikPointersConfiguration() *TraefikConfiguration { func NewTraefikDefaultPointersConfiguration() *TraefikConfiguration {
//default Docker //default Docker
var defaultDocker provider.Docker var defaultDocker provider.Docker
defaultDocker.Watch = true defaultDocker.Watch = true
@ -281,6 +283,7 @@ func NewTraefikPointersConfiguration() *TraefikConfiguration {
Zookeeper: &defaultZookeeper, Zookeeper: &defaultZookeeper,
Boltdb: &defaultBoltDb, Boltdb: &defaultBoltDb,
Kubernetes: &defaultKubernetes, Kubernetes: &defaultKubernetes,
Retry: &Retry{MaxMem: 2},
} }
return &TraefikConfiguration{ return &TraefikConfiguration{
GlobalConfiguration: defaultConfiguration, GlobalConfiguration: defaultConfiguration,

View file

@ -9,7 +9,7 @@ import (
// Zookepper holds configurations of the Zookepper provider. // Zookepper holds configurations of the Zookepper provider.
type Zookepper struct { type Zookepper struct {
Kv `description:"go through"` Kv
} }
// Provide allows the provider to provide configurations to traefik // Provide allows the provider to provide configurations to traefik

View file

@ -5,6 +5,7 @@ import (
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/containous/flaeg" "github.com/containous/flaeg"
"github.com/containous/staert" "github.com/containous/staert"
"github.com/containous/traefik/acme"
"github.com/containous/traefik/middlewares" "github.com/containous/traefik/middlewares"
"github.com/containous/traefik/provider" "github.com/containous/traefik/provider"
fmtlog "log" fmtlog "log"
@ -20,7 +21,7 @@ func main() {
//traefik config inits //traefik config inits
traefikConfiguration := NewTraefikConfiguration() traefikConfiguration := NewTraefikConfiguration()
traefikPointersConfiguration := NewTraefikPointersConfiguration() traefikPointersConfiguration := NewTraefikDefaultPointersConfiguration()
//traefik Command init //traefik Command init
traefikCmd := &flaeg.Command{ traefikCmd := &flaeg.Command{
Name: "traefik", Name: "traefik",
@ -52,6 +53,8 @@ Complete documentation is available at https://traefik.io`,
f.AddParser(reflect.TypeOf(EntryPoints{}), &EntryPoints{}) f.AddParser(reflect.TypeOf(EntryPoints{}), &EntryPoints{})
f.AddParser(reflect.TypeOf(DefaultEntryPoints{}), &DefaultEntryPoints{}) f.AddParser(reflect.TypeOf(DefaultEntryPoints{}), &DefaultEntryPoints{})
f.AddParser(reflect.TypeOf(provider.Namespaces{}), &provider.Namespaces{}) f.AddParser(reflect.TypeOf(provider.Namespaces{}), &provider.Namespaces{})
f.AddParser(reflect.TypeOf([]acme.Domain{}), &acme.Domains{})
//add version command //add version command
f.AddCommand(versionCmd) f.AddCommand(versionCmd)
if _, err := f.Parse(traefikCmd); err != nil { if _, err := f.Parse(traefikCmd); err != nil {