如何按名称空间删除超过10天的Helm版本

时间:2020-08-24 03:07:25

标签: kubernetes-helm

我正在尝试删除10天以上的发行版,但不应触摸某些名称空间(例如:监视) 在helm2中,我使用awk做到了这一点,但是在helm3中,他们更改了日期类型,因此无法正常工作。 有什么办法吗?

1 个答案:

答案 0 :(得分:0)

让我告诉您如何解决类似问题。在我们的流程中,我们为每个功能分支自动推出了头盔发布,并且我们决定实施自动清除过程,以删除开发流程中的旧功能发布。

当前的实现需要jq作为依赖项。

#!/usr/bin/env bash
set -e

echo "Staring delete-old-helm-release.sh ..."

helm_release_name=${1:-$HELM_RELEASE_NAME}
k8s_namespace=${2:-$KUBERNETES_NAMESPACE}

# Get helm release date, take updated field and remove UTC from string
helm_release_updated=$(helm list --filter "${helm_release_name}" -n "${k8s_namespace}" -o json \
  | jq --raw-output ".[0].updated" \
  | sed s/"UTC"// \
)

if [[ "$helm_release_name" == null ]]; then
  echo "Helm release: ${helm_release_name} in namespace: ${k8s_namespace} not found"
  echo "Exit from delete-old-helm-release.sh ..."
  exit 1
fi

# Convert date string to timestamp, get current timestamp and calculate time delta
helm_release_date_timestamp=$(date --utc --date="${helm_release_updated}" +"+%s")
current_date_timestamp=$(date --utc +"+%s")
time_difference=$((current_date_timestamp - helm_release_date_timestamp))

# 86400 means 24 hours (60*60*24) in seconds
if [[ (( $time_difference -gt 86400 )) ]]; then
  echo "Detected old release: ${helm_release_name} in namespace: ${k8s_namespace}"
  echo "Difference is more than 24hr: $((time_difference/60/60))hr"
  echo "Deliting it ..."
  helm delete "${helm_release_name}" -n "${k8s_namespace}" --purge
  echo "Done"
else
  echo "Detected fresh release"
  echo "Current time difference is less than 24hr: $((time_difference/60/60))hr"
  echo "Skipping ..."
fi

exit 0

它已经在头盔3.2.4中进行了测试,我认为它应该与所有头盔3.x.x一起使用,直到它们更改日期格式为止。

顺便说一句,请更新您的问题描述,这样它会更加清楚并在搜索引擎中具有更高的优先级:)

请让我知道是否有帮助, 祝好运, 奥列格(Oleg)