<p>I am trying to write a CronJob for executing a shell script within a ConfigMap for Kafka.</p>

<p>My intention is to reassign partitions at specific intervals of time.</p>

<p>However, I am facing issues with it. I am very new to it. Any help would be appreciated.</p>

<p>cron-job.yaml</p>

<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: partition-cron
spec:
  schedule: "*/10 * * * *"
  startingDeadlineSeconds: 20
  successfulJobsHistoryLimit: 5
  jobTemplate:
    spec:
      completions: 2
      template:
        spec:
          containers:
          - name: partition-reassignment
            image: busybox
            command: ["/configmap/runtimeConfig.sh"]
            volumeMounts:
            - name: configmap
              mountPath: /configmap
          restartPolicy: Never
          volumes:
            - name: configmap
              configMap:
                name: configmap-config
</code></pre>

<p>configmap-config.yaml</p>

<pre><code>{{- if .Values.topics -}}
{{- $zk := include "zookeeper.url" . -}}
apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: {{ template "kafka.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: "{{ .Release.Service }}"
    release: "{{ .Release.Name }}"
  name: {{ template "kafka.fullname" . }}-config
data:
  runtimeConfig.sh: |
    #!/bin/bash
    set -e
    cd /usr/bin
    until kafka-configs --zookeeper {{ $zk }} --entity-type topics --describe || (( count++ &gt;= 6 ))
    do
      echo "Waiting for Zookeeper..."
      sleep 20
    done
    until nc -z {{ template "kafka.fullname" . }} 9092 || (( retries++ &gt;= 6 ))
    do
      echo "Waiting for Kafka..."
      sleep 20
    done
    echo "Applying runtime configuration using {{ .Values.image }}:{{ .Values.imageTag }}"
  {{- range $n, $topic := .Values.topics }}
    {{- if and $topic.partitions $topic.replicationFactor $topic.reassignPartitions }}
    cat &lt;&lt; EOF &gt; {{ $topic.name }}-increase-replication-factor.json
  {"version":1, "partitions":[
    {{- $partitions := (int $topic.partitions) }}
    {{- $replicas := (int $topic.replicationFactor) }}
    {{- range $i := until $partitions }}
      {"topic":"{{ $topic.name }}","partition":{{ $i }},"replicas":[{{- range $j := until $replicas }}{{ $j }}{{- if ne $j (sub $replicas 1) }},{{- end }}{{- end }}]}{{- if ne $i (sub $partitions 1) }},{{- end }}
    {{- end }}
  ]}
EOF
kafka-reassign-partitions --zookeeper {{ $zk }} --reassignment-json-file {{ $topic.name }}-increase-replication-factor.json --execute
kafka-reassign-partitions --zookeeper {{ $zk }} --reassignment-json-file {{ $topic.name }}-increase-replication-factor.json --verify
  {{- end }}
{{- end -}}
</code></pre>

<p>My intention is to run the runtimeConfig.sh script as a cron job at regular intervals for partition reassignment in Kafka.</p>

<p>I am not sure if my approach is correct.</p>

<p>Also, I have randomly put <strong>image: busybox</strong> in the cron-job.yaml file. I am not sure about what should I be putting in there.</p>

<p>Information Part</p>

<pre><code>$ kubectl get cronjobs
NAME             SCHEDULE       SUSPEND   ACTIVE    LAST SCHEDULE   AGE
partition-cron   */10 * * * *   False     1         5m              12m
$ kubectl get pods
NAME                                               READY     STATUS              RESTARTS   AGE
elegant-hedgehog-metrics-server-58995fcf8b-2vzg6   1/1       Running             0          5d
my-kafka-0                                         1/1       Running             1          12m
my-kafka-1                                         1/1       Running             0          10m
my-kafka-2                                         1/1       Running             0          9m
my-kafka-config-644f815a-pbpl8                     0/1       Completed           0          12m
my-kafka-zookeeper-0                               1/1       Running             0          12m
partition-cron-1548672000-w728w                    0/1       ContainerCreating   0          5m
$ kubectl logs partition-cron-1548672000-w728w
Error from server (BadRequest): container "partition-reassignment" in pod "partition-cron-1548672000-w728w" is waiting to start: ContainerCreating
</code></pre>

<p>Modified Cron Job YAML</p>

<pre><code>apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: partition-cron
spec:
  schedule: "*/5 * * * *"
  startingDeadlineSeconds: 20
  successfulJobsHistoryLimit: 5
  jobTemplate:
    spec:
      completions: 1
      template:
        spec:
          containers:
          - name: partition-reassignment
            image: busybox
            command: ["/configmap/runtimeConfig.sh"]
            volumeMounts:
              - name: configmap
                mountPath: /configmap
          restartPolicy: Never
          volumes:
            - name: configmap
              configMap:
                name: {{ template "kafka.fullname" . }}-config
</code></pre>

<p>Now, I am getting Status of Cron Job pods as <strong>ContainerCannotRun</strong>.</p>

<p>You've set the ConfigMap to <code>name: {{ template "kafka.fullname" . }}-config</code> but in the job you are mounting <code>configmap-config</code>. Unless you installed the Helm chart using <code>configmap</code> as the name of the release, that Job will never start. </p>

<p>One way to fix it would be to define the volume as:</p>

<pre><code>          volumes:
            - name: configmap
              configMap:
                name: {{ template "kafka.fullname" . }}-config

</code></pre>
