diff --git a/files/gitlab-ctl-commands-ee/lib/postgresql/ee.rb b/files/gitlab-ctl-commands-ee/lib/postgresql/ee.rb index 3a3a9c7e2cfeba58909988865f7869e4cbf95c64_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9saWIvcG9zdGdyZXNxbC9lZS5yYg==..42089b89a660babb2c55fdb67774b1a26586ca39_ZmlsZXMvZ2l0bGFiLWN0bC1jb21tYW5kcy1lZS9saWIvcG9zdGdyZXNxbC9lZS5yYg== 100644 --- a/files/gitlab-ctl-commands-ee/lib/postgresql/ee.rb +++ b/files/gitlab-ctl-commands-ee/lib/postgresql/ee.rb @@ -5,7 +5,6 @@ class EE class << self def get_primary - node_attributes = GitlabCtl::Util.get_node_attributes consul_enable = node_attributes.dig('consul', 'enable') postgresql_service_name = node_attributes.dig('patroni', 'scope') @@ -14,7 +13,7 @@ raise 'PostgreSQL service name is not defined' if postgresql_service_name.nil? || postgresql_service_name.empty? result = [] - Resolv::DNS.open(nameserver_port: [['127.0.0.1', 8600]]) do |dns| + Resolv::DNS.open(nameserver_port: [['127.0.0.1', consul_dns_port]]) do |dns| ['master', 'standby-leader'].each do |postgresql_primary_service_name| result = dns.getresources("#{postgresql_primary_service_name}.#{postgresql_service_name}.service.consul", Resolv::DNS::Resource::IN::SRV).map do |srv| "#{dns.getaddress(srv.target)}:#{srv.port}" @@ -26,6 +25,16 @@ raise 'PostgreSQL Primary could not be found via Consul DNS' end + + def consul_dns_port + node_attributes.dig('consul', 'configuration', 'ports', 'dns') || 8600 + end + + private + + def node_attributes + @node_attributes ||= GitlabCtl::Util.get_node_attributes + end end end end diff --git a/spec/chef/gitlab-ctl-commands-ee/lib/postgresql/ee_spec.rb b/spec/chef/gitlab-ctl-commands-ee/lib/postgresql/ee_spec.rb index 3a3a9c7e2cfeba58909988865f7869e4cbf95c64_c3BlYy9jaGVmL2dpdGxhYi1jdGwtY29tbWFuZHMtZWUvbGliL3Bvc3RncmVzcWwvZWVfc3BlYy5yYg==..42089b89a660babb2c55fdb67774b1a26586ca39_c3BlYy9jaGVmL2dpdGxhYi1jdGwtY29tbWFuZHMtZWUvbGliL3Bvc3RncmVzcWwvZWVfc3BlYy5yYg== 100644 --- a/spec/chef/gitlab-ctl-commands-ee/lib/postgresql/ee_spec.rb +++ b/spec/chef/gitlab-ctl-commands-ee/lib/postgresql/ee_spec.rb @@ -8,6 +8,10 @@ RSpec.describe GitlabCtl::PostgreSQL::EE do describe "#get_primary" do + before(:each) do + described_class.instance_variable_set(:@node_attributes, nil) + end + context 'when Consul disabled' do before do allow(GitlabCtl::Util).to receive(:get_node_attributes) @@ -57,7 +61,11 @@ .and_return('1.2.3.4') end + it 'should expect consul to listen for dns on port 8600' do + expect(described_class.consul_dns_port).to be(8600) + end + it 'should get the list of PostgreSQL endpoints' do expect(described_class.get_primary).to eq ['1.2.3.4:6432'] end end @@ -60,6 +68,36 @@ it 'should get the list of PostgreSQL endpoints' do expect(described_class.get_primary).to eq ['1.2.3.4:6432'] end end + context 'when consul has a dns port override' do + before do + allow(GitlabCtl::Util).to receive(:get_node_attributes) + .and_return( + { + 'consul' => { + 'enable' => true, + 'configuration' => { + "ports" => { + "dns" => 18600 + } + } + }, + 'patroni' => { + 'scope' => 'fake' + } + } + ) + allow_any_instance_of(Resolv::DNS).to receive(:getresources) + .with('master.fake.service.consul', Resolv::DNS::Resource::IN::SRV) + .and_return([Struct.new(:target, :port).new('fake.address', 6432)]) + allow_any_instance_of(Resolv::DNS).to receive(:getaddress) + .with('fake.address') + .and_return('1.2.3.4') + end + + it 'should expect consul to listen at the user configured port' do + expect(described_class.consul_dns_port).to be(18600) + end + end end end