Skip to content

Commit faca982

Browse files
committed
Accept multiple bind addresses in Redis config
As specified in https://redis.io/docs/management/config-file/, Redis can bind to multiple addresses with a space-separated field. Previously attempting to do this without setting `gitlab_rails['redis_host']` would fail because a URI could not be built with a space in the hostname. This commit now splits the string with the space and picks the first address as the default Redis host. Relates to https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/8465 Changelog: added
1 parent 64a47d3 commit faca982

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

files/gitlab-cookbooks/gitlab/libraries/redis.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ def parse_redis_daemon!
7878
return unless redis_managed?
7979

8080
redis_bind = Gitlab['redis']['bind'] || node['redis']['bind']
81+
Gitlab['redis']['default_host'] = redis_bind.split(' ').first
8182

82-
Gitlab['gitlab_rails']['redis_host'] ||= redis_bind
83+
Gitlab['gitlab_rails']['redis_host'] ||= Gitlab['redis']['default_host']
8384

8485
redis_port_config_key = if Gitlab['redis'].key?('port') && !Gitlab['redis']['port'].zero?
8586
# If Redis is specified to run on a non-TLS port

files/gitlab-cookbooks/package/libraries/helpers/new_redis_helper/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def redis_cli_connect_options
5151
end
5252

5353
def redis_cli_tcp_connect_options(args)
54-
args << ["-h #{redis['bind']}"]
54+
args << ["-h #{redis['default_host']}"]
5555
port = redis['port'].to_i
5656

5757
if port.zero?

files/gitlab-cookbooks/redis/templates/default/gitlab-redis-cli-rc.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
redis_dir='<%= node['redis']['dir'] %>'
2-
redis_host='<%= node['redis']['bind'] %>'
2+
redis_host='<%= node['redis']['default_host'] %>'
33
redis_port='<%= node['redis']['port'] %>'
44
redis_tls_port='<%= node['redis']['tls_port'] %>'
55
redis_tls_auth_clients='<%= node['redis']['tls_auth_clients'] %>'

spec/chef/cookbooks/gitlab/libraries/redis_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@
3131
let(:redis_host) { '1.2.3.4' }
3232
let(:redis_port) { 6370 }
3333

34+
context 'when binding to multiple addresses' do
35+
before do
36+
stub_gitlab_rb(
37+
redis: {
38+
bind: '1.2.3.4 5.6.7.8',
39+
port: redis_port
40+
}
41+
)
42+
end
43+
44+
it 'expects redis_host to match first bind value from redis' do
45+
expect(node['gitlab']['gitlab_rails']['redis_host']).to eq '1.2.3.4'
46+
47+
subject.parse_redis_settings
48+
end
49+
end
50+
3451
context 'when not using sentinels' do
3552
before do
3653
stub_gitlab_rb(

spec/chef/cookbooks/redis/recipes/redis_spec.rb

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
let(:gitlab_redis_cli_rc) do
1313
<<-EOF
1414
redis_dir='/var/opt/gitlab/redis'
15-
redis_host='127.0.0.1'
15+
redis_host=''
1616
redis_port='0'
1717
redis_tls_port=''
1818
redis_tls_auth_clients='optional'
@@ -201,6 +201,46 @@
201201
end
202202
end
203203

204+
context 'with multiple bind addresses' do
205+
let(:redis_host) { '1.2.3.4 5.6.7.8' }
206+
let(:redis_port) { 6370 }
207+
let(:master_ip) { '10.0.0.0' }
208+
let(:master_port) { 6371 }
209+
210+
let(:gitlab_redis_cli_rc) do
211+
<<-EOF
212+
redis_dir='/var/opt/gitlab/redis'
213+
redis_host='1.2.3.4'
214+
redis_port='6370'
215+
redis_tls_port=''
216+
redis_tls_auth_clients='optional'
217+
redis_tls_cacert_file='/opt/gitlab/embedded/ssl/certs/cacert.pem'
218+
redis_tls_cacert_dir='/opt/gitlab/embedded/ssl/certs/'
219+
redis_tls_cert_file=''
220+
redis_tls_key_file=''
221+
redis_socket=''
222+
EOF
223+
end
224+
225+
before do
226+
stub_gitlab_rb(
227+
redis: {
228+
bind: redis_host,
229+
port: redis_port,
230+
master_ip: master_ip,
231+
master_port: master_port,
232+
master_password: 'password',
233+
master: false
234+
}
235+
)
236+
end
237+
238+
it 'creates gitlab-redis-cli-rc' do
239+
expect(chef_run).to render_file('/opt/gitlab/etc/gitlab-redis-cli-rc')
240+
.with_content(gitlab_redis_cli_rc)
241+
end
242+
end
243+
204244
context 'with a replica configured' do
205245
let(:redis_host) { '1.2.3.4' }
206246
let(:redis_port) { 6370 }

0 commit comments

Comments
 (0)