engine

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultStepTimeout = 60 * time.Second

DefaultStepTimeout is the maximum time a single inject or probe step can take before being cancelled. This prevents indefinite hangs from stuck API calls.

View Source
const MaxHistorySize = 1000

MaxHistorySize limits the number of event records kept in memory. This prevents unbounded memory growth during long-running daemon sessions.

Variables

View Source
var ActionHandlers = map[string]ActionHandler{
	"stop":         actionStop,
	"restart":      actionRestart,
	"pause":        actionPause,
	"delay":        actionDelay,
	"loss":         actionLoss,
	"limit_cpu":    actionLimitCPU,
	"limit_memory": actionLimitMemory,
}

Functions

func DiscoverComposeServices

func DiscoverComposeServices(cwd string) ([]string, string, error)

func DiscoverK8sTargets

func DiscoverK8sTargets(namespace string) ([]string, string, error)

DiscoverK8sTargets connects to Kubernetes and returns a list of target application names based on Deployments and StatefulSets in the current namespace.

func EvaluateSteadyState

func EvaluateSteadyState(probes []config.SteadyStateProbe) error

EvaluateSteadyState checks the steady state probes against their sources.

func GenerateDefaultChaosConfig

func GenerateDefaultChaosConfig(services []string, outPath string) error

func GenerateTopologyTree

func GenerateTopologyTree(dir string) (*pterm.TreeNode, error)

GenerateTopologyTree analyzes the compose file and generates a pterm.TreeNode representing the blast radius based on networks and depends_on.

Types

type ActionHandler

type ActionHandler func(ctx context.Context, client ContainerRuntime, target string, spec config.ActionSpec) (*ContainerInfo, error)

type ChaosEngine

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

func NewChaosEngine

func NewChaosEngine(cfg *config.ChaosConfig, runtimeType string, onEvent func(utils.EventRecord), logger *utils.ChaosLogger) *ChaosEngine

func (*ChaosEngine) Start

func (e *ChaosEngine) Start() error

func (*ChaosEngine) Status

func (e *ChaosEngine) Status() EngineStatus

func (*ChaosEngine) Stop

func (e *ChaosEngine) Stop()

type ComposeFile

type ComposeFile struct {
	Services map[string]ComposeService `yaml:"services"`
}

type ComposeService

type ComposeService struct {
	Image       string             `yaml:"image"`
	Deploy      *DeployConfig      `yaml:"deploy"`
	Restart     string             `yaml:"restart"`
	HealthCheck *HealthCheckConfig `yaml:"healthcheck"`
	Privileged  bool               `yaml:"privileged"`
	Networks    interface{}        `yaml:"networks"` // Can be list or map
}

ComposeService represents a subset of docker-compose service configuration used by the doctor for analyzing resilience.

type ContainerInfo

type ContainerInfo struct {
	ID     string            `json:"id"`
	Name   string            `json:"name"`
	Image  string            `json:"image"`
	Status string            `json:"status"`
	Ports  map[string]string `json:"ports"`
}

ContainerInfo represents the standardized output of a container's state, regardless of the underlying runtime (Docker, Kubernetes, etc.)

func Dispatch

func Dispatch(ctx context.Context, action config.ActionSpec, client ContainerRuntime, target string) (*ContainerInfo, error)

type ContainerRuntime

type ContainerRuntime interface {
	StopContainer(ctx context.Context, name string, timeout int) (*ContainerInfo, error)
	RestartContainer(ctx context.Context, name string, timeout int) (*ContainerInfo, error)
	PauseContainer(ctx context.Context, name string) (*ContainerInfo, error)
	UnpauseContainer(ctx context.Context, name string) (*ContainerInfo, error)
	GetContainerPID(ctx context.Context, name string) (int, error)
	UpdateContainerResources(ctx context.Context, name string, cpuQuota int64, cpuPeriod int64, memLimit int64) (*ContainerInfo, error)
	ScheduleResourceRestore(ctx context.Context, target string, duration int)
	InjectNetworkDelay(ctx context.Context, target string, latencyMs int, jitterMs int, duration *int) error
	InjectNetworkLoss(ctx context.Context, target string, lossPercent int, duration *int) error
	ExecCommand(ctx context.Context, name string, cmd []string) (int, error)
	ListContainers(ctx context.Context, all bool) ([]ContainerInfo, error)
	CleanupAll(ctx context.Context)
	Close()
}

ContainerRuntime defines the interface for interacting with container orchestration platforms. By abstracting the runtime, Entropy can support Docker, Kubernetes, and other platforms seamlessly.

All methods accept a context.Context as the first argument. Callers should pass a context with an appropriate deadline or cancellation signal to ensure operations don't block indefinitely. Use context.Background() only at the top-level entry points (CLI commands, daemon loop).

func GetRuntime

func GetRuntime(runtimeType string, allowedTargets []string) (ContainerRuntime, error)

GetRuntime creates and returns a ContainerRuntime instance based on the provided string.

type DeployConfig

type DeployConfig struct {
	Replicas  int             `yaml:"replicas"`
	Resources *ResourceConfig `yaml:"resources"`
}

type DockerClient

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

func NewDockerClient

func NewDockerClient(allowedTargets []string) (*DockerClient, error)

func (*DockerClient) CleanupAll added in v1.1.0

func (d *DockerClient) CleanupAll(ctx context.Context)

func (*DockerClient) Close

func (d *DockerClient) Close()

func (*DockerClient) ExecCommand

func (d *DockerClient) ExecCommand(ctx context.Context, name string, cmd []string) (int, error)

func (*DockerClient) GetContainerPID

func (d *DockerClient) GetContainerPID(ctx context.Context, name string) (int, error)

func (*DockerClient) InjectNetworkDelay

func (d *DockerClient) InjectNetworkDelay(ctx context.Context, target string, latencyMs int, jitterMs int, duration *int) error

func (*DockerClient) InjectNetworkLoss

func (d *DockerClient) InjectNetworkLoss(ctx context.Context, target string, lossPercent int, duration *int) error

func (*DockerClient) ListContainers

func (d *DockerClient) ListContainers(ctx context.Context, all bool) ([]ContainerInfo, error)

func (*DockerClient) PauseContainer

func (d *DockerClient) PauseContainer(ctx context.Context, name string) (*ContainerInfo, error)

func (*DockerClient) RestartContainer

func (d *DockerClient) RestartContainer(ctx context.Context, name string, timeout int) (*ContainerInfo, error)

func (*DockerClient) ScheduleResourceRestore added in v1.1.0

func (d *DockerClient) ScheduleResourceRestore(ctx context.Context, target string, duration int)

func (*DockerClient) StopContainer

func (d *DockerClient) StopContainer(ctx context.Context, name string, timeout int) (*ContainerInfo, error)

func (*DockerClient) UnpauseContainer

func (d *DockerClient) UnpauseContainer(ctx context.Context, name string) (*ContainerInfo, error)

func (*DockerClient) UpdateContainerResources

func (d *DockerClient) UpdateContainerResources(ctx context.Context, name string, cpuQuota int64, cpuPeriod int64, memLimit int64) (*ContainerInfo, error)

type DoctorIssue

type DoctorIssue struct {
	Severity string // "CRITICAL", "WARNING"
	Category string // "SPOF", "RESOURCES", "RECOVERY", "OBSERVABILITY", "SECURITY"
	Message  string
}

DoctorIssue represents a single resilience issue found in a service

type DoctorResult

type DoctorResult struct {
	ServiceName string
	Issues      []DoctorIssue
}

DoctorResult represents the analysis result for a specific service

func AnalyzeKubernetes

func AnalyzeKubernetes(namespace string) ([]DoctorResult, error)

AnalyzeKubernetes connects to the cluster and analyzes Deployments in the given namespace for resilience anti-patterns (SPOF, Resource Limits, Probes, Privileged mode).

func AnalyzeTopology

func AnalyzeTopology(dir string) ([]DoctorResult, error)

AnalyzeTopology reads docker-compose.yml from the given directory and analyzes it for resilience anti-patterns.

type EngineStatus

type EngineStatus struct {
	Running           bool
	Config            *config.ChaosConfig
	CycleCount        int
	DownContainers    []string
	LastEvent         *utils.EventRecord
	History           []utils.EventRecord
	LastInjectionTime time.Time
	CooldownRemaining float64
}

type HealthCheckConfig

type HealthCheckConfig struct {
	Test    interface{} `yaml:"test"` // can be string or list
	Disable bool        `yaml:"disable"`
}

type KubernetesClient

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

KubernetesClient implements the ContainerRuntime interface for Kubernetes clusters.

Supported actions:

  • stop → deletes the pod (ReplicaSet/Deployment recreates it)
  • restart → same as stop (delete + recreate by controller)
  • delay → injects tc/netem via an ephemeral netshoot sidecar
  • loss → injects tc/netem via an ephemeral netshoot sidecar
  • exec → runs a command in the first container of the pod
  • limit_cpu → patches the pod's container resource limits via JSON patch

Unsupported actions (return clear errors):

  • pause → requires CRI-level SIGSTOP, not available via K8s API
  • unpause → same as pause
  • GetContainerPID → not available via standard K8s API

func NewKubernetesClient

func NewKubernetesClient(allowedTargets []string) (*KubernetesClient, error)

func (*KubernetesClient) CleanupAll added in v1.1.0

func (k *KubernetesClient) CleanupAll(ctx context.Context)

func (*KubernetesClient) Close

func (k *KubernetesClient) Close()

func (*KubernetesClient) ExecCommand

func (k *KubernetesClient) ExecCommand(ctx context.Context, name string, cmd []string) (int, error)

func (*KubernetesClient) GetContainerPID

func (k *KubernetesClient) GetContainerPID(ctx context.Context, name string) (int, error)

GetContainerPID is not supported in Kubernetes via the standard API.

func (*KubernetesClient) InjectNetworkDelay

func (k *KubernetesClient) InjectNetworkDelay(ctx context.Context, target string, latencyMs int, jitterMs int, duration *int) error

func (*KubernetesClient) InjectNetworkLoss

func (k *KubernetesClient) InjectNetworkLoss(ctx context.Context, target string, lossPercent int, duration *int) error

func (*KubernetesClient) ListContainers

func (k *KubernetesClient) ListContainers(ctx context.Context, all bool) ([]ContainerInfo, error)

func (*KubernetesClient) PauseContainer

func (k *KubernetesClient) PauseContainer(ctx context.Context, name string) (*ContainerInfo, error)

PauseContainer is not supported in Kubernetes via the standard API. Pausing a container requires sending SIGSTOP to the container process via the CRI (containerd/cri-o), which is not exposed through the Kubernetes API server.

func (*KubernetesClient) RestartContainer

func (k *KubernetesClient) RestartContainer(ctx context.Context, name string, timeout int) (*ContainerInfo, error)

func (*KubernetesClient) ScheduleResourceRestore added in v1.1.0

func (k *KubernetesClient) ScheduleResourceRestore(ctx context.Context, target string, duration int)

func (*KubernetesClient) StopContainer

func (k *KubernetesClient) StopContainer(ctx context.Context, name string, timeout int) (*ContainerInfo, error)

func (*KubernetesClient) UnpauseContainer

func (k *KubernetesClient) UnpauseContainer(ctx context.Context, name string) (*ContainerInfo, error)

UnpauseContainer is not supported in Kubernetes via the standard API.

func (*KubernetesClient) UpdateContainerResources

func (k *KubernetesClient) UpdateContainerResources(ctx context.Context, name string, cpuQuota int64, cpuPeriod int64, memLimit int64) (*ContainerInfo, error)

UpdateContainerResources applies resource limits to the first container of a pod via JSON patch. Note: cpuPeriod is ignored for K8s (uses millicores via cpuQuota/1000). Only memory and CPU limits are patched; requests are left unchanged.

type NetworkChaosManager

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

func NewNetworkChaosManager

func NewNetworkChaosManager() *NetworkChaosManager

func (*NetworkChaosManager) Clear

func (m *NetworkChaosManager) Clear(name string)

Clear removes active network chaos rules from a specific container.

func (*NetworkChaosManager) ClearAll

func (m *NetworkChaosManager) ClearAll()

ClearAll removes all active network chaos rules across all containers.

func (*NetworkChaosManager) InjectDelay

func (m *NetworkChaosManager) InjectDelay(ctx context.Context, runtime ContainerRuntime, name string, latencyMs int, jitterMs int, duration *int) error

InjectDelay injects network latency into the target container using tc/netem via the container runtime exec API.

func (*NetworkChaosManager) InjectLoss

func (m *NetworkChaosManager) InjectLoss(ctx context.Context, runtime ContainerRuntime, name string, percent int, duration *int) error

InjectLoss injects packet loss into the target container using tc/netem via the container runtime exec API.

type ProbeResult

type ProbeResult struct {
	Success bool
	Message string
}

func RunProbe

func RunProbe(spec *config.ProbeSpec, runtime ContainerRuntime) ProbeResult

func RunProbeWithContext added in v1.1.0

func RunProbeWithContext(ctx context.Context, spec *config.ProbeSpec, runtime ContainerRuntime) ProbeResult

type PrometheusResponse

type PrometheusResponse struct {
	Status string `json:"status"`
	Data   struct {
		ResultType string `json:"resultType"`
		Result     []struct {
			Value []interface{} `json:"value"`
		} `json:"result"`
	} `json:"data"`
}

type ResourceChaosManager

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

func NewResourceChaosManager

func NewResourceChaosManager() *ResourceChaosManager

func (*ResourceChaosManager) ClearAll

func (m *ResourceChaosManager) ClearAll()

func (*ResourceChaosManager) ScheduleRestore

func (m *ResourceChaosManager) ScheduleRestore(client ContainerRuntime, target string, duration int)

type ResourceConfig

type ResourceConfig struct {
	Limits *ResourceLimits `yaml:"limits"`
}

type ResourceLimits

type ResourceLimits struct {
	CPUs   string `yaml:"cpus"`
	Memory string `yaml:"memory"`
}

type ScenarioResult

type ScenarioResult struct {
	Success           bool
	ProbesPassed      int
	ProbesTotal       int
	SteadyStatePassed int
	SteadyStateTotal  int
	ExecutedSteps     int
	TotalSteps        int
	Error             string
	SteadyStateError  string
}

type ScenarioRunner

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

func NewScenarioRunner

func NewScenarioRunner(cfg *config.ScenarioConfig, runtimeType string, logCb func(string)) *ScenarioRunner

func (*ScenarioRunner) RevertAll

func (r *ScenarioRunner) RevertAll()

func (*ScenarioRunner) Run

func (r *ScenarioRunner) Run() ScenarioResult

type TopologyNode

type TopologyNode struct {
	Name      string
	Networks  []string
	DependsOn []string
}

Jump to

Keyboard shortcuts

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