Skip to content
Snippets Groups Projects
Commit 42089b89 authored by Robert Marshall's avatar Robert Marshall
Browse files

Merge branch 'master' into 'master'

Feat: get consul DNS port from config in `get-postgresql-primary`

See merge request https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/7576



Merged-by: default avatarRobert Marshall <rmarshall@gitlab.com>
Approved-by: default avatarRobert Marshall <rmarshall@gitlab.com>
Co-authored-by: default avatarYu Shao Pang (SQPC) <yushao.pang@squarepoint-capital.com>
parents 3a3a9c7e 73ee28f5
1 merge request!137Remerge after conversion fixups
......@@ -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
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment