base

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package base provides a shared framework for managing containerd-based service components. Components like etcd, victorialogs, and victoriametrics embed BaseComponent to share common functionality for container lifecycle, exit monitoring, and auto-restart.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseComponent

type BaseComponent struct {
	Log       *slog.Logger
	CC        *containerd.Client
	Namespace string
	DataPath  string

	// ComponentName is used in log messages to identify the component
	ComponentName string

	// Callbacks for component-specific behavior
	CreateTask    TaskCreator
	GetReadyPort  ReadyPortGetter
	RestartPolicy RestartPolicy
	ReadyConfig   ReadyCheckConfig
	// contains filtered or unexported fields
}

BaseComponent provides shared functionality for containerd-based service components.

func NewBaseComponent

func NewBaseComponent(log *slog.Logger, cc *containerd.Client, namespace, dataPath, componentName string) *BaseComponent

NewBaseComponent creates a new BaseComponent with default policies.

func (*BaseComponent) CleanupExistingContainer

func (b *BaseComponent) CleanupExistingContainer(ctx context.Context, container containerd.Container)

CleanupExistingContainer stops the task and deletes a container during restart scenarios.

func (*BaseComponent) DeleteContainerWithRetry

func (b *BaseComponent) DeleteContainerWithRetry(ctx context.Context)

DeleteContainerWithRetry is the exported version for use by components.

func (*BaseComponent) GetContainer

func (b *BaseComponent) GetContainer() containerd.Container

GetContainer returns the current container reference.

func (*BaseComponent) GetTask

func (b *BaseComponent) GetTask() containerd.Task

GetTask returns the current task reference.

func (*BaseComponent) IfRunning

func (b *BaseComponent) IfRunning(fn func() string) string

IfRunning executes the given function while holding the state lock, but only if the component is currently running. Returns empty string if not running. This is useful for safely reading component state that should only be accessed when the component is running.

func (*BaseComponent) IsRunning

func (b *BaseComponent) IsRunning() bool

IsRunning returns whether the component is currently running.

func (*BaseComponent) LockOp

func (b *BaseComponent) LockOp()

LockOp acquires the operation mutex for serializing major operations.

func (*BaseComponent) SetContainer

func (b *BaseComponent) SetContainer(container containerd.Container)

SetContainer sets the container reference.

func (*BaseComponent) SetRunning

func (b *BaseComponent) SetRunning(running bool)

SetRunning sets the running state.

func (*BaseComponent) SetTask

func (b *BaseComponent) SetTask(task containerd.Task)

SetTask sets the task reference and marks the component as running.

func (*BaseComponent) StartExitMonitor

func (b *BaseComponent) StartExitMonitor(ctx context.Context)

StartExitMonitor starts a goroutine that monitors for unexpected task exits. If a monitor is already running, this is a no-op.

func (*BaseComponent) Stop

func (b *BaseComponent) Stop(ctx context.Context) error

Stop stops the component, including the exit monitor, task, and container. This method acquires the operation mutex.

func (*BaseComponent) StopTask

func (b *BaseComponent) StopTask(ctx context.Context, task containerd.Task)

StopTask is the exported version of stopTask for use by components.

func (*BaseComponent) UnlockOp

func (b *BaseComponent) UnlockOp()

UnlockOp releases the operation mutex.

func (*BaseComponent) WaitForReady

func (b *BaseComponent) WaitForReady(ctx context.Context, host string, port int) error

WaitForReady waits until the component is ready by checking TCP connectivity.

type ReadyCheckConfig

type ReadyCheckConfig struct {
	MaxAttempts int           // Maximum number of attempts (default: 30)
	DialTimeout time.Duration // Timeout for each dial attempt (default: 2s)
	Interval    time.Duration // Time between attempts (default: 2s)
}

ReadyCheckConfig configures the readiness check behavior.

func DefaultReadyCheckConfig

func DefaultReadyCheckConfig() ReadyCheckConfig

DefaultReadyCheckConfig returns the default readiness check configuration.

type ReadyPortGetter

type ReadyPortGetter func() int

ReadyPortGetter is a callback that returns the port to check for readiness.

type RestartPolicy

type RestartPolicy struct {
	MaxRestarts int           // Maximum restart attempts before giving up (0 = unlimited)
	BackoffBase time.Duration // Base delay for exponential backoff (default: 2s)
	BackoffMax  time.Duration // Maximum backoff delay (default: 60s)
	ResetWindow time.Duration // Time after which restart count resets (default: 5m)
}

RestartPolicy configures the auto-restart behavior for a component.

func AggressiveRestartPolicy

func AggressiveRestartPolicy() RestartPolicy

AggressiveRestartPolicy returns a restart policy with shorter backoff times, suitable for critical components like etcd that the system cannot run without.

func DefaultRestartPolicy

func DefaultRestartPolicy() RestartPolicy

DefaultRestartPolicy returns the default restart policy with unlimited restarts.

type TaskCreator

type TaskCreator func(ctx context.Context, container containerd.Container) (containerd.Task, error)

TaskCreator is a callback that creates a new containerd task for the component. Each component implements this to provide component-specific task creation (e.g., logging options).

Jump to

Keyboard shortcuts

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