2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-03-12 02:24:58 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-06-16 20:30:29 +00:00
|
|
|
"time"
|
2016-03-12 02:24:58 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/hcl"
|
2022-01-10 15:19:07 +00:00
|
|
|
client "github.com/hashicorp/nomad/client/config"
|
2020-03-06 15:09:10 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2016-05-22 03:15:58 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs/config"
|
2016-03-12 02:24:58 +00:00
|
|
|
)
|
|
|
|
|
2022-01-10 15:19:07 +00:00
|
|
|
// ParseConfigFile returns an agent.Config from parsed from a file.
|
2016-03-12 02:24:58 +00:00
|
|
|
func ParseConfigFile(path string) (*Config, error) {
|
2019-04-04 19:06:05 +00:00
|
|
|
// slurp
|
|
|
|
var buf bytes.Buffer
|
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if _, err := io.Copy(&buf, f); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse
|
|
|
|
c := &Config{
|
2022-01-10 15:19:07 +00:00
|
|
|
Client: &ClientConfig{
|
|
|
|
ServerJoin: &ServerJoin{},
|
|
|
|
TemplateConfig: &client.ClientTemplateConfig{
|
|
|
|
Wait: &client.WaitConfig{},
|
|
|
|
WaitBounds: &client.WaitConfig{},
|
|
|
|
ConsulRetry: &client.RetryConfig{},
|
|
|
|
VaultRetry: &client.RetryConfig{},
|
2022-08-03 20:56:30 +00:00
|
|
|
NomadRetry: &client.RetryConfig{},
|
2022-01-10 15:19:07 +00:00
|
|
|
},
|
|
|
|
},
|
2022-07-12 22:40:20 +00:00
|
|
|
Server: &ServerConfig{
|
|
|
|
PlanRejectionTracker: &PlanRejectionTracker{},
|
|
|
|
ServerJoin: &ServerJoin{},
|
|
|
|
},
|
2019-04-04 19:06:05 +00:00
|
|
|
ACL: &ACLConfig{},
|
2020-03-22 16:17:33 +00:00
|
|
|
Audit: &config.AuditConfig{},
|
2019-06-11 19:14:24 +00:00
|
|
|
Consul: &config.ConsulConfig{},
|
|
|
|
Autopilot: &config.AutopilotConfig{},
|
2019-04-04 19:06:05 +00:00
|
|
|
Telemetry: &Telemetry{},
|
2019-06-11 19:14:24 +00:00
|
|
|
Vault: &config.VaultConfig{},
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = hcl.Decode(c, buf.String())
|
|
|
|
if err != nil {
|
2022-07-12 22:40:20 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode HCL file %s: %w", path, err)
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert strings to time.Durations
|
2022-01-10 15:19:07 +00:00
|
|
|
tds := []durationConversionMap{
|
|
|
|
{"gc_interval", &c.Client.GCInterval, &c.Client.GCIntervalHCL, nil},
|
|
|
|
{"acl.token_ttl", &c.ACL.TokenTTL, &c.ACL.TokenTTLHCL, nil},
|
|
|
|
{"acl.policy_ttl", &c.ACL.PolicyTTL, &c.ACL.PolicyTTLHCL, nil},
|
2022-10-20 07:37:32 +00:00
|
|
|
{"acl.policy_ttl", &c.ACL.RoleTTL, &c.ACL.RoleTTLHCL, nil},
|
2022-07-12 11:43:25 +00:00
|
|
|
{"acl.token_min_expiration_ttl", &c.ACL.TokenMinExpirationTTL, &c.ACL.TokenMinExpirationTTLHCL, nil},
|
|
|
|
{"acl.token_max_expiration_ttl", &c.ACL.TokenMaxExpirationTTL, &c.ACL.TokenMaxExpirationTTLHCL, nil},
|
2022-01-10 15:19:07 +00:00
|
|
|
{"client.server_join.retry_interval", &c.Client.ServerJoin.RetryInterval, &c.Client.ServerJoin.RetryIntervalHCL, nil},
|
|
|
|
{"server.heartbeat_grace", &c.Server.HeartbeatGrace, &c.Server.HeartbeatGraceHCL, nil},
|
|
|
|
{"server.min_heartbeat_ttl", &c.Server.MinHeartbeatTTL, &c.Server.MinHeartbeatTTLHCL, nil},
|
|
|
|
{"server.failover_heartbeat_ttl", &c.Server.FailoverHeartbeatTTL, &c.Server.FailoverHeartbeatTTLHCL, nil},
|
2022-07-12 22:40:20 +00:00
|
|
|
{"server.plan_rejection_tracker.node_window", &c.Server.PlanRejectionTracker.NodeWindow, &c.Server.PlanRejectionTracker.NodeWindowHCL, nil},
|
2022-01-10 15:19:07 +00:00
|
|
|
{"server.retry_interval", &c.Server.RetryInterval, &c.Server.RetryIntervalHCL, nil},
|
|
|
|
{"server.server_join.retry_interval", &c.Server.ServerJoin.RetryInterval, &c.Server.ServerJoin.RetryIntervalHCL, nil},
|
|
|
|
{"consul.timeout", &c.Consul.Timeout, &c.Consul.TimeoutHCL, nil},
|
|
|
|
{"autopilot.server_stabilization_time", &c.Autopilot.ServerStabilizationTime, &c.Autopilot.ServerStabilizationTimeHCL, nil},
|
|
|
|
{"autopilot.last_contact_threshold", &c.Autopilot.LastContactThreshold, &c.Autopilot.LastContactThresholdHCL, nil},
|
|
|
|
{"telemetry.collection_interval", &c.Telemetry.collectionInterval, &c.Telemetry.CollectionInterval, nil},
|
|
|
|
{"client.template.block_query_wait", nil, &c.Client.TemplateConfig.BlockQueryWaitTimeHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.BlockQueryWaitTime = d
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"client.template.max_stale", nil, &c.Client.TemplateConfig.MaxStaleHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.MaxStale = d
|
|
|
|
}},
|
|
|
|
{"client.template.wait.min", nil, &c.Client.TemplateConfig.Wait.MinHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.Wait.Min = d
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"client.template.wait.max", nil, &c.Client.TemplateConfig.Wait.MaxHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.Wait.Max = d
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"client.template.wait_bounds.min", nil, &c.Client.TemplateConfig.WaitBounds.MinHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.WaitBounds.Min = d
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"client.template.wait_bounds.max", nil, &c.Client.TemplateConfig.WaitBounds.MaxHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.WaitBounds.Max = d
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"client.template.consul_retry.backoff", nil, &c.Client.TemplateConfig.ConsulRetry.BackoffHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.ConsulRetry.Backoff = d
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"client.template.consul_retry.max_backoff", nil, &c.Client.TemplateConfig.ConsulRetry.MaxBackoffHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.ConsulRetry.MaxBackoff = d
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"client.template.vault_retry.backoff", nil, &c.Client.TemplateConfig.VaultRetry.BackoffHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.VaultRetry.Backoff = d
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"client.template.vault_retry.max_backoff", nil, &c.Client.TemplateConfig.VaultRetry.MaxBackoffHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.VaultRetry.MaxBackoff = d
|
|
|
|
},
|
|
|
|
},
|
2022-08-03 20:56:30 +00:00
|
|
|
{"client.template.nomad_retry.backoff", nil, &c.Client.TemplateConfig.NomadRetry.BackoffHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.NomadRetry.Backoff = d
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{"client.template.nomad_retry.max_backoff", nil, &c.Client.TemplateConfig.NomadRetry.MaxBackoffHCL,
|
|
|
|
func(d *time.Duration) {
|
|
|
|
c.Client.TemplateConfig.NomadRetry.MaxBackoff = d
|
|
|
|
},
|
|
|
|
},
|
2020-03-22 16:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add enterprise audit sinks for time.Duration parsing
|
|
|
|
for i, sink := range c.Audit.Sinks {
|
2022-01-10 15:19:07 +00:00
|
|
|
tds = append(tds, durationConversionMap{
|
|
|
|
fmt.Sprintf("audit.sink.%d", i), &sink.RotateDuration, &sink.RotateDurationHCL, nil})
|
2020-03-22 16:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert strings to time.Durations
|
2022-01-10 15:19:07 +00:00
|
|
|
err = convertDurations(tds)
|
2019-04-04 19:06:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// report unexpected keys
|
|
|
|
err = extraKeys(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-10 15:19:07 +00:00
|
|
|
// Set client template config or its members to nil if not set.
|
|
|
|
finalizeClientTemplateConfig(c)
|
|
|
|
|
2019-04-04 19:06:05 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 15:19:07 +00:00
|
|
|
// durationConversionMap holds args for one duration conversion
|
|
|
|
type durationConversionMap struct {
|
|
|
|
targetFieldPath string
|
|
|
|
targetField *time.Duration
|
|
|
|
sourceField *string
|
|
|
|
setFunc func(*time.Duration)
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 15:19:07 +00:00
|
|
|
// convertDurations parses the duration strings specified in the config files
|
2019-04-04 19:06:05 +00:00
|
|
|
// into time.Durations
|
2022-01-10 15:19:07 +00:00
|
|
|
func convertDurations(xs []durationConversionMap) error {
|
2019-04-04 19:06:05 +00:00
|
|
|
for _, x := range xs {
|
2022-01-10 15:19:07 +00:00
|
|
|
// if targetField is not a pointer itself, use the field map.
|
|
|
|
if x.targetField != nil && x.sourceField != nil && "" != *x.sourceField {
|
|
|
|
d, err := time.ParseDuration(*x.sourceField)
|
2019-04-04 19:06:05 +00:00
|
|
|
if err != nil {
|
2022-01-10 15:19:07 +00:00
|
|
|
return fmt.Errorf("%s can't parse time duration %s", x.targetFieldPath, *x.sourceField)
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 15:19:07 +00:00
|
|
|
*x.targetField = d
|
|
|
|
} else if x.setFunc != nil && x.sourceField != nil && "" != *x.sourceField {
|
|
|
|
// if targetField is a pointer itself, use the setFunc closure.
|
|
|
|
d, err := time.ParseDuration(*x.sourceField)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%s can't parse time duration %s", x.targetFieldPath, *x.sourceField)
|
|
|
|
}
|
|
|
|
x.setFunc(&d)
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func extraKeys(c *Config) error {
|
|
|
|
// hcl leaves behind extra keys when parsing JSON. These keys
|
|
|
|
// are kept on the top level, taken from slices or the keys of
|
|
|
|
// structs contained in slices. Clean up before looking for
|
|
|
|
// extra keys.
|
|
|
|
for range c.HTTPAPIResponseHeaders {
|
2020-03-06 15:09:10 +00:00
|
|
|
helper.RemoveEqualFold(&c.ExtraKeysHCL, "http_api_response_headers")
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range c.Plugins {
|
2020-03-06 15:09:10 +00:00
|
|
|
helper.RemoveEqualFold(&c.ExtraKeysHCL, p.Name)
|
|
|
|
helper.RemoveEqualFold(&c.ExtraKeysHCL, "config")
|
|
|
|
helper.RemoveEqualFold(&c.ExtraKeysHCL, "plugin")
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range []string{"options", "meta", "chroot_env", "servers", "server_join"} {
|
2020-03-06 15:09:10 +00:00
|
|
|
helper.RemoveEqualFold(&c.ExtraKeysHCL, k)
|
|
|
|
helper.RemoveEqualFold(&c.ExtraKeysHCL, "client")
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stats is an unused key, continue to silently ignore it
|
2020-03-06 15:09:10 +00:00
|
|
|
helper.RemoveEqualFold(&c.Client.ExtraKeysHCL, "stats")
|
2019-04-04 19:06:05 +00:00
|
|
|
|
2019-07-25 14:45:41 +00:00
|
|
|
// Remove HostVolume extra keys
|
|
|
|
for _, hv := range c.Client.HostVolumes {
|
2020-03-06 15:09:10 +00:00
|
|
|
helper.RemoveEqualFold(&c.Client.ExtraKeysHCL, hv.Name)
|
|
|
|
helper.RemoveEqualFold(&c.Client.ExtraKeysHCL, "host_volume")
|
2019-07-25 14:45:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 15:53:10 +00:00
|
|
|
// Remove HostNetwork extra keys
|
|
|
|
for _, hn := range c.Client.HostNetworks {
|
|
|
|
helper.RemoveEqualFold(&c.Client.ExtraKeysHCL, hn.Name)
|
|
|
|
helper.RemoveEqualFold(&c.Client.ExtraKeysHCL, "host_network")
|
|
|
|
}
|
|
|
|
|
2020-03-22 16:17:33 +00:00
|
|
|
// Remove AuditConfig extra keys
|
|
|
|
for _, f := range c.Audit.Filters {
|
2020-03-23 18:30:53 +00:00
|
|
|
helper.RemoveEqualFold(&c.Audit.ExtraKeysHCL, f.Name)
|
|
|
|
helper.RemoveEqualFold(&c.Audit.ExtraKeysHCL, "filter")
|
2020-03-22 16:17:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range c.Audit.Sinks {
|
2020-03-23 18:30:53 +00:00
|
|
|
helper.RemoveEqualFold(&c.Audit.ExtraKeysHCL, s.Name)
|
|
|
|
helper.RemoveEqualFold(&c.Audit.ExtraKeysHCL, "sink")
|
2020-03-22 16:17:33 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 19:06:05 +00:00
|
|
|
for _, k := range []string{"enabled_schedulers", "start_join", "retry_join", "server_join"} {
|
2020-03-06 15:09:10 +00:00
|
|
|
helper.RemoveEqualFold(&c.ExtraKeysHCL, k)
|
|
|
|
helper.RemoveEqualFold(&c.ExtraKeysHCL, "server")
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 08:48:19 +00:00
|
|
|
for _, k := range []string{"preemption_config"} {
|
|
|
|
helper.RemoveEqualFold(&c.Server.ExtraKeysHCL, k)
|
|
|
|
}
|
|
|
|
|
2019-04-04 19:06:05 +00:00
|
|
|
for _, k := range []string{"datadog_tags"} {
|
2020-03-06 15:09:10 +00:00
|
|
|
helper.RemoveEqualFold(&c.ExtraKeysHCL, k)
|
|
|
|
helper.RemoveEqualFold(&c.ExtraKeysHCL, "telemetry")
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 15:09:10 +00:00
|
|
|
return helper.UnusedKeys(c)
|
2019-04-04 19:06:05 +00:00
|
|
|
}
|
2022-01-10 15:19:07 +00:00
|
|
|
|
|
|
|
// hcl.Decode will error if the ClientTemplateConfig isn't initialized with empty
|
|
|
|
// structs, however downstream code expect nils if the struct only contains fields
|
|
|
|
// with the zero value for its type. This function nils out type members that are
|
|
|
|
// structs where all the member fields are just the zero value for its type.
|
|
|
|
func finalizeClientTemplateConfig(config *Config) {
|
|
|
|
if config.Client.TemplateConfig.Wait.IsEmpty() {
|
|
|
|
config.Client.TemplateConfig.Wait = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Client.TemplateConfig.WaitBounds.IsEmpty() {
|
|
|
|
config.Client.TemplateConfig.WaitBounds = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Client.TemplateConfig.ConsulRetry.IsEmpty() {
|
|
|
|
config.Client.TemplateConfig.ConsulRetry = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Client.TemplateConfig.VaultRetry.IsEmpty() {
|
|
|
|
config.Client.TemplateConfig.VaultRetry = nil
|
|
|
|
}
|
|
|
|
|
2022-08-03 20:56:30 +00:00
|
|
|
if config.Client.TemplateConfig.NomadRetry.IsEmpty() {
|
|
|
|
config.Client.TemplateConfig.NomadRetry = nil
|
|
|
|
}
|
|
|
|
|
2022-01-10 15:19:07 +00:00
|
|
|
if config.Client.TemplateConfig.IsEmpty() {
|
|
|
|
config.Client.TemplateConfig = nil
|
|
|
|
}
|
|
|
|
}
|