Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add youngerThan filter #33

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}