Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: run init-job on first install #450

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 23 additions & 46 deletions cockroachdb/templates/job.init.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ metadata:
{{- toYaml . | nindent 4 }}
{{- end }}
annotations:
helm.sh/hook: post-install,post-upgrade
{{- if not .Release.IsInstall }}
helm.sh/hook: post-upgrade
helm.sh/hook-delete-policy: before-hook-creation
{{- end }}
{{- with .Values.init.jobAnnotations }}
{{- toYaml . | nindent 4 }}
{{- end }}
Expand Down Expand Up @@ -102,57 +104,32 @@ spec:
- name: cluster-init
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
# Run the command in an `while true` loop because this Job is bound
# to come up before the CockroachDB Pods (due to the time needed to
# get PersistentVolumes attached to Nodes), and sleeping 5 seconds
# between attempts is much better than letting the Pod fail when
# the init command does and waiting out Kubernetes' non-configurable
# exponential back-off for Pod restarts.
# Command completes either when cluster initialization succeeds,
# or when cluster has been initialized already.
command:
- /bin/bash
- -c
- >-
- |
{{- if $isClusterInitEnabled }}
initCluster() {
while true; do
local output=$(
set -x;

/cockroach/cockroach init \
{{- if .Values.tls.enabled }}
--certs-dir=/cockroach-certs/ \
{{- else }}
--insecure \
{{- end }}
{{- with index .Values.conf "cluster-name" }}
--cluster-name={{.}} \
{{- end }}
--host={{ template "cockroachdb.fullname" . }}-0.{{ template "cockroachdb.fullname" . -}}
:{{ .Values.service.ports.grpc.internal.port | int64 }} \
{{- if .Values.init.pcr.enabled -}}
{{- if .Values.init.pcr.isPrimary }}
--virtualized \
{{- else }}
--virtualized-empty \
{{- end }}
{{- end }}
2>&1);

local exitCode="$?";
echo $output;

if [[ "$output" =~ .*"Cluster successfully initialized".* || "$output" =~ .*"cluster has already been initialized".* ]]; then
break;
fi

echo "Cluster is not ready to be initialized, retrying in 5 seconds"
sleep 5;
done
}

initCluster;
set -x;
/cockroach/cockroach init \
{{- if .Values.tls.enabled }}
--certs-dir=/cockroach-certs/
{{- else }}
--insecure
{{- end }} \
{{- with index .Values.conf "cluster-name" }}
--cluster-name={{.}} \
{{- end }}
--host={{ template "cockroachdb.fullname" . }}-0.{{ template "cockroachdb.fullname" . -}}
:{{ .Values.service.ports.grpc.internal.port | int64 }}
{{- if .Values.init.pcr.enabled -}} \
{{- if .Values.init.pcr.isPrimary }}
--virtualized
{{- else }}
--virtualized-empty
{{- end }}
{{- end }}
{{- end }}

{{- if $isDatabaseProvisioningEnabled }}
Expand Down