From f0658a0ede454bb23a5421dbb7d1cab701e13734 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 29 Jun 2018 23:09:58 -0700 Subject: [PATCH] agent/config: support configuring alias check --- agent/config/builder.go | 2 ++ agent/config/config.go | 2 ++ agent/config/runtime_test.go | 22 ++++++++++++++++++++++ agent/structs/check_definition.go | 4 ++++ agent/structs/check_type.go | 19 ++++++++++++++++--- 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/agent/config/builder.go b/agent/config/builder.go index e7958056e1..940e6f2074 100644 --- a/agent/config/builder.go +++ b/agent/config/builder.go @@ -1046,6 +1046,8 @@ func (b *Builder) checkVal(v *CheckDefinition) *structs.CheckDefinition { GRPC: b.stringVal(v.GRPC), GRPCUseTLS: b.boolVal(v.GRPCUseTLS), TLSSkipVerify: b.boolVal(v.TLSSkipVerify), + AliasNode: b.stringVal(v.AliasNode), + AliasService: b.stringVal(v.AliasService), Timeout: b.durationVal(fmt.Sprintf("check[%s].timeout", id), v.Timeout), TTL: b.durationVal(fmt.Sprintf("check[%s].ttl", id), v.TTL), DeregisterCriticalServiceAfter: b.durationVal(fmt.Sprintf("check[%s].deregister_critical_service_after", id), v.DeregisterCriticalServiceAfter), diff --git a/agent/config/config.go b/agent/config/config.go index ab81c2f718..c54090b3f7 100644 --- a/agent/config/config.go +++ b/agent/config/config.go @@ -348,6 +348,8 @@ type CheckDefinition struct { GRPC *string `json:"grpc,omitempty" hcl:"grpc" mapstructure:"grpc"` GRPCUseTLS *bool `json:"grpc_use_tls,omitempty" hcl:"grpc_use_tls" mapstructure:"grpc_use_tls"` TLSSkipVerify *bool `json:"tls_skip_verify,omitempty" hcl:"tls_skip_verify" mapstructure:"tls_skip_verify"` + AliasNode *string `json:"alias_node,omitempty" hcl:"alias_node" mapstructure:"alias_node"` + AliasService *string `json:"alias_service,omitempty" hcl:"alias_service" mapstructure:"alias_service"` Timeout *string `json:"timeout,omitempty" hcl:"timeout" mapstructure:"timeout"` TTL *string `json:"ttl,omitempty" hcl:"ttl" mapstructure:"ttl"` DeregisterCriticalServiceAfter *string `json:"deregister_critical_service_after,omitempty" hcl:"deregister_critical_service_after" mapstructure:"deregister_critical_service_after"` diff --git a/agent/config/runtime_test.go b/agent/config/runtime_test.go index 4df0bd5a09..1be66cc4bb 100644 --- a/agent/config/runtime_test.go +++ b/agent/config/runtime_test.go @@ -1941,6 +1941,24 @@ func TestConfigFlagsAndEdgecases(t *testing.T) { rt.DataDir = dataDir }, }, + { + desc: "alias check with no node", + args: []string{ + `-data-dir=` + dataDir, + }, + json: []string{ + `{ "check": { "name": "a", "alias_service": "foo" } }`, + }, + hcl: []string{ + `check = { name = "a", alias_service = "foo" }`, + }, + patch: func(rt *RuntimeConfig) { + rt.Checks = []*structs.CheckDefinition{ + &structs.CheckDefinition{Name: "a", AliasService: "foo"}, + } + rt.DataDir = dataDir + }, + }, { desc: "multiple service files", args: []string{ @@ -4271,6 +4289,8 @@ func TestSanitize(t *testing.T) { "CheckUpdateInterval": "0s", "Checks": [ { + "AliasNode": "", + "AliasService": "", "DeregisterCriticalServiceAfter": "0s", "DockerContainerID": "", "GRPC": "", @@ -4417,6 +4437,8 @@ func TestSanitize(t *testing.T) { { "Address": "", "Check": { + "AliasNode": "", + "AliasService": "", "CheckID": "", "DeregisterCriticalServiceAfter": "0s", "DockerContainerID": "", diff --git a/agent/structs/check_definition.go b/agent/structs/check_definition.go index 0ee6c20495..42e9692fce 100644 --- a/agent/structs/check_definition.go +++ b/agent/structs/check_definition.go @@ -32,6 +32,8 @@ type CheckDefinition struct { GRPC string GRPCUseTLS bool TLSSkipVerify bool + AliasNode string + AliasService string Timeout time.Duration TTL time.Duration DeregisterCriticalServiceAfter time.Duration @@ -63,6 +65,8 @@ func (c *CheckDefinition) CheckType() *CheckType { Notes: c.Notes, ScriptArgs: c.ScriptArgs, + AliasNode: c.AliasNode, + AliasService: c.AliasService, HTTP: c.HTTP, GRPC: c.GRPC, GRPCUseTLS: c.GRPCUseTLS, diff --git a/agent/structs/check_type.go b/agent/structs/check_type.go index 23a6830777..8586e77263 100644 --- a/agent/structs/check_type.go +++ b/agent/structs/check_type.go @@ -9,10 +9,10 @@ import ( ) // CheckType is used to create either the CheckMonitor or the CheckTTL. -// Six types are supported: Script, HTTP, TCP, Docker, TTL and GRPC. Script, +// The following types are supported: Script, HTTP, TCP, Docker, TTL, GRPC, Alias. Script, // HTTP, Docker, TCP and GRPC all require Interval. Only one of the types may // to be provided: TTL or Script/Interval or HTTP/Interval or TCP/Interval or -// Docker/Interval or GRPC/Interval. +// Docker/Interval or GRPC/Interval or AliasService. type CheckType struct { // fields already embedded in CheckDefinition // Note: CheckType.CheckID == CheckDefinition.ID @@ -31,6 +31,8 @@ type CheckType struct { Method string TCP string Interval time.Duration + AliasNode string + AliasService string DockerContainerID string Shell string GRPC string @@ -56,7 +58,13 @@ func (c *CheckType) Validate() error { if intervalCheck && c.Interval <= 0 { return fmt.Errorf("Interval must be > 0 for Script, HTTP, or TCP checks") } - if !intervalCheck && c.TTL <= 0 { + if intervalCheck && c.IsAlias() { + return fmt.Errorf("Interval cannot be set for Alias checks") + } + if c.IsAlias() && c.TTL > 0 { + return fmt.Errorf("TTL must be not be set for Alias checks") + } + if !intervalCheck && !c.IsAlias() && c.TTL <= 0 { return fmt.Errorf("TTL must be > 0 for TTL checks") } return nil @@ -67,6 +75,11 @@ func (c *CheckType) Empty() bool { return reflect.DeepEqual(c, &CheckType{}) } +// IsAlias checks if this is an alias check. +func (c *CheckType) IsAlias() bool { + return c.AliasService != "" +} + // IsScript checks if this is a check that execs some kind of script. func (c *CheckType) IsScript() bool { return len(c.ScriptArgs) > 0