config

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: GPL-3.0, LGPL-3.0 Imports: 31 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 config finds, validates, merges and resolves the clusterctl configuration.

Each file is a stream of YAML documents. A document is decoded into a plain tree first, validated against the schema of its kind, and only then merged, so that a typo is reported at the line it was written on rather than wherever the merged value happens to be used.

Index

Constants

View Source
const (
	// EnvConfig lists the configuration files or directories to read, most
	// general first, separated the way PATH is. It replaces the search path
	// entirely.
	EnvConfig = "CLUSTERCTL_CONFIG"
	// EnvContext selects the context, as --context does.
	EnvContext = "CLUSTERCTL_CONTEXT"
	// EnvNodes holds the node set commands act on when -n is not given.
	EnvNodes = "CLUSTERCTL_NODES"
	// EnvProgress chooses how progress is shown when --progress is not
	// given.
	EnvProgress = "CLUSTERCTL_PROGRESS"
	// EnvProgressLog names the file the progress events of a command are
	// appended to when --progress-log is not given.
	EnvProgressLog = "CLUSTERCTL_PROGRESS_LOG"
)

Environment variables that steer where configuration and state live, and stand in for global flags that are not given.

Variables

This section is empty.

Functions

func CacheDir

func CacheDir() (string, error)

CacheDir returns the directory holding fetched copies of remote files and resolved group listings.

func ConfigDirs

func ConfigDirs() []string

ConfigDirs returns the directories searched for configuration when CLUSTERCTL_CONFIG is not set, most specific last.

func Decode

func Decode(data map[string]any, target any) error

Decode converts a parsed document into a typed value, rejecting keys the type does not define.

func EnvEntries added in v0.2.0

func EnvEntries(env func(string) string) []string

EnvEntries returns the files and directories CLUSTERCTL_CONFIG names, or nil when it is not set.

func ExpandEntries

func ExpandEntries(entries []string) ([]string, error)

ExpandEntries turns files and directories into the list of files to read, in the order they are layered. A directory contributes its YAML files in name order.

Every entry has to exist. These are the ones named with --config or CLUSTERCTL_CONFIG, and one that is not there is most likely misspelled: leaving it out would take its currentContext and its overrides with it, and the command would run against another context without a word.

func ExpandPath

func ExpandPath(path, base string) string

ExpandPath expands a leading ~ and makes a relative path absolute against base, which is the directory the value was configured in.

func ExpandSearchDirs added in v0.3.0

func ExpandSearchDirs(dirs []string) ([]string, error)

ExpandSearchDirs is ExpandEntries for the directories of the built-in search path, which a site may or may not use: one that does not exist is skipped.

func FormatValue

func FormatValue(v any) string

FormatValue renders a merged value the way config view prints it.

func ParseSetValue

func ParseSetValue(raw string) (any, error)

ParseSetValue reads the right hand side of a --set assignment. The value is read as YAML, so that "30s" stays a string, "8" becomes a number and "[a,b]" becomes a list.

func QuoteYAML added in v0.3.0

func QuoteYAML(s string) string

QuoteYAML writes a string as a double quoted YAML scalar. The escapes Go writes are a subset of the ones YAML reads.

func SchemaJSON

func SchemaJSON(kind string) ([]byte, error)

SchemaJSON returns the JSON Schema of one kind, ready to be written to a file an editor can use.

func SchemaKinds

func SchemaKinds() []string

SchemaKinds lists the kinds a schema can be produced for.

func SearchEntries added in v0.2.0

func SearchEntries(env func(string) string) []string

SearchEntries returns the files and directories configuration is read from when --config is not given: the entries of CLUSTERCTL_CONFIG when it is set, the configuration directories otherwise.

func SearchPath

func SearchPath(env func(string) string) ([]string, error)

SearchPath returns the files to read, in the order they are layered. The entries of CLUSTERCTL_CONFIG have to exist; the configuration directories searched without it may not.

func SecretKeys

func SecretKeys(doc *Document) []string

SecretKeys lists the keys a Secret document holds, which are readable without decrypting it.

func SecretSections added in v0.3.0

func SecretSections() []string

SecretSections are the top level mappings of a Secret document that hold its values, which is what to ask the decryption for.

func SecretValues

func SecretValues(file string, sections map[string]map[string]string) (map[string][]byte, error)

SecretValues turns the decrypted sections of a Secret document into its values by key, decoding binaryData. A value under data is used as written. No error quotes a value.

func StateDir

func StateDir() (string, error)

StateDir returns the directory holding the generated ssh configuration, the control sockets and the BMC certificate pins.

func UserConfigDir added in v0.2.0

func UserConfigDir() (string, error)

UserConfigDir returns the administrator's own configuration directory, usually ~/.config/clusterctl, which is searched after /etc/clusterctl.

func ValidateDocument

func ValidateDocument(doc *Document) error

ValidateDocument checks a document against the schema of its kind and reports every problem with the position it was written at.

Types

type Bundle

type Bundle struct {
	// Files are the files that were read, in layering order.
	Files []string
	// Documents are all documents, in the order they were read.
	Documents []*Document
	// Config is the merged Config document.
	Config v1alpha1.Config
	// Sites, Clusters, Inventories and Workstations are indexed by name. A
	// document without a name is stored under the empty string, which is
	// what a single cluster installation usually has.
	Sites        map[string]*Document
	Clusters     map[string]*Document
	Inventories  map[string]*Document
	Workstations map[string]*Document
	// Secrets are the sops encrypted Secret documents, indexed by name.
	// Their values stay encrypted until a command asks for one.
	Secrets map[string]*Document
	// contains filtered or unexported fields
}

Bundle is everything read from the configuration files, before a context selects what applies.

func Load

func Load(files []string) (*Bundle, error)

Load reads the configuration files, validates every document against the schema of its kind and indexes them by name.

Validation happens per document, before anything is merged, so a mistake is reported at the line it was written on.

func (*Bundle) Context

func (b *Bundle) Context(name string) (v1alpha1.Context, error)

Context returns the named context, or the current one when name is empty.

func (*Bundle) ContextNames

func (b *Bundle) ContextNames() []string

ContextNames lists the configured contexts in the order they were read.

func (*Bundle) Resolve

func (b *Bundle) Resolve(opts ResolveOptions) (*Resolved, error)

Resolve merges the layers in order and decodes the result.

type Document

type Document struct {
	// File is the file the document was read from.
	File string
	// Index is its position in that file, counted from zero.
	Index int
	// APIVersion and Kind are taken from the document itself.
	APIVersion string
	Kind       string
	// Data is the document as a plain tree of map[string]any, []any and
	// scalars.
	Data map[string]any
	// Positions maps a dotted path to the position its value was written at.
	Positions map[string]Origin
}

Document is one YAML document together with the positions of its values.

func ParseDocuments

func ParseDocuments(file string, data []byte) ([]*Document, error)

ParseDocuments reads a YAML stream into documents, recording where every value was written.

func (*Document) Position

func (d *Document) Position(path string) Origin

Position returns the origin of a path inside the document, falling back to the closest parent that is known and finally to the document itself.

type Origin

type Origin struct {
	// Layer names the merge layer, "site", "cluster", "context" and so on.
	Layer string `json:"layer" yaml:"layer"`
	// File is the file the value was written in, empty for values that were
	// not read from a file.
	File string `json:"file,omitempty" yaml:"file,omitempty"`
	// Line and Column locate the value in that file.
	Line   int `json:"line,omitempty" yaml:"line,omitempty"`
	Column int `json:"column,omitempty" yaml:"column,omitempty"`
}

Origin says where a value came from.

func (Origin) String

func (o Origin) String() string

String renders the origin the way config view --show-sources prints it.

type ResolveOptions

type ResolveOptions struct {
	// Context selects the context; empty takes the current one.
	Context string
	// Env reads environment variables; nil reads the process environment.
	Env func(string) string
	// Set are the --set assignments, applied last.
	Set map[string]string
	// Fanout is what --fanout gave, zero when it was not given. It is
	// applied with the flags, after --set, so that config explain reports
	// the value the commands use.
	Fanout int
}

ResolveOptions carry the layers that do not come from files.

type Resolved

type Resolved struct {
	// Context is the context that was selected.
	Context v1alpha1.Context
	// SiteName and ClusterName name the documents that were used.
	SiteName    string
	ClusterName string
	// Spec is the merged configuration.
	Spec v1alpha1.EffectiveSpec
	// Tree is the merged tree with the origin of every value.
	Tree *Tree
	// Bundle is the configuration the resolution started from.
	Bundle *Bundle
	// BaseDir is the directory relative paths in the configuration resolve
	// against, the directory of the Site document.
	BaseDir string
}

Resolved is the configuration one command runs against, together with the provenance of every value in it.

func (*Resolved) FromCommandLine added in v0.3.0

func (r *Resolved) FromCommandLine(path string) bool

FromCommandLine reports whether the value at a dotted path was given in the environment or on the command line rather than in a document, by the path itself or by a section it is part of. A relative path given there means what it means to the shell it was typed in, not what it means to the site.

type ScaffoldFile added in v0.2.0

type ScaffoldFile struct {
	// Name is the file name, without a directory.
	Name string
	// Kind and DocName describe the document the file holds.
	Kind    string
	DocName string
	// Data is the content to write.
	Data []byte
}

ScaffoldFile is one file of a new configuration.

func Scaffold added in v0.2.0

func Scaffold(opts ScaffoldOptions) ([]ScaffoldFile, error)

Scaffold renders the least configuration that resolves: a Config with one context, a Site, a Cluster and an empty NodeInventory, one document to a file, with comments saying what to fill in.

The files are loaded and resolved the way files read from disk are before they are returned, so a value that would not resolve is reported instead of written.

type ScaffoldOptions added in v0.2.0

type ScaffoldOptions struct {
	// Site names the Site document, and the NodeInventory of its nodes.
	Site string
	// Cluster names the Cluster document and the context that acts on it.
	Cluster string
	// Domain is the DNS domain of the cluster nodes, domains.hpc.
	Domain string
	// Login is the host name of the login node. Empty takes login in
	// Domain.
	Login string
	// User is the remote account of the context. Empty leaves it to ssh.
	User string
}

ScaffoldOptions are the values a new configuration is written with.

func ScaffoldDefaults added in v0.2.0

func ScaffoldDefaults() ScaffoldOptions

ScaffoldDefaults returns the values a scaffold is written with when nothing else is given: those of the example site.

type Tree

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

Tree is a configuration tree being built up layer by layer, together with the origin of every value in it.

func NewTree

func NewTree() *Tree

NewTree returns an empty tree.

func (*Tree) Data

func (t *Tree) Data() map[string]any

Data returns the merged tree. The caller must not modify it.

func (*Tree) MergeDocument

func (t *Tree) MergeDocument(layer string, doc *Document, from, into string)

MergeDocument merges the body of a document into the tree, attributing every value it sets to the given layer. Only the subtree named by prefix is taken; an empty prefix takes the whole document body.

Mappings merge key by key. Sequences and scalars replace what was there, because a list of naming rules or of protected hosts only makes sense as a whole.

func (*Tree) MergeDocumentExcept

func (t *Tree) MergeDocumentExcept(layer string, doc *Document, from, into string, except ...string)

MergeDocumentExcept is MergeDocument without the named top level keys of the merged subtree.

It keeps a document's own overrides table out of the merged tree: the overrides are applied by path, and mirroring them as data as well would show every override twice, once under its own path and once nested under the table it was written in.

func (*Tree) Origin

func (t *Tree) Origin(path string) (Origin, bool)

Origin returns where the value at a dotted path came from.

func (*Tree) Origins

func (t *Tree) Origins() map[string]Origin

Origins returns every recorded path in sorted order.

func (*Tree) Paths

func (t *Tree) Paths() []string

Paths returns the recorded paths in sorted order.

func (*Tree) SetPath

func (t *Tree) SetPath(layer, path string, value any, o Origin) error

SetPath applies one override, the way an overrides table, an environment variable or --set does. The path is dotted; the value is checked against the schema of the merged configuration before anything is changed.

A mapping value merges key by key, the way a mapping in a document does: an override written as safety: {confirmAbove: 4} changes confirmAbove and leaves the protected hosts alone.

func (*Tree) Value added in v0.3.0

func (t *Tree) Value(path string) any

Value returns the merged value at a dotted path, nil when nothing is there. A key with a dot in it, such as the group rack.R01, is found as well: at each mapping the longest run of path elements that names a key is tried first.

Directories

Path Synopsis
Package configtest writes configuration directories for the tests of the packages that read one.
Package configtest writes configuration directories for the tests of the packages that read one.

Jump to

Keyboard shortcuts

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