簡體   English   中英

如何將 kubernetes 命名空間動態傳遞給 k8s cronjob

[英]How to pass kubernetes namespaces dynamically to k8s cronjob

我有一個 cronjob 可以清理一些工作,因為我的 kubernetes 是舊版本,所以不能使用ttlafterfinished 如何獲取部署了此作業的命名空間並動態傳遞命名空間名稱,而不是多次重復相同的命令?

這是我的 cronjob:

kind: CronJob
metadata:
  name: jobs-cleanup
spec:
  schedule: "*/30 * * * *"
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 1
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: cleanup-operator
          containers:
          - name: jobs-cleanup
            image: google.io/test/job:1.0
            args:
            - -c
            - |
              for i in $(seq 9);do 
              kubectl get jobs --sort-by=.metadata.creationTimestamp -n dev$i | grep "job-init" | cut -d' ' -f1 | tac | tail -n +2 | xargs -I % kubectl delete jobs % -n dev$i;done
              kubectl get jobs --sort-by=.metadata.creationTimestamp -n stg1 | grep "fusionauth-job" | cut -d' ' -f1 | tac | tail -n +2 | xargs -I % kubectl delete jobs % -n stg1
              kubectl get jobs --sort-by=.metadata.creationTimestamp -n pt1 | grep "fusionauth-job" | cut -d' ' -f1 | tac | tail -n +2 | xargs -I % kubectl delete jobs % -n pt1
              kubectl get jobs --sort-by=.metadata.creationTimestamp -n sit1 | grep "fusionauth-job" | cut -d' ' -f1 | tac | tail -n +2 | xargs -I % kubectl delete jobs % -n sit1
            command:
            - /bin/sh
          restartPolicy: Never
          imagePullSecrets:
            - name: secret

您現在正在做的更靈活的替代方法是將要刪除的作業名稱列表存儲在列表/數組中。 然后,您可以遍歷作業名稱列表並執行命令。

下面是您的命令的更簡單 (IMO) 版本,它在kubectl中使用-o=jsonpath支持來指定搜索條件。

# The list of jobs you want to delete from any/all namespaces.
jobs_list="job-init fusionauth-job"

for job_name in ${jobs_list}; do
    kubectl get jobs -A --sort-by=.metadata.creationTimestamp \
        -o=jsonpath="{range .items[?(@.metadata.name == '${job_name}')]}{.metadata.namespace} {.metadata.name}{'\n'}{end}" \
          | while read namespace job;do kubectl delete job ${job} -n ${namespace};done;
done

由於下面的評論/對話,添加了以下內容。

如果集群中的作業名稱具有遞增的后綴,例如fusionauth-job-01fusionauth-job-02等,並且您只想在特定命名空間中保留該作業的最新實例,那么您將需要利用像jq這樣你就可以做一些正則表達式匹配。 kubectljsonpath特性不支持正則表達式。

例如,假設您的集群具有如下所示的作業。

NAMESPACE   NAME
default     fusionauth-job-01
team1       fusionauth-job-01
team1       fusionauth-job-02
team1       job-init-01
team1       job-init-02 
team2       fusionauth-job-01
team2       fusionauth-job-02
team2       fusionauth-job-03

此外,作業按時間戳順序列出,這意味着fusionauth-job-03是命名空間team2中最新的作業(其名稱與fusionauth-job*匹配), job-init-02是最新的作業(其名稱與job-init*匹配) 在命名空間team1中, fusionauth-job-02是命名空間team1中的最新作業(該名稱模式),而fusionauth-job-01是在命名空間default中運行的唯一作業。

運行您的jobs-cleanup作業后,預期會留下以下作業:

NAMESPACE   NAME
default     fusionauth-job-01
team1       fusionauth-job-02
team1       job-init-02 
team2       fusionauth-job-03

生成這些結果的腳本如下所示:

# The list of job name prefixes you want to delete from any/all namespaces.
jobs_list="job-init fusionauth-job"

for job_name in ${jobs_list}; do
    ns_observed=""
    echo "Searching for ${job_name}*"
    kubectl get jobs -A --sort-by=.metadata.creationTimestamp -o json \
        | jq -r '.items[] | select(.metadata.name | test('\"${job_name}\"')) | .metadata.namespace + " " + .metadata.name' \
        | tac \
        | while read namespace job;do
            if test "${ns_observed#*$namespace}" != "$ns_observed";then
                # If we've seen this namespace already for this job, then delete the
                # job, since the one we found previously is the newest.
                kubectl delete job ${job} -n ${namespace};
            else
                # Otherwise, if this is the first time observing this namespace, then
                # this is the newest job that starts with this job name in this namespace.
                ns_observed="$ns_observed $namespace";
            fi;
        done;
done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM