chameleonbr / node-red-contrib-redis

Node RED client for Redis with pub/sub, list, lua scripting and other commands support.
MIT License
46 stars 40 forks source link

Enhance Redis Config Node to Handle JSON Strings from Environment Variables #68

Open PJ-Schulz opened 2 months ago

PJ-Schulz commented 2 months ago

This PR addresses an issue where selecting env as the configuration source and providing a string value would incorrectly treat the string as a hostname. If the string is actually a JSON configuration, this results in an error as the JSON string is incorrectly used as a hostname.

Changes:

When env is selected as the configuration source and the value is a string, it can either be a simple hostname or a JSON configuration. Without this change, JSON strings cause errors since they are misinterpreted as hostnames. This enhancement ensures that environment variable configurations are more flexible. This improves the usability of the Redis Config Node in environments where configuration is provided via environment variables like in Docker or Kubernetes.

Example 1:

REDIS_CONFIG = '{"host":"localhost","port":6379}'

is parsed and used as a object.

REDIS_CONFIG = 'localhost'

continues to be used as a hostname.

Example 2:

in my use-case i execute NodeRED in Kubernetes and pass configurations via environment variables to it. For anyone how is interested, here is a snipped of my manifest and how I use it in NodeRed:

    spec:
      containers:
        - name: nodered
          image: nodered/node-red:4.0.2
          env:
            - name: REDIS_CONFIG
              value: |
                {
                  "host": "redis-server",
                  "port": 6379,
                  "db": 0,
                  "username": "$(redis_username)",
                  "password": "$(redis_password)"
                }
          envFrom:
            - secretRef:
                name: redis-user-nodered  # containes redis_username redis_password

grafik

[{"id":"e3b3d83b76ec9607","type":"redis-instance","z":"ec1fd2a9a2f3cff8","server":"73589485eb3882eb","name":"","topic":"Redis","location":"global","x":190,"y":180,"wires":[]},{"id":"73589485eb3882eb","type":"redis-config","name":"Redis","options":"REDIS_CONFIG","cluster":false,"optionsType":"env"}]