Skip to content

Commit

Permalink
Inject hooks to executor constructor.
Browse files Browse the repository at this point in the history
Move creation and configuration of hooks to main function.
This will enable users to simply replace main.go with
their own implementation to use custom hooks.
Hooks configuration is indepenedent from Executor configuration
so there is no need to keep it in Executor.
  • Loading branch information
janisz committed Nov 23, 2017
1 parent 9f3058b commit 7ff14a8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
27 changes: 26 additions & 1 deletion cmd/executor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/mesos/mesos-go/api/v1/lib/executor/config"

"github.com/allegro/mesos-executor"
"github.com/allegro/mesos-executor/hook"
"github.com/allegro/mesos-executor/hook/consul"
"github.com/allegro/mesos-executor/hook/vaas"
"github.com/allegro/mesos-executor/runenv"
)

Expand Down Expand Up @@ -75,14 +78,36 @@ func initSentry(config executor.Config) error {
return nil
}

func createHooks() []hook.Hook {
var consulConfig consul.Config
if err := envconfig.Process(environmentPrefix, &consulConfig); err != nil {
log.WithError(err).Fatal("Failed to load Consul hook configuration")
}
consulHook, err := consul.NewHook(consulConfig)
if err != nil {
log.WithError(err).Fatalf("Error loading Consul hook %s", err)
}

var vaasConfig vaas.Config
if err := envconfig.Process(environmentPrefix, &vaasConfig); err != nil {
log.WithError(err).Fatal("Failed to load VaaS hook configuration")
}
vaasHook, err := vaas.NewHook(vaasConfig)
if err != nil {
log.WithError(err).Fatalf("Error loading VaaS service hook %s", err)
}

return []hook.Hook{consulHook, vaasHook}
}

func main() {
log.Infof("Allegro Mesos Executor (version: %s)", Version)
cfg, err := config.FromEnv()
if err != nil {
log.WithError(err).Fatal("Failed to load Mesos configuration")
}
Config.MesosConfig = cfg
exec := executor.NewExecutor(Config)
exec := executor.NewExecutor(Config, createHooks()...)
if err := exec.Start(); err != nil {
log.WithError(err).Fatal("Executor exited with error")
}
Expand Down
32 changes: 3 additions & 29 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"github.com/mesos/mesos-go/api/v1/lib/httpcli"

"github.com/allegro/mesos-executor/hook"
"github.com/allegro/mesos-executor/hook/consul"
"github.com/allegro/mesos-executor/hook/vaas"
"github.com/allegro/mesos-executor/mesosutils"
"github.com/allegro/mesos-executor/state"
)
Expand All @@ -47,16 +45,6 @@ type Config struct {
// Mesos framework configuration
MesosConfig config.Config `ignore:"true"`

// Varnish as a Service API url
VaasAPIHost string `default:"" envconfig:"vaas_host"`
// Varnish as a Service username
VaasAPIUsername string `default:"" envconfig:"vaas_username"`
// Varnish as a Service access token
VaasAPIKey string `default:"" envconfig:"vaas_token"`

// Consul ACL Token
ConsulToken string `default:"" envconfig:"consul_token"`

// SentryDSN is an address used for sending logs to Sentry
SentryDSN string `split_words:"true"`

Expand Down Expand Up @@ -124,8 +112,8 @@ const (
Launch
)

// NewExecutor creates new instance of executor configured with by `cfg`.
func NewExecutor(cfg Config) *Executor {
// NewExecutor creates new instance of executor configured with by `cfg` with hooks
func NewExecutor(cfg Config, hooks ...hook.Hook) *Executor {

log.Info("Initializing executor with following configuration:")
log.Infof("AgentEndpoint = %s", cfg.MesosConfig.AgentEndpoint)
Expand All @@ -145,27 +133,13 @@ func NewExecutor(cfg Config) *Executor {
context: ctx,
contextCancel: ctxCancel,
events: make(chan Event),
hookManager: newHookManager(cfg),
hookManager: hook.Manager{hooks},
stateUpdater: state.BufferedUpdater(cfg.MesosConfig, cfg.StateUpdateBufferSize),
clock: systemClock{},
random: newRandom(),
}
}

func newHookManager(config Config) hook.Manager {
vaasHook, err := vaas.NewHook(config.VaasAPIHost, config.VaasAPIUsername, config.VaasAPIKey)
if err != nil {
log.WithError(err).Fatalf("Error loading VaaS service hook %s", err)
}

consulHook, err := consul.NewHook(config.ConsulToken)
if err != nil {
log.WithError(err).Fatalf("Error loading Consul hook %s", err)
}

return hook.Manager{Hooks: []hook.Hook{vaasHook, consulHook}}
}

// Start registers executor in Mesos agent and waits for events from it.
func (e *Executor) Start() error {

Expand Down
10 changes: 8 additions & 2 deletions hook/consul/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ type Hook struct {
serviceInstances []instance
}

// Config is Consul hook configuration settable from environment
type Config struct {
// Consul ACL Token
ConsulToken string `default:"" envconfig:"consul_token"`
}

// HandleEvent calls appropriate hook functions that correspond to supported
// event types. Unsupported events are ignored.
func (h *Hook) HandleEvent(event hook.Event) error {
Expand Down Expand Up @@ -206,9 +212,9 @@ func generateURL(info *mesos.HealthCheck_HTTPCheckInfo, port int) string {
}

// NewHook creates new Consul hook that is responsible for graceful Consul deregistration.
func NewHook(token string) (hook.Hook, error) {
func NewHook(cfg Config) (hook.Hook, error) {
config := api.DefaultConfig()
config.Token = token
config.Token = cfg.ConsulToken
client, err := api.NewClient(config)
if err != nil {
return nil, err
Expand Down
18 changes: 14 additions & 4 deletions hook/vaas/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ type Hook struct {
client Client
}

// Config is Varnish configuration settable from environment
type Config struct {
// Varnish as a Service API url
VaasAPIHost string `default:"" envconfig:"vaas_host"`
// Varnish as a Service username
VaasAPIUsername string `default:"" envconfig:"vaas_username"`
// Varnish as a Service access token
VaasAPIKey string `default:"" envconfig:"vaas_token"`
}

// RegisterBackend adds new backend to VaaS if it does not exist.
func (sh *Hook) RegisterBackend(taskInfo mesos.TaskInfo) error {
handyTaskInfo := mesosutils.TaskInfo{TaskInfo: taskInfo}
Expand Down Expand Up @@ -200,12 +210,12 @@ func (sh *Hook) HandleEvent(event hook.Event) error {
}

// NewHook returns new instance of Hook.
func NewHook(apiHost string, apiUsername string, apiKey string) (*Hook, error) {
func NewHook(cfg Config) (*Hook, error) {
return &Hook{
client: NewClient(
apiHost,
apiUsername,
apiKey,
cfg.VaasAPIHost,
cfg.VaasAPIUsername,
cfg.VaasAPIKey,
),
}, nil
}

0 comments on commit 7ff14a8

Please sign in to comment.