Skip to content

Commit a708e06

Browse files
committed
Refactor Workhorse Redis Params
Make GitLab Workhorse use the new RedisHelper architecture. Signed-off-by: Balasankar 'Balu' C <[email protected]>
1 parent 27d225c commit a708e06

File tree

3 files changed

+222
-2
lines changed

3 files changed

+222
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616
#
1717
account_helper = AccountHelper.new(node)
18-
redis_helper = RedisHelper.new(node)
18+
redis_helper = NewRedisHelper::GitlabWorkhorse.new(node)
1919
workhorse_helper = GitlabWorkhorseHelper.new(node)
2020
logfiles_helper = LogfilesHelper.new(node)
2121
logging_settings = logfiles_helper.logging_settings('gitlab-workhorse')
@@ -89,7 +89,7 @@
8989
alt_document_root = node['gitlab']['gitlab_workhorse']['alt_document_root']
9090
shutdown_timeout = node['gitlab']['gitlab_workhorse']['shutdown_timeout']
9191
workhorse_keywatcher = node['gitlab']['gitlab_workhorse']['workhorse_keywatcher']
92-
redis_params = redis_helper.workhorse_params
92+
redis_params = redis_helper.redis_params
9393
config_file_path = File.join(working_dir, "config.toml")
9494
image_scaler_max_procs = node['gitlab']['gitlab_workhorse']['image_scaler_max_procs']
9595
image_scaler_max_filesize = node['gitlab']['gitlab_workhorse']['image_scaler_max_filesize']
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module NewRedisHelper
2+
class GitlabWorkhorse < NewRedisHelper::Base
3+
def redis_params
4+
{
5+
url: redis_url,
6+
password: redis_credentials[:password],
7+
sentinels: sentinel_urls,
8+
sentinelMaster: master_name,
9+
sentinelPassword: redis_sentinels_password
10+
}
11+
end
12+
13+
private
14+
15+
def node_access_keys
16+
%w[gitlab gitlab_workhorse]
17+
end
18+
19+
def support_sentinel_groupname?
20+
true
21+
end
22+
end
23+
end
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
require 'chef_helper'
2+
3+
RSpec.describe NewRedisHelper::GitlabWorkhorse do
4+
let(:chef_run) { ChefSpec::SoloRunner.new(step_into: %w(templatesymlink)).converge('gitlab::default') }
5+
let(:workhorse_redis_yml_template) { chef_run.template('/var/opt/gitlab/gitlab-rails/etc/redis.workhorse.yml') }
6+
let(:workhorse_redis_yml_file_content) { ChefSpec::Renderer.new(chef_run, workhorse_redis_yml_template).content }
7+
let(:workhorse_redis_yml) { YAML.safe_load(workhorse_redis_yml_file_content, aliases: true, symbolize_names: true) }
8+
9+
subject { described_class.new(chef_run.node) }
10+
11+
before do
12+
allow(Gitlab).to receive(:[]).and_call_original
13+
end
14+
15+
context '#redis_params' do
16+
context 'by default' do
17+
it 'returns information about the default Redis instance' do
18+
expect(subject.redis_params).to eq(
19+
url: 'unix:/var/opt/gitlab/redis/redis.socket',
20+
password: nil,
21+
sentinels: [],
22+
sentinelMaster: 'gitlab-redis',
23+
sentinelPassword: nil
24+
)
25+
end
26+
end
27+
28+
context 'with user specified values' do
29+
context 'when settings specified via gitlab_rails' do
30+
before do
31+
stub_gitlab_rb(
32+
gitlab_rails: {
33+
redis_host: 'my.redis.host',
34+
redis_password: 'redis-password'
35+
}
36+
)
37+
end
38+
39+
it 'returns information about the provided Redis instance via gitlab_rails' do
40+
expect(subject.redis_params).to eq(
41+
url: 'redis://:[email protected]/',
42+
password: 'redis-password',
43+
sentinels: [],
44+
sentinelMaster: 'gitlab-redis',
45+
sentinelPassword: nil
46+
)
47+
end
48+
end
49+
50+
context 'when settings specified via gitlab_rails for separate Redis instance' do
51+
before do
52+
stub_gitlab_rb(
53+
gitlab_rails: {
54+
redis_host: 'my.redis.host',
55+
redis_password: 'redis-password',
56+
redis_workhorse_instance: 'different.workhorse.redis.instance',
57+
redis_workhorse_password: 'different-redis-password'
58+
}
59+
)
60+
end
61+
62+
it 'returns information about the provided Redis instance via gitlab_rails workhorse instance' do
63+
expect(subject.redis_params).to eq(
64+
url: 'redis://:[email protected]/',
65+
password: 'different-redis-password',
66+
sentinels: [],
67+
sentinelMaster: 'gitlab-redis',
68+
sentinelPassword: nil
69+
)
70+
end
71+
end
72+
73+
context 'when settings specified via gitlab_workhorse' do
74+
context 'when pointing to different Redis instances' do
75+
before do
76+
stub_gitlab_rb(
77+
gitlab_rails: {
78+
redis_host: 'my.redis.host',
79+
redis_password: 'redis-password',
80+
redis_workhorse_instance: 'different.workhorse.redis.instance',
81+
redis_workhorse_password: 'different-redis-password'
82+
},
83+
gitlab_workhorse: {
84+
redis_host: 'workhorse.redis.host',
85+
redis_password: 'redis-workhorse-password'
86+
}
87+
)
88+
end
89+
90+
it 'returns information about the provided Redis instance via gitlab_workhorse' do
91+
expect(subject.redis_params).to eq(
92+
url: 'redis://:[email protected]/',
93+
password: 'redis-workhorse-password',
94+
sentinels: [],
95+
sentinelMaster: 'gitlab-redis',
96+
sentinelPassword: nil
97+
)
98+
end
99+
100+
# TODO: When Workhorse recipe spec is cleaned up, this should ideally
101+
# end up there.
102+
it 'populates workhorse.redis.yml with values from gitlab_workhorse' do
103+
expect(workhorse_redis_yml).to eq(
104+
production: {
105+
url: 'redis://:[email protected]/',
106+
secret_file: '/var/opt/gitlab/gitlab-rails/shared/encrypted_settings/redis.workhorse.yml.enc'
107+
}
108+
)
109+
end
110+
end
111+
112+
context 'when pointing to same Redis instance' do
113+
before do
114+
stub_gitlab_rb(
115+
gitlab_rails: {
116+
redis_host: 'my.redis.host',
117+
redis_password: 'redis-password',
118+
},
119+
gitlab_workhorse: {
120+
redis_host: 'my.redis.host',
121+
redis_password: 'redis-password'
122+
}
123+
)
124+
end
125+
126+
it 'returns information about the provided Redis instance via gitlab_workhorse' do
127+
expect(subject.redis_params).to eq(
128+
url: 'redis://:[email protected]/',
129+
password: 'redis-password',
130+
sentinels: [],
131+
sentinelMaster: 'gitlab-redis',
132+
sentinelPassword: nil
133+
)
134+
end
135+
136+
# TODO: When Workhorse recipe spec is cleaned up, this should ideally
137+
# end up there.
138+
it 'does not populate workhorse.redis.yml' do
139+
expect(chef_run).not_to render_file('/var/opt/gitlab/gitlab-rails/etc/redis.workhorse.yml')
140+
end
141+
end
142+
end
143+
144+
context 'when sentinels are specified' do
145+
context 'when Redis master settings are specified via redis key' do
146+
before do
147+
stub_gitlab_rb(
148+
gitlab_workhorse: {
149+
redis_host: 'workhorse.redis.host',
150+
redis_sentinels: [
151+
{ host: '10.0.0.1', port: 26379 },
152+
{ host: '10.0.0.2', port: 26379 },
153+
{ host: '10.0.0.3', port: 26379 }
154+
],
155+
redis_sentinels_password: 'workhorse-sentinel-password'
156+
},
157+
redis: {
158+
master_name: 'redis-for-workhorse',
159+
master_password: 'redis-password'
160+
}
161+
)
162+
end
163+
164+
it 'returns information about the provided Redis instance via gitlab_workhorse' do
165+
expect(subject.redis_params).to eq(
166+
url: 'redis://:redis-password@redis-for-workhorse/',
167+
password: 'redis-password',
168+
sentinels: [
169+
"redis://:[email protected]:26379",
170+
"redis://:[email protected]:26379",
171+
"redis://:[email protected]:26379"
172+
],
173+
sentinelMaster: 'redis-for-workhorse',
174+
sentinelPassword: 'workhorse-sentinel-password'
175+
)
176+
end
177+
178+
# TODO: When Workhorse recipe spec is cleaned up, this should ideally
179+
# end up there.
180+
it 'populates workhorse.redis.yml with values from gitlab_workhorse' do
181+
expect(workhorse_redis_yml).to eq(
182+
production: {
183+
url: 'redis://:[email protected]/',
184+
secret_file: '/var/opt/gitlab/gitlab-rails/shared/encrypted_settings/redis.workhorse.yml.enc',
185+
sentinels: [
186+
{ host: '10.0.0.1', port: 26379, password: 'workhorse-sentinel-password' },
187+
{ host: '10.0.0.2', port: 26379, password: 'workhorse-sentinel-password' },
188+
{ host: '10.0.0.3', port: 26379, password: 'workhorse-sentinel-password' },
189+
]
190+
}
191+
)
192+
end
193+
end
194+
end
195+
end
196+
end
197+
end

0 commit comments

Comments
 (0)