From 97926193f8efe904b33c7ffa5cb9ed89cfcb7a97 Mon Sep 17 00:00:00 2001 From: Vladyslav Deryhin Date: Thu, 22 Aug 2024 09:52:23 +0300 Subject: [PATCH] [geth] add startupProbe (#315) * [geth] port startupProbe from op-geth chart --- dysnix/geth/Chart.yaml | 4 +-- dysnix/geth/templates/_helpers.tpl | 13 +++++++ dysnix/geth/templates/configmap-scripts.yaml | 2 ++ .../geth/templates/scripts/_wait-for-sync.tpl | 36 +++++++++++++++++++ dysnix/geth/templates/statefulset.yaml | 22 ++++-------- dysnix/geth/values.yaml | 12 +++++++ 6 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 dysnix/geth/templates/scripts/_wait-for-sync.tpl diff --git a/dysnix/geth/Chart.yaml b/dysnix/geth/Chart.yaml index d510e9b0..6647f6e0 100644 --- a/dysnix/geth/Chart.yaml +++ b/dysnix/geth/Chart.yaml @@ -2,8 +2,8 @@ apiVersion: v2 name: geth description: Go-ethereum blockchain node Helm Chart -version: 1.0.17 -appVersion: v1.14.5 +version: 1.0.18 +appVersion: v1.14.8 keywords: - geth diff --git a/dysnix/geth/templates/_helpers.tpl b/dysnix/geth/templates/_helpers.tpl index 096abf0b..636d57f5 100644 --- a/dysnix/geth/templates/_helpers.tpl +++ b/dysnix/geth/templates/_helpers.tpl @@ -76,4 +76,17 @@ Convert Golang slice to Toml array {{- $element | quote }} {{- end -}} {{ print "]" -}} +{{- end }} + +{{- define "geth.healthcheck" -}} +{{- $context := index . 0 }} +{{- $root := index . 1 }} +{{- if and $root.exec (kindIs "string" $root.exec.command) }} +{{- omit $root "enabled" "exec" | toYaml }} +exec: + command: + {{- tpl $root.exec.command $context | nindent 4 }} +{{- else }} +{{- omit $root "enabled" | toYaml }} +{{- end }} {{- end }} \ No newline at end of file diff --git a/dysnix/geth/templates/configmap-scripts.yaml b/dysnix/geth/templates/configmap-scripts.yaml index d5cf02fb..88b56665 100644 --- a/dysnix/geth/templates/configmap-scripts.yaml +++ b/dysnix/geth/templates/configmap-scripts.yaml @@ -9,6 +9,8 @@ data: {{- include (print $.Template.BasePath "/scripts/_readiness.tpl") . | nindent 4 }} liveness.sh: |- {{- include (print $.Template.BasePath "/scripts/_liveness.tpl") . | nindent 4 }} + wait-for-sync.sh: |- + {{- include (print $.Template.BasePath "/scripts/_wait-for-sync.tpl") . | nindent 4 }} {{- if or .Values.syncToS3.enabled .Values.initFromS3.enabled }} init-from-s3.sh: |- {{- include (print $.Template.BasePath "/scripts/_init-from-s3.tpl") . | nindent 4 }} diff --git a/dysnix/geth/templates/scripts/_wait-for-sync.tpl b/dysnix/geth/templates/scripts/_wait-for-sync.tpl new file mode 100644 index 00000000..b3492edc --- /dev/null +++ b/dysnix/geth/templates/scripts/_wait-for-sync.tpl @@ -0,0 +1,36 @@ +#!/usr/bin/env sh +# shellcheck disable=SC3040 + +# We assume that node is syncing from initial snapshot when: +# (get_block_number == 0x0) OR (is_syncing == true) + +set -e + +HTTP_PORT="{{ .Values.config.node.http.port }}" + +# expected output format: 0x50938d +get_block_number() { + wget "http://localhost:$HTTP_PORT" -qO- \ + --header 'Content-Type: application/json' \ + --post-data '{"jsonrpc":"2.0","method":"eth_blockNumber","id":1}' \ + | sed -r 's/.*"result":"([^"]+)".*/\1/g' +} + +# exit codes: 1 = sync completed, 0 = sync in progress +is_syncing() { + wget "http://localhost:$HTTP_PORT" -qO- \ + --header 'Content-Type: application/json' \ + --post-data '{"jsonrpc":"2.0","method":"eth_syncing","id":1}' \ + | grep -qv "false" +} + +if ! get_block_number | grep -qE '^0x[a-z0-9]+'; then + echo "Error reading block number"; exit 1 +fi + +if is_syncing || [ "$(get_block_number)" = "0x0" ]; then + echo "Initial sync is in progress" + exit 1 +else + exit 0 +fi \ No newline at end of file diff --git a/dysnix/geth/templates/statefulset.yaml b/dysnix/geth/templates/statefulset.yaml index 2fbdcae2..704b7f72 100644 --- a/dysnix/geth/templates/statefulset.yaml +++ b/dysnix/geth/templates/statefulset.yaml @@ -193,25 +193,15 @@ spec: mountPath: /scripts {{- if .Values.livenessProbe.enabled }} livenessProbe: - {{- if and .Values.livenessProbe.exec (kindIs "string" .Values.livenessProbe.exec.command) }} - {{- omit .Values.livenessProbe "enabled" "exec" | toYaml | nindent 10 }} - exec: - command: - {{- tpl .Values.livenessProbe.exec.command . | nindent 14 }} - {{- else }} - {{- omit .Values.livenessProbe "enabled" | toYaml | nindent 10 }} - {{- end }} + {{- include "geth.healthcheck" (list $ .Values.livenessProbe) | nindent 10 }} {{- end }} {{- if .Values.readinessProbe.enabled }} readinessProbe: - {{- if and .Values.readinessProbe.exec (kindIs "string" .Values.readinessProbe.exec.command) }} - {{- omit .Values.readinessProbe "enabled" "exec" | toYaml | nindent 10 }} - exec: - command: - {{- tpl .Values.readinessProbe.exec.command . | nindent 14 }} - {{- else }} - {{- omit .Values.readinessProbe "enabled" | toYaml | nindent 10 }} - {{- end }} + {{- include "geth.healthcheck" (list $ .Values.readinessProbe) | nindent 10 }} + {{- end }} + {{- if .Values.startupProbe.enabled }} + startupProbe: + {{- include "geth.healthcheck" (list $ .Values.startupProbe) | nindent 10 }} {{- end }} resources: {{- toYaml .Values.resources | nindent 10 }} diff --git a/dysnix/geth/values.yaml b/dysnix/geth/values.yaml index c620fc6c..84cbc48e 100644 --- a/dysnix/geth/values.yaml +++ b/dysnix/geth/values.yaml @@ -214,6 +214,18 @@ readinessProbe: - /scripts/readiness.sh - "60" +# .startupProbe.exec.command can also be in a templated string format +startupProbe: + enabled: false + initialDelaySeconds: 10 + periodSeconds: 5 + failureThreshold: 120960 # periodSeconds * failureThreshold = 7 days + timeoutSeconds: 10 + exec: + command: + - sh + - /scripts/wait-for-sync.sh + ## Main Geth config config: ## Use a utility like OpenSSL to create JWT via command: openssl rand -hex 32