Use init job to create All-Projects and All-Users before startup

So far the Gerrit replica deployment did not initialize the All-Projects
and All-Users repositories and instead waited for them to be replicated
from the primary Gerrit. For development setups there was a test mode
that would perform the initialization of those repositories and allow
startup without requiring replication to be configured on the primary
instance.

Using the test mode however did not work well while using multiple
pod replicas, since the init would try to perform a schema upgrade, which
failed and caused the init to stop, which in turn did leave the
group index not ready.

Instead of adding checks and solutions for these issues during the init
of the pod, which would only increase startup times, the test mode was
removed. Instead a job was added, which is run, when the chart is
installed for the first time and will run a Gerrit init once to create
an empty All-Projects and All-Users repository. The Gerrit replica pods
will in the meantime wait for these repositories to appear in the
shared volume and only start up, when the job finished successfully.

Since replication uses force push, having those repositories already
created, but in a vanilla state, should not pose an issue. Thus, the
job is run also, when replication is intended to be used.

Change-Id: I7ee6a1eeb99eebd8b320eaa38ef2e4579812fe97
diff --git a/helm-charts/gerrit-replica/README.md b/helm-charts/gerrit-replica/README.md
index dbc13b2..32a2e13 100644
--- a/helm-charts/gerrit-replica/README.md
+++ b/helm-charts/gerrit-replica/README.md
@@ -221,7 +221,6 @@
 | `gerritReplica.replicas`                      | Number of pod replicas to deploy                                                                         | `1`                               |
 | `gerritReplica.maxSurge`                      | Max. percentage or number of pods allowed to be scheduled above the desired number                       | `25%`                             |
 | `gerritReplica.maxUnavailable`                | Max. percentage or number of pods allowed to be unavailable at a time                                    | `100%`                            |
-| `gerritReplica.initializeTestSite.enabled`    | Enable the initialization of a site. USE ONLY for testing, if you do not plan to replicate repositories. | `true`                            |
 | `gerritReplica.resources`                     | Configure the amount of resources the pod requests/is allowed                                            | `requests.cpu: 1`                 |
 |                                               |                                                                                                          | `requests.memory: 5Gi`            |
 |                                               |                                                                                                          | `limits.cpu: 1`                   |
diff --git a/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml b/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml
index ed0e7ac..075307f 100644
--- a/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml
+++ b/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml
@@ -50,7 +50,6 @@
         volumeMounts:
         - name: gerrit-site
           mountPath: "/var/gerrit"
-      {{ if not .Values.gerritReplica.initializeTestSite.enabled -}}
       # Initialize the volume containing the whole Gerrit-site with defaults
       # Not needed, when running in test mode, since then the configured site will
       # be initialized
@@ -74,7 +73,6 @@
         - name: gerrit-init-config
           mountPath: "/var/config/gerrit-init.yaml"
           subPath: gerrit-init.yaml
-      {{- end }}
       # If configured, run initialization taking the given Gerrit configuration
       # and persisted volumes into account.
       - name: gerrit-init
@@ -100,14 +98,6 @@
             ln -sf /var/mnt/git /var/gerrit/
           fi
 
-          {{ if .Values.gerritReplica.initializeTestSite.enabled -}}
-          /var/tools/gerrit_init.py \
-            -c /var/config/gerrit-init.yaml \
-            -s /var/gerrit
-
-          symlink_config_to_site
-          {{- end }}
-
           /var/tools/validate_notedb.py -s /var/gerrit
         volumeMounts:
         - name: gerrit-site
diff --git a/helm-charts/gerrit-replica/templates/git-repositories-init.job.yaml b/helm-charts/gerrit-replica/templates/git-repositories-init.job.yaml
new file mode 100644
index 0000000..34ff66d
--- /dev/null
+++ b/helm-charts/gerrit-replica/templates/git-repositories-init.job.yaml
@@ -0,0 +1,69 @@
+apiVersion: batch/v1
+kind: Job
+metadata:
+  name: {{ .Release.Name }}-git-filesystem-init
+  labels:
+    app: gerrit-replica
+    chart: {{ template "gerrit-replica.chart" . }}
+    heritage: {{ .Release.Service }}
+    release: {{ .Release.Name }}
+spec:
+  template:
+    {{ if .Values.istio.enabled -}}
+    metadata:
+      annotations:
+        sidecar.istio.io/inject: "false"
+    {{- end }}
+    spec:
+      securityContext:
+        fsGroup: 100
+      {{ if .Values.images.registry.ImagePullSecret.name -}}
+      imagePullSecrets:
+      - name: {{ .Values.images.registry.ImagePullSecret.name }}
+      {{- end }}
+      containers:
+      - name: create-repositories
+        image: {{ template "registry" . }}{{ .Values.gerritReplica.images.gerritInit }}:{{ .Values.images.version }}
+        imagePullPolicy: {{ .Values.images.imagePullPolicy }}
+        command:
+        - /bin/ash
+        - -ce
+        args:
+        - |
+          if test -d /var/mnt/git/All-Projects.git && \
+              test -d /var/mnt/git/All-Users.git; then
+            echo "Repositories already exist. Won't perform initial creation."
+            exit 0
+          fi
+
+          mkdir -p /var/gerrit/etc
+          ln -sf /var/config/gerrit.config /var/gerrit/etc/gerrit.config
+          ln -sf /var/mnt/git /var/gerrit/
+
+          /var/tools/gerrit_init.py \
+            -c /var/config/gerrit-init.yaml \
+            -s /var/gerrit
+        volumeMounts:
+        - name: gerrit-site
+          mountPath: "/var/gerrit"
+        - name: git-filesystem
+          mountPath: "/var/mnt/git"
+        - name: gerrit-init-config
+          mountPath: "/var/config/gerrit-init.yaml"
+          subPath: gerrit-init.yaml
+        - name: gerrit-config
+          mountPath: "/var/config/gerrit.config"
+          subPath: gerrit.config
+      volumes:
+      - name: gerrit-site
+        emptyDir: {}
+      - name: git-filesystem
+        persistentVolumeClaim:
+          claimName: {{ .Release.Name }}-git-filesystem-pvc
+      - name: gerrit-init-config
+        configMap:
+          name: {{ .Release.Name }}-gerrit-init-configmap
+      - name: gerrit-config
+        configMap:
+          name: {{ .Release.Name }}-gerrit-replica-configmap
+      restartPolicy: Never
diff --git a/helm-charts/gerrit-replica/values.yaml b/helm-charts/gerrit-replica/values.yaml
index 3b679dc..3a91396 100644
--- a/helm-charts/gerrit-replica/values.yaml
+++ b/helm-charts/gerrit-replica/values.yaml
@@ -165,12 +165,6 @@
   # work.
   maxUnavailable: 100%
 
-  # If you only intend to test the Gerrit replica and do not wish to actually
-  # replicate repositories, activate this option to initialize a new site,
-  # including a notedb.
-  initializeTestSite:
-    enabled: true
-
   # The memory limit has to be higher than the configures heap-size for Java!
   resources:
     requests: