Documentation
¶
Index ¶
- Constants
- func ParseInterval(interval string) (time.Duration, error)
- type Config
- type DefaultHealthChecker
- type HealthChecker
- type HeartbeatReconciler
- type StatusUpdater
- func (u *StatusUpdater) UpdateEmptyEndpointStatus(ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat) error
- func (u *StatusUpdater) UpdateHealthCheckErrorStatus(ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, statusCode int, ...) error
- func (u *StatusUpdater) UpdateHealthStatus(ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, healthy bool, ...) error
- func (u *StatusUpdater) UpdateInvalidRangeStatus(ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, statusCode int) error
- func (u *StatusUpdater) UpdateMissingKeyStatus(ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, key string) error
- func (u *StatusUpdater) UpdateSecretErrorStatus(ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, err error) error
- func (u *StatusUpdater) UpdateStatus(ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, statusCode int, ...) error
Constants ¶
const ( // ErrFailedToCreateRequest indicates that an HTTP request could not be created. // This typically occurs when the endpoint URL is malformed or invalid. ErrFailedToCreateRequest = "failed to create request" // ErrFailedToMakeRequest indicates that an HTTP request failed after maximum retry attempts. // This includes network errors, connection refused, DNS resolution failures, etc. ErrFailedToMakeRequest = "failed to make request after maximum attempts" // ErrFailedToGetSecret indicates that a Kubernetes secret could not be retrieved. // This occurs when the secret doesn't exist or the controller lacks permissions. ErrFailedToGetSecret = "failed to get secret" // ErrMissingRequiredKey indicates that a required key is missing from a Kubernetes secret. // This happens when the secret exists but doesn't contain the expected endpoint keys. ErrMissingRequiredKey = "missing required key" // ErrEndpointTimeout indicates that an endpoint request timed out. // This occurs when the endpoint takes longer than the configured timeout to respond. ErrEndpointTimeout = "endpoint timed out" // ErrFailedToCheckEndpoint indicates that the health check process failed. // This is a general error for any failure during endpoint health checking. ErrFailedToCheckEndpoint = "failed to check endpoint health" // ErrInvalidStatusCodeRange indicates that a status code range has invalid min/max values. // This occurs when the minimum value is greater than the maximum value. ErrInvalidStatusCodeRange = "invalid status code range" // ErrFailedToUpdateStatus indicates that the Heartbeat status could not be updated. // This occurs when there are permission issues or the resource has been modified. ErrFailedToUpdateStatus = "failed to update Heartbeat status" // ErrEmptyEndpoint indicates that an endpoint URL is empty or not specified. // This occurs when the secret contains an empty string for an endpoint key. ErrEmptyEndpoint = "endpoint URL is empty" // ErrStatusCodeNotInRange indicates that the endpoint returned a status code outside expected ranges. // This occurs when the endpoint is healthy but returns an unexpected status code. ErrStatusCodeNotInRange = "status code is not within expected ranges" // ErrEndpointHealthy indicates that the endpoint is healthy and responding correctly. // This is a success message indicating the endpoint passed all health checks. ErrEndpointHealthy = "endpoint is healthy" // ErrEndpointNotSpecified indicates that an endpoint URL is not specified. // This occurs when the endpoint key is missing from the secret. ErrEndpointNotSpecified = "endpoint is not specified" )
Error messages used throughout the controller for consistent error reporting. These constants provide standardised error messages for various failure scenarios.
const ( // MaxConcurrentReconciles is the number of concurrent reconciliations. // This allows multiple Heartbeat resources to be processed in parallel, // preventing a single failing endpoint from blocking others. MaxConcurrentReconciles = 10 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
DefaultTimeout time.Duration // Maximum time to wait for HTTP requests
MaxRetries int // Maximum number of retry attempts for failed requests
RetryDelay time.Duration // Delay between retry attempts
RequeueAfter time.Duration // How often to requeue reconciliation
}
Config holds the controller configuration parameters. It defines timeouts, retry settings, and requeue intervals for the heartbeat controller.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the default configuration for the heartbeat controller. This provides sensible defaults for production use.
Returns:
- Config: A configuration struct with default values:
- DefaultTimeout: 10 seconds for HTTP requests
- MaxRetries: 3 retry attempts
- RetryDelay: 1 second between retries
- RequeueAfter: 5 seconds between reconciliations
type DefaultHealthChecker ¶
type DefaultHealthChecker struct {
// contains filtered or unexported fields
}
DefaultHealthChecker is the default implementation of HealthChecker. It uses HTTP requests and configurable retry logic to check endpoint health.
func (*DefaultHealthChecker) CheckEndpointHealth ¶
func (h *DefaultHealthChecker) CheckEndpointHealth( ctx context.Context, endpoint string, expectedStatusCodeRanges []monitoringv1alpha1.StatusCodeRange, endpointsSecret monitoringv1alpha1.EndpointsSecret, ) (bool, int, bool, error)
CheckEndpointHealth checks if the endpoint is healthy by making an HTTP request. Returns:
bool: true if the endpoint is considered healthy (status code in expected range), false otherwise.
int: the HTTP status code returned by the endpoint, or 0 if no response was received.
bool: true if the report request succeeded, false otherwise.
error: non-nil if an error occurred during the health check (e.g., network error, timeout, invalid request),
or nil if the check was successful.
type HealthChecker ¶
type HealthChecker interface {
CheckEndpointHealth(
ctx context.Context,
endpoint string,
expectedStatusCodeRanges []monitoringv1alpha1.StatusCodeRange,
endpointsSecret monitoringv1alpha1.EndpointsSecret,
) (bool, int, bool, error)
}
HealthChecker is an interface for checking endpoint health. Implementations should provide a method to check the health of an endpoint given expected status code ranges.
func NewHealthChecker ¶
func NewHealthChecker(config Config) HealthChecker
NewHealthChecker creates a new DefaultHealthChecker with the provided configuration. Returns a HealthChecker interface.
type HeartbeatReconciler ¶
type HeartbeatReconciler struct {
client.Client
Scheme *runtime.Scheme
Config Config
HealthChecker HealthChecker
StatusUpdater *StatusUpdater
}
HeartbeatReconciler reconciles a Heartbeat object
func (*HeartbeatReconciler) Reconcile ¶
Reconcile is part of the main kubernetes reconciliation loop
func (*HeartbeatReconciler) SetupWithManager ¶
func (r *HeartbeatReconciler) SetupWithManager(mgr ctrl.Manager) error
SetupWithManager sets up the controller with the Manager.
type StatusUpdater ¶
StatusUpdater handles updating the status of Heartbeat resources. It provides methods for updating various aspects of the Heartbeat status including health status, error conditions, and metadata.
func NewStatusUpdater ¶
func NewStatusUpdater(client client.Client) *StatusUpdater
NewStatusUpdater creates a new StatusUpdater instance with the provided Kubernetes client.
Parameters:
- client: The Kubernetes client used for updating Heartbeat resources
Returns:
- *StatusUpdater: A new StatusUpdater instance
func (*StatusUpdater) UpdateEmptyEndpointStatus ¶
func (u *StatusUpdater) UpdateEmptyEndpointStatus( ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, ) error
UpdateEmptyEndpointStatus updates the status when the endpoint URL is empty. This method logs the issue and sets the status to indicate an empty endpoint configuration.
Parameters:
- ctx: Context for the operation
- heartbeat: The Heartbeat resource to update
Returns:
- error: Non-nil if the status update failed, nil otherwise
func (*StatusUpdater) UpdateHealthCheckErrorStatus ¶
func (u *StatusUpdater) UpdateHealthCheckErrorStatus( ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, statusCode int, err error, ) error
UpdateHealthCheckErrorStatus updates the status when there's an error during health checking. This method logs the error and sets the status to indicate a health check failure.
Parameters:
- ctx: Context for the operation
- heartbeat: The Heartbeat resource to update
- statusCode: HTTP status code if available (0 if not applicable)
- err: The error that occurred during health checking
Returns:
- error: Non-nil if the status update failed, nil otherwise
func (*StatusUpdater) UpdateHealthStatus ¶
func (u *StatusUpdater) UpdateHealthStatus( ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, healthy bool, statusCode int, err error, reportSuccess bool, ) error
UpdateHealthStatus updates the status based on the health check result. This method sets the status to reflect whether the endpoint is healthy or unhealthy, including the report status for monitoring systems.
Parameters:
- ctx: Context for the operation
- heartbeat: The Heartbeat resource to update
- healthy: Whether the endpoint is considered healthy
- statusCode: HTTP status code returned by the endpoint
- err: Error from health checking (nil if successful)
- reportSuccess: Whether the report to monitoring systems was successful
Returns:
- error: Non-nil if the status update failed, nil otherwise
func (*StatusUpdater) UpdateInvalidRangeStatus ¶
func (u *StatusUpdater) UpdateInvalidRangeStatus( ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, statusCode int, ) error
UpdateInvalidRangeStatus updates the status when the status code range is invalid. This method logs the issue and sets the status to indicate an invalid configuration.
Parameters:
- ctx: Context for the operation
- heartbeat: The Heartbeat resource to update
- statusCode: HTTP status code if available (0 if not applicable)
Returns:
- error: Non-nil if the status update failed, nil otherwise
func (*StatusUpdater) UpdateMissingKeyStatus ¶
func (u *StatusUpdater) UpdateMissingKeyStatus( ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, key string, ) error
UpdateMissingKeyStatus updates the status when a required key is missing from the secret. This method logs the missing key and sets the status to indicate the configuration issue.
Parameters:
- ctx: Context for the operation
- heartbeat: The Heartbeat resource to update
- key: The name of the missing key in the secret
Returns:
- error: Non-nil if the status update failed, nil otherwise
func (*StatusUpdater) UpdateSecretErrorStatus ¶
func (u *StatusUpdater) UpdateSecretErrorStatus( ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, err error, ) error
UpdateSecretErrorStatus updates the status when there's an error retrieving the secret. This method logs the error and sets the status to indicate a secret-related failure.
Parameters:
- ctx: Context for the operation
- heartbeat: The Heartbeat resource to update
- err: The error that occurred while retrieving the secret
Returns:
- error: Non-nil if the status update failed, nil otherwise
func (*StatusUpdater) UpdateStatus ¶
func (u *StatusUpdater) UpdateStatus( ctx context.Context, heartbeat *monitoringv1alpha1.Heartbeat, statusCode int, healthy bool, message string, ) error
UpdateStatus updates the status of a Heartbeat resource with the provided values. This is the core method that all other status update methods use internally.
Parameters:
- ctx: Context for the operation
- heartbeat: The Heartbeat resource to update
- statusCode: HTTP status code from the health check (0 if not applicable)
- healthy: Whether the endpoint is considered healthy
- message: Status message describing the current state
Returns:
- error: Non-nil if the status update failed, nil otherwise