# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: batch/v1
kind: Job
metadata:
  name: redis-cluster-start
  namespace: ${namespace}
spec:
  suspend: false
  completions: 1
  template:
    spec:
      restartPolicy: Never
      volumes:
      - name: shared-data
        emptyDir: {}

      initContainers:
        # we resolve node names in an init container using alpine
        # because the redis image doesn't include nslookup
      - name: resolve-node-names
        image: alpine
        volumeMounts:
        - name: shared-data
          mountPath: /tmp/shared-data
        command:
        - /bin/sh
        - -c
        - |
          %{~ for n in nodes ~}
          echo "$(nslookup ${n} | awk '/^Address: / { print $2 }'):6379" >> /tmp/shared-data/nodes
          %{~ endfor ~}

      containers:
      - name: redis-cluster-start
        image: ${image}
        volumeMounts:
        - name: shared-data
          mountPath: /tmp/shared-data
        command:
        - /bin/sh
        - -c
        - |
          redis-cli --cluster-yes --cluster-replicas 1 --cluster create $(cat /tmp/shared-data/nodes)
