mirror of https://github.com/hashicorp/consul
agent/config: support configuring alias check
parent
632e4a2c69
commit
f0658a0ede
|
@ -1046,6 +1046,8 @@ func (b *Builder) checkVal(v *CheckDefinition) *structs.CheckDefinition {
|
||||||
GRPC: b.stringVal(v.GRPC),
|
GRPC: b.stringVal(v.GRPC),
|
||||||
GRPCUseTLS: b.boolVal(v.GRPCUseTLS),
|
GRPCUseTLS: b.boolVal(v.GRPCUseTLS),
|
||||||
TLSSkipVerify: b.boolVal(v.TLSSkipVerify),
|
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),
|
Timeout: b.durationVal(fmt.Sprintf("check[%s].timeout", id), v.Timeout),
|
||||||
TTL: b.durationVal(fmt.Sprintf("check[%s].ttl", id), v.TTL),
|
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),
|
DeregisterCriticalServiceAfter: b.durationVal(fmt.Sprintf("check[%s].deregister_critical_service_after", id), v.DeregisterCriticalServiceAfter),
|
||||||
|
|
|
@ -348,6 +348,8 @@ type CheckDefinition struct {
|
||||||
GRPC *string `json:"grpc,omitempty" hcl:"grpc" mapstructure:"grpc"`
|
GRPC *string `json:"grpc,omitempty" hcl:"grpc" mapstructure:"grpc"`
|
||||||
GRPCUseTLS *bool `json:"grpc_use_tls,omitempty" hcl:"grpc_use_tls" mapstructure:"grpc_use_tls"`
|
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"`
|
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"`
|
Timeout *string `json:"timeout,omitempty" hcl:"timeout" mapstructure:"timeout"`
|
||||||
TTL *string `json:"ttl,omitempty" hcl:"ttl" mapstructure:"ttl"`
|
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"`
|
DeregisterCriticalServiceAfter *string `json:"deregister_critical_service_after,omitempty" hcl:"deregister_critical_service_after" mapstructure:"deregister_critical_service_after"`
|
||||||
|
|
|
@ -1941,6 +1941,24 @@ func TestConfigFlagsAndEdgecases(t *testing.T) {
|
||||||
rt.DataDir = dataDir
|
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",
|
desc: "multiple service files",
|
||||||
args: []string{
|
args: []string{
|
||||||
|
@ -4271,6 +4289,8 @@ func TestSanitize(t *testing.T) {
|
||||||
"CheckUpdateInterval": "0s",
|
"CheckUpdateInterval": "0s",
|
||||||
"Checks": [
|
"Checks": [
|
||||||
{
|
{
|
||||||
|
"AliasNode": "",
|
||||||
|
"AliasService": "",
|
||||||
"DeregisterCriticalServiceAfter": "0s",
|
"DeregisterCriticalServiceAfter": "0s",
|
||||||
"DockerContainerID": "",
|
"DockerContainerID": "",
|
||||||
"GRPC": "",
|
"GRPC": "",
|
||||||
|
@ -4417,6 +4437,8 @@ func TestSanitize(t *testing.T) {
|
||||||
{
|
{
|
||||||
"Address": "",
|
"Address": "",
|
||||||
"Check": {
|
"Check": {
|
||||||
|
"AliasNode": "",
|
||||||
|
"AliasService": "",
|
||||||
"CheckID": "",
|
"CheckID": "",
|
||||||
"DeregisterCriticalServiceAfter": "0s",
|
"DeregisterCriticalServiceAfter": "0s",
|
||||||
"DockerContainerID": "",
|
"DockerContainerID": "",
|
||||||
|
|
|
@ -32,6 +32,8 @@ type CheckDefinition struct {
|
||||||
GRPC string
|
GRPC string
|
||||||
GRPCUseTLS bool
|
GRPCUseTLS bool
|
||||||
TLSSkipVerify bool
|
TLSSkipVerify bool
|
||||||
|
AliasNode string
|
||||||
|
AliasService string
|
||||||
Timeout time.Duration
|
Timeout time.Duration
|
||||||
TTL time.Duration
|
TTL time.Duration
|
||||||
DeregisterCriticalServiceAfter time.Duration
|
DeregisterCriticalServiceAfter time.Duration
|
||||||
|
@ -63,6 +65,8 @@ func (c *CheckDefinition) CheckType() *CheckType {
|
||||||
Notes: c.Notes,
|
Notes: c.Notes,
|
||||||
|
|
||||||
ScriptArgs: c.ScriptArgs,
|
ScriptArgs: c.ScriptArgs,
|
||||||
|
AliasNode: c.AliasNode,
|
||||||
|
AliasService: c.AliasService,
|
||||||
HTTP: c.HTTP,
|
HTTP: c.HTTP,
|
||||||
GRPC: c.GRPC,
|
GRPC: c.GRPC,
|
||||||
GRPCUseTLS: c.GRPCUseTLS,
|
GRPCUseTLS: c.GRPCUseTLS,
|
||||||
|
|
|
@ -9,10 +9,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// CheckType is used to create either the CheckMonitor or the CheckTTL.
|
// 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
|
// 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
|
// 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 {
|
type CheckType struct {
|
||||||
// fields already embedded in CheckDefinition
|
// fields already embedded in CheckDefinition
|
||||||
// Note: CheckType.CheckID == CheckDefinition.ID
|
// Note: CheckType.CheckID == CheckDefinition.ID
|
||||||
|
@ -31,6 +31,8 @@ type CheckType struct {
|
||||||
Method string
|
Method string
|
||||||
TCP string
|
TCP string
|
||||||
Interval time.Duration
|
Interval time.Duration
|
||||||
|
AliasNode string
|
||||||
|
AliasService string
|
||||||
DockerContainerID string
|
DockerContainerID string
|
||||||
Shell string
|
Shell string
|
||||||
GRPC string
|
GRPC string
|
||||||
|
@ -56,7 +58,13 @@ func (c *CheckType) Validate() error {
|
||||||
if intervalCheck && c.Interval <= 0 {
|
if intervalCheck && c.Interval <= 0 {
|
||||||
return fmt.Errorf("Interval must be > 0 for Script, HTTP, or TCP checks")
|
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 fmt.Errorf("TTL must be > 0 for TTL checks")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -67,6 +75,11 @@ func (c *CheckType) Empty() bool {
|
||||||
return reflect.DeepEqual(c, &CheckType{})
|
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.
|
// IsScript checks if this is a check that execs some kind of script.
|
||||||
func (c *CheckType) IsScript() bool {
|
func (c *CheckType) IsScript() bool {
|
||||||
return len(c.ScriptArgs) > 0
|
return len(c.ScriptArgs) > 0
|
||||||
|
|
Loading…
Reference in New Issue