Skip to content

Commit fb6645c

Browse files
balasankarcSylvester Chin
andcommitted
Merge branch 'sc1-add-external-workhorse-redis' into 'master'
Provide option to configure a separate workhorse redis See merge request https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/7071 Merged-by: Balasankar 'Balu' C <[email protected]> Approved-by: Balasankar 'Balu' C <[email protected]> Approved-by: Clemens Beck <[email protected]> Reviewed-by: Balasankar 'Balu' C <[email protected]> Co-authored-by: Sylvester Chin <[email protected]>
2 parents 66851e1 + 8a0d77b commit fb6645c

File tree

6 files changed

+204
-12
lines changed

6 files changed

+204
-12
lines changed

files/gitlab-config-template/gitlab.rb.template

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,14 @@ external_url 'GENERATED_EXTERNAL_URL'
858858
# gitlab_rails['redis_repository_cache_username'] = nil
859859
# gitlab_rails['redis_repository_cache_password'] = nil
860860
# gitlab_rails['redis_repository_cache_cluster_nodes'] = nil
861-
861+
# gitlab_rails['redis_workhorse_instance'] = nil
862+
# gitlab_rails['redis_workhorse_sentinels'] = nil
863+
# gitlab_rails['redis_workhorse_sentinels_password'] = nil
864+
# gitlab_rails['redis_workhorse_username'] = nil
865+
# gitlab_rails['redis_workhorse_password'] = nil
866+
# gitlab_rails['redis_workhorse_cluster_nodes'] = nil
867+
868+
# gitlab_rails['redis_workhorse_sentinel_master'] = nil
862869

863870
# gitlab_rails['redis_yml_override'] = nil
864871

files/gitlab-cookbooks/gitlab/attributes/default.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,15 @@
497497
default['gitlab']['gitlab_rails']['redis_cluster_rate_limiting_username'] = nil
498498
default['gitlab']['gitlab_rails']['redis_cluster_rate_limiting_password'] = nil
499499
default['gitlab']['gitlab_rails']['redis_cluster_rate_limiting_cluster_nodes'] = []
500+
default['gitlab']['gitlab_rails']['redis_workhorse_instance'] = nil
501+
default['gitlab']['gitlab_rails']['redis_workhorse_sentinels'] = []
502+
default['gitlab']['gitlab_rails']['redis_workhorse_sentinels_password'] = nil
503+
default['gitlab']['gitlab_rails']['redis_workhorse_username'] = nil
504+
default['gitlab']['gitlab_rails']['redis_workhorse_password'] = nil
505+
default['gitlab']['gitlab_rails']['redis_workhorse_cluster_nodes'] = []
506+
507+
# used by workhorse to connect to a separate external redis instead of the omnibus-gitlab redis
508+
default['gitlab']['gitlab_rails']['redis_workhorse_sentinel_master'] = nil
500509

501510
default['gitlab']['gitlab_rails']['redis_yml_override'] = nil
502511

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'cgi'
33

44
class RedisHelper
5-
REDIS_INSTANCES = %w[cache queues shared_state trace_chunks rate_limiting sessions repository_cache cluster_rate_limiting].freeze
5+
REDIS_INSTANCES = %w[cache queues shared_state trace_chunks rate_limiting sessions repository_cache cluster_rate_limiting workhorse].freeze
66
ALLOWED_REDIS_CLUSTER_INSTANCE = %w[cache rate_limiting cluster_rate_limiting].freeze
77

88
def initialize(node)
@@ -51,6 +51,27 @@ def redis_url(support_sentinel_groupname: true)
5151
uri
5252
end
5353

54+
def workhorse_params
55+
gitlab_rails = @node['gitlab']['gitlab_rails']
56+
if gitlab_rails['redis_workhorse_instance'] || !gitlab_rails['redis_workhorse_sentinels'].empty?
57+
{
58+
url: gitlab_rails['redis_workhorse_instance'],
59+
password: gitlab_rails['redis_workhorse_password'],
60+
sentinels: redis_sentinel_urls('redis_workhorse_sentinels'),
61+
sentinelMaster: gitlab_rails['redis_workhorse_sentinel_master'],
62+
sentinelPassword: gitlab_rails['redis_workhorse_sentinels_password']
63+
}
64+
else
65+
{
66+
url: redis_url,
67+
password: redis_params.last,
68+
sentinels: redis_sentinel_urls('redis_sentinels'),
69+
sentinelMaster: @node['redis']['master_name'],
70+
sentinelPassword: @node['redis']['master_password']
71+
}
72+
end
73+
end
74+
5475
def validate_instance_shard_config(instance)
5576
gitlab_rails = @node['gitlab']['gitlab_rails']
5677

files/gitlab-cookbooks/gitlab/recipes/gitlab-workhorse.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,8 @@
8888

8989
alt_document_root = node['gitlab']['gitlab_workhorse']['alt_document_root']
9090
shutdown_timeout = node['gitlab']['gitlab_workhorse']['shutdown_timeout']
91-
_redis_host, _redis_port, redis_password = redis_helper.redis_params
9291
workhorse_keywatcher = node['gitlab']['gitlab_workhorse']['workhorse_keywatcher']
93-
redis_url = redis_helper.redis_url.to_s
94-
redis_sentinel_urls = redis_helper.redis_sentinel_urls('redis_sentinels')
95-
redis_sentinel_master = node['redis']['master_name']
96-
redis_sentinel_master_password = node['redis']['master_password']
92+
redis_params = redis_helper.workhorse_params
9793
config_file_path = File.join(working_dir, "config.toml")
9894
image_scaler_max_procs = node['gitlab']['gitlab_workhorse']['image_scaler_max_procs']
9995
image_scaler_max_filesize = node['gitlab']['gitlab_workhorse']['image_scaler_max_filesize']
@@ -108,12 +104,12 @@
108104
variables(
109105
alt_document_root: alt_document_root,
110106
workhorse_keywatcher: workhorse_keywatcher,
111-
redis_url: redis_url,
112-
password: redis_password,
113-
sentinels: redis_sentinel_urls,
114-
sentinel_master: redis_sentinel_master,
107+
redis_url: redis_params[:url],
108+
password: redis_params[:password],
109+
sentinels: redis_params[:sentinels],
110+
sentinel_master: redis_params[:sentinelMaster],
111+
master_password: redis_params[:sentinelPassword],
115112
shutdown_timeout: shutdown_timeout,
116-
master_password: redis_sentinel_master_password,
117113
image_scaler_max_procs: image_scaler_max_procs,
118114
image_scaler_max_filesize: image_scaler_max_filesize,
119115
trusted_cidrs_for_propagation: trusted_cidrs_for_propagation,

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,100 @@
139139
end
140140
end
141141

142+
describe '#workhorse_params' do
143+
let(:baseline_config_with_sentinel) do
144+
{
145+
gitlab_rails: {
146+
redis_host: 'redis.example.com',
147+
redis_port: 8888,
148+
redis_password: 'mypass',
149+
redis_sentinels: [
150+
{ 'host' => 'sentinel1.example.com', 'port' => '12345' },
151+
{ 'host' => 'sentinel2.example.com', 'port' => '12345' }
152+
]
153+
},
154+
redis: {
155+
master_name: 'master_from.redis.master_name',
156+
master_password: 'password_from.redis.master_password'
157+
}
158+
}
159+
end
160+
161+
before { allow(Gitlab).to receive(:[]).and_call_original }
162+
163+
context 'without external workhorse redis' do
164+
context 'when no sentinels are configured' do
165+
it 'renders parameters for workhorse redis' do
166+
stub_gitlab_rb(
167+
gitlab_rails: {
168+
redis_host: 'redis.example.com',
169+
redis_port: 8888,
170+
redis_password: 'mypass'
171+
}
172+
)
173+
174+
params = subject.workhorse_params
175+
expect(params[:password]).to eq('mypass')
176+
expect(params[:sentinels]).to eq([])
177+
expect(params[:url].to_s).to eq('redis://:[email protected]:8888/')
178+
end
179+
end
180+
181+
context 'when sentinels are configured' do
182+
it 'renders parameters for workhorse redis' do
183+
stub_gitlab_rb(baseline_config_with_sentinel)
184+
185+
params = subject.workhorse_params
186+
expect(params[:password]).to eq('password_from.redis.master_password')
187+
expect(params[:sentinels].map(&:to_s)).to eq(%w[redis://sentinel1.example.com:12345 redis://sentinel2.example.com:12345])
188+
expect(params[:sentinelMaster]).to eq('master_from.redis.master_name')
189+
expect(params[:sentinelPassword]).to eq('password_from.redis.master_password')
190+
expect(params[:url].to_s).to eq("redis://:password_from.redis.master_password@master_from.redis.master_name/")
191+
end
192+
end
193+
end
194+
195+
context 'with external workhorse redis' do
196+
context 'when no sentinels are configured' do
197+
it 'renders parameters for workhorse redis' do
198+
stub_gitlab_rb(baseline_config_with_sentinel.merge({
199+
gitlab_rails: {
200+
redis_workhorse_instance: "redis://:redis.workhorse.com:8888",
201+
redis_workhorse_password: "workhorse.password"
202+
}
203+
}))
204+
205+
params = subject.workhorse_params
206+
expect(params[:password]).to eq('workhorse.password')
207+
expect(params[:sentinels]).to eq([])
208+
expect(params[:url].to_s).to eq('redis://:redis.workhorse.com:8888')
209+
end
210+
end
211+
212+
context 'when sentinels are configured' do
213+
it 'renders parameters for workhorse redis' do
214+
stub_gitlab_rb(baseline_config_with_sentinel.merge({
215+
gitlab_rails: {
216+
redis_workhorse_sentinels: [
217+
{ 'host' => 'sentinel1.workhorse.com', 'port' => '12345' },
218+
{ 'host' => 'sentinel2.workhorse.com', 'port' => '12345' }
219+
],
220+
redis_workhorse_sentinels_password: "workhorse.password",
221+
redis_workhorse_sentinel_master: "workhorse.master"
222+
}
223+
}))
224+
225+
params = subject.workhorse_params
226+
expect(params[:password]).to be_nil
227+
expect(params[:sentinels].map(&:to_s)).to eq(%w[redis://:[email protected]:12345 redis://:[email protected]:12345])
228+
expect(params[:sentinelMaster]).to eq('workhorse.master')
229+
expect(params[:sentinelPassword]).to eq('workhorse.password')
230+
expect(params[:url]).to eq(nil)
231+
end
232+
end
233+
end
234+
end
235+
142236
describe '#running_version' do
143237
let(:redis_cli_output) do
144238
<<~MSG

spec/chef/cookbooks/gitlab/recipes/gitlab-workhorse_spec.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,71 @@
619619
end
620620
end
621621

622+
context 'with separate workhorse redis details specified' do
623+
context 'with standalone redis defined' do
624+
before do
625+
stub_gitlab_rb(
626+
gitlab_rails: {
627+
redis_sentinels: [
628+
{ 'host' => '127.0.0.1', 'port' => 26379 },
629+
{ 'host' => '127.0.8.1', 'port' => 12345 }
630+
],
631+
redis_workhorse_instance: "unix:/home/random/path.socket",
632+
redis_workhorse_password: 'some pass'
633+
},
634+
redis: {
635+
master_name: 'examplemaster',
636+
master_password: 'examplepassword'
637+
}
638+
)
639+
end
640+
641+
it 'should generate config file with the specified values' do
642+
content_url = 'URL = "unix:/home/random/path.socket"'
643+
content_password = 'Password = "some pass"'
644+
expect(chef_run).to render_file("/var/opt/gitlab/gitlab-workhorse/config.toml").with_content(content_url)
645+
expect(chef_run).to render_file("/var/opt/gitlab/gitlab-workhorse/config.toml").with_content(content_password)
646+
expect(chef_run).not_to render_file("/var/opt/gitlab/gitlab-workhorse/config.toml").with_content(/Sentinel/)
647+
expect(chef_run).not_to render_file("/var/opt/gitlab/gitlab-workhorse/config.toml").with_content(/SentinelMaster/)
648+
end
649+
end
650+
651+
context 'with sentinels defined' do
652+
before do
653+
stub_gitlab_rb(
654+
gitlab_rails: {
655+
redis_sentinels: [
656+
{ 'host' => '127.0.0.1', 'port' => 26379 },
657+
{ 'host' => '127.0.8.1', 'port' => 12345 }
658+
],
659+
redis_sentinels_password: 'some pass',
660+
redis_workhorse_sentinels: [
661+
{ 'host' => '127.0.0.2', 'port' => 26379 },
662+
{ 'host' => '127.0.8.2', 'port' => 12345 }
663+
],
664+
redis_workhorse_sentinels_password: 'some workhorse pass',
665+
redis_workhorse_sentinel_master: 'worhorse.master'
666+
},
667+
redis: {
668+
master_name: 'examplemaster',
669+
master_password: 'examplepassword'
670+
}
671+
)
672+
end
673+
674+
it 'should generate config file with the specified values' do
675+
content = 'Sentinel = ["redis://:[email protected]:26379","redis://:[email protected]:12345"]'
676+
content_sentinel_master = 'SentinelMaster = "worhorse.master"'
677+
content_sentinel_password = 'Password = "some workhorse pass"'
678+
content_url = 'URL ='
679+
expect(chef_run).to render_file("/var/opt/gitlab/gitlab-workhorse/config.toml").with_content(content)
680+
expect(chef_run).to render_file("/var/opt/gitlab/gitlab-workhorse/config.toml").with_content(content_sentinel_master)
681+
expect(chef_run).to render_file("/var/opt/gitlab/gitlab-workhorse/config.toml").with_content(content_sentinel_password)
682+
expect(chef_run).not_to render_file("/var/opt/gitlab/gitlab-workhorse/config.toml").with_content(content_url)
683+
end
684+
end
685+
end
686+
622687
context 'image scaler' do
623688
context 'with default values' do
624689
it 'sets the default maximum file size' do

0 commit comments

Comments
 (0)