2023-03-28 19:39:22 +01:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 09:12:13 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:39:22 +01:00
|
|
|
|
2017-06-15 18:46:06 +02:00
|
|
|
package structs
|
2014-01-30 15:35:38 -08:00
|
|
|
|
2014-02-03 15:15:35 -08:00
|
|
|
import (
|
2017-05-15 21:49:13 +02:00
|
|
|
"time"
|
|
|
|
|
2022-04-05 14:10:06 -07:00
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
|
2017-04-19 16:00:11 -07:00
|
|
|
"github.com/hashicorp/consul/api"
|
2019-12-06 11:14:56 -05:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2016-06-06 13:19:31 -07:00
|
|
|
"github.com/hashicorp/consul/types"
|
2014-02-03 15:15:35 -08:00
|
|
|
)
|
|
|
|
|
2016-06-07 15:24:51 -05:00
|
|
|
// CheckDefinition is used to JSON decode the Check definitions
|
2014-01-30 15:35:38 -08:00
|
|
|
type CheckDefinition struct {
|
2016-06-06 13:19:31 -07:00
|
|
|
ID types.CheckID
|
2014-02-03 15:15:35 -08:00
|
|
|
Name string
|
|
|
|
Notes string
|
2015-01-13 17:52:17 -08:00
|
|
|
ServiceID string
|
2015-04-28 12:44:46 -07:00
|
|
|
Token string
|
2015-04-12 00:53:48 +00:00
|
|
|
Status string
|
2017-05-15 21:49:13 +02:00
|
|
|
|
|
|
|
// Copied fields from CheckType without the fields
|
|
|
|
// already present in CheckDefinition:
|
|
|
|
//
|
|
|
|
// ID (CheckID), Name, Status, Notes
|
|
|
|
//
|
2017-10-04 16:48:00 -07:00
|
|
|
ScriptArgs []string
|
2017-05-15 21:49:13 +02:00
|
|
|
HTTP string
|
2021-04-09 15:12:10 -04:00
|
|
|
H2PING string
|
2021-10-04 21:36:18 -04:00
|
|
|
H2PingUseTLS bool
|
2017-06-07 01:11:56 +02:00
|
|
|
Header map[string][]string
|
|
|
|
Method string
|
2020-02-10 11:27:12 -05:00
|
|
|
Body string
|
2022-04-01 14:31:15 -07:00
|
|
|
DisableRedirects bool
|
2017-05-15 21:49:13 +02:00
|
|
|
TCP string
|
2023-09-05 16:34:44 -04:00
|
|
|
TCPUseTLS bool
|
2022-06-06 21:13:19 +02:00
|
|
|
UDP string
|
2017-05-15 21:49:13 +02:00
|
|
|
Interval time.Duration
|
|
|
|
DockerContainerID string
|
|
|
|
Shell string
|
2017-12-26 23:35:22 -05:00
|
|
|
GRPC string
|
2018-02-02 17:29:34 -08:00
|
|
|
GRPCUseTLS bool
|
2022-06-07 18:27:14 +01:00
|
|
|
OSService string
|
2021-02-24 22:35:34 -08:00
|
|
|
TLSServerName string
|
2017-05-15 21:49:13 +02:00
|
|
|
TLSSkipVerify bool
|
2018-06-29 23:09:58 -07:00
|
|
|
AliasNode string
|
|
|
|
AliasService string
|
2017-05-15 21:49:13 +02:00
|
|
|
Timeout time.Duration
|
|
|
|
TTL time.Duration
|
2019-10-14 22:49:49 +02:00
|
|
|
SuccessBeforePassing int
|
2021-09-14 12:47:52 -04:00
|
|
|
FailuresBeforeWarning int
|
2019-10-14 22:49:49 +02:00
|
|
|
FailuresBeforeCritical int
|
2017-05-15 21:49:13 +02:00
|
|
|
DeregisterCriticalServiceAfter time.Duration
|
2019-06-26 17:43:25 +02:00
|
|
|
OutputMaxSize int
|
2019-12-09 21:26:41 -05:00
|
|
|
|
2022-03-12 19:55:53 -08:00
|
|
|
acl.EnterpriseMeta `hcl:",squash" mapstructure:",squash"`
|
2014-01-30 15:35:38 -08:00
|
|
|
}
|
|
|
|
|
2019-10-29 11:13:36 -07:00
|
|
|
func (t *CheckDefinition) UnmarshalJSON(data []byte) (err error) {
|
|
|
|
type Alias CheckDefinition
|
|
|
|
aux := &struct {
|
|
|
|
// Parse special values
|
|
|
|
Interval interface{}
|
|
|
|
Timeout interface{}
|
|
|
|
TTL interface{}
|
|
|
|
DeregisterCriticalServiceAfter interface{}
|
|
|
|
|
|
|
|
// Translate fields
|
|
|
|
|
|
|
|
// "args" -> ScriptArgs
|
|
|
|
Args []string `json:"args"`
|
|
|
|
ScriptArgsSnake []string `json:"script_args"`
|
|
|
|
DeregisterCriticalServiceAfterSnake interface{} `json:"deregister_critical_service_after"`
|
|
|
|
DockerContainerIDSnake string `json:"docker_container_id"`
|
2021-02-24 22:35:34 -08:00
|
|
|
TLSServerNameSnake string `json:"tls_server_name"`
|
2019-10-29 11:13:36 -07:00
|
|
|
TLSSkipVerifySnake bool `json:"tls_skip_verify"`
|
2023-09-05 16:34:44 -04:00
|
|
|
TCPUseTLSSnake bool `json:"tcp_use_tls"`
|
2020-09-29 18:29:56 +03:00
|
|
|
GRPCUseTLSSnake bool `json:"grpc_use_tls"`
|
2019-10-29 11:13:36 -07:00
|
|
|
ServiceIDSnake string `json:"service_id"`
|
2021-10-04 21:36:18 -04:00
|
|
|
H2PingUseTLSSnake bool `json:"h2ping_use_tls"`
|
2022-04-01 14:31:15 -07:00
|
|
|
DisableRedirectsSnake bool `json:"disable_redirects"`
|
2019-10-29 11:13:36 -07:00
|
|
|
|
|
|
|
*Alias
|
|
|
|
}{
|
|
|
|
Alias: (*Alias)(t),
|
|
|
|
}
|
2021-10-10 17:52:26 -04:00
|
|
|
|
|
|
|
// Preevaluate struct values to determine where to set defaults
|
|
|
|
if err = lib.UnmarshalJSON(data, &aux); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Set defaults
|
|
|
|
if aux.H2PING != "" {
|
|
|
|
aux.H2PingUseTLS = true
|
|
|
|
aux.H2PingUseTLSSnake = true
|
|
|
|
}
|
|
|
|
|
2019-12-06 11:14:56 -05:00
|
|
|
if err = lib.UnmarshalJSON(data, &aux); err != nil {
|
2019-10-29 11:13:36 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate Fields
|
|
|
|
if aux.DeregisterCriticalServiceAfter == nil {
|
|
|
|
aux.DeregisterCriticalServiceAfter = aux.DeregisterCriticalServiceAfterSnake
|
|
|
|
}
|
|
|
|
if len(t.ScriptArgs) == 0 {
|
|
|
|
t.ScriptArgs = aux.Args
|
|
|
|
}
|
|
|
|
if len(t.ScriptArgs) == 0 {
|
|
|
|
t.ScriptArgs = aux.ScriptArgsSnake
|
|
|
|
}
|
|
|
|
if t.DockerContainerID == "" {
|
|
|
|
t.DockerContainerID = aux.DockerContainerIDSnake
|
|
|
|
}
|
2021-02-24 22:35:34 -08:00
|
|
|
if t.TLSServerName == "" {
|
|
|
|
t.TLSServerName = aux.TLSServerNameSnake
|
|
|
|
}
|
2019-10-29 11:13:36 -07:00
|
|
|
if aux.TLSSkipVerifySnake {
|
|
|
|
t.TLSSkipVerify = aux.TLSSkipVerifySnake
|
|
|
|
}
|
2023-09-05 16:34:44 -04:00
|
|
|
if aux.TCPUseTLSSnake {
|
|
|
|
t.TCPUseTLS = aux.TCPUseTLSSnake
|
|
|
|
}
|
2020-09-29 18:29:56 +03:00
|
|
|
if aux.GRPCUseTLSSnake {
|
|
|
|
t.GRPCUseTLS = aux.GRPCUseTLSSnake
|
|
|
|
}
|
2019-10-29 11:13:36 -07:00
|
|
|
if t.ServiceID == "" {
|
|
|
|
t.ServiceID = aux.ServiceIDSnake
|
|
|
|
}
|
2022-04-01 14:31:15 -07:00
|
|
|
if aux.DisableRedirectsSnake {
|
|
|
|
t.DisableRedirects = aux.DisableRedirectsSnake
|
|
|
|
}
|
2021-10-10 17:52:26 -04:00
|
|
|
|
|
|
|
if (aux.H2PING != "" && !aux.H2PingUseTLSSnake) || (aux.H2PING == "" && aux.H2PingUseTLSSnake) {
|
2021-10-04 22:12:26 -04:00
|
|
|
t.H2PingUseTLS = aux.H2PingUseTLSSnake
|
|
|
|
}
|
2019-10-29 11:13:36 -07:00
|
|
|
|
|
|
|
// Parse special values
|
|
|
|
if aux.Interval != nil {
|
|
|
|
switch v := aux.Interval.(type) {
|
|
|
|
case string:
|
|
|
|
if t.Interval, err = time.ParseDuration(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
t.Interval = time.Duration(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if aux.Timeout != nil {
|
|
|
|
switch v := aux.Timeout.(type) {
|
|
|
|
case string:
|
|
|
|
if t.Timeout, err = time.ParseDuration(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
t.Timeout = time.Duration(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if aux.TTL != nil {
|
|
|
|
switch v := aux.TTL.(type) {
|
|
|
|
case string:
|
|
|
|
if t.TTL, err = time.ParseDuration(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
t.TTL = time.Duration(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if aux.DeregisterCriticalServiceAfter != nil {
|
|
|
|
switch v := aux.DeregisterCriticalServiceAfter.(type) {
|
|
|
|
case string:
|
|
|
|
if t.DeregisterCriticalServiceAfter, err = time.ParseDuration(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case float64:
|
|
|
|
t.DeregisterCriticalServiceAfter = time.Duration(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 18:46:06 +02:00
|
|
|
func (c *CheckDefinition) HealthCheck(node string) *HealthCheck {
|
|
|
|
health := &HealthCheck{
|
2019-12-09 21:26:41 -05:00
|
|
|
Node: node,
|
|
|
|
CheckID: c.ID,
|
|
|
|
Name: c.Name,
|
|
|
|
Status: api.HealthCritical,
|
|
|
|
Notes: c.Notes,
|
|
|
|
ServiceID: c.ServiceID,
|
2021-08-03 15:26:49 -07:00
|
|
|
Interval: c.Interval.String(),
|
|
|
|
Timeout: c.Timeout.String(),
|
2019-12-09 21:26:41 -05:00
|
|
|
EnterpriseMeta: c.EnterpriseMeta,
|
2014-02-03 15:15:35 -08:00
|
|
|
}
|
2015-04-12 00:53:48 +00:00
|
|
|
if c.Status != "" {
|
|
|
|
health.Status = c.Status
|
|
|
|
}
|
2014-02-03 15:15:35 -08:00
|
|
|
if health.CheckID == "" && health.Name != "" {
|
2016-06-06 13:19:31 -07:00
|
|
|
health.CheckID = types.CheckID(health.Name)
|
2014-02-03 15:15:35 -08:00
|
|
|
}
|
|
|
|
return health
|
2014-01-30 15:35:38 -08:00
|
|
|
}
|
2015-04-27 19:01:02 -07:00
|
|
|
|
2017-05-15 21:49:13 +02:00
|
|
|
func (c *CheckDefinition) CheckType() *CheckType {
|
|
|
|
return &CheckType{
|
2017-06-23 01:15:48 -07:00
|
|
|
CheckID: c.ID,
|
|
|
|
Name: c.Name,
|
|
|
|
Status: c.Status,
|
|
|
|
Notes: c.Notes,
|
|
|
|
|
2018-11-07 02:16:03 -08:00
|
|
|
ScriptArgs: c.ScriptArgs,
|
|
|
|
AliasNode: c.AliasNode,
|
|
|
|
AliasService: c.AliasService,
|
|
|
|
HTTP: c.HTTP,
|
2021-04-09 15:12:10 -04:00
|
|
|
H2PING: c.H2PING,
|
2021-10-04 21:36:18 -04:00
|
|
|
H2PingUseTLS: c.H2PingUseTLS,
|
2018-11-07 02:16:03 -08:00
|
|
|
GRPC: c.GRPC,
|
|
|
|
GRPCUseTLS: c.GRPCUseTLS,
|
|
|
|
Header: c.Header,
|
|
|
|
Method: c.Method,
|
2020-02-10 11:27:12 -05:00
|
|
|
Body: c.Body,
|
2022-04-01 14:31:15 -07:00
|
|
|
DisableRedirects: c.DisableRedirects,
|
2019-06-26 17:43:25 +02:00
|
|
|
OutputMaxSize: c.OutputMaxSize,
|
2018-11-07 02:16:03 -08:00
|
|
|
TCP: c.TCP,
|
2023-09-05 16:34:44 -04:00
|
|
|
TCPUseTLS: c.TCPUseTLS,
|
2022-06-06 21:13:19 +02:00
|
|
|
UDP: c.UDP,
|
2018-11-07 02:16:03 -08:00
|
|
|
Interval: c.Interval,
|
|
|
|
DockerContainerID: c.DockerContainerID,
|
|
|
|
Shell: c.Shell,
|
2022-06-07 18:27:14 +01:00
|
|
|
OSService: c.OSService,
|
2021-02-24 22:35:34 -08:00
|
|
|
TLSServerName: c.TLSServerName,
|
2018-11-07 02:16:03 -08:00
|
|
|
TLSSkipVerify: c.TLSSkipVerify,
|
|
|
|
Timeout: c.Timeout,
|
|
|
|
TTL: c.TTL,
|
2019-10-14 22:49:49 +02:00
|
|
|
SuccessBeforePassing: c.SuccessBeforePassing,
|
2021-09-14 12:47:52 -04:00
|
|
|
FailuresBeforeWarning: c.FailuresBeforeWarning,
|
2019-10-14 22:49:49 +02:00
|
|
|
FailuresBeforeCritical: c.FailuresBeforeCritical,
|
2017-05-15 21:49:13 +02:00
|
|
|
DeregisterCriticalServiceAfter: c.DeregisterCriticalServiceAfter,
|
|
|
|
}
|
|
|
|
}
|