readiness.sh
Overview
`readiness.sh` is a lightweight Bash script designed to perform a readiness check for a service named **midgard** running locally on port 8080. It verifies whether the service's health endpoint reports that the system is "in sync," indicating readiness to handle requests. The script is intended to be used as a readiness probe in container orchestration platforms (like Kubernetes), or as a simple health check utility in deployment pipelines or monitoring systems.
Detailed Explanation
Script Purpose
Query the health status of the midgard service via HTTP.
Parse the response to check if the service is "in sync".
Exit with status code
0if ready,1otherwise.
Script Breakdown
#!/bin/bash
Specifies the script interpreter as Bash.
HEALTH=$(curl -sf http://localhost:8080/v2/health) || exit 1
Uses
curlto silently (-s) and fail silently on errors (-f) to request the/v2/healthendpoint onlocalhost:8080.Assigns the JSON response to the variable
HEALTH.If the request fails (e.g., server not responding), the script immediately exits with status code
1(failure).
IN_SYNC=$(echo $HEALTH | jq -r '.inSync')
Uses
jq(a command-line JSON processor) to extract the value of theinSyncfield from the JSON response.The
-roption outputs the raw string value.Assumes the JSON response contains a boolean property
inSync.
if [[ $IN_SYNC == true ]]; then
echo "midgard is synced"
exit 0
fi
Checks if the extracted
inSyncvalue istrue.If yes, prints a success message and exits with code
0(success).
echo "midgard is still syncing"
exit 1
If
inSyncis nottrue, prints a message indicating the service is still syncing and exits with code1(failure).
Parameters and Environment
No parameters: The script does not accept any command-line arguments.
Dependencies:
curl: to perform HTTP requests.jq: to parse JSON responses.
Assumptions:
The midgard service is accessible at
http://localhost:8080/v2/health.The health endpoint returns a JSON object with an
inSyncboolean field.
Usage Example
Run the script as a standalone command:
./readiness.sh
Possible outputs and exit codes:
Output | Exit Code | Meaning |
|---|---|---|
`midgard is synced` | 0 | Service is ready and in sync. |
`midgard is still syncing` | 1 | Service is not ready yet. |
(no output, immediate exit) | 1 | Health endpoint unreachable or error. |
This makes the script suitable for integration as a readiness probe in orchestration systems:
readinessProbe:
exec:
command:
- /bin/bash
- /path/to/readiness.sh
initialDelaySeconds: 10
periodSeconds: 5
Implementation Details / Algorithms
The script relies on a simple HTTP GET request and JSON parsing.
It uses a fail-fast approach: if the HTTP request fails, it immediately exits.
The decision logic is a straightforward conditional check on the
inSyncJSON value.No loops or complex algorithms are used, ensuring minimal runtime and resource usage.
Interaction With Other System Components
Midgard Service: The script interacts directly with the midgard service by querying its health endpoint.
Container Orchestrators / Monitoring Tools: Can be invoked by Kubernetes readiness probes or external monitoring scripts to determine if midgard is ready to serve traffic.
System Utilities: Depends on
curlandjqinstalled in the environment.
The script acts as a bridge between midgard's internal health status and external systems that need to make decisions about routing traffic or triggering alerts.
Flowchart Representing Script Workflow
flowchart TD
A[Start] --> B{Run curl request<br>to /v2/health}
B -- Success --> C[Parse JSON with jq<br>extract 'inSync']
B -- Failure --> G[Exit 1: Health check failed]
C --> D{Is inSync == true?}
D -- Yes --> E[Print "midgard is synced"<br>Exit 0]
D -- No --> F[Print "midgard is still syncing"<br>Exit 1]
Summary
`readiness.sh` is a simple, effective readiness probe script for the midgard service. It performs an HTTP health check, parses JSON output to verify service synchronization status, and returns appropriate exit codes to signal readiness. Its minimal dependencies and clear logic make it ideal for integration into automated deployment and monitoring pipelines.