Skip to content

Commit

Permalink
helm: move ApplySchema to keyspace
Browse files Browse the repository at this point in the history
Signed-off-by: Sugu Sougoumarane <[email protected]>
  • Loading branch information
sougou committed Nov 24, 2018
1 parent 8632ec1 commit d93381b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 66 deletions.
67 changes: 67 additions & 0 deletions helm/vitess/templates/_keyspace.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,73 @@
# define image to use
{{- $vitessTag := .vitessTag | default $defaultVtctlclient.vitessTag -}}

{{- if $keyspace.schema }}
---
###################################
# ApplySchema Job
###################################
apiVersion: batch/v1
kind: Job
metadata:
name: {{ $keyspaceClean }}-apply-schema
spec:
backoffLimit: 1
template:
spec:
restartPolicy: OnFailure
containers:
- name: apply-schema
image: "vitess/vtctlclient:{{$vitessTag}}"
volumeMounts:
{{ include "user-secret-volumeMounts" $defaultVtctlclient.secrets | indent 10 }}

command: ["bash"]
args:
- "-c"
- |
set -ex

VTCTLD_SVC=vtctld.{{ $namespace }}:15999
SECONDS=0
TIMEOUT_SECONDS=600
VTCTL_EXTRA_FLAGS=({{ include "format-flags-inline" $defaultVtctlclient.extraFlags }})

# poll every 5 seconds to see if vtctld is ready
until vtctlclient ${VTCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC ListAllTablets {{ $cell.name }} > /dev/null 2>&1; do
if (( $SECONDS > $TIMEOUT_SECONDS )); then
echo "timed out waiting for vtctlclient to be ready"
exit 1
fi
sleep 5
done

while true; do
if (( $SECONDS > $TIMEOUT_SECONDS )); then
echo "timed out waiting for master"
exit 1
fi

# wait for all shards to have a master
{{- range $shard := $keyspace.shards }}
master_alias=$(vtctlclient ${VTLCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC GetShard {{ $keyspace.name }}/{{ $shard.name }} | jq '.master_alias.uid')
if [ "$master_alias" == "null" -o "$master_alias" == "" ]; then
echo "no master for '{{ $keyspace.name }}/{{ $shard.name }}' yet, continuing to wait"
sleep 5
continue
fi
{{- end }}

break
done

vtctlclient ${VTCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC ApplySchema -sql "$(cat <<END_OF_COMMAND
{{ $keyspace.schema | indent 14}}
END_OF_COMMAND
)" {{ $keyspace.name }}
volumes:
{{ include "user-secret-volumes" (.secrets | default $defaultVtctlclient.secrets) | indent 8 }}
{{ end }}
{{- if $keyspace.vschema }}
---
###################################
Expand Down
69 changes: 3 additions & 66 deletions helm/vitess/templates/_shard.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ spec:
VTCTL_EXTRA_FLAGS=({{ include "format-flags-inline" $defaultVtctlclient.extraFlags }})

# poll every 5 seconds to see if vtctld is ready
until vtctlclient ${VTCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC ListAllTablets {{ $cellClean }} > /dev/null 2>&1; do
until vtctlclient ${VTCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC ListAllTablets {{ $cell.name }} > /dev/null 2>&1; do
if (( $SECONDS > $TIMEOUT_SECONDS )); then
echo "timed out waiting for vtctlclient to be ready"
exit 1
Expand All @@ -60,7 +60,7 @@ spec:

until [ $TABLETS_READY ]; do
# get all the tablets in the current cell
cellTablets="$(vtctlclient ${VTCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC ListAllTablets {{ $cellClean }})"
cellTablets="$(vtctlclient ${VTCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC ListAllTablets {{ $cell.name }})"

# filter to only the tablets in our current shard
shardTablets=$( echo "$cellTablets" | awk 'substr( $5,1,{{ len $shardName }} ) == "{{ $shardName }}" {print $0}')
Expand All @@ -74,7 +74,7 @@ spec:

# check for a master tablet from the GetShard call
master_alias=$(vtctlclient ${VTLCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC GetShard {{ $keyspace.name }}/{{ $shard.name }} | jq '.master_alias.uid')
if [ $master_alias != "null" ]; then
if [ "$master_alias" != "null" -a "$master_alias" != "" ]; then
echo "'$master_alias' is already the master tablet, exiting without running InitShardMaster"
exit
fi
Expand Down Expand Up @@ -112,68 +112,5 @@ spec:
{{ include "user-secret-volumes" (.secrets | default $defaultVtctlclient.secrets) | indent 8 }}


{{ if $keyspace.schema }}
---
###################################
# ApplySchema Job
###################################
apiVersion: batch/v1
kind: Job
metadata:
name: {{ $shardName }}-apply-schema
spec:
backoffLimit: 1
template:
spec:
restartPolicy: OnFailure
containers:
- name: apply-schema
image: "vitess/vtctlclient:{{$vitessTag}}"
volumeMounts:
{{ include "user-secret-volumeMounts" $defaultVtctlclient.secrets | indent 10 }}

command: ["bash"]
args:
- "-c"
- |
set -ex

VTCTLD_SVC=vtctld.{{ $namespace }}:15999
SECONDS=0
TIMEOUT_SECONDS=600
VTCTL_EXTRA_FLAGS=({{ include "format-flags-inline" $defaultVtctlclient.extraFlags }})

# poll every 5 seconds to see if vtctld is ready
until vtctlclient ${VTCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC ListAllTablets {{ $cellClean }} > /dev/null 2>&1; do
if (( $SECONDS > $TIMEOUT_SECONDS )); then
echo "timed out waiting for vtctlclient to be ready"
exit 1
fi
sleep 5
done

while true; do
if (( $SECONDS > $TIMEOUT_SECONDS )); then
echo "timed out waiting for master"
exit 1
fi

# check for a master tablet from the GetShard call
master_alias=$(vtctlclient ${VTLCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC GetShard {{ $keyspace.name }}/{{ $shard.name }} | jq '.master_alias.uid')
if [ $master_alias != "null" ]; then
echo "found master '$master_alias', deploying schema"
break
fi
sleep 5
done

vtctlclient ${VTCTL_EXTRA_FLAGS[@]} -server $VTCTLD_SVC ApplySchema -sql "$(cat <<END_OF_COMMAND
{{ $keyspace.schema | indent 14}}
END_OF_COMMAND
)" {{ $keyspace.name }}
volumes:
{{ include "user-secret-volumes" (.secrets | default $defaultVtctlclient.secrets) | indent 8 }}
{{ end }}
{{- end -}}
{{- end -}}

0 comments on commit d93381b

Please sign in to comment.