safety

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: GPL-3.0, LGPL-3.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package safety gates the commands that change or destroy something.

The shell toolkit had no preview, no confirmation and no dry run, and one tool scheduled a reboot when it was asked for its help text. Every command that powers off, reinstalls, drains or overwrites goes through here first.

Index

Constants

View Source
const EffectAnnotation = "clusterctl.effect"

EffectAnnotation is the key under which a command records its effect.

Variables

View Source
var ErrDryRun = errDryRun{}

ErrDryRun is returned by Confirm when the gate is in dry run mode. A command treats it as "stop here, successfully".

Functions

func IsDryRun

func IsDryRun(err error) bool

IsDryRun reports whether an error is the dry run signal.

Types

type Action

type Action struct {
	// Verb is what is being done, "power off" or "reinstall".
	Verb string
	// Targets are the hosts it happens to.
	Targets *nodeset.NodeSet
	// Detail is an extra line shown before the prompt, such as the command
	// that will run.
	Detail string
}

Action describes what is about to happen, for the prompt and the preview.

type Effect

type Effect string

Effect says what running a command does to the site. The command tree records it on every command, so that anything driving the tree on behalf of someone else, such as the MCP server, can tell a question from a change without keeping its own list.

const (
	// EffectRead only looks. It may contact hosts, but changes nothing on
	// them.
	EffectRead Effect = "read"
	// EffectChange changes or destroys something, or may do so depending on
	// its arguments.
	EffectChange Effect = "change"
	// EffectInteractive needs a terminal, opens a program or keeps running
	// until it is stopped.
	EffectInteractive Effect = "interactive"
)

func EffectOf

func EffectOf(annotations map[string]string) Effect

EffectOf reads the effect a command recorded in its annotations. An unmarked command counts as a change, so that forgetting to classify one never makes it look harmless.

type Gate

type Gate struct {
	// Protected are the hosts no destructive command touches unless Force
	// is set.
	Protected *nodeset.NodeSet
	// ConfirmAbove asks for the host count to be typed back when an action
	// targets more than this many hosts.
	ConfirmAbove int
	// AssumeYes answers every prompt with yes, for -y and for scripts.
	AssumeYes bool
	// Force allows a protected host to be touched.
	Force bool
	// DryRun reports what would happen and does nothing.
	DryRun bool
	// Interactive says whether a prompt can be answered at all. When it is
	// false and AssumeYes is not set, an action is refused rather than
	// silently carried out.
	Interactive bool

	// In and Out are the terminal the prompt uses.
	In  io.Reader
	Out io.Writer
}

Gate decides whether a destructive action may go ahead.

func NewGate

func NewGate(spec v1alpha1.SafetySpec) (*Gate, error)

NewGate builds a gate from the safety configuration of a site.

func (*Gate) Check

func (g *Gate) Check(a Action) error

Check refuses an action that touches a protected host, unless the gate was forced.

func (*Gate) Confirm

func (g *Gate) Confirm(a Action) error

Confirm runs the full gate: the protected host check, then the preview and the prompt. It returns nil when the action may proceed.

func (*Gate) Preview

func (g *Gate) Preview(a Action) (Preview, error)

Preview runs the checks of the gate and describes the question it would ask, without asking it.

type Preview

type Preview struct {
	// Verb is what is being done.
	Verb string `json:"verb"`
	// Targets is the folded node set the action touches.
	Targets string `json:"targets"`
	// Count is how many hosts that is.
	Count int `json:"count"`
	// Detail is the extra line shown before the question.
	Detail string `json:"detail,omitempty"`
	// CountRequired says that a yes is not enough: the host count has to be
	// read off the preview and given back.
	CountRequired bool `json:"countRequired"`
	// ConfirmAbove is the host count above which the count is required.
	ConfirmAbove int `json:"confirmAbove,omitempty"`
}

Preview is an action that passed the checks and waits for an answer. It carries everything the question is asked from, so that a caller without a terminal can put the same question some other way and have the answer judged by the same rule.

func (Preview) Accept

func (p Preview) Accept(answer string) error

Accept judges an answer to the question: the host count when it is required, a yes otherwise. It returns nil when the action may proceed.

func (Preview) Question

func (p Preview) Question() string

Question is what the administrator is asked.

func (Preview) Summary

func (p Preview) Summary() string

Summary says what is about to happen, for example "drain 3 hosts: exe[1-3]".

Jump to

Keyboard shortcuts

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