Linting provider package
Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
parent
9387235a04
commit
89bb1ae835
9 changed files with 84 additions and 48 deletions
|
@ -2,6 +2,7 @@ package provider
|
||||||
|
|
||||||
import "github.com/emilevauge/traefik/types"
|
import "github.com/emilevauge/traefik/types"
|
||||||
|
|
||||||
|
// BoltDb holds configurations of the BoltDb provider.
|
||||||
type BoltDb struct {
|
type BoltDb struct {
|
||||||
Watch bool
|
Watch bool
|
||||||
Endpoint string
|
Endpoint string
|
||||||
|
@ -10,6 +11,8 @@ type BoltDb struct {
|
||||||
KvProvider *Kv
|
KvProvider *Kv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provide allows the provider to provide configurations to traefik
|
||||||
|
// using the given configuration channel.
|
||||||
func (provider *BoltDb) Provide(configurationChan chan<- types.ConfigMessage) error {
|
func (provider *BoltDb) Provide(configurationChan chan<- types.ConfigMessage) error {
|
||||||
provider.KvProvider = NewBoltDbProvider(provider)
|
provider.KvProvider = NewBoltDbProvider(provider)
|
||||||
return provider.KvProvider.provide(configurationChan)
|
return provider.KvProvider.provide(configurationChan)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package provider
|
||||||
|
|
||||||
import "github.com/emilevauge/traefik/types"
|
import "github.com/emilevauge/traefik/types"
|
||||||
|
|
||||||
|
// Consul holds configurations of the Consul provider.
|
||||||
type Consul struct {
|
type Consul struct {
|
||||||
Watch bool
|
Watch bool
|
||||||
Endpoint string
|
Endpoint string
|
||||||
|
@ -10,6 +11,8 @@ type Consul struct {
|
||||||
KvProvider *Kv
|
KvProvider *Kv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provide allows the provider to provide configurations to traefik
|
||||||
|
// using the given configuration channel.
|
||||||
func (provider *Consul) Provide(configurationChan chan<- types.ConfigMessage) error {
|
func (provider *Consul) Provide(configurationChan chan<- types.ConfigMessage) error {
|
||||||
provider.KvProvider = NewConsulProvider(provider)
|
provider.KvProvider = NewConsulProvider(provider)
|
||||||
return provider.KvProvider.provide(configurationChan)
|
return provider.KvProvider.provide(configurationChan)
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Docker holds configurations of the Docker provider.
|
||||||
type Docker struct {
|
type Docker struct {
|
||||||
Watch bool
|
Watch bool
|
||||||
Endpoint string
|
Endpoint string
|
||||||
|
@ -25,12 +26,16 @@ type Docker struct {
|
||||||
Domain string
|
Domain string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provide allows the provider to provide configurations to traefik
|
||||||
|
// using the given configuration channel.
|
||||||
func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage) error {
|
func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage) error {
|
||||||
if dockerClient, err := docker.NewClient(provider.Endpoint); err != nil {
|
|
||||||
|
dockerClient, err := docker.NewClient(provider.Endpoint)
|
||||||
|
if err != nil {
|
||||||
log.Errorf("Failed to create a client for docker, error: %s", err)
|
log.Errorf("Failed to create a client for docker, error: %s", err)
|
||||||
return err
|
return err
|
||||||
} else {
|
}
|
||||||
err := dockerClient.Ping()
|
err = dockerClient.Ping()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Docker connection error %+v", err)
|
log.Errorf("Docker connection error %+v", err)
|
||||||
return err
|
return err
|
||||||
|
@ -69,7 +74,6 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage) er
|
||||||
|
|
||||||
configuration := provider.loadDockerConfig(dockerClient)
|
configuration := provider.loadDockerConfig(dockerClient)
|
||||||
configurationChan <- types.ConfigMessage{"docker", configuration}
|
configurationChan <- types.ConfigMessage{"docker", configuration}
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -223,15 +227,17 @@ func (provider *Docker) getLabel(container docker.Container, label string) (stri
|
||||||
func (provider *Docker) getLabels(container docker.Container, labels []string) (map[string]string, error) {
|
func (provider *Docker) getLabels(container docker.Container, labels []string) (map[string]string, error) {
|
||||||
foundLabels := map[string]string{}
|
foundLabels := map[string]string{}
|
||||||
for _, label := range labels {
|
for _, label := range labels {
|
||||||
if foundLabel, err := provider.getLabel(container, label); err != nil {
|
foundLabel, err := provider.getLabel(container, label)
|
||||||
|
if err != nil {
|
||||||
return nil, errors.New("Label not found: " + label)
|
return nil, errors.New("Label not found: " + label)
|
||||||
} else {
|
|
||||||
foundLabels[label] = foundLabel
|
|
||||||
}
|
}
|
||||||
|
foundLabels[label] = foundLabel
|
||||||
}
|
}
|
||||||
return foundLabels, nil
|
return foundLabels, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetFrontendValue returns the frontend value for the specified container, using
|
||||||
|
// it's label. It returns a default one if the label is not present.
|
||||||
func (provider *Docker) GetFrontendValue(container docker.Container) string {
|
func (provider *Docker) GetFrontendValue(container docker.Container) string {
|
||||||
if label, err := provider.getLabel(container, "traefik.frontend.value"); err == nil {
|
if label, err := provider.getLabel(container, "traefik.frontend.value"); err == nil {
|
||||||
return label
|
return label
|
||||||
|
@ -239,6 +245,8 @@ func (provider *Docker) GetFrontendValue(container docker.Container) string {
|
||||||
return provider.getEscapedName(container.Name) + "." + provider.Domain
|
return provider.getEscapedName(container.Name) + "." + provider.Domain
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetFrontendRule returns the frontend rule for the specified container, using
|
||||||
|
// it's label. It returns a default one (Host) if the label is not present.
|
||||||
func (provider *Docker) GetFrontendRule(container docker.Container) string {
|
func (provider *Docker) GetFrontendRule(container docker.Container) string {
|
||||||
if label, err := provider.getLabel(container, "traefik.frontend.rule"); err == nil {
|
if label, err := provider.getLabel(container, "traefik.frontend.rule"); err == nil {
|
||||||
return label
|
return label
|
||||||
|
|
|
@ -2,6 +2,7 @@ package provider
|
||||||
|
|
||||||
import "github.com/emilevauge/traefik/types"
|
import "github.com/emilevauge/traefik/types"
|
||||||
|
|
||||||
|
// Etcd holds configurations of the Etcd provider.
|
||||||
type Etcd struct {
|
type Etcd struct {
|
||||||
Watch bool
|
Watch bool
|
||||||
Endpoint string
|
Endpoint string
|
||||||
|
@ -10,6 +11,8 @@ type Etcd struct {
|
||||||
KvProvider *Kv
|
KvProvider *Kv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provide allows the provider to provide configurations to traefik
|
||||||
|
// using the given configuration channel.
|
||||||
func (provider *Etcd) Provide(configurationChan chan<- types.ConfigMessage) error {
|
func (provider *Etcd) Provide(configurationChan chan<- types.ConfigMessage) error {
|
||||||
provider.KvProvider = NewEtcdProvider(provider)
|
provider.KvProvider = NewEtcdProvider(provider)
|
||||||
return provider.KvProvider.provide(configurationChan)
|
return provider.KvProvider.provide(configurationChan)
|
||||||
|
|
|
@ -11,11 +11,14 @@ import (
|
||||||
"gopkg.in/fsnotify.v1"
|
"gopkg.in/fsnotify.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// File holds configurations of the File provider.
|
||||||
type File struct {
|
type File struct {
|
||||||
Watch bool
|
Watch bool
|
||||||
Filename string
|
Filename string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provide allows the provider to provide configurations to traefik
|
||||||
|
// using the given configuration channel.
|
||||||
func (provider *File) Provide(configurationChan chan<- types.ConfigMessage) error {
|
func (provider *File) Provide(configurationChan chan<- types.ConfigMessage) error {
|
||||||
watcher, err := fsnotify.NewWatcher()
|
watcher, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -39,7 +42,7 @@ func (provider *File) Provide(configurationChan chan<- types.ConfigMessage) erro
|
||||||
case event := <-watcher.Events:
|
case event := <-watcher.Events:
|
||||||
if strings.Contains(event.Name, file.Name()) {
|
if strings.Contains(event.Name, file.Name()) {
|
||||||
log.Debug("File event:", event)
|
log.Debug("File event:", event)
|
||||||
configuration := provider.LoadFileConfig(file.Name())
|
configuration := provider.loadFileConfig(file.Name())
|
||||||
if configuration != nil {
|
if configuration != nil {
|
||||||
configurationChan <- types.ConfigMessage{"file", configuration}
|
configurationChan <- types.ConfigMessage{"file", configuration}
|
||||||
}
|
}
|
||||||
|
@ -56,12 +59,12 @@ func (provider *File) Provide(configurationChan chan<- types.ConfigMessage) erro
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration := provider.LoadFileConfig(file.Name())
|
configuration := provider.loadFileConfig(file.Name())
|
||||||
configurationChan <- types.ConfigMessage{"file", configuration}
|
configurationChan <- types.ConfigMessage{"file", configuration}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *File) LoadFileConfig(filename string) *types.Configuration {
|
func (provider *File) loadFileConfig(filename string) *types.Configuration {
|
||||||
configuration := new(types.Configuration)
|
configuration := new(types.Configuration)
|
||||||
if _, err := toml.DecodeFile(filename, configuration); err != nil {
|
if _, err := toml.DecodeFile(filename, configuration); err != nil {
|
||||||
log.Error("Error reading file:", err)
|
log.Error("Error reading file:", err)
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
/*
|
// Package provider holds the different provider implementation.
|
||||||
Copyright
|
|
||||||
*/
|
|
||||||
package provider
|
package provider
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -23,6 +21,7 @@ import (
|
||||||
"github.com/emilevauge/traefik/types"
|
"github.com/emilevauge/traefik/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Kv holds common configurations of key-value providers.
|
||||||
type Kv struct {
|
type Kv struct {
|
||||||
Watch bool
|
Watch bool
|
||||||
Endpoint string
|
Endpoint string
|
||||||
|
@ -32,6 +31,7 @@ type Kv struct {
|
||||||
kvclient store.Store
|
kvclient store.Store
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewConsulProvider returns a Consul provider.
|
||||||
func NewConsulProvider(provider *Consul) *Kv {
|
func NewConsulProvider(provider *Consul) *Kv {
|
||||||
kvProvider := new(Kv)
|
kvProvider := new(Kv)
|
||||||
kvProvider.Watch = provider.Watch
|
kvProvider.Watch = provider.Watch
|
||||||
|
@ -42,6 +42,7 @@ func NewConsulProvider(provider *Consul) *Kv {
|
||||||
return kvProvider
|
return kvProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewEtcdProvider returns a Etcd provider.
|
||||||
func NewEtcdProvider(provider *Etcd) *Kv {
|
func NewEtcdProvider(provider *Etcd) *Kv {
|
||||||
kvProvider := new(Kv)
|
kvProvider := new(Kv)
|
||||||
kvProvider.Watch = provider.Watch
|
kvProvider.Watch = provider.Watch
|
||||||
|
@ -52,6 +53,7 @@ func NewEtcdProvider(provider *Etcd) *Kv {
|
||||||
return kvProvider
|
return kvProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewZkProvider returns a Zookepper provider.
|
||||||
func NewZkProvider(provider *Zookepper) *Kv {
|
func NewZkProvider(provider *Zookepper) *Kv {
|
||||||
kvProvider := new(Kv)
|
kvProvider := new(Kv)
|
||||||
kvProvider.Watch = provider.Watch
|
kvProvider.Watch = provider.Watch
|
||||||
|
@ -62,6 +64,7 @@ func NewZkProvider(provider *Zookepper) *Kv {
|
||||||
return kvProvider
|
return kvProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBoltDbProvider returns a BoldDb provider.
|
||||||
func NewBoltDbProvider(provider *BoltDb) *Kv {
|
func NewBoltDbProvider(provider *BoltDb) *Kv {
|
||||||
kvProvider := new(Kv)
|
kvProvider := new(Kv)
|
||||||
kvProvider.Watch = provider.Watch
|
kvProvider.Watch = provider.Watch
|
||||||
|
|
|
@ -15,15 +15,18 @@ import (
|
||||||
"github.com/gambol99/go-marathon"
|
"github.com/gambol99/go-marathon"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Marathon holds configuration of the Marathon provider.
|
||||||
type Marathon struct {
|
type Marathon struct {
|
||||||
Watch bool
|
Watch bool
|
||||||
Endpoint string
|
Endpoint string
|
||||||
marathonClient marathon.Marathon
|
|
||||||
Domain string
|
Domain string
|
||||||
Filename string
|
Filename string
|
||||||
NetworkInterface string
|
NetworkInterface string
|
||||||
|
marathonClient marathon.Marathon
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provide allows the provider to provide configurations to traefik
|
||||||
|
// using the given configuration channel.
|
||||||
func (provider *Marathon) Provide(configurationChan chan<- types.ConfigMessage) error {
|
func (provider *Marathon) Provide(configurationChan chan<- types.ConfigMessage) error {
|
||||||
config := marathon.NewDefaultConfig()
|
config := marathon.NewDefaultConfig()
|
||||||
config.URL = provider.Endpoint
|
config.URL = provider.Endpoint
|
||||||
|
@ -238,6 +241,8 @@ func (provider *Marathon) getEscapedName(name string) string {
|
||||||
return strings.Replace(name, "/", "", -1)
|
return strings.Replace(name, "/", "", -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetFrontendValue returns the frontend value for the specified application, using
|
||||||
|
// it's label. It returns a default one if the label is not present.
|
||||||
func (provider *Marathon) GetFrontendValue(application marathon.Application) string {
|
func (provider *Marathon) GetFrontendValue(application marathon.Application) string {
|
||||||
if label, err := provider.getLabel(application, "traefik.frontend.value"); err == nil {
|
if label, err := provider.getLabel(application, "traefik.frontend.value"); err == nil {
|
||||||
return label
|
return label
|
||||||
|
@ -245,6 +250,8 @@ func (provider *Marathon) GetFrontendValue(application marathon.Application) str
|
||||||
return provider.getEscapedName(application.ID) + "." + provider.Domain
|
return provider.getEscapedName(application.ID) + "." + provider.Domain
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetFrontendRule returns the frontend rule for the specified application, using
|
||||||
|
// it's label. It returns a default one (Host) if the label is not present.
|
||||||
func (provider *Marathon) GetFrontendRule(application marathon.Application) string {
|
func (provider *Marathon) GetFrontendRule(application marathon.Application) string {
|
||||||
if label, err := provider.getLabel(application, "traefik.frontend.rule"); err == nil {
|
if label, err := provider.getLabel(application, "traefik.frontend.rule"); err == nil {
|
||||||
return label
|
return label
|
||||||
|
|
|
@ -2,6 +2,9 @@ package provider
|
||||||
|
|
||||||
import "github.com/emilevauge/traefik/types"
|
import "github.com/emilevauge/traefik/types"
|
||||||
|
|
||||||
|
// Provider defines methods of a provider.
|
||||||
type Provider interface {
|
type Provider interface {
|
||||||
|
// Provide allows the provider to provide configurations to traefik
|
||||||
|
// using the given configuration channel.
|
||||||
Provide(configurationChan chan<- types.ConfigMessage) error
|
Provide(configurationChan chan<- types.ConfigMessage) error
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package provider
|
||||||
|
|
||||||
import "github.com/emilevauge/traefik/types"
|
import "github.com/emilevauge/traefik/types"
|
||||||
|
|
||||||
|
// Zookepper holds configurations of the Zookepper provider.
|
||||||
type Zookepper struct {
|
type Zookepper struct {
|
||||||
Watch bool
|
Watch bool
|
||||||
Endpoint string
|
Endpoint string
|
||||||
|
@ -10,6 +11,8 @@ type Zookepper struct {
|
||||||
KvProvider *Kv
|
KvProvider *Kv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Provide allows the provider to provide configurations to traefik
|
||||||
|
// using the given configuration channel.
|
||||||
func (provider *Zookepper) Provide(configurationChan chan<- types.ConfigMessage) error {
|
func (provider *Zookepper) Provide(configurationChan chan<- types.ConfigMessage) error {
|
||||||
provider.KvProvider = NewZkProvider(provider)
|
provider.KvProvider = NewZkProvider(provider)
|
||||||
return provider.KvProvider.provide(configurationChan)
|
return provider.KvProvider.provide(configurationChan)
|
||||||
|
|
Loading…
Add table
Reference in a new issue