cluster

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: AGPL-3.0 Imports: 23 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 cluster lets several virtomird instances act as one (ADR-0020). Every host keeps running a complete VirtoMir; the only shared state is the member list, which members push to each other. Peers talk HTTPS pinned to each other's certificate fingerprint and prove who they are with a per-node key (only its SHA-256 is shared).

Index

Constants

View Source
const (
	HeaderUser  = "X-VirtoMir-User"
	HeaderRole  = "X-VirtoMir-Role"
	HeaderToken = "X-VirtoMir-Token"
	HeaderVia   = "X-VirtoMir-Via"
	// AuthScheme prefixes the peer credential "<node id>.<key>".
	AuthScheme = "VirtoMirPeer "
)

Headers a peer sets on forwarded requests. They are only believed together with a valid peer Authorization; on any other request they are ignored.

View Source
const InviteTTL = 15 * time.Minute

InviteTTL is how long a join code works.

Variables

This section is empty.

Functions

func Fingerprint

func Fingerprint(der []byte) string

Fingerprint is the SHA-256 of a certificate (DER), as hex.

func IsStatus

func IsStatus(err error, code int) bool

IsStatus reports whether err is a peer answer with the given HTTP status.

func PinnedClient

func PinnedClient(fingerprint string, timeout time.Duration) *http.Client

PinnedClient returns an HTTPS client that trusts exactly one certificate (by fingerprint), whatever its name or issuer. That is how members recognize each other's self-signed certificates without a CA.

Types

type Code

type Code struct {
	Address     string `json:"a"`
	Fingerprint string `json:"f"`
	Secret      string `json:"s"`
}

Code is what a join code carries: where the inviting node is, how to recognize it, and the one-time secret.

func ParseCode

func ParseCode(s string) (Code, error)

ParseCode reads a join code as shown by "Add host" (surrounding whitespace is fine).

func (Code) String

func (c Code) String() string

type Hello

type Hello struct {
	ID      string `json:"id"`
	Name    string `json:"name"`
	Version string `json:"version"`
}

Hello is a node's answer to /api/v1/cluster/peer/hello: who it is and which version runs.

type JoinRequest

type JoinRequest struct {
	Secret string `json:"secret"`
	Node   Node   `json:"node"`
}

JoinRequest is what a joining node sends to the inviting node.

type MemberList

type MemberList struct {
	Version int    `json:"version"`
	Members []Node `json:"members"`
}

MemberList is a versioned member list (join answers, pushes, GET /cluster).

type Node

type Node struct {
	ID          string `json:"id"`
	Name        string `json:"name"`
	Address     string `json:"address"`     // https://host:port, as the other members reach it
	Fingerprint string `json:"fingerprint"` // SHA-256 of its TLS certificate, hex
	KeyHash     string `json:"keyHash"`     // SHA-256 of its peer key, hex
}

Node is one member of the cluster.

type Peer

type Peer struct {
	Node Node
	// contains filtered or unexported fields
}

Peer calls another member's API.

func NewPeer added in v0.9.0

func NewPeer(n Node, client *http.Client, auth ...string) *Peer

NewPeer creates a Peer pointing to node using the provided HTTP client (useful for tests). Optional auth arguments can specify selfID and key (and optionally user).

func (*Peer) As

func (p *Peer) As(user, role, token string) *Peer

As returns a copy of p that acts for a signed-in user: the member applies that user's role.

func (*Peer) Call

func (p *Peer) Call(ctx context.Context, method, path string, in, out any) error

Call sends a JSON request to the peer and decodes a JSON answer into out (may be nil).

func (*Peer) Download added in v0.9.0

func (p *Peer) Download(ctx context.Context, path string, headers map[string]string) (*http.Response, error)

Download sends an HTTP GET request to the peer at path, applying optional extra headers (e.g. Range) and peer authorization credentials. It returns the active HTTP response; the caller must close resp.Body.

func (*Peer) Hello

func (p *Peer) Hello(ctx context.Context) (Hello, error)

Hello asks the peer who it is. It carries no credential: it is also how a host checks a newcomer that does not know it yet.

func (*Peer) Send

func (p *Peer) Send(ctx context.Context, method, path string, body io.Reader, size int64, out any) error

Send streams body (size bytes, e.g. a disk image) to the peer and decodes the answer.

func (*Peer) WithCredentials added in v0.9.0

func (p *Peer) WithCredentials(selfID, key string) *Peer

WithCredentials returns a copy of p configured with peer authentication credentials.

type StatusError

type StatusError struct {
	Code    int
	Message string
}

StatusError is an answer from a peer with an HTTP error status.

func (*StatusError) Error

func (e *StatusError) Error() string

type Store

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

func Open

func Open(path, name, fingerprint string) (*Store, error)

Open loads the cluster state, creating this node's identity on first use.

func (*Store) Admit

func (s *Store) Admit(secret string, n Node, selfAddress string) ([]Node, int, error)

Admit uses up an invite secret and adds n. selfAddress is how n reached this node, which becomes this node's address if it had none yet (first join of a standalone host).

func (*Store) Clustered

func (s *Store) Clustered() bool

Clustered reports whether this node has peers.

func (*Store) Credential

func (s *Store) Credential() string

Credential is the Authorization header value this node sends to peers.

func (*Store) Invite

func (s *Store) Invite() string

Invite creates a one-time secret for a join code.

func (*Store) InviteValid added in v1.2.1

func (s *Store) InviteValid(secret string) bool

InviteValid reports whether secret is a live invite, without using it up: a join is checked against it before this node reaches out to the joining one.

func (*Store) Join

func (s *Store) Join(ctx context.Context, code Code, advertisePort string) error

Join asks the node behind code to admit self. advertisePort is this node's listen port; the IP is the one this host used to reach the other node, so it is routable from there.

func (*Store) Key

func (s *Store) Key() string

Key is this node's secret peer key, sent to peers in the Authorization header.

func (*Store) Member

func (s *Store) Member(id string) (Node, bool)

Member looks up a node by ID.

func (*Store) Members

func (s *Store) Members() ([]Node, int)

Members returns the member list (this node included) and its version.

func (*Store) Peer

func (s *Store) Peer(n Node, rt http.RoundTripper) *Peer

Peer returns a client for member n authenticated as this node. rt must be pinned to n's certificate (PinnedClient(n.Fingerprint, 0).Transport); callers keep one per member.

func (*Store) Rejoin

func (s *Store) Rejoin(members []Node, version int) error

Rejoin is called on a joining node with the list its new cluster sent back.

func (*Store) Remove

func (s *Store) Remove(id string) ([]Node, int, error)

Remove drops a member and returns the new list for pushing to the remaining members.

func (*Store) Replace

func (s *Store) Replace(members []Node, version int) error

Replace installs a member list pushed by a peer, unless it is older than ours. A list without this node means it was removed: it becomes standalone again.

func (*Store) Self

func (s *Store) Self(address string) Node

Self describes this node with the given address.

func (*Store) SelfID

func (s *Store) SelfID() string

SelfID is this node's stable ID.

func (*Store) VerifyPeer

func (s *Store) VerifyPeer(credential string) (Node, bool)

VerifyPeer checks "<node id>.<key>" from a peer's Authorization header.

Jump to

Keyboard shortcuts

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