Skip to content

Commit

Permalink
Add the ability to connect to redis cluster (#291)
Browse files Browse the repository at this point in the history
* Add the ability to connect to redis cluster

* Add the redis cluster check to the helm test
  • Loading branch information
VyacheslavSemin authored Jan 30, 2024
1 parent 4cb2810 commit eb4cc1a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ The `helm delete` command removes all the Kubernetes components associated with
| `connections.redisPort` | The Redis server port number | `6379` |
| `connections.redisUser` | The Redis [user](https://redis.io/docs/management/security/acl/) name. The value in this parameter overrides the value set in the `options` object in `local.json` if you add custom configuration file | `default` |
| `connections.redisDBNum` | Number of the redis logical database to be [selected](https://redis.io/commands/select/). The value in this parameter overrides the value set in the `options` object in `local.json` if you add custom configuration file | `0` |
| `connections.redisClusterNodes` | List of nodes in the Redis cluster. There is no need to specify every node in the cluster, 3 should be enough. You can specify multiple values. It must be specified in the `host:port` format | `[]` |
| `connections.redisSentinelGroupName` | Name of a group of Redis instances composed of a master and one or more slaves. Used if `connections.redisConnectorName` is set to `ioredis` | `mymaster` |
| `connections.redisPassword` | The password set for the Redis account. If set to, it takes priority over the `connections.redisExistingSecret`. The value in this parameter overrides the value set in the `options` object in `local.json` if you add custom configuration file | `""` |
| `connections.redisSecretKeyName` | The name of the key that contains the Redis user password | `redis-password` |
Expand Down Expand Up @@ -422,8 +423,8 @@ The `helm delete` command removes all the Kubernetes components associated with
| `imagePullSecrets` | Container image registry secret name | `""` |
| `requestFilteringAgent.allowPrivateIPAddress` | Defines if it is allowed to connect private IP address or not. `requestFilteringAgent` parameters are used if JWT is disabled: `jwt.enabled=false` | `false` |
| `requestFilteringAgent.allowMetaIPAddress` | Defines if it is allowed to connect meta address or not | `false` |
| `requestFilteringAgent.allowIPAddressList` | Defines the list of IP addresses allowed to connect. This values are preferred than `requestFilteringAgent.denyIPAddressList` | '[]' |
| `requestFilteringAgent.denyIPAddressList` | Defines the list of IP addresses allowed to connect | '[]' |
| `requestFilteringAgent.allowIPAddressList` | Defines the list of IP addresses allowed to connect. This values are preferred than `requestFilteringAgent.denyIPAddressList` | `[]` |
| `requestFilteringAgent.denyIPAddressList` | Defines the list of IP addresses allowed to connect | `[]` |
| `docservice.podAnnotations` | Map of annotations to add to the Docservice deployment pods | `rollme: "{{ randAlphaNum 5 \| quote }}"` |
| `docservice.replicas` | Docservice replicas quantity. If the `docservice.autoscaling.enabled` parameter is enabled, it is ignored | `2` |
| `docservice.updateStrategy.type` | Docservice deployment update strategy type | `Recreate` |
Expand Down
40 changes: 35 additions & 5 deletions sources/scripts/test_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
redisPassword = os.environ.get('REDIS_SERVER_PWD')
redisDBNum = os.environ.get('REDIS_SERVER_DB_NUM')
redisConnectTimeout = 15
if os.environ.get('REDIS_CLUSTER_NODES'):
redisClusterNodes = list(os.environ.get('REDIS_CLUSTER_NODES').split(" "))
redisClusterNode = redisClusterNodes[0].split(":")[0]
redisClusterPort = redisClusterNodes[0].split(":")[1]
if redisConnectorName == 'ioredis':
redisSentinelGroupName = os.environ.get('REDIS_SENTINEL_GROUP_NAME')

Expand Down Expand Up @@ -72,10 +76,33 @@ def get_redis_status():
)
rc.ping()
except Exception as msg_redis:
logger_test_ds.error(f'Failed to check the availability of the Redis... {msg_redis}\n')
logger_test_ds.error(f'Failed to check the availability of the Redis Standalone... {msg_redis}\n')
total_result['CheckRedis'] = 'Failed'
else:
logger_test_ds.info('Successful connection to Redis')
logger_test_ds.info('Successful connection to Redis Standalone')
return rc.ping()


def get_redis_cluster_status():
install_module('redis')
from redis.cluster import RedisCluster as Redis
from redis.cluster import ClusterNode
global rc
try:
nodes = [ClusterNode(redisClusterNode, redisClusterPort)]
rc = Redis(
startup_nodes=nodes,
username=redisUser,
password=redisPassword,
socket_connect_timeout=redisConnectTimeout,
retry_on_timeout=True
)
rc.ping()
except Exception as msg_redis:
logger_test_ds.error(f'Failed to check the availability of the Redis Cluster... {msg_redis}\n')
total_result['CheckRedis'] = 'Failed'
else:
logger_test_ds.info('Successful connection to Redis Cluster')
return rc.ping()


Expand All @@ -98,10 +125,10 @@ def get_redis_sentinel_status():
)
rc.ping()
except Exception as msg_redis:
logger_test_ds.error(f'Failed to check the availability of the Redis... {msg_redis}\n')
logger_test_ds.error(f'Failed to check the availability of the Redis Sentinel... {msg_redis}\n')
total_result['CheckRedis'] = 'Failed'
else:
logger_test_ds.info('Successful connection to Redis')
logger_test_ds.info('Successful connection to Redis Sentinel')
return rc.ping()


Expand All @@ -122,9 +149,12 @@ def check_redis_key():

def check_redis():
logger_test_ds.info('Checking Redis availability...')
if redisConnectorName == 'redis':
if redisConnectorName == 'redis' and not os.environ.get('REDIS_CLUSTER_NODES'):
if get_redis_status() is True:
check_redis_key()
elif redisConnectorName == 'redis' and os.environ.get('REDIS_CLUSTER_NODES'):
if get_redis_cluster_status() is True:
check_redis_key()
elif redisConnectorName == 'ioredis':
if get_redis_sentinel_status() is True:
check_redis_key()
Expand Down
3 changes: 3 additions & 0 deletions templates/configmaps/documentserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ data:
REDIS_SERVER_PORT: {{ .Values.connections.redisPort | quote }}
REDIS_SERVER_USER: {{ .Values.connections.redisUser }}
REDIS_SERVER_DB_NUM: {{ .Values.connections.redisDBNum | quote }}
{{- if.Values.connections.redisClusterNodes }}
REDIS_CLUSTER_NODES: {{ join " " .Values.connections.redisClusterNodes }}
{{- end }}
{{- if eq .Values.connections.redisConnectorName "ioredis" }}
REDIS_SENTINEL_GROUP_NAME: {{ .Values.connections.redisSentinelGroupName }}
{{- end }}
Expand Down
12 changes: 12 additions & 0 deletions values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ connections:
## If you need to connect to Redis Sentinel, set the value `ioredis`
redisConnectorName: redis
## connections.redisHost The IP address or the name of the Redis host
## Not used if values are set in `connections.redisClusterNodes`
redisHost: redis-master
## connections.redisPort The Redis server port number
## Not used if values are set in `connections.redisClusterNodes`
redisPort: "6379"
## connections.redisUser The Redis user name
## ref: https://redis.io/docs/management/security/acl/
Expand All @@ -45,7 +47,17 @@ connections:
## connections.redisDBNum Number of the redis logical database to be selected
## ref: https://redis.io/commands/select/
## The value in this parameter overrides the value set in the `options` object in `local.json` if you add custom configuration file
## Not used if values are set in `connections.redisClusterNodes`
redisDBNum: "0"
## connections.redisClusterNodes List of nodes in the Redis cluster
## There is no need to specify every node in the cluster, 3 should be enough
## You can specify multiple values
## It must be specified in the `host:port` format
## Example:
## redisClusterNodes:
## - 10.244.0.79:6379
## - 192.168.1.25:6379
redisClusterNodes: []
## connections.redisSentinelGroupName Name of a group of Redis instances composed of a master and one or more slaves
## Used if `connections.redisConnectorName` is set to `ioredis`
redisSentinelGroupName: mymaster
Expand Down

0 comments on commit eb4cc1a

Please sign in to comment.