# HG changeset patch
# User Stan Hu <stanhu@gmail.com>
# Date 1714016909 25200
#      Wed Apr 24 20:48:29 2024 -0700
# Node ID f6f99aa5eac7cb7aed96d235b66276f282c0beeb
# Parent  5b184256a7fdab5540f2faacf964d29cc1055693
Fix reconfigure failure if Redis node has Rails Sentinel config

https://gitlab.com/gitlab-org/omnibus-gitlab/-/merge_requests/7500
introduced a regression when a node with a managed Redis node has
Sentinels configured in `gitlab_rails` section. Previously the
`redis['default_host']` would only be set if
`gitlab_rails['redis_sentinels']` were empty.

Relates to
https://gitlab.com/gitlab-org/omnibus-gitlab/-/issues/8465#note_1879691545

Changelog: fixed

diff --git a/files/gitlab-cookbooks/gitlab/libraries/redis.rb b/files/gitlab-cookbooks/gitlab/libraries/redis.rb
--- a/files/gitlab-cookbooks/gitlab/libraries/redis.rb
+++ b/files/gitlab-cookbooks/gitlab/libraries/redis.rb
@@ -30,6 +30,7 @@
         # The user wants Redis to listen via TCP instead of unix socket.
         Gitlab['redis']['unixsocket'] = false
 
+        parse_redis_bind_address
         # Try to discover gitlab_rails redis connection params
         # based on redis daemon
         parse_redis_daemon! unless RedisHelper::Checks.has_sentinels?
@@ -74,12 +75,17 @@
 
     private
 
+    def parse_redis_bind_address
+      return unless redis_managed?
+
+      redis_bind = Gitlab['redis']['bind'] || node['redis']['bind']
+      Gitlab['redis']['default_host'] = redis_bind.split(' ').first
+    end
+
     def parse_redis_daemon!
       return unless redis_managed?
 
       redis_bind = Gitlab['redis']['bind'] || node['redis']['bind']
-      Gitlab['redis']['default_host'] = redis_bind.split(' ').first
-
       Gitlab['gitlab_rails']['redis_host'] ||= Gitlab['redis']['default_host']
 
       redis_port_config_key = if Gitlab['redis'].key?('port') && !Gitlab['redis']['port'].zero?
diff --git a/spec/chef/cookbooks/redis/recipes/redis_spec.rb b/spec/chef/cookbooks/redis/recipes/redis_spec.rb
--- a/spec/chef/cookbooks/redis/recipes/redis_spec.rb
+++ b/spec/chef/cookbooks/redis/recipes/redis_spec.rb
@@ -332,6 +332,34 @@
       expect(chef_run).to render_file('/opt/gitlab/etc/gitlab-redis-cli-rc')
         .with_content(gitlab_redis_cli_rc)
     end
+
+    context 'with Sentinels configured' do
+      before do
+        stub_gitlab_rb(
+          redis: {
+            bind: redis_host,
+            port: redis_port,
+            ha: true,
+            master_ip: master_ip,
+            master_port: master_port,
+            master_password: 'password',
+            master: false
+          },
+          gitlab_rails: {
+            redis_sentinels: [
+              { 'host' => '127.0.0.1', 'port' => 2637 }
+            ]
+          }
+        )
+      end
+
+      it_behaves_like 'started down runit service', 'redis'
+
+      it 'creates gitlab-redis-cli-rc' do
+        expect(chef_run).to render_file('/opt/gitlab/etc/gitlab-redis-cli-rc')
+          .with_content(gitlab_redis_cli_rc)
+      end
+    end
   end
 
   context 'with rename_commands disabled' do