readiness.sh


Overview

`readiness.sh` is a lightweight shell script designed as a Kubernetes readiness probe to determine whether a service (specifically, a Blockbook API) is ready to accept traffic. The script performs a health check by querying the local Blockbook API and evaluating its synchronization status. It also supports a manual override to disable the readiness check if required.

This script is typically invoked periodically by Kubernetes or other orchestration tools to decide if the associated pod/container should receive network traffic.


Detailed Explanation

Purpose

Script Flow and Logic

  1. Manual Disable Check
    The script first checks for the presence of the file /data/disable_readiness. If this file exists, the readiness probe is considered disabled, and the script exits successfully (exit code 0).

  2. Query Blockbook API
    The script performs an HTTP GET request (curl) to the local Blockbook API endpoint http://localhost:8001/api/v2. If this request fails (non-success HTTP status or no response), the script exits with failure (exit code 1).

  3. Parse API Response
    It uses jq to extract the .blockbook.inSync field from the JSON response. This boolean indicates whether the blockchain data is fully synchronized.

  4. Determine Readiness

    • If .blockbook.inSync is "true", the script prints "blockbook is synced" and exits with success (0).

    • Otherwise, it prints "blockbook is still syncing" and exits with failure (1).

Exit Codes


Key Variables and Commands

Variable / Command

Description

DISABLE_READINESS_PROBE

Path to the file `/data/disable_readiness` which disables the readiness probe if present.

`curl -sf`

Fetches the API data silently (`-s`) and fails on errors (`-f`).

jq -r '.blockbook.inSync'

Parses and extracts the `inSync` boolean from the Blockbook API JSON response.


Usage Example

The script itself is designed to be called by the Kubernetes readiness probe configuration:

readinessProbe:
  exec:
    command:
      - /bin/bash
      - /path/to/readiness.sh
  initialDelaySeconds: 5
  periodSeconds: 10

Manually, a user could run:

./readiness.sh

Possible outputs:


Important Implementation Details


Interaction with Other System Components


Mermaid Diagram

flowchart TD
    A[Start readiness.sh] --> B{Check /data/disable_readiness file}
    B -- Yes --> C[Print "readiness probe disabled"]
    C --> D[Exit 0]
    B -- No --> E[Run curl to http://localhost:8001/api/v2]
    E -- Fail --> F[Exit 1]
    E -- Success --> G[Parse JSON field .blockbook.inSync]
    G --> H{Is inSync == "true"?}
    H -- Yes --> I[Print "blockbook is synced"]
    I --> D
    H -- No --> J[Print "blockbook is still syncing"]
    J --> F

Summary

`readiness.sh` is a concise, reliable readiness probe script for Kubernetes environments running Blockbook. It smartly combines a manual override mechanism with a live sync check against the Blockbook API. The script’s exit codes drive Kubernetes load balancing and pod lifecycle decisions, ensuring traffic is only routed to fully ready and synced instances.


Requirements


Potential Enhancements


This documentation provides a comprehensive understanding of the `readiness.sh` script, its role, and its practical usage within the system architecture.