sidecar

package
v1.10.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package sidecar implements the sidecar logic that runs alongside each Valkey pod. It polls the local Valkey instance, patches the pod's role label, and exposes a readiness endpoint.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(ctx context.Context, cfg Config) error

Run starts the sidecar polling loop and health server. It blocks until the context is cancelled (SIGTERM/SIGINT), then runs the graceful drain handler before shutting down.

Types

type Config

type Config struct {
	PollInterval time.Duration
	ValkeyAddr   string
	HealthAddr   string
	PodName      string
	PodNamespace string
	Password     string

	// TLS settings.
	TLSEnabled bool
	TLSCACert  string
	TLSCert    string
	TLSKey     string

	// Drain/failover settings.
	SentinelEnabled     bool
	SentinelMonitor     string
	SentinelAddrs       string // comma-separated sentinel addresses
	SentinelDisableAuth bool   // sentinel does not require password from clients
	HeadlessSvc         string // headless service FQDN for replica discovery
	Replicas            int    // number of Valkey replicas in the StatefulSet
	FailoverTimeout     time.Duration
}

Config holds the sidecar configuration parsed from flags/env vars.

type DrainHandler

type DrainHandler struct {
	// contains filtered or unexported fields
}

DrainHandler handles graceful failover on SIGTERM. When the pod is the master, it patches the label to "draining", triggers a failover (via Sentinel or manually), and waits for the role to change.

func NewDrainHandlerWithDeps

func NewDrainHandlerWithDeps(
	detector RoleDetector,
	patcher PodPatcher,
	clientFactory ValkeyClientFactory,
	sentinelClientFactory ValkeyClientFactory,
	podName, podNamespace string,
	sentinelEnabled bool,
	sentinelMonitor string,
	sentinelAddrs []string,
	headlessSvc string,
	replicas int,
	valkeyPort string,
) *DrainHandler

NewDrainHandlerWithDeps creates a DrainHandler with injected dependencies (for testing).

func (*DrainHandler) Handle

func (d *DrainHandler) Handle(ctx context.Context) error

Handle processes a SIGTERM signal. It detects the current role and triggers a graceful failover if the pod is the master. Returns nil when safe to exit.

type HealthServer

type HealthServer struct {
	// contains filtered or unexported fields
}

HealthServer exposes a readiness endpoint for the sidecar. It returns 503 until the sidecar has successfully detected the Valkey role, then returns 200 on /readyz.

func NewHealthServer

func NewHealthServer(addr string) *HealthServer

NewHealthServer creates a new health server listening on the given address.

func (*HealthServer) IsReady

func (h *HealthServer) IsReady() bool

IsReady reports whether the sidecar is ready.

func (*HealthServer) ListenAndServe

func (h *HealthServer) ListenAndServe() error

ListenAndServe starts the HTTP server. Blocks until the server is shut down.

func (*HealthServer) SetReady

func (h *HealthServer) SetReady()

SetReady marks the sidecar as ready (role has been detected).

func (*HealthServer) Shutdown

func (h *HealthServer) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the server.

type Labeler

type Labeler struct {
	// contains filtered or unexported fields
}

Labeler polls the local Valkey instance and patches the pod's role label.

func NewLabeler

func NewLabeler(cfg Config) (*Labeler, error)

NewLabeler creates a new Labeler from the sidecar config.

func NewLabelerWithDeps

func NewLabelerWithDeps(detector RoleDetector, patcher PodPatcher, podName, podNamespace string, pollInterval time.Duration) *Labeler

NewLabelerWithDeps creates a Labeler with injected dependencies (for testing).

func (*Labeler) Run

func (l *Labeler) Run(ctx context.Context, health *HealthServer)

Run starts the polling loop. It blocks until the context is done. It notifies the health server once a role is successfully detected.

func (*Labeler) SetSentinelCrossCheck added in v1.9.0

func (l *Labeler) SetSentinelCrossCheck(querier SentinelMasterQuerier, monitor, myFQDN string)

SetSentinelCrossCheck enables Sentinel cross-checking for the labeler. When enabled, the labeler verifies with Sentinel before labeling a pod as master. If Sentinel reports a different master, the pod is labeled as replica instead.

type PodPatcher

type PodPatcher interface {
	// PatchLabel patches the given label key to the given value on the pod.
	PatchLabel(ctx context.Context, namespace, name, labelKey, labelValue string) error
}

PodPatcher patches labels on a pod. This interface allows mocking in tests.

type RoleDetector

type RoleDetector interface {
	// DetectRole returns "master", "replica", or an error.
	DetectRole() (string, error)
}

RoleDetector detects the current Valkey replication role. This interface allows mocking in tests.

type SentinelMasterQuerier added in v1.9.0

type SentinelMasterQuerier interface {
	// GetMasterAddress returns the hostname/FQDN of the current master as known by Sentinel.
	GetMasterAddress(monitor string) (string, error)
}

SentinelMasterQuerier queries Sentinel for the current master address. This interface allows mocking in tests.

type ValkeyClientFactory

type ValkeyClientFactory interface {
	NewClient(addr string) ValkeyCommander
}

ValkeyClientFactory creates Valkey clients for arbitrary addresses. This interface enables mocking in tests.

type ValkeyCommander

type ValkeyCommander interface {
	InfoReplication() (*valkeyclient.ReplicationInfo, error)
	SentinelFailover(name string) error
	ReplicaOf(host, port string) error
	Ping() error
}

ValkeyCommander provides the Valkey commands needed by the drain handler for sentinel and manual failover.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL