apiVersion: v1
kind: Pod
metadata:
  name: "{{ .Release.Name }}-canary"
  annotations:
    "helm.sh/hook": test-success
    "helm.sh/hook-delete-policy": "before-hook-creation,hook-succeeded"
spec:
  containers:
  - name: {{ .Release.Name }}-canary
    image: "{{ .Values.image }}:{{ .Values.imageTag }}"
    imagePullPolicy: "{{ .Values.imagePullPolicy }}"
    command:
    - sh
    - -c
    - |
      # Delete the topic if it exists
      kafka-topics --zookeeper {{ template "cp-kafka.cp-zookeeper.service-name" . }} --topic {{ template "cp-kafka.fullname" . }}-canary-topic --delete --if-exists
      # Create the topic
      kafka-topics --zookeeper {{ template "cp-kafka.cp-zookeeper.service-name" . }} --topic {{ template "cp-kafka.fullname" . }}-canary-topic --create --partitions 1 --replication-factor 1 --if-not-exists && \
      # Create a message
      MESSAGE="`date -u`" && \
      # Produce a test message to the topic
      echo "$MESSAGE" | kafka-console-producer --broker-list {{ template "cp-kafka.fullname" . }}:9092 --topic {{ template "cp-kafka.fullname" . }}-canary-topic && \
      # Consume a test message from the topic
      kafka-console-consumer --bootstrap-server {{ template "cp-kafka.fullname" . }}-headless:9092 --topic {{ template "cp-kafka.fullname" . }}-canary-topic --from-beginning --timeout-ms 2000 | grep "$MESSAGE"
  restartPolicy: Never