這是我當前正在使用的示例 test.txt 檔案:
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-
目前,這行代碼輸出了所有早于 X 天的 helm release NAME(在下面的測驗用例中 X 為 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
}
}'
}
X 為 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
我該如何做到這一點,以便我過濾 X 天數和某些帶有 "preview-"、"-preview-" 的版本?
uj5u.com熱心網友回復:
你可以做這樣的事情來過濾行
awk -v pattern="preview" -v today="$today" -v expiryTime="$secondsUntilStale" '
$1 ~ pattern {
... this is exactly the same as your code ...
}
'
string ~ pattern是 awk 的顯式正則運算式匹配運算子。
這也將提高性能,因為您需要呼叫date的次數更少。
這部分做了一些假設:
如果你有 GNU awk 或 mawk,有內置的時間函式,所以你不需要呼叫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
其中,截至當前時間,輸出:
app-education-content-creator-1651375038
ci-external-secrets
ci-flux
ci-helm-operator
istio-gateways
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/479322.html
