Commit f39beb16 authored by Yorick Peterse's avatar Yorick Peterse

Merge branch 'require-lb-service-discovery' into 'master'

Require a LoadBalancer for service discovery

See merge request gitlab-org/gitlab!68856
parents eef6f55f ea7a01bf
...@@ -102,7 +102,9 @@ module Gitlab ...@@ -102,7 +102,9 @@ module Gitlab
def self.start_service_discovery def self.start_service_discovery
return unless service_discovery_enabled? return unless service_discovery_enabled?
ServiceDiscovery.new(service_discovery_configuration).start ServiceDiscovery
.new(proxy.load_balancer, **service_discovery_configuration)
.start
end end
# Configures proxying of requests. # Configures proxying of requests.
...@@ -111,7 +113,9 @@ module Gitlab ...@@ -111,7 +113,9 @@ module Gitlab
# Populate service discovery immediately if it is configured # Populate service discovery immediately if it is configured
if service_discovery_enabled? if service_discovery_enabled?
ServiceDiscovery.new(service_discovery_configuration).perform_service_discovery ServiceDiscovery
.new(proxy.load_balancer, **service_discovery_configuration)
.perform_service_discovery
end end
end end
......
...@@ -49,14 +49,14 @@ module Gitlab ...@@ -49,14 +49,14 @@ module Gitlab
# use_tcp - Use TCP instaed of UDP to look up resources # use_tcp - Use TCP instaed of UDP to look up resources
# load_balancer - The load balancer instance to use # load_balancer - The load balancer instance to use
def initialize( def initialize(
load_balancer,
nameserver:, nameserver:,
port:, port:,
record:, record:,
record_type: 'A', record_type: 'A',
interval: 60, interval: 60,
disconnect_timeout: 120, disconnect_timeout: 120,
use_tcp: false, use_tcp: false
load_balancer: LoadBalancing.proxy.load_balancer
) )
@nameserver = nameserver @nameserver = nameserver
@port = port @port = port
......
...@@ -6,10 +6,10 @@ RSpec.describe Gitlab::Database::LoadBalancing::ServiceDiscovery do ...@@ -6,10 +6,10 @@ RSpec.describe Gitlab::Database::LoadBalancing::ServiceDiscovery do
let(:load_balancer) { Gitlab::Database::LoadBalancing::LoadBalancer.new([]) } let(:load_balancer) { Gitlab::Database::LoadBalancing::LoadBalancer.new([]) }
let(:service) do let(:service) do
described_class.new( described_class.new(
load_balancer,
nameserver: 'localhost', nameserver: 'localhost',
port: 8600, port: 8600,
record: 'foo', record: 'foo'
load_balancer: load_balancer
) )
end end
...@@ -26,11 +26,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::ServiceDiscovery do ...@@ -26,11 +26,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::ServiceDiscovery do
describe ':record_type' do describe ':record_type' do
subject do subject do
described_class.new( described_class.new(
load_balancer,
nameserver: 'localhost', nameserver: 'localhost',
port: 8600, port: 8600,
record: 'foo', record: 'foo',
record_type: record_type, record_type: record_type
load_balancer: load_balancer
) )
end end
...@@ -217,11 +217,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::ServiceDiscovery do ...@@ -217,11 +217,11 @@ RSpec.describe Gitlab::Database::LoadBalancing::ServiceDiscovery do
describe '#addresses_from_dns' do describe '#addresses_from_dns' do
let(:service) do let(:service) do
described_class.new( described_class.new(
load_balancer,
nameserver: 'localhost', nameserver: 'localhost',
port: 8600, port: 8600,
record: 'foo', record: 'foo',
record_type: record_type, record_type: record_type
load_balancer: load_balancer
) )
end end
......
...@@ -204,9 +204,11 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -204,9 +204,11 @@ RSpec.describe Gitlab::Database::LoadBalancing do
end end
describe '.configure_proxy' do describe '.configure_proxy' do
it 'configures the connection proxy' do before do
allow(ActiveRecord::Base).to receive(:load_balancing_proxy=) allow(ActiveRecord::Base).to receive(:load_balancing_proxy=)
end
it 'configures the connection proxy' do
described_class.configure_proxy described_class.configure_proxy
expect(ActiveRecord::Base).to have_received(:load_balancing_proxy=) expect(ActiveRecord::Base).to have_received(:load_balancing_proxy=)
...@@ -214,15 +216,22 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -214,15 +216,22 @@ RSpec.describe Gitlab::Database::LoadBalancing do
end end
context 'when service discovery is enabled' do context 'when service discovery is enabled' do
let(:service_discovery) { double(Gitlab::Database::LoadBalancing::ServiceDiscovery) }
it 'runs initial service discovery when configuring the connection proxy' do it 'runs initial service discovery when configuring the connection proxy' do
discover = instance_spy(Gitlab::Database::LoadBalancing::ServiceDiscovery)
allow(described_class) allow(described_class)
.to receive(:configuration) .to receive(:configuration)
.and_return('discover' => { 'record' => 'foo' }) .and_return('discover' => { 'record' => 'foo' })
expect(Gitlab::Database::LoadBalancing::ServiceDiscovery).to receive(:new).and_return(service_discovery) expect(Gitlab::Database::LoadBalancing::ServiceDiscovery)
expect(service_discovery).to receive(:perform_service_discovery) .to receive(:new)
.with(
an_instance_of(Gitlab::Database::LoadBalancing::LoadBalancer),
an_instance_of(Hash)
)
.and_return(discover)
expect(discover).to receive(:perform_service_discovery)
described_class.configure_proxy described_class.configure_proxy
end end
...@@ -297,10 +306,16 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -297,10 +306,16 @@ RSpec.describe Gitlab::Database::LoadBalancing do
.and_return(true) .and_return(true)
instance = double(:instance) instance = double(:instance)
lb = instance_spy(Gitlab::Database::LoadBalancing::LoadBalancer)
proxy = double(:proxy, load_balancer: lb)
allow(Gitlab::Database::LoadBalancing)
.to receive(:proxy)
.and_return(proxy)
expect(Gitlab::Database::LoadBalancing::ServiceDiscovery) expect(Gitlab::Database::LoadBalancing::ServiceDiscovery)
.to receive(:new) .to receive(:new)
.with(an_instance_of(Hash)) .with(lb, an_instance_of(Hash))
.and_return(instance) .and_return(instance)
expect(instance) expect(instance)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment