From e06fb6525b3697b28b66f02ad34e52d93be4f6dd Mon Sep 17 00:00:00 2001 From: Fabio Buso Date: Mon, 4 Sep 2023 09:21:39 +0200 Subject: [PATCH] [HWORKS-719] Add chef code to configure WAN federated consul (#57) --- attributes/default.rb | 5 +++++ metadata.rb | 16 +++++++++++++ recipes/slave.rb | 30 +------------------------ recipes/worker.rb | 29 ++++++++++++++++++++++++ templates/default/config/master.hcl.erb | 10 +++++++-- 5 files changed, 59 insertions(+), 31 deletions(-) create mode 100644 recipes/worker.rb diff --git a/attributes/default.rb b/attributes/default.rb index c0bc0ac1..3fc74ab3 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -27,6 +27,7 @@ default['consul']['http_api_port'] = "8501" default['consul']['rpc_port'] = "8300" default['consul']['domain'] = "consul" +default['consul']['datacenter'] = "lc" default['consul']['bind_address'] = "" # Default bind to localhost but accepts any go-sockaddr template @@ -42,3 +43,7 @@ default['consul']['health-check']['multiplier'] = 1.2 default['consul']['metrics']['prometheus_retention_time'] = "1m" + +default['consul']['wan']['enabled'] = "false" +default['consul']['wan']['serf_port'] = "8302" +default['consul']['wan']['nodes'] = nil \ No newline at end of file diff --git a/metadata.rb b/metadata.rb index b4b878c1..a9804aa0 100644 --- a/metadata.rb +++ b/metadata.rb @@ -59,11 +59,27 @@ attribute "consul/rpc_port", :description => "RPC Server port", :type => 'string' + +attribute "consul/wan/enabled", + :description => "Enable Consul WAN federation across datacenters (Default: false)", + :type => 'string' + +attribute "consul/wan/enabled", + :description => "Enable Consul WAN federation across datacenters (Default: false)", + :type => 'string' + +attribute "consul/wan/nodes", + :description => "If WAN federation enabled, nodes to include in the federation", + :type => 'string' attribute "consul/domain", :description => "Domain to be handled by Consul", :type => 'string' +attribute "consul/datacenter", + :description => "Datacenter configuration (Default: lc)", + :type => 'string' + attribute "consul/bind_address", :description => "IP address Consul agent will bind to. You can also set a go-sockaddr template. Check https://www.consul.io/docs/agent/options.html#_bind for more information.", :type => 'string' diff --git a/recipes/slave.rb b/recipes/slave.rb index 3d80cf38..18723371 100644 --- a/recipes/slave.rb +++ b/recipes/slave.rb @@ -1,29 +1 @@ -include_recipe "consul::default" -include_recipe "consul::security" - -if node['consul']['retry_join']['provider'].empty? - masters = private_recipe_ips("consul", "master") -elsif not node['consul']['retry_join']['provider'].empty? and node['consul']['retry_join']['tag_key'].nil? - masters = private_recipe_ips("consul", "master") -else - masters = ["provider=#{node['consul']['retry_join']['provider'].strip} tag_key=#{node['consul']['retry_join']['tag_key'].strip} tag_value=#{node['consul']['retry_join']['tag_value'].strip}"] -end - -crypto_dir = x509_helper.get_crypto_dir(node['consul']['user']) -hops_ca = "#{crypto_dir}/#{x509_helper.get_hops_ca_bundle_name()}" -certificate = "#{crypto_dir}/#{x509_helper.get_certificate_bundle_name(node['consul']['user'])}" -key = "#{crypto_dir}/#{x509_helper.get_private_key_pkcs8_name(node['consul']['user'])}" -template "#{node['consul']['conf_dir']}/consul.hcl" do - source "config/slave.hcl.erb" - owner node['consul']['user'] - group node['consul']['group'] - mode 0750 - variables({ - :masters => masters, - :hops_ca => hops_ca, - :certificate => certificate, - :key => key - }) -end - -include_recipe "consul::start" +include_recipe "consul::worker" \ No newline at end of file diff --git a/recipes/worker.rb b/recipes/worker.rb new file mode 100644 index 00000000..3d80cf38 --- /dev/null +++ b/recipes/worker.rb @@ -0,0 +1,29 @@ +include_recipe "consul::default" +include_recipe "consul::security" + +if node['consul']['retry_join']['provider'].empty? + masters = private_recipe_ips("consul", "master") +elsif not node['consul']['retry_join']['provider'].empty? and node['consul']['retry_join']['tag_key'].nil? + masters = private_recipe_ips("consul", "master") +else + masters = ["provider=#{node['consul']['retry_join']['provider'].strip} tag_key=#{node['consul']['retry_join']['tag_key'].strip} tag_value=#{node['consul']['retry_join']['tag_value'].strip}"] +end + +crypto_dir = x509_helper.get_crypto_dir(node['consul']['user']) +hops_ca = "#{crypto_dir}/#{x509_helper.get_hops_ca_bundle_name()}" +certificate = "#{crypto_dir}/#{x509_helper.get_certificate_bundle_name(node['consul']['user'])}" +key = "#{crypto_dir}/#{x509_helper.get_private_key_pkcs8_name(node['consul']['user'])}" +template "#{node['consul']['conf_dir']}/consul.hcl" do + source "config/slave.hcl.erb" + owner node['consul']['user'] + group node['consul']['group'] + mode 0750 + variables({ + :masters => masters, + :hops_ca => hops_ca, + :certificate => certificate, + :key => key + }) +end + +include_recipe "consul::start" diff --git a/templates/default/config/master.hcl.erb b/templates/default/config/master.hcl.erb index 8878b20a..6aa9204b 100644 --- a/templates/default/config/master.hcl.erb +++ b/templates/default/config/master.hcl.erb @@ -1,10 +1,15 @@ -datacenter = "lc" +datacenter = "<%= node['consul']['datacenter'] %>" domain = "<%= node['consul']['domain'] %>" server = true <% if !@masters.nil? -%> retry_join = <%= @masters %> <% end -%> bootstrap_expect = <%= @num_masters %> + +<% if !node['consul']['wan']['nodes'].nil? -%> +retry_join_wan = <%= node['consul']['wan']['nodes'] %> +<% end -%> + ui = <%= node['consul']['master']['ui'] %> data_dir = "<%= node['consul']['data_dir'] %>" enable_local_script_checks = true @@ -24,7 +29,8 @@ key_file = "<%= @key %>" ports = { http = -1, https = <%= node['consul']['http_api_port'] %>, - server = <%= node['consul']['rpc_port'] %> + server = <%= node['consul']['rpc_port'] %>, + serf_wan = <%= node['consul']['wan']['serf_port'] %> } telemetry = { prometheus_retention_time = "<%= node['consul']['metrics']['prometheus_retention_time'] %>",