Skip to content

Commit

Permalink
✨ Add namespace feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kasefuchs committed Nov 6, 2024
1 parent 843ddad commit 873fbb3
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 8 deletions.
4 changes: 3 additions & 1 deletion pkg/config/allocation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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),
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/plugin/config.go
Original file line number Diff line number Diff line change
@@ -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",
}
}
30 changes: 30 additions & 0 deletions pkg/config/plugin/parse.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 14 additions & 3 deletions pkg/plugin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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)
}
}

Expand All @@ -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,
}
}
15 changes: 14 additions & 1 deletion pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 10 additions & 3 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
}
}

Expand Down

0 comments on commit 873fbb3

Please # to comment.