Skip to content

Commit

Permalink
Added test scenarios for environment variable expansion in log config
Browse files Browse the repository at this point in the history
Signed-off-by: wasim-nihal <sswasim64@gmail.com>
  • Loading branch information
wasim-nihal committed Sep 25, 2024
1 parent bcd4d5c commit e4ee00b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/build_and_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,8 @@ jobs:
kubectl exec sidecar -- sh -c "! test -e /tmp/relative/relative.txt" && kubectl exec sidecar -- sh -c "test -e /tmp/relative/change-relative.txt" &&
kubectl exec sidecar -- sh -c "! test -e /tmp/orig-dir/change-dir.txt" && kubectl exec sidecar -- sh -c "test -e /tmp/new-dir/change-dir.txt" &&
kubectl exec sidecar -- sh -c "! test -e /tmp/similar-configmap.txt" && kubectl exec sidecar -- sh -c "test -e /tmp/change-similar-configmap.txt" &&
kubectl exec sidecar -- sh -c "! test -e /tmp/similar-secret.txt" && kubectl exec sidecar -- sh -c "test -e /tmp/change-similar-secret.txt"
kubectl exec sidecar -- sh -c "! test -e /tmp/similar-secret.txt" && kubectl exec sidecar -- sh -c "test -e /tmp/change-similar-secret.txt"
- name: Verify environment variable expansion feature in log config
run: |
cat /tmp/logs/sidecar-pythonscript-logfile.log | jq 'has("msg") and has("level")' -e >/dev/null &&
test $(cat /tmp/logs/sidecar-pythonscript-logfile.log | jq '. | select(.level == "DEBUG")' | wc -l) -gt 0
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ If the filename ends with `.url` suffix, the content will be processed as a URL
| `LOG_TZ` | Set the log timezone. (LOCAL or UTC) | false | `LOCAL` | string |
| `LOG_CONFIG` | Log configuration file path. If not configured, uses the default log config for backward compatibility support. When not configured `LOG_LEVEL, LOG_FORMAT and LOG_TZ` would be used. Refer to [Python logging](https://docs.python.org/3/library/logging.config.html) for log configuration. For sample configuration file refer to file examples/example_logconfig.yaml | false | - | string |

# Environment variable expansion in LOG_CONFIG

## Environment variable expansion in LOG_CONFIG
Kiwigrid k8s-sidecar supports expansion of environment variables in the log config.
This can be done by wrapping the name of environment variable in the regex placeholder `$(<env_var_name>)` in the log config.
At the startup, the k8s container will look for the regex wrapper and replace all the matched occurrences with the content of the environment variables.
At the startup, the k8s-sidecar container will look for the regex wrapper and replace all the matched occurrences with the content of the environment variables.

For instance the below snippet from the log config,

Expand All @@ -117,7 +118,7 @@ root:
...
```

would be read as below, replacing the content of `$(LV_DBG)` with the value of environment variable `LV_DBG`.
would be read as below, replacing the content of `$(LV_DBG)` with the value of environment variable `LV_DBG` (assuming it set to `DEBUG` here).

```commandline
version: 1
Expand All @@ -127,4 +128,6 @@ root:
level: DEBUG
handlers: [console]
...
```
```

> **Note:** The k8s-sidecar will terminate if it finds a match for the environment variable expansion placeholder and the corresponding environment variable is not set.
18 changes: 11 additions & 7 deletions test/resources/sidecar.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,22 +166,22 @@ data:
disable_existing_loggers: false
root:
level: DEBUG
level: $(ENV_LEVEL_DEBUG)
handlers: [console]
handlers:
console:
class: logging.StreamHandler
level: DEBUG
level: $(ENV_LEVEL_DEBUG)
formatter: JSON
formatters:
JSON:
(): logger.JsonFormatter
format: '%(levelname)s %(message)s'
rename_fields: {
"message": "msg",
"levelname": "level"
"message": "$(ENV_RENAME_FIELD_MSG)",
"levelname": "$(ENV_RENAME_FIELD_LEVEL)"
}
---
apiVersion: v1
Expand All @@ -204,7 +204,7 @@ data:
class: logging.handlers.RotatingFileHandler
level: DEBUG
formatter: JSON
filename: "$(OUTPUT_LOG_FILE)"
filename: "/opt/logs/sidecar.log"
maxBytes: 2097152
backupCount: 3
Expand Down Expand Up @@ -248,6 +248,12 @@ spec:
value: "DEBUG"
- name: LOG_CONFIG
value: "/etc/k8s-sidecar/log_conf.yaml"
- name: ENV_LEVEL_DEBUG
value: "DEBUG"
- name: ENV_RENAME_FIELD_MSG
value: "msg"
- name: ENV_RENAME_FIELD_LEVEL
value: "level"
volumes:
- name: shared-volume
emptyDir: { }
Expand Down Expand Up @@ -294,8 +300,6 @@ spec:
value: "DEBUG"
- name: LOG_CONFIG
value: "/etc/k8s-sidecar/log_conf.yaml"
- name: OUTPUT_LOG_FILE
value: "/opt/logs/sidecar.log"
volumes:
- name: shared-volume
emptyDir: { }
Expand Down

0 comments on commit e4ee00b

Please sign in to comment.