2019-11-28 20:56:04 +00:00
package consul
import (
2022-01-12 13:42:21 +00:00
"errors"
"github.com/kvtools/valkeyrie/store"
2022-06-03 10:00:09 +00:00
"github.com/traefik/traefik/v2/pkg/log"
2020-09-16 13:46:04 +00:00
"github.com/traefik/traefik/v2/pkg/provider"
"github.com/traefik/traefik/v2/pkg/provider/kv"
2019-11-28 20:56:04 +00:00
)
2022-06-03 10:00:09 +00:00
// providerName is the Consul provider name.
const providerName = "consul"
2019-11-28 20:56:04 +00:00
var _ provider . Provider = ( * Provider ) ( nil )
2022-06-03 10:00:09 +00:00
// ProviderBuilder is responsible for constructing namespaced instances of the Consul provider.
type ProviderBuilder struct {
2020-10-30 11:44:05 +00:00
kv . Provider ` export:"true" `
2022-06-03 10:00:09 +00:00
// Deprecated: use Namespaces instead.
Namespace string ` description:"Sets the namespace used to discover the configuration (Consul Enterprise only)." json:"namespace,omitempty" toml:"namespace,omitempty" yaml:"namespace,omitempty" `
Namespaces [ ] string ` description:"Sets the namespaces used to discover the configuration (Consul Enterprise only)." json:"namespaces,omitempty" toml:"namespaces,omitempty" yaml:"namespaces,omitempty" `
2019-11-28 20:56:04 +00:00
}
// SetDefaults sets the default values.
2022-06-03 10:00:09 +00:00
func ( p * ProviderBuilder ) SetDefaults ( ) {
2019-11-28 20:56:04 +00:00
p . Provider . SetDefaults ( )
p . Endpoints = [ ] string { "127.0.0.1:8500" }
}
2022-06-03 10:00:09 +00:00
// BuildProviders builds Consul provider instances for the given namespaces configuration.
func ( p * ProviderBuilder ) BuildProviders ( ) [ ] * Provider {
// We can warn about that, because we've already made sure before that
// Namespace and Namespaces are mutually exclusive.
if p . Namespace != "" {
log . WithoutContext ( ) . Warnf ( "Namespace option is deprecated, please use the Namespaces option instead." )
}
if len ( p . Namespaces ) == 0 {
return [ ] * Provider { {
Provider : p . Provider ,
name : providerName ,
// p.Namespace could very well be empty.
namespace : p . Namespace ,
} }
}
var providers [ ] * Provider
for _ , namespace := range p . Namespaces {
providers = append ( providers , & Provider {
Provider : p . Provider ,
name : providerName + "-" + namespace ,
namespace : namespace ,
} )
}
return providers
}
// Provider holds configurations of the provider.
type Provider struct {
kv . Provider
name string
namespace string
}
2020-05-11 10:06:07 +00:00
// Init the provider.
2019-11-28 20:56:04 +00:00
func ( p * Provider ) Init ( ) error {
2022-01-12 13:42:21 +00:00
// Wildcard namespace allows fetching KV values from any namespace for recursive requests (see https://www.consul.io/api/kv#ns).
// As we are not supporting multiple namespaces at the same time, wildcard namespace is not allowed.
2022-06-03 10:00:09 +00:00
if p . namespace == "*" {
2022-01-12 13:42:21 +00:00
return errors . New ( "wildcard namespace is not supported" )
}
2022-06-03 10:00:09 +00:00
// In case they didn't initialize with BuildProviders.
if p . name == "" {
p . name = providerName
}
return p . Provider . Init ( store . CONSUL , p . name , p . namespace )
2019-11-28 20:56:04 +00:00
}