2019-08-09 19:18:53 +00:00
|
|
|
package jobspec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/hcl"
|
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/helper"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
func parseGroupServices(g *api.TaskGroup, serviceObjs *ast.ObjectList) error {
|
|
|
|
g.Services = make([]*api.Service, len(serviceObjs.Items))
|
|
|
|
for idx, o := range serviceObjs.Items {
|
|
|
|
service, err := parseService(o)
|
|
|
|
if err != nil {
|
|
|
|
return multierror.Prefix(err, fmt.Sprintf("service (%d):", idx))
|
|
|
|
}
|
|
|
|
g.Services[idx] = service
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseServices(serviceObjs *ast.ObjectList) ([]*api.Service, error) {
|
|
|
|
services := make([]*api.Service, len(serviceObjs.Items))
|
|
|
|
for idx, o := range serviceObjs.Items {
|
|
|
|
service, err := parseService(o)
|
|
|
|
if err != nil {
|
|
|
|
return nil, multierror.Prefix(err, fmt.Sprintf("service (%d):", idx))
|
|
|
|
}
|
|
|
|
services[idx] = service
|
|
|
|
}
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
func parseService(o *ast.ObjectItem) (*api.Service, error) {
|
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"name",
|
|
|
|
"tags",
|
|
|
|
"canary_tags",
|
client: enable configuring enable_tag_override for services
Consul provides a feature of Service Definitions where the tags
associated with a service can be modified through the Catalog API,
overriding the value(s) configured in the agent's service configuration.
To enable this feature, the flag enable_tag_override must be configured
in the service definition.
Previously, Nomad did not allow configuring this flag, and thus the default
value of false was used. Now, it is configurable.
Because Nomad itself acts as a state machine around the the service definitions
of the tasks it manages, it's worth describing what happens when this feature
is enabled and why.
Consider the basic case where there is no Nomad, and your service is provided
to consul as a boring JSON file. The ultimate source of truth for the definition
of that service is the file, and is stored in the agent. Later, Consul performs
"anti-entropy" which synchronizes the Catalog (stored only the leaders). Then
with enable_tag_override=true, the tags field is available for "external"
modification through the Catalog API (rather than directly configuring the
service definition file, or using the Agent API). The important observation
is that if the service definition ever changes (i.e. the file is changed &
config reloaded OR the Agent API is used to modify the service), those
"external" tag values are thrown away, and the new service definition is
once again the source of truth.
In the Nomad case, Nomad itself is the source of truth over the Agent in
the same way the JSON file was the source of truth in the example above.
That means any time Nomad sets a new service definition, any externally
configured tags are going to be replaced. When does this happen? Only on
major lifecycle events, for example when a task is modified because of an
updated job spec from the 'nomad job run <existing>' command. Otherwise,
Nomad's periodic re-sync's with Consul will now no longer try to restore
the externally modified tag values (as long as enable_tag_override=true).
Fixes #2057
2020-02-07 21:22:19 +00:00
|
|
|
"enable_tag_override",
|
2019-08-09 19:18:53 +00:00
|
|
|
"port",
|
|
|
|
"check",
|
|
|
|
"address_mode",
|
|
|
|
"check_restart",
|
|
|
|
"connect",
|
2019-08-23 16:49:02 +00:00
|
|
|
"meta",
|
2019-11-13 03:27:54 +00:00
|
|
|
"canary_meta",
|
2019-08-09 19:18:53 +00:00
|
|
|
}
|
|
|
|
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var service api.Service
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(m, "check")
|
|
|
|
delete(m, "check_restart")
|
|
|
|
delete(m, "connect")
|
2019-08-23 16:49:02 +00:00
|
|
|
delete(m, "meta")
|
2019-11-13 03:27:54 +00:00
|
|
|
delete(m, "canary_meta")
|
2019-08-09 19:18:53 +00:00
|
|
|
|
|
|
|
if err := mapstructure.WeakDecode(m, &service); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-23 16:49:02 +00:00
|
|
|
// Filter list
|
|
|
|
var listVal *ast.ObjectList
|
2019-08-09 19:18:53 +00:00
|
|
|
if ot, ok := o.Val.(*ast.ObjectType); ok {
|
2019-08-23 16:49:02 +00:00
|
|
|
listVal = ot.List
|
2019-08-09 19:18:53 +00:00
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("'%s': should be an object", service.Name)
|
|
|
|
}
|
|
|
|
|
2019-08-23 16:49:02 +00:00
|
|
|
if co := listVal.Filter("check"); len(co.Items) > 0 {
|
2019-08-09 19:18:53 +00:00
|
|
|
if err := parseChecks(&service, co); err != nil {
|
|
|
|
return nil, multierror.Prefix(err, fmt.Sprintf("'%s',", service.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter check_restart
|
2019-08-23 16:49:02 +00:00
|
|
|
if cro := listVal.Filter("check_restart"); len(cro.Items) > 0 {
|
2019-08-09 19:18:53 +00:00
|
|
|
if len(cro.Items) > 1 {
|
|
|
|
return nil, fmt.Errorf("check_restart '%s': cannot have more than 1 check_restart", service.Name)
|
|
|
|
}
|
|
|
|
cr, err := parseCheckRestart(cro.Items[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, multierror.Prefix(err, fmt.Sprintf("'%s',", service.Name))
|
|
|
|
}
|
|
|
|
service.CheckRestart = cr
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter connect
|
2019-08-23 16:49:02 +00:00
|
|
|
if co := listVal.Filter("connect"); len(co.Items) > 0 {
|
2019-08-09 19:18:53 +00:00
|
|
|
if len(co.Items) > 1 {
|
|
|
|
return nil, fmt.Errorf("connect '%s': cannot have more than 1 connect stanza", service.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := parseConnect(co.Items[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, multierror.Prefix(err, fmt.Sprintf("'%s',", service.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
service.Connect = c
|
|
|
|
}
|
|
|
|
|
2019-08-23 16:49:02 +00:00
|
|
|
// Parse out meta fields. These are in HCL as a list so we need
|
|
|
|
// to iterate over them and merge them.
|
|
|
|
if metaO := listVal.Filter("meta"); len(metaO.Items) > 0 {
|
|
|
|
for _, o := range metaO.Elem().Items {
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(m, &service.Meta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 03:27:54 +00:00
|
|
|
// Parse out canary_meta fields. These are in HCL as a list so we need
|
|
|
|
// to iterate over them and merge them.
|
|
|
|
if metaO := listVal.Filter("canary_meta"); len(metaO.Items) > 0 {
|
|
|
|
for _, o := range metaO.Elem().Items {
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(m, &service.CanaryMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:18:53 +00:00
|
|
|
return &service, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseConnect(co *ast.ObjectItem) (*api.ConsulConnect, error) {
|
|
|
|
valid := []string{
|
|
|
|
"native",
|
|
|
|
"sidecar_service",
|
|
|
|
"sidecar_task",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := helper.CheckHCLKeys(co.Val, valid); err != nil {
|
|
|
|
return nil, multierror.Prefix(err, "connect ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
var connect api.ConsulConnect
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, co.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(m, "sidecar_service")
|
|
|
|
delete(m, "sidecar_task")
|
|
|
|
|
|
|
|
if err := mapstructure.WeakDecode(m, &connect); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var connectList *ast.ObjectList
|
|
|
|
if ot, ok := co.Val.(*ast.ObjectType); ok {
|
|
|
|
connectList = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("connect should be an object")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the sidecar_service
|
|
|
|
o := connectList.Filter("sidecar_service")
|
|
|
|
if len(o.Items) == 0 {
|
|
|
|
return &connect, nil
|
|
|
|
}
|
|
|
|
if len(o.Items) > 1 {
|
|
|
|
return nil, fmt.Errorf("only one 'sidecar_service' block allowed per task")
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := parseSidecarService(o.Items[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("sidecar_service, %v", err)
|
|
|
|
}
|
|
|
|
connect.SidecarService = r
|
|
|
|
|
|
|
|
// Parse the sidecar_task
|
|
|
|
o = connectList.Filter("sidecar_task")
|
|
|
|
if len(o.Items) == 0 {
|
|
|
|
return &connect, nil
|
|
|
|
}
|
|
|
|
if len(o.Items) > 1 {
|
|
|
|
return nil, fmt.Errorf("only one 'sidecar_task' block allowed per task")
|
|
|
|
}
|
|
|
|
|
2019-08-20 05:22:46 +00:00
|
|
|
t, err := parseSidecarTask(o.Items[0])
|
2019-08-09 19:18:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("sidecar_task, %v", err)
|
|
|
|
}
|
|
|
|
connect.SidecarTask = t
|
|
|
|
|
|
|
|
return &connect, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSidecarService(o *ast.ObjectItem) (*api.ConsulSidecarService, error) {
|
|
|
|
valid := []string{
|
|
|
|
"port",
|
|
|
|
"proxy",
|
2019-10-08 19:19:09 +00:00
|
|
|
"tags",
|
2019-08-09 19:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
|
|
|
|
return nil, multierror.Prefix(err, "sidecar_service ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
var sidecar api.ConsulSidecarService
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(m, "proxy")
|
|
|
|
|
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: &sidecar,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := dec.Decode(m); err != nil {
|
2019-10-08 19:19:09 +00:00
|
|
|
return nil, fmt.Errorf("sidecar_service: %v", err)
|
2019-08-09 19:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var proxyList *ast.ObjectList
|
|
|
|
if ot, ok := o.Val.(*ast.ObjectType); ok {
|
|
|
|
proxyList = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("sidecar_service: should be an object")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the proxy
|
|
|
|
po := proxyList.Filter("proxy")
|
|
|
|
if len(po.Items) == 0 {
|
|
|
|
return &sidecar, nil
|
|
|
|
}
|
|
|
|
if len(po.Items) > 1 {
|
|
|
|
return nil, fmt.Errorf("only one 'proxy' block allowed per task")
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := parseProxy(po.Items[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("proxy, %v", err)
|
|
|
|
}
|
|
|
|
sidecar.Proxy = r
|
|
|
|
|
|
|
|
return &sidecar, nil
|
|
|
|
}
|
|
|
|
|
2019-08-20 05:22:46 +00:00
|
|
|
func parseSidecarTask(item *ast.ObjectItem) (*api.SidecarTask, error) {
|
|
|
|
// We need this later
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := item.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("should be an object")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"config",
|
|
|
|
"driver",
|
|
|
|
"env",
|
|
|
|
"kill_timeout",
|
|
|
|
"logs",
|
|
|
|
"meta",
|
|
|
|
"resources",
|
|
|
|
"shutdown_delay",
|
|
|
|
"user",
|
|
|
|
"kill_signal",
|
|
|
|
}
|
|
|
|
if err := helper.CheckHCLKeys(listVal, valid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
task, err := parseTask(item)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sidecarTask := &api.SidecarTask{
|
|
|
|
Name: task.Name,
|
|
|
|
Driver: task.Driver,
|
|
|
|
User: task.User,
|
|
|
|
Config: task.Config,
|
|
|
|
Env: task.Env,
|
|
|
|
Resources: task.Resources,
|
|
|
|
Meta: task.Meta,
|
|
|
|
KillTimeout: task.KillTimeout,
|
|
|
|
LogConfig: task.LogConfig,
|
|
|
|
KillSignal: task.KillSignal,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse ShutdownDelay seperatly to get pointer
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, item.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m = map[string]interface{}{
|
|
|
|
"shutdown_delay": m["shutdown_delay"],
|
|
|
|
}
|
|
|
|
|
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: sidecarTask,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := dec.Decode(m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sidecarTask, nil
|
|
|
|
}
|
|
|
|
|
2019-08-09 19:18:53 +00:00
|
|
|
func parseProxy(o *ast.ObjectItem) (*api.ConsulProxy, error) {
|
|
|
|
valid := []string{
|
2019-09-23 18:30:48 +00:00
|
|
|
"local_service_address",
|
|
|
|
"local_service_port",
|
2019-08-09 19:18:53 +00:00
|
|
|
"upstreams",
|
|
|
|
"config",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := helper.CheckHCLKeys(o.Val, valid); err != nil {
|
|
|
|
return nil, multierror.Prefix(err, "proxy ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
var proxy api.ConsulProxy
|
|
|
|
|
|
|
|
var listVal *ast.ObjectList
|
|
|
|
if ot, ok := o.Val.(*ast.ObjectType); ok {
|
|
|
|
listVal = ot.List
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("proxy: should be an object")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the proxy
|
|
|
|
uo := listVal.Filter("upstreams")
|
|
|
|
proxy.Upstreams = make([]*api.ConsulUpstream, len(uo.Items))
|
|
|
|
for i := range uo.Items {
|
|
|
|
u, err := parseUpstream(uo.Items[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy.Upstreams[i] = u
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have config, then parse that
|
|
|
|
if o := listVal.Filter("config"); len(o.Items) > 1 {
|
|
|
|
return nil, fmt.Errorf("only 1 meta object supported")
|
|
|
|
} else if len(o.Items) == 1 {
|
|
|
|
var mSlice []map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&mSlice, o.Items[0].Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(mSlice) > 1 {
|
|
|
|
return nil, fmt.Errorf("only 1 meta object supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
m := mSlice[0]
|
|
|
|
|
|
|
|
if err := mapstructure.WeakDecode(m, &proxy.Config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy.Config = flattenMapSlice(proxy.Config)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proxy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseUpstream(uo *ast.ObjectItem) (*api.ConsulUpstream, error) {
|
|
|
|
valid := []string{
|
|
|
|
"destination_name",
|
|
|
|
"local_bind_port",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := helper.CheckHCLKeys(uo.Val, valid); err != nil {
|
|
|
|
return nil, multierror.Prefix(err, "upstream ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
var upstream api.ConsulUpstream
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, uo.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: &upstream,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dec.Decode(m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &upstream, nil
|
|
|
|
}
|
|
|
|
func parseChecks(service *api.Service, checkObjs *ast.ObjectList) error {
|
|
|
|
service.Checks = make([]api.ServiceCheck, len(checkObjs.Items))
|
|
|
|
for idx, co := range checkObjs.Items {
|
|
|
|
// Check for invalid keys
|
|
|
|
valid := []string{
|
|
|
|
"name",
|
|
|
|
"type",
|
|
|
|
"interval",
|
|
|
|
"timeout",
|
|
|
|
"path",
|
|
|
|
"protocol",
|
|
|
|
"port",
|
|
|
|
"command",
|
|
|
|
"args",
|
|
|
|
"initial_status",
|
|
|
|
"tls_skip_verify",
|
|
|
|
"header",
|
|
|
|
"method",
|
|
|
|
"check_restart",
|
|
|
|
"address_mode",
|
|
|
|
"grpc_service",
|
|
|
|
"grpc_use_tls",
|
2019-08-19 13:17:38 +00:00
|
|
|
"task",
|
2019-08-09 19:18:53 +00:00
|
|
|
}
|
|
|
|
if err := helper.CheckHCLKeys(co.Val, valid); err != nil {
|
|
|
|
return multierror.Prefix(err, "check ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
var check api.ServiceCheck
|
|
|
|
var cm map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&cm, co.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// HCL allows repeating stanzas so merge 'header' into a single
|
|
|
|
// map[string][]string.
|
|
|
|
if headerI, ok := cm["header"]; ok {
|
|
|
|
headerRaw, ok := headerI.([]map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("check -> header -> expected a []map[string][]string but found %T", headerI)
|
|
|
|
}
|
|
|
|
m := map[string][]string{}
|
|
|
|
for _, rawm := range headerRaw {
|
|
|
|
for k, vI := range rawm {
|
|
|
|
vs, ok := vI.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("check -> header -> %q expected a []string but found %T", k, vI)
|
|
|
|
}
|
|
|
|
for _, vI := range vs {
|
|
|
|
v, ok := vI.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("check -> header -> %q expected a string but found %T", k, vI)
|
|
|
|
}
|
|
|
|
m[k] = append(m[k], v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
check.Header = m
|
|
|
|
|
|
|
|
// Remove "header" as it has been parsed
|
|
|
|
delete(cm, "header")
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(cm, "check_restart")
|
|
|
|
|
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: &check,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := dec.Decode(cm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter check_restart
|
|
|
|
var checkRestartList *ast.ObjectList
|
|
|
|
if ot, ok := co.Val.(*ast.ObjectType); ok {
|
|
|
|
checkRestartList = ot.List
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("check_restart '%s': should be an object", check.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cro := checkRestartList.Filter("check_restart"); len(cro.Items) > 0 {
|
|
|
|
if len(cro.Items) > 1 {
|
|
|
|
return fmt.Errorf("check_restart '%s': cannot have more than 1 check_restart", check.Name)
|
|
|
|
}
|
|
|
|
cr, err := parseCheckRestart(cro.Items[0])
|
|
|
|
if err != nil {
|
|
|
|
return multierror.Prefix(err, fmt.Sprintf("check: '%s',", check.Name))
|
|
|
|
}
|
|
|
|
check.CheckRestart = cr
|
|
|
|
}
|
|
|
|
|
|
|
|
service.Checks[idx] = check
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCheckRestart(cro *ast.ObjectItem) (*api.CheckRestart, error) {
|
|
|
|
valid := []string{
|
|
|
|
"limit",
|
|
|
|
"grace",
|
|
|
|
"ignore_warnings",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := helper.CheckHCLKeys(cro.Val, valid); err != nil {
|
|
|
|
return nil, multierror.Prefix(err, "check_restart ->")
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkRestart api.CheckRestart
|
|
|
|
var crm map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&crm, cro.Val); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: &checkRestart,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := dec.Decode(crm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &checkRestart, nil
|
|
|
|
}
|