Repository: spotahome/redis-operator
Pull request #576 (2023-03-20 21:05:09)

Fix command line for Redis liveness probe
Fixes always succeeding liveness probe described in #575

Changes proposed on the PR:
- Pass the `PING` command to `redis-cli` after credentials to avoid auth failures
- Explicitly check for a `PONG` response, so that error status is properly returned


samof76: @mpihlak why do we do a grep, isn't exit status `1` not returned when ping fails?
mpihlak: > @mpihlak why do we do a grep, isn't exit status `1` not returned when ping fails?

`redis-cli` is not returning proper exit status in this case:

```
/data $ redis-cli -h localhost -p 6379 ping --user pinger --pass pingpass --no-auth-warning
(error) NOAUTH Authentication required.
/data $ echo $?
0
```

There's also [this](https://github.com/redis/redis/issues/5458) issue open on the Redis side and some other issues mentioning error statuses not properly returned. 

ese: Thanks @mpihlak Could you merge with master to fix CI?


Patch:
diff --git a/example/operator/custom-startup-config.yaml b/example/operator/custom-startup-config.yaml
index 2b184144..c14cd051 100644
--- a/example/operator/custom-startup-config.yaml
+++ b/example/operator/custom-startup-config.yaml
@@ -6,7 +6,7 @@ metadata:
 data:
   startup.sh: |
     #!/bin/bash
-    redis-cli -h 127.0.0.1 -p ${REDIS_PORT} ping --user pinger --pass pingpass --no-auth-warning
+    redis-cli -h 127.0.0.1 -p ${REDIS_PORT} --user pinger --pass pingpass --no-auth-warning ping | grep PONG
 ---
 apiVersion: databases.spotahome.com/v1
 kind: RedisFailover
diff --git a/operator/redisfailover/service/generator.go b/operator/redisfailover/service/generator.go
index e423850a..e25685f6 100644
--- a/operator/redisfailover/service/generator.go
+++ b/operator/redisfailover/service/generator.go
@@ -383,7 +383,7 @@ func generateRedisStatefulSet(rf *redisfailoverv1.RedisFailover, labels map[stri
 					Command: []string{
 						"sh",
 						"-c",
-						fmt.Sprintf("redis-cli -h $(hostname) -p %[1]v ping --user pinger --pass pingpass --no-auth-warning", rf.Spec.Redis.Port),
+						fmt.Sprintf("redis-cli -h $(hostname) -p %[1]v --user pinger --pass pingpass --no-auth-warning ping | grep PONG", rf.Spec.Redis.Port),
 					},
 				},
 			},


Test patch:
diff --git a/operator/redisfailover/service/generator_test.go b/operator/redisfailover/service/generator_test.go
index 7cfa1546..3b520621 100644
--- a/operator/redisfailover/service/generator_test.go
+++ b/operator/redisfailover/service/generator_test.go
@@ -1898,7 +1898,7 @@ func TestRedisCustomLivenessProbe(t *testing.T) {
 						Command: []string{
 							"sh",
 							"-c",
-							"redis-cli -h 127.0.0.1 -p ${REDIS_PORT} ping --user pinger --pass pingpass --no-auth-warning",
+							"redis-cli -h 127.0.0.1 -p ${REDIS_PORT} --user pinger --pass pingpass --no-auth-warning ping | grep PONG",
 						},
 					},
 				},
@@ -1913,7 +1913,7 @@ func TestRedisCustomLivenessProbe(t *testing.T) {
 						Command: []string{
 							"sh",
 							"-c",
-							"redis-cli -h 127.0.0.1 -p ${REDIS_PORT} ping --user pinger --pass pingpass --no-auth-warning",
+							"redis-cli -h 127.0.0.1 -p ${REDIS_PORT} --user pinger --pass pingpass --no-auth-warning ping | grep PONG",
 						},
 					},
 				},
@@ -1932,7 +1932,7 @@ func TestRedisCustomLivenessProbe(t *testing.T) {
 						Command: []string{
 							"sh",
 							"-c",
-							"redis-cli -h $(hostname) -p 6379 ping --user pinger --pass pingpass --no-auth-warning",
+							"redis-cli -h $(hostname) -p 6379 --user pinger --pass pingpass --no-auth-warning ping | grep PONG",
 						},
 					},
 				},