A Kubernetes operator that monitors the health of HTTP endpoints by periodically sending requests and tracking their responses.
AI-GENERATED CODE AHEAD! Every single line of code in this repository is generated.
This entire repo is the result of me going "Hey, I wonder if I can vibe-code a Kubernetes operator in a weekend?"
Me over the weekend:
Buddy-coded with Cursor Pro, 307 premium model requests.
Use at your own risk. No guarantees, no warranties, just pure vibes and questionable life choices. 🤪
The Heartbeats Operator provides a custom resource Heartbeat that allows you to monitor
the health of HTTP endpoints in your Kubernetes cluster. It periodically checks the endpoints
and updates their status based on HTTP response codes with comprehensive error handling and reporting.
- Robust Health Monitoring: Monitor HTTP endpoints with configurable intervals and retry logic
- Flexible Status Code Ranges: Define custom status code ranges for healthy/unhealthy states
- Secure Configuration: Store endpoint URLs securely in Kubernetes secrets
- Real-time Status Updates: Detailed health information with comprehensive error messages
- Configurable Timeouts: Adjustable timeout and retry settings for different network conditions
- Comprehensive Error Handling: Detailed error messages for troubleshooting
- Report Endpoints: Reporting to healthy/unhealthy endpoints for external monitoring systems
- Controller-runtime metrics: HTTPS
/metricson port 8443
- Kubernetes cluster
- kubectl configured to access your cluster
make install
make deploy IMG=ghcr.io/siutsin/heartbeats:latestmake install applies the CRD. make deploy builds config/default with kustomize.
-
Create a secret containing your endpoint URLs:
apiVersion: v1 kind: Secret metadata: name: heartbeat-endpoints type: Opaque stringData: targetEndpoint: "https://api.example.com/health" healthyEndpoint: "https://httpbin.org/status/200" unhealthyEndpoint: "https://httpbin.org/status/500"
-
Create a Heartbeat resource:
apiVersion: monitoring.siutsin.com/v1alpha1 kind: Heartbeat metadata: name: api-health spec: endpointsSecret: name: heartbeat-endpoints targetEndpointKey: targetEndpoint healthyEndpointKey: healthyEndpoint unhealthyEndpointKey: unhealthyEndpoint healthyEndpointMethod: GET unhealthyEndpointMethod: GET expectedStatusCodeRanges: - min: 200 max: 299 interval: 30s
apiVersion: monitoring.siutsin.com/v1alpha1
kind: Heartbeat
metadata:
name: api-health-with-reporting
spec:
endpointsSecret:
name: heartbeat-endpoints
targetEndpointKey: targetEndpoint
healthyEndpointKey: healthyEndpoint
unhealthyEndpointKey: unhealthyEndpoint
healthyEndpointMethod: GET
unhealthyEndpointMethod: GET
expectedStatusCodeRanges:
- min: 200
max: 299
- min: 404
max: 404 # Accept 404 as healthy for certain endpoints
interval: 30s| Field | Type | Description | Required |
|---|---|---|---|
| endpointsSecret | object | Reference to the secret containing endpoint URLs | Yes |
| expectedStatusCodeRanges | array | Ranges of HTTP status codes considered healthy | Yes |
| interval | string | Time between health checks (e.g., "30s", "5m", "1h") | Yes |
| Field | Type | Description | Required |
|---|---|---|---|
| name | string | Name of the secret | Yes |
| namespace | string | Namespace of the secret (defaults to Heartbeat's namespace) | No |
| targetEndpointKey | string | Key containing the target endpoint URL | Yes |
| healthyEndpointKey | string | Key containing the healthy endpoint URL for reporting | Yes |
| unhealthyEndpointKey | string | Key containing the unhealthy endpoint URL for reporting | Yes |
| healthyEndpointMethod | string | HTTP method for the healthy report (GET, POST, PUT, PATCH) |
No |
| unhealthyEndpointMethod | string | HTTP method for the unhealthy report (GET, POST, PUT, PATCH) |
No |
| Field | Type | Description | Required |
|---|---|---|---|
| min | integer | Minimum status code in range (100-599) | Yes |
| max | integer | Maximum status code in range (100-599) | Yes |
The Heartbeat resource's status includes:
healthy: Boolean indicating if the endpoint is healthylastStatus: Last HTTP status code receivedmessage: Human-readable status message with detailed error informationlastChecked: Timestamp of the last health checkreportStatus: Status of reporting to external endpoints ("Success" or "Failure")
The operator provides detailed error messages for troubleshooting:
missing required key: A required endpoint key is missing from the secretendpoint is not specified: An endpoint URL is empty or not providedfailed to check endpoint health: Network or HTTP errors during health checksendpoint timed out: The endpoint took too long to respondinvalid status code range: Status code range has invalid min/max valuesstatus code is not within expected ranges: Endpoint returned unexpected status code
- Go 1.24+
- Docker
- Kind (for local testing)
- kubectl
# Run unit tests
make test
# Run unit tests with race detection (for CI)
make test-ci
# Run e2e tests locally
make test-e2e LOCAL=true
# Run e2e tests with race detection (for CI)
make test-e2e-ci LOCAL=true
# Build the operator
make build
# Build Docker image
make docker-build# Format code
make fmt
# Run linter
make lint
# Run linter with fixes
make lint-fix
# Check markdown files
make lint-markdownThe operator logs JSON to stdout via slog.
- INFO: General operational information, health check results
- ERROR: Error conditions, failed health checks, configuration issues
- DEBUG/V(1): Detailed debugging information, HTTP request details
The operator exposes controller-runtime metrics at /metrics on port 8443.
The metrics endpoint is secured and requires authentication. Access is controlled via RBAC:
# Get service account token
TOKEN=$(kubectl create token heartbeats-operator-controller-manager -n heartbeats-operator-system)
# Access metrics
curl -k -H "Authorization: Bearer $TOKEN" \
https://heartbeats-operator-controller-manager-metrics-service.heartbeats-operator-system.svc.cluster.local:8443/metrics-
Endpoint Timeout
- Check if the endpoint is accessible from the cluster
- Verify network policies allow the connection
- Consider increasing the timeout duration
- Check logs for detailed timeout information
-
Invalid Status Code
- Ensure the endpoint returns expected status codes
- Verify the status code ranges in the Heartbeat spec
- Check the status message for specific error details
-
Secret Not Found
- Confirm the secret exists in the correct namespace
- Verify the secret keys match the Heartbeat configuration
- Check for "missing required key" error messages
-
Report Endpoint Failures
- Verify the report endpoints are accessible
- Check the
reportStatusfield in the Heartbeat status - Review logs for report endpoint errors
# Check Heartbeat status
kubectl get heartbeat <name> -o yaml
# View operator logs
kubectl logs -n heartbeats-operator-system deployment/heartbeats-operator-controller-manager
# Check metrics
kubectl port-forward -n heartbeats-operator-system svc/heartbeats-operator-controller-manager-metrics-service 8443:8443Contributions are welcome! Please see our Contributing Guide for details.
- Follow the existing code style and patterns
- Add comprehensive unit tests for new features
- Include e2e tests for integration scenarios
- Use structured logging with appropriate log levels
- Follow error handling patterns established in the codebase
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
0 comments
log in to comment.