apiVersion: batch/v1
kind: Job
metadata:
  name: reassign-partitions
  namespace: kafka
spec:
  template:
    metadata:
      name: reassign-partitions
    spec:
      containers:
      - name: kafka
        image: solsson/kafka:2.4.1@sha256:c6205d213c0ec3c37a8294cca48273860d5a42e42620530eaf7217d31e3205c6
        env:
        - name: ZOOKEEPER
          value: zookeeper.kafka:2181
        # the following must be edited per job
        - name: TOPICS
          value: test-produce-consume,test-kafkacat
        - name: BROKERS
          value: 0,2
        command:
        - /bin/bash
        - -ce
        - >
          echo '{"topics":[' > /tmp/reassign-topics.json;
          echo -n ' {"topic":"' >> /tmp/reassign-topics.json;
          echo -n $TOPICS | sed 's/,/"},\n {"topic":"/g' >> /tmp/reassign-topics.json;
          echo '"}' >> /tmp/reassign-topics.json;
          echo ']}' >> /tmp/reassign-topics.json;

          echo "# reassign-topics.json";
          cat /tmp/reassign-topics.json;

          ./bin/kafka-reassign-partitions.sh 
          --zookeeper=$ZOOKEEPER
          --generate
          --topics-to-move-json-file=/tmp/reassign-topics.json
          --broker-list=$BROKERS > /tmp/generated.txt;

          tail -n 1 /tmp/generated.txt > /tmp/proposed-reassignment.json;

          echo "# proposed-reassignment.json";
          cat /tmp/proposed-reassignment.json;

          ./bin/kafka-reassign-partitions.sh
          --zookeeper=$ZOOKEEPER
          --execute
          --reassignment-json-file=/tmp/proposed-reassignment.json;
      restartPolicy: Never
  backoffLimit: 3
