Here is an example test.txt file that I am currently using:
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
app-education-content-creator-1651375038 ci 1 2022-05-01 03:28:36.824230864 0000 UTC deployed prodigy-application-0.3.11 1.0.0
app-education-content-creator-preview-2255856586 ci 1 2022-05-02 03:51:24.017628998 0000 UTC deployed prodigy-application-0.3.11 1.0.0
app-education-content-creator-preview-2255904996 ci 1 2022-05-02 03:58:50.959635169 0000 UTC deployed prodigy-application-0.3.11 1.0.0
ci-external-secrets ci 2 2021-09-13 19:58:32.975711779 0000 UTC deployed kubernetes-external-secrets-3.1.0 3.1.0
ci-flux ci 7 2022-01-04 18:37:49.503585809 0000 UTC deployed flux-1.11.2 1.24.1
ci-helm-operator ci 5 2021-08-24 04:12:49.836776337 0000 UTC deployed helm-operator-1.2.0 1.2.0
cortex-preview-850-game-cortex-server ci 1 2022-05-04 12:51:35.725983628 0000 UTC deployed prodigy-application-0.3.11 1.0.0
cortex-preview-853-game-cortex-server ci 1 2022-05-18 19:22:11.062215547 0000 UTC deployed prodigy-application-0.4.14 1.0.0
istio-gateways ci 4 2022-04-27 20:01:21.569001421 0000 UTC deployed istio-gateways-0.1.0
launcher-preview-90-game-launcher ci 1 2022-05-18 18:34:27.09314326 0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-415-game-launcher ci 1 2022-05-17 15:42:23.055718169 0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-415-game-loader ci 1 2022-05-17 15:42:16.490564899 0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-415-passthrough ci 1 2022-05-17 15:42:20.406590056 0000 UTC deployed istio-passthrough-0.1.4 1.8.3
loader-preview-454-game-launcher ci 1 2022-04-29 13:56:41.144878095 0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-454-game-loader ci 1 2022-04-29 13:56:35.559380733 0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-454-passthrough ci 1 2022-04-29 13:56:38.359154911 0000 UTC deployed istio-passthrough-0.1.4 1.8.3
loader-preview-458-game-launcher ci 1 2022-05-17 22:14:26.959236354 0000 UTC deployed prodigy-application-0.3.11 1.0.0
loader-preview-458-game-loader ci 1 2022-05-17 22:14:20.956443867 0000 UTC deployed prodigy-application-0.4.14 1.0.0
loader-preview-458-passthrough ci 1 2022-05-17 22:14:24.043742858 0000 UTC deployed istio-passthrough-0.1.4 1.8.3
math-preview-5305-feature-flag-api ci 1 2022-04-25 21:36:18.331495893 0000 UTC deployed prodigy-application-0.3.11 1.0.0
math-preview-5305-feature-flag-client ci 1 2022-04-25 21:36:20.665011277 0000 UTC deployed prodigy-application-0.3.11 1.0.0
math-preview-5305-flags-redis ci 1 2022-04-25 21:35:45.851677929 0000 UTC deployed redis-15.4.2 6.2.5
math-preview-5305-game-client ci 1 2022-04-25 21:36:28.290028875 0000 UTC deployed prodigy-application-0.3.11 1.0.0
math-preview-5305-game-cortex-server ci 1 2022-04-
Currently this line of code outputs all of the helm release NAME's that are older than X amount of days (in the test case below X is 3):
getOldPreviews() {
secondsUntilStale=$1
helmReleaseList=$2
echo " "
echo "$helmReleaseList" |
awk 'f;/NAME/{f=1}' |
awk -v today="$today" -v expiryTime="$secondsUntilStale" '{
getPreviewDateEpoch = "gdate -d \""$4" "$5"\" %s"
getPreviewDateEpoch | getline previewCreationDate
if(today - previewCreationDate > expiryTime){
print $1
}
}'
}
Test case when X is 3:
Namespace is: ci
Days until stale: 3
Helm list file path: test.txt
Filtered previews older than 3 days
app-education-content-creator-1651375038
app-education-content-creator-preview-2255856586
app-education-content-creator-preview-2255904996
ci-external-secrets
ci-flux
ci-helm-operator
cortex-preview-850-game-cortex-server
istio-gateways
loader-preview-454-game-launcher
loader-preview-454-game-loader
loader-preview-454-passthrough
math-preview-5305-feature-flag-api
math-preview-5305-feature-flag-client
math-preview-5305-flags-redis
math-preview-5305-game-client
math-preview-5305-game-cortex-server
How do I make it so that I filter for the X amount of days AND certain releases such anything with "preview-", "-preview-" ?
CodePudding user response:
You can do something like this to filter the lines
awk -v pattern="preview" -v today="$today" -v expiryTime="$secondsUntilStale" '
$1 ~ pattern {
... this is exactly the same as your code ...
}
'
string ~ pattern
is awk's explicit regex-matching operator.
This will also improve performance as you need to call out to date
fewer times.
This part is making some assumptions:
If you have GNU awk or mawk, there are builtin time functions, so you don't need to call out to date
gawk -v pattern="preview" -v X=3 '
# convert "YYYY-mm-dd", "HH:MM:SS.sssss" to an epoch value
function timestamp (date, time) {
gsub(/-/, " ", date)
sub(/\.[0-9 ]$/, "", time)
gsub(/:/, " ", time)
return mktime(date " " time)
}
BEGIN {
# "X" days ago
expiryTime = systime() - X * 86400
}
# skipping the header line, for lines not matching the pattern
# and where the timestamp is older than X days ago, print the Name
NR > 1 && $1 !~ pattern && timestamp($4, $5) < expiryTime {print $1}
' releaseListFile
which, as of the current time, outputs:
app-education-content-creator-1651375038
ci-external-secrets
ci-flux
ci-helm-operator
istio-gateways