diff --git a/pkg/config/allocation/config.go b/pkg/config/allocation/config.go index f09561f..8d3204c 100644 --- a/pkg/config/allocation/config.go +++ b/pkg/config/allocation/config.go @@ -9,7 +9,8 @@ import ( // Config represents specific server config. type Config struct { - Server string `validate:"required"` // The upstream server name. + Server string `validate:"required"` // The upstream server name. + Namespace string // Namespace to associate this allocation with. Time *Time // Time related server configuration. Queue *Queue // Queue related configuration. @@ -42,6 +43,7 @@ type QueueWait struct { // DefaultConfig returns default config. func DefaultConfig() *Config { return &Config{ + Namespace: "default", Time: &Time{ MinimumOnline: ptypes.Duration(time.Minute), InactivityThreshold: ptypes.Duration(time.Minute), diff --git a/pkg/config/plugin/config.go b/pkg/config/plugin/config.go new file mode 100644 index 0000000..1a12772 --- /dev/null +++ b/pkg/config/plugin/config.go @@ -0,0 +1,13 @@ +package plugin + +// Config represents plugin config. +type Config struct { + Namespace string // Namespace of this proxy. +} + +// DefaultConfig returns default config. +func DefaultConfig() *Config { + return &Config{ + Namespace: "default", + } +} diff --git a/pkg/config/plugin/parse.go b/pkg/config/plugin/parse.go new file mode 100644 index 0000000..7a0f3a6 --- /dev/null +++ b/pkg/config/plugin/parse.go @@ -0,0 +1,30 @@ +package plugin + +import ( + "os" + + "github.com/go-playground/validator/v10" + "github.com/traefik/paerser/env" +) + +// Root env prefix. +const rootPrefix = "LAZYGATE_" + +// Config validator. +var validate = validator.New() + +// ParseEnv parses plugin configuration from environment. +func ParseEnv() (*Config, error) { + cfg := DefaultConfig() + + vars := env.FindPrefixedEnvVars(os.Environ(), rootPrefix, cfg) + if err := env.Decode(vars, rootPrefix, cfg); err != nil { + return nil, err + } + + if err := validate.Struct(cfg); err != nil { + return nil, err + } + + return cfg, nil +} diff --git a/pkg/plugin/options.go b/pkg/plugin/options.go index 0ff2f2c..4befae4 100644 --- a/pkg/plugin/options.go +++ b/pkg/plugin/options.go @@ -5,6 +5,7 @@ import ( "os" "strings" + pconfig "github.com/kasefuchs/lazygate/pkg/config/plugin" "github.com/kasefuchs/lazygate/pkg/provider" "github.com/kasefuchs/lazygate/pkg/provider/docker" "github.com/kasefuchs/lazygate/pkg/provider/nomad" @@ -19,10 +20,14 @@ type providerSelector func() (provider.Provider, error) // queuesSelector represents function used to select queues to use. type queuesSelector func() ([]queue.Queue, error) -// Options contains customizable plugin options. +// configLoader represents function used to load pconfig configuration. +type configLoader func() (*pconfig.Config, error) + +// Options contains customizable pconfig options. type Options struct { ProviderSelector providerSelector // Selector of provider. QueuesSelector queuesSelector // Selector of available queues. + ConfigLoader configLoader // Loader of plugin config. } // DefaultProviderSelector contains default provider selector. @@ -35,9 +40,9 @@ func DefaultProviderSelector() (provider.Provider, error) { case "docker": return &docker.Provider{}, nil case "": - return nil, fmt.Errorf("no plugin provider specified") + return nil, fmt.Errorf("no allocation provider specified") default: - return nil, fmt.Errorf("unknown provider: %s", name) + return nil, fmt.Errorf("unknown allocation provider: %s", name) } } @@ -49,10 +54,16 @@ func DefaultQueuesSelector() ([]queue.Queue, error) { }, nil } +// DefaultConfigLoader loads plugin config from environment. +func DefaultConfigLoader() (*pconfig.Config, error) { + return pconfig.ParseEnv() +} + // DefaultOptions returns options object with default parameters. func DefaultOptions() *Options { return &Options{ ProviderSelector: DefaultProviderSelector, QueuesSelector: DefaultQueuesSelector, + ConfigLoader: DefaultConfigLoader, } } diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index f20be3b..3370c0d 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -5,6 +5,7 @@ import ( "math" "github.com/go-logr/logr" + pconfig "github.com/kasefuchs/lazygate/pkg/config/plugin" "github.com/kasefuchs/lazygate/pkg/provider" "github.com/kasefuchs/lazygate/pkg/queue" "github.com/kasefuchs/lazygate/pkg/registry" @@ -24,6 +25,7 @@ type Plugin struct { log logr.Logger // Plugin logger. proxy *proxy.Proxy // Gate proxy instance. queues *queue.Repository // Plugin queues repository. + config *pconfig.Config // Plugin configuration. options *Options // Plugin options. registry *registry.Registry // Plugin registry. provider provider.Provider // Allocation provider. @@ -54,6 +56,14 @@ func NewProxyPlugin(options ...*Options) proxy.Plugin { } } +// initConfig loads plugin config. +func (p *Plugin) initConfig() error { + var err error + p.config, err = p.options.ConfigLoader() + + return err +} + // initProvider initializes server provider. func (p *Plugin) initProvider() error { var err error @@ -72,7 +82,7 @@ func (p *Plugin) initProvider() error { // initRegistry initializes new registry. func (p *Plugin) initRegistry() error { p.registry = registry.NewRegistry(p.proxy, p.provider) - p.registry.Refresh() + p.registry.Refresh(p.config.Namespace) return nil } @@ -121,6 +131,9 @@ func (p *Plugin) initHandlers() error { func (p *Plugin) Init() error { p.log = logr.FromContextOrDiscard(p.ctx).WithName(logName) + if err := p.initConfig(); err != nil { + return err + } if err := p.initProvider(); err != nil { return err } diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index 85cba73..f52c6dc 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -29,7 +29,7 @@ func (r *Registry) Clear() { } // Refresh updates registry data with new info. -func (r *Registry) Refresh() { +func (r *Registry) Refresh(namespace string) { r.Clear() for _, srv := range r.proxy.Servers() { @@ -38,8 +38,15 @@ func (r *Registry) Refresh() { continue } - ent := NewEntry(srv, alloc) - r.EntryRegister(ent) + cfg, err := alloc.Config() + if err != nil { + continue + } + + if cfg.Namespace == namespace { + ent := NewEntry(srv, alloc) + r.EntryRegister(ent) + } } }