Skip to content

Commit 62bba84

Browse files
authored
Allow disabling hooks. (#102)
Added configuration option to disable specific hooks. When hook is diabled, trying to create it from configration results in creation of `NoopHook` ignoring all events. Fixes: #100
1 parent a7e50fa commit 62bba84

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

hook/consul/hook.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type Hook struct {
4141

4242
// Config is Consul hook configuration settable from environment
4343
type Config struct {
44+
// Enabled is a flag to control whether hook should be used
45+
Enabled bool `default:"true" envconfig:"consul_hook_enabled"`
4446
// Consul ACL Token
4547
ConsulToken string `default:"" envconfig:"consul_token"`
4648
// ConsulGlobalTag is a tag added to every service registered in Consul.
@@ -213,6 +215,9 @@ func generateURL(path string, port int) string {
213215

214216
// NewHook creates new Consul hook that is responsible for graceful Consul deregistration.
215217
func NewHook(cfg Config) (hook.Hook, error) {
218+
if !cfg.Enabled {
219+
return hook.NoopHook{}, nil
220+
}
216221
config := api.DefaultConfig()
217222
config.Token = cfg.ConsulToken
218223
client, err := api.NewClient(config)

hook/consul/hook_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func TestIfGeneratesCorrectNameIfConsulLabelEmpty(t *testing.T) {
286286
}
287287

288288
func TestIfNoErrorOnUnsupportedEvent(t *testing.T) {
289-
h, err := NewHook(Config{})
289+
h, err := NewHook(Config{Enabled: true})
290290

291291
require.NoError(t, err)
292292

@@ -346,6 +346,13 @@ func TestIfErrorHandledOnNoConsul(t *testing.T) {
346346
require.Len(t, h.serviceInstances, 0)
347347
}
348348

349+
func TestIfNewHookCreatesNoopHookWhenHookDisabled(t *testing.T) {
350+
h, err := NewHook(Config{Enabled: false})
351+
352+
require.NoError(t, err)
353+
require.IsType(t, hook.NoopHook{}, h)
354+
}
355+
349356
func stopConsul(server *testutil.TestServer) {
350357
_ = server.Stop()
351358
}

hook/hook.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ const (
1919
BeforeTerminateEvent
2020
)
2121

22+
// NoopHook is a hook that ignores all events
23+
type NoopHook struct {
24+
}
25+
26+
// HandleEvent ignores all events
27+
func (noop NoopHook) HandleEvent(event Event) (Env, error) {
28+
return nil, nil
29+
}
30+
2231
// Event is a container type for various event specific data.
2332
type Event struct {
2433
Type EventType

hook/vaas/hook.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type Hook struct {
3333

3434
// Config is Varnish configuration settable from environment
3535
type Config struct {
36+
// Enabled is a flag to control whether hook should be used
37+
Enabled bool `default:"true" envconfig:"vaas_hook_enabled"`
3638
// Varnish as a Service API url
3739
VaasAPIHost string `default:"" envconfig:"vaas_host"`
3840
// Varnish as a Service username
@@ -161,7 +163,10 @@ func (sh *Hook) HandleEvent(event hook.Event) (hook.Env, error) {
161163
}
162164

163165
// NewHook returns new instance of Hook.
164-
func NewHook(cfg Config) (*Hook, error) {
166+
func NewHook(cfg Config) (hook.Hook, error) {
167+
if !cfg.Enabled {
168+
return hook.NoopHook{}, nil
169+
}
165170
return &Hook{
166171
client: NewClient(
167172
cfg.VaasAPIHost,

hook/vaas/hook_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func TestDoNotRegisterVaasBackendWhenDirectorNotSet(t *testing.T) {
259259
}
260260

261261
func TestIfNoErrorOnUnsupportedEvent(t *testing.T) {
262-
h, err := NewHook(Config{})
262+
h, err := NewHook(Config{Enabled: true})
263263

264264
require.NoError(t, err)
265265

@@ -269,3 +269,10 @@ func TestIfNoErrorOnUnsupportedEvent(t *testing.T) {
269269

270270
require.NoError(t, err)
271271
}
272+
273+
func TestIfNewHookCreatesNoopHookWhenHookDisabled(t *testing.T) {
274+
h, err := NewHook(Config{Enabled: false})
275+
276+
require.NoError(t, err)
277+
assert.IsType(t, hook.NoopHook{}, h)
278+
}

0 commit comments

Comments
 (0)