github.imxd.top/hashicorp/consul@v1.4.5/agent/config/default.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/hashicorp/consul/agent/consul"
     8  	"github.com/hashicorp/consul/version"
     9  )
    10  
    11  func DefaultRPCProtocol() (int, error) {
    12  	src := DefaultSource()
    13  	c, err := Parse(src.Data, src.Format)
    14  	if err != nil {
    15  		return 0, fmt.Errorf("Error parsing default config: %s", err)
    16  	}
    17  	if c.RPCProtocol == nil {
    18  		return 0, fmt.Errorf("No default RPC protocol set")
    19  	}
    20  	return *c.RPCProtocol, nil
    21  }
    22  
    23  // DefaultSource is the default agent configuration.
    24  // This needs to be merged first in the head.
    25  // todo(fs): The values are sourced from multiple sources.
    26  // todo(fs): IMO, this should be the definitive default for all configurable values
    27  // todo(fs): and whatever is in here should clobber every default value. Hence, no sourcing.
    28  func DefaultSource() Source {
    29  	cfg := consul.DefaultConfig()
    30  	serfLAN := cfg.SerfLANConfig.MemberlistConfig
    31  	serfWAN := cfg.SerfWANConfig.MemberlistConfig
    32  
    33  	// DEPRECATED (ACL-Legacy-Compat) - when legacy ACL support is removed these defaults
    34  	//   the acl_* config entries here should be transitioned to their counterparts in the
    35  	//   acl stanza for now we need to be able to detect the new entries not being set (not
    36  	//   just set to the defaults here) so that we can use the old entries. So the true
    37  	//   default still needs to reside in the original config values
    38  	return Source{
    39  		Name:   "default",
    40  		Format: "hcl",
    41  		Data: `
    42  		acl_default_policy = "allow"
    43  		acl_down_policy = "extend-cache"
    44  		acl_enforce_version_8 = true
    45  		acl_ttl = "30s"
    46  		acl = {
    47  			policy_ttl = "30s"
    48  		}
    49  		bind_addr = "0.0.0.0"
    50  		bootstrap = false
    51  		bootstrap_expect = 0
    52  		check_update_interval = "5m"
    53  		client_addr = "127.0.0.1"
    54  		datacenter = "` + consul.DefaultDC + `"
    55  		disable_coordinates = false
    56  		disable_host_node_id = true
    57  		disable_remote_exec = true
    58  		domain = "consul."
    59  		encrypt_verify_incoming = true
    60  		encrypt_verify_outgoing = true
    61  		log_level = "INFO"
    62  		protocol =  2
    63  		retry_interval = "30s"
    64  		retry_interval_wan = "30s"
    65  		server = false
    66  		syslog_facility = "LOCAL0"
    67  		tls_min_version = "tls12"
    68  
    69  		// TODO (slackpad) - Until #3744 is done, we need to keep these
    70  		// in sync with agent/consul/config.go.
    71  		autopilot = {
    72  			cleanup_dead_servers = true
    73  			last_contact_threshold = "200ms"
    74  			max_trailing_logs = 250
    75  			server_stabilization_time = "10s"
    76  		}
    77  		gossip_lan = {
    78  			gossip_interval = "` + serfLAN.GossipInterval.String() + `"
    79  			gossip_nodes = ` + strconv.Itoa(serfLAN.GossipNodes) + `
    80  			retransmit_mult = ` + strconv.Itoa(serfLAN.RetransmitMult) + `
    81  			probe_interval = "` + serfLAN.ProbeInterval.String() + `"
    82  			probe_timeout = "` + serfLAN.ProbeTimeout.String() + `"
    83  			suspicion_mult = ` + strconv.Itoa(serfLAN.SuspicionMult) + `
    84  		}
    85  		gossip_wan = {
    86  			gossip_interval = "` + serfWAN.GossipInterval.String() + `"
    87  			gossip_nodes = ` + strconv.Itoa(serfLAN.GossipNodes) + `
    88  			retransmit_mult = ` + strconv.Itoa(serfLAN.RetransmitMult) + `
    89  			probe_interval = "` + serfWAN.ProbeInterval.String() + `"
    90  			probe_timeout = "` + serfWAN.ProbeTimeout.String() + `"
    91  			suspicion_mult = ` + strconv.Itoa(serfWAN.SuspicionMult) + `
    92  		}
    93  		dns_config = {
    94  			allow_stale = true
    95  			a_record_limit = 0
    96  			udp_answer_limit = 3
    97  			max_stale = "87600h"
    98  			recursor_timeout = "2s"
    99  		}
   100  		limits = {
   101  			rpc_rate = -1
   102  			rpc_max_burst = 1000
   103  		}
   104  		performance = {
   105  			leave_drain_time = "5s"
   106  			raft_multiplier = ` + strconv.Itoa(int(consul.DefaultRaftMultiplier)) + `
   107  			rpc_hold_timeout = "7s"
   108  		}
   109  		ports = {
   110  			dns = 8600
   111  			http = 8500
   112  			https = -1
   113  			grpc = -1
   114  			serf_lan = ` + strconv.Itoa(consul.DefaultLANSerfPort) + `
   115  			serf_wan = ` + strconv.Itoa(consul.DefaultWANSerfPort) + `
   116  			server = ` + strconv.Itoa(consul.DefaultRPCPort) + `
   117  			proxy_min_port = 20000
   118  			proxy_max_port = 20255
   119  			sidecar_min_port = 21000
   120  			sidecar_max_port = 21255
   121  		}
   122  		telemetry = {
   123  			metrics_prefix = "consul"
   124  			filter_default = true
   125  		}
   126  
   127  	`,
   128  	}
   129  }
   130  
   131  // DevSource is the additional default configuration for dev mode.
   132  // This should be merged in the head after the default configuration.
   133  func DevSource() Source {
   134  	return Source{
   135  		Name:   "dev",
   136  		Format: "hcl",
   137  		Data: `
   138  		bind_addr = "127.0.0.1"
   139  		disable_anonymous_signature = true
   140  		disable_keyring_file = true
   141  		enable_debug = true
   142  		ui = true
   143  		log_level = "DEBUG"
   144  		server = true
   145  
   146  		gossip_lan = {
   147  			gossip_interval = "100ms"
   148  			probe_interval = "100ms"
   149  			probe_timeout = "100ms"
   150  			suspicion_mult = 3
   151  		}
   152  		gossip_wan = {
   153  			gossip_interval = "100ms"
   154  			probe_interval = "100ms"
   155  			probe_timeout = "100ms"
   156  			suspicion_mult = 3
   157  		}
   158  		connect = {
   159  			enabled = true
   160  		}
   161  		performance = {
   162  			raft_multiplier = 1
   163  		}
   164  		ports = {
   165  			grpc = 8502
   166  		}
   167  	`,
   168  	}
   169  }
   170  
   171  // NonUserSource contains the values the user cannot configure.
   172  // This needs to be merged in the tail.
   173  func NonUserSource() Source {
   174  	return Source{
   175  		Name:   "non-user",
   176  		Format: "hcl",
   177  		Data: `
   178  		acl = {
   179  			disabled_ttl = "120s"
   180  		}
   181  		check_deregister_interval_min = "1m"
   182  		check_reap_interval = "30s"
   183  		ae_interval = "1m"
   184  		sync_coordinate_rate_target = 64
   185  		sync_coordinate_interval_min = "15s"
   186  
   187  		# segment_limit is the maximum number of network segments that may be declared.
   188  		segment_limit = 64
   189  
   190  		# SegmentNameLimit is the maximum segment name length.
   191  		segment_name_limit = 64
   192  	`,
   193  	}
   194  }
   195  
   196  // VersionSource creates a config source for the version parameters.
   197  // This should be merged in the tail since these values are not
   198  // user configurable.
   199  func VersionSource(rev, ver, verPre string) Source {
   200  	return Source{
   201  		Name:   "version",
   202  		Format: "hcl",
   203  		Data:   fmt.Sprintf(`revision = %q version = %q version_prerelease = %q`, rev, ver, verPre),
   204  	}
   205  }
   206  
   207  // DefaultVersionSource returns the version config source for the embedded
   208  // version numbers.
   209  func DefaultVersionSource() Source {
   210  	return VersionSource(version.GitCommit, version.Version, version.VersionPrerelease)
   211  }
   212  
   213  // DefaultConsulSource returns the default configuration for the consul agent.
   214  // This should be merged in the tail since these values are not user configurable.
   215  func DefaultConsulSource() Source {
   216  	cfg := consul.DefaultConfig()
   217  	raft := cfg.RaftConfig
   218  	return Source{
   219  		Name:   "consul",
   220  		Format: "hcl",
   221  		Data: `
   222  		consul = {
   223  			coordinate = {
   224  				update_batch_size = ` + strconv.Itoa(cfg.CoordinateUpdateBatchSize) + `
   225  				update_max_batches = ` + strconv.Itoa(cfg.CoordinateUpdateMaxBatches) + `
   226  				update_period = "` + cfg.CoordinateUpdatePeriod.String() + `"
   227  			}
   228  			raft = {
   229  				election_timeout = "` + raft.ElectionTimeout.String() + `"
   230  				heartbeat_timeout = "` + raft.HeartbeatTimeout.String() + `"
   231  				leader_lease_timeout = "` + raft.LeaderLeaseTimeout.String() + `"
   232  			}
   233  			server = {
   234  				health_interval = "` + cfg.ServerHealthInterval.String() + `"
   235  			}
   236  		}
   237  	`,
   238  	}
   239  }
   240  
   241  // DevConsulSource returns the consul agent configuration for the dev mode.
   242  // This should be merged in the tail after the DefaultConsulSource.
   243  func DevConsulSource() Source {
   244  	return Source{
   245  		Name:   "consul-dev",
   246  		Format: "hcl",
   247  		Data: `
   248  		consul = {
   249  			coordinate = {
   250  				update_period = "100ms"
   251  			}
   252  			raft = {
   253  				election_timeout = "52ms"
   254  				heartbeat_timeout = "35ms"
   255  				leader_lease_timeout = "20ms"
   256  			}
   257  			server = {
   258  				health_interval = "10ms"
   259  			}
   260  		}
   261  	`,
   262  	}
   263  }
   264  
   265  func DefaultRuntimeConfig(hcl string) *RuntimeConfig {
   266  	b, err := NewBuilder(Flags{HCL: []string{hcl}})
   267  	if err != nil {
   268  		panic(err)
   269  	}
   270  	rt, err := b.BuildAndValidate()
   271  	if err != nil {
   272  		panic(err)
   273  	}
   274  	return &rt
   275  }