【问题标题】:loop thru lines of plain text file passed by --set-file helm option then parse each line by column循环通过 --set-file helm 选项传递的纯文本文件行,然后逐列解析每一行
【发布时间】:2020-04-10 14:36:36
【问题描述】:

我有一个 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

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

{{- 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  }}

有没有像fromYaml 这样的函数,它使纯文本文件可以被range 函数迭代?

【问题讨论】:

    标签: kubernetes kubernetes-helm sprig-template-functions


    【解决方案1】:

    Sprig 支持库包括splitting and joining strings into listsmanipulating lists in general 的函数。如果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 -}}
    

    【讨论】:

    • 我得到:在 :错误调用切片:reflect.Value.Slice:切片索引超出范围
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多