Skip to content

Commit

Permalink
Add youngerThan filter (#33)
Browse files Browse the repository at this point in the history
Added support for filtering by creation timestamp that is `youngerThan`
a specified amount of time.

```yaml
source:
  filter:
    youngerThan: 3600
```

This inversely complements the existing filter functionality for
`olderThan`, and can also be used together to define a time window.

For example, resources created between 1 and 2 hours ago:
```yaml
source:
  filter:
    olderThan: 3600
    youngerThan: 7200
```

Signed-off-by: Justin Griffin <[email protected]>
  • Loading branch information
Justin Griffin authored Apr 13, 2022
1 parent 8ebb3d9 commit ca2c610
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ source:
olderThan: 86400
----

** `youngerThan`: Time in seconds that the `metadata.creationTimestamp` must be younger than (ex: `3600` for 1hr).
+
[source,yaml]
----
source:
filter:
youngerThan: 3600
----

** `phases`: List of `status.phase` value(s) the resource must match at least one of. This varies depending on the resource.
For example, a https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase[pod's status] can be one of `Pending`, `Running`, `Succeeded`, `Failed` or `Unknown`.
To retrieve only `Failed` or `Unknown` pods:
Expand Down
12 changes: 12 additions & 0 deletions assets/query
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ queryAndFilter() {
queryForVersions
filterByName
filterByCreationOlderThan
filterByCreationYoungerThan
filterByPhases
filterByJQExpressions
}
Expand Down Expand Up @@ -44,6 +45,17 @@ filterByCreationOlderThan() {
fi
}

filterByCreationYoungerThan() {
local filter_younger_than=$(jq -r '.source.filter.youngerThan // ""' < $payload)
if [ ! -z "$filter_younger_than" ]; then
log -p "--> filtering by creation timestamp younger than: ${cyan}$filter_younger_than${reset}"
new_versions=$(echo $new_versions | tr '\r\n' ' ' | jq --argjson MIN_AGE $(( $(date +%s) - $filter_younger_than)) -r '[.[] | select(.metadata.creationTimestamp | fromdate | tonumber | select(. > $MIN_AGE)) ]')
log "$new_versions"
else
log "--> creation timestamp (younger than) filter not configured, skipping...."
fi
}

filterByPhases() {
local filter_phases=$(jq -r '.source.filter.phases // ""' < $payload)
if [ ! -z "$filter_phases" ]; then
Expand Down
104 changes: 104 additions & 0 deletions test/check.bats
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,110 @@ teardown() {
assert_equal "$(jq -r '.[2].metadata.name' <<< "$new_versions")" 'namespace-3'
}

@test "[check] filter by youngerThan" {
source_check "stdin-source-filter-youngerThan"

now="$(date +%Y-%m-%dT%H:%M:%SZ)"
hourAgo="$(date -d '1 hour ago' +%Y-%m-%dT%H:%M:%SZ)"
dayAgo="$(date -d '25 hours ago' +%Y-%m-%dT%H:%M:%SZ)"

new_versions="[
{
\"metadata\": {
\"name\": \"namespace-1\",
\"creationTimestamp\": \"$now\"
}
},
{
\"metadata\": {
\"name\": \"namespace-2\",
\"creationTimestamp\": \"$hourAgo\"
}
},
{
\"metadata\": {
\"name\": \"namespace-3\",
\"creationTimestamp\": \"$dayAgo\"
}
}
]"

filterByCreationYoungerThan

# then we only have namespaces younger than our criteria (1 hour)
assert_equal $(jq length <<< "$new_versions") 1
assert_equal "$(jq -r '.[0].metadata.name' <<< "$new_versions")" 'namespace-1'
}

@test "[check] filter by youngerThan not configured" {
source_check "stdin-source-empty"

new_versions="[
{
\"metadata\": {
\"name\": \"namespace-1\",
\"creationTimestamp\": \"$now\"
}
},
{
\"metadata\": {
\"name\": \"namespace-2\",
\"creationTimestamp\": \"$hourAgo\"
}
},
{
\"metadata\": {
\"name\": \"namespace-3\",
\"creationTimestamp\": \"$dayAgo\"
}
}
]"

filterByCreationYoungerThan

# then our 'new_versions' is left unchanged
assert_equal $(jq length <<< "$new_versions") 3
assert_equal "$(jq -r '.[0].metadata.name' <<< "$new_versions")" 'namespace-1'
assert_equal "$(jq -r '.[1].metadata.name' <<< "$new_versions")" 'namespace-2'
assert_equal "$(jq -r '.[2].metadata.name' <<< "$new_versions")" 'namespace-3'
}

@test "[check] filter by youngerThan and olderThan (together)" {
source_check "stdin-source-filter-youngerThan-olderThan"

now="$(date +%Y-%m-%dT%H:%M:%SZ)"
hourAgo="$(date -d '1 hour ago' +%Y-%m-%dT%H:%M:%SZ)"
dayAgo="$(date -d '25 hours ago' +%Y-%m-%dT%H:%M:%SZ)"

new_versions="[
{
\"metadata\": {
\"name\": \"namespace-1\",
\"creationTimestamp\": \"$now\"
}
},
{
\"metadata\": {
\"name\": \"namespace-2\",
\"creationTimestamp\": \"$hourAgo\"
}
},
{
\"metadata\": {
\"name\": \"namespace-3\",
\"creationTimestamp\": \"$dayAgo\"
}
}
]"

filterByCreationOlderThan
filterByCreationYoungerThan

# then we only have namespaces that fit between our age range
assert_equal $(jq length <<< "$new_versions") 1
assert_equal "$(jq -r '.[0].metadata.name' <<< "$new_versions")" 'namespace-2'
}

@test "[check] pare down version info and emit only uid/resourceVersion" {
source_check

Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/stdin-source-filter-youngerThan-olderThan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"source": {
"filter": {
"olderThan": 3000,
"youngerThan": 80000
}
}
}
7 changes: 7 additions & 0 deletions test/fixtures/stdin-source-filter-youngerThan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"source": {
"filter": {
"youngerThan": 3600
}
}
}

0 comments on commit ca2c610

Please sign in to comment.