循环通过 --set-file helm 选项传递的纯文本文件行,然后逐列解析每一行

Abd*_*UMI 1 kubernetes kubernetes-helm sprig-template-functions

我有一个 cron 文件,我正在尝试通过--set-file选项传递它。我想遍历 cron 文件行,并为每一行创建新的 CronJob 类型的 Kubernetes 对象。

我是这样用的 helm instal ... --set-file crons.file=mycron

其中 mycron 文件看起来像一个典型的 cron 文件:

0,10,20,30,40,50 * * * * /usr/bin/cmd1 opta optb
35 2-23/3 * * * /usr/bin/cmd2
Run Code Online (Sandbox Code Playgroud)

我无法遍历这个简单的纯文本行:

{{- range $indx, $line := .Values.crons.file }}
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: {{ regexFind "[^/]+$" "$line"}}
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: cron-{{ $indx }}
            image: busybox
            args: 
            - /bin/sh
            - -c
            - {{ regexFind "[^/]+$" "$line"}}
          restartPolicy: OnFailure
{{- end  }}
Run Code Online (Sandbox Code Playgroud)

是否有类似的函数fromYaml可以通过range函数迭代纯文本文件?

Dav*_*aze 5

该小枝支持库包括功能分割和连接字符串成列表,并在一般操作列表。如果splitList文件在换行符上,你会得到一个行列表。您可以再次splitList使用空格上的每一行来从各个 cron 行中获取单独的时间和命令部分。

{{/* Iterate over individual lines in the string */}}
{{- range $line := splitList "\n" .Values.crons.file -}}

{{/* Break the line into words */}}
{{- $words := splitList " " $line -}}

{{/* Reconstitute the schedule and command parts from the words */}}
{{- $time := slice $words 0 5 | join " " -}}
{{- $command := slice $words 5 -}}

---
schedule: {{ $time }}
command: {{- $command | toYaml | nindent 2}}
{{ end -}}
Run Code Online (Sandbox Code Playgroud)