Helm 范围 yaml 模板 kafka 主题

Helm range yaml template kafka topics

我是 helm 的新手,我正在尝试使用范围函数为 kafka 生成不同的主题,以免每个主题都有一个 yaml 文件:

我有不同的主题(topic1,topic2,topic3,...),它们唯一的区别是主题和名称的毫秒保留,一些主题有 3600000,其他主题有 540000,这是我的值文件:

KafkaTopics:
  shortRetentionTopics:
    name:
    - topic1
    - topic2
    - topic3
    - topic4
    spec:
      config: 
        retention.ms: 540000
      topicName:
      - topic1logs
      - topic2logs
      - topic3logs
      - topic4logs
  longRetentionTopics:
    name:
    - topic34
    - topic35
    spec:
      config: 
        retention.ms: 3600000
      topicName:
      - topic34logs
      - topic34logs

我想在此模板上设置名称、topicName 和 retention.ms,从值文件执行 for 循环:

apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
  labels:
    strimzi.io/cluster: kafka
  name: (here the name of the topic)
  namespace: default
spec:
  config:
    retention.ms: (here the retention of the topic)
  partitions: 12
  replicas: 1
  topicName: (here the topicName of the topic)

或者,如果您对更改值文件的结构有任何建议,以便更容易地将值解析为模板,我也很感兴趣。

这是一个例子

$ cat values.yaml
KafkaTopics:
  shortRetentionTopics:
    name:
    - topic1
    - topic2
    - topic3
    - topic4
    spec:
      config:
        retention.ms: 540000
  longRetentionTopics:
    name:
    - topic34
    - topic35
    spec:
      config:
        retention.ms: 3600000
$ cat templates/topics.yml
{{- with .Values.KafkaTopics.shortRetentionTopics }}{{- range .name }}
---
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
  labels:
    strimzi.io/cluster: kafka
  name: {{ $.Release.Name }}-{{ . }}
  namespace: default
spec:
{{- toYaml $.Values.KafkaTopics.shortRetentionTopics.spec | nindent 2 }}
  partitions: 12
  replicas: 1
  topicName: {{.}}logs
{{- end}}{{- end}}

重复长期保留主题,或使用单独的模板文件。

示例调试输出 - helm template topics ./topic-example --debug

---
# Source: topic-example/templates/topics.yml
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
  labels:
    strimzi.io/cluster: kafka
  name: topics-topic1
  namespace: default
spec:
  config:
    retention.ms: 540000
  partitions: 12
  replicas: 1
  topicName: topic1logs
---
# Source: topic-example/templates/topics.yml
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
  labels:
    strimzi.io/cluster: kafka
  name: topics-topic2
  namespace: default
spec:
  config:
    retention.ms: 540000
  partitions: 12
  replicas: 1
  topicName: topic2logs
---
# Source: topic-example/templates/topics.yml
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
  labels:
    strimzi.io/cluster: kafka
  name: topics-topic3
  namespace: default
spec:
  config:
    retention.ms: 540000
  partitions: 12
  replicas: 1
  topicName: topic3logs
---
# Source: topic-example/templates/topics.yml
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
  labels:
    strimzi.io/cluster: kafka
  name: topics-topic4
  namespace: default
spec:
  config:
    retention.ms: 540000
  partitions: 12
  replicas: 1
  topicName: topic4logs

最后我这样做了:

{{- range $topics := .Values.kafkaTopicList }}
{{ $spec := default dict $topics.spec }}
{{ $config := default dict $spec.config }}
{{ $retention := default dict $config.retentionMs }}
---
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
  labels:
    strimzi.io/cluster: kafka
  name: {{ $topics.name }}
  namespace: default
spec:
  config:
    retention.ms: {{ $retention | default "540000" }}
  partitions: 12
  replicas: 1
  topicName: {{ $topics.name | replace "-" "." }}
{{- end}}

值文件:

kafkaTopicList:
  topic1:
    name: events-1
  topic2:
    name: events-2
  topic3:
    name: events-3
  topic4:
    name: events-4
  topic5:
    name: events-5
  topic6:
    name: events-6
  topic7:
    name: events-7
    spec:
      config: 
        retentionMs: 3600000