Skip to content

Commit

Permalink
added chaosk8s.pod.probes.should_be_found_in_logs
Browse files Browse the repository at this point in the history
Signed-off-by: Sylvain Hellegouarch <[email protected]>
  • Loading branch information
Lawouach committed May 6, 2024
1 parent 20c8a17 commit aa8a0ca
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
51 changes: 50 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,56 @@

## [Unreleased][]

[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-kubernetes/compare/0.38.2...HEAD
[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-kubernetes/compare/0.39.0...HEAD

## [0.39.0][] - 2024-05-06

[0.39.0]: https://github.com/chaostoolkit/chaostoolkit-kubernetes/compare/0.39.0...0.39.0

### Added

* Added the `chaosk8s.pod.probes.should_be_found_in_logs` probe to search
into container logs:

```json
{
"title": "Check pod logs",
"description": "n/a",
"steady-state-hypothesis": {
"title": "extract-logs-with-regex",
"probes": [
{
"name": "at-least-one-pod-should-have-restarted",
"type": "probe",
"tolerance": {
"name": "search-probe",
"type": "probe",
"provider": {
"type": "python",
"module": "chaosk8s.pod.probes",
"func": "should_be_found_in_logs",
"arguments": {
"pattern": "startup",
"all_containers": false
}
}
},
"provider": {
"type": "python",
"module": "chaosk8s.pod.probes",
"func": "read_pod_logs",
"arguments": {
"last": "60s",
"label_selector": "app=producer",
"container_name": "producer"
}
}
}
]
},
"method": []
}
```

## [0.38.2][] - 2024-04-18

Expand Down
42 changes: 42 additions & 0 deletions chaosk8s/pod/probes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
from datetime import datetime
from typing import Dict, List, Union

Expand All @@ -17,6 +18,7 @@
"count_pods",
"pod_is_not_available",
"count_min_pods",
"should_be_found_in_logs",
]
logger = logging.getLogger("chaostoolkit")

Expand Down Expand Up @@ -370,3 +372,43 @@ def count_min_pods(
label_selector=label_selector, phase=phase, ns=ns, secrets=secrets
)
return count >= min_count


def should_be_found_in_logs(
pattern: str,
all_containers: bool = True,
value: Dict[str, str] = None,
secrets: Secrets = None,
) -> bool:
"""
Lookup for the first occurence of `pattern` in the logs of each container
fetched by the `read_pod_logs` probe.
If `all_containers` is set the match must occur on all continers. Otherwise,
allow for only a subset of containers to match the search.
"""
if not value:
raise ActivityFailed("no logs to search from")

c_pattern = re.compile(pattern)

matched: list[re.Match] = []

for container_name in value:
logs = value[container_name]
m = c_pattern.search(logs)
if m:
logger.debug(
f"Container '{container_name}' matched at position {m.span()}"
)
matched.append(m)
continue

logger.debug(f"Container '{container_name}' did not match")

if all_containers and (len(matched) != len(value)):
return False
elif not matched:
return False

return True

0 comments on commit aa8a0ca

Please sign in to comment.