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
- func Fingerprint(der []byte) string
- func IsStatus(err error, code int) bool
- func PinnedClient(fingerprint string, timeout time.Duration) *http.Client
- type Code
- type Hello
- type JoinRequest
- type MemberList
- type Node
- type Peer
- func (p *Peer) As(user, role, token string) *Peer
- func (p *Peer) Call(ctx context.Context, method, path string, in, out any) error
- func (p *Peer) Download(ctx context.Context, path string, headers map[string]string) (*http.Response, error)
- func (p *Peer) Hello(ctx context.Context) (Hello, error)
- func (p *Peer) Send(ctx context.Context, method, path string, body io.Reader, size int64, out any) error
- func (p *Peer) WithCredentials(selfID, key string) *Peer
- type StatusError
- type Store
- func (s *Store) Admit(secret string, n Node, selfAddress string) ([]Node, int, error)
- func (s *Store) Clustered() bool
- func (s *Store) Credential() string
- func (s *Store) Invite() string
- func (s *Store) InviteValid(secret string) bool
- func (s *Store) Join(ctx context.Context, code Code, advertisePort string) error
- func (s *Store) Key() string
- func (s *Store) Member(id string) (Node, bool)
- func (s *Store) Members() ([]Node, int)
- func (s *Store) Peer(n Node, rt http.RoundTripper) *Peer
- func (s *Store) Rejoin(members []Node, version int) error
- func (s *Store) Remove(id string) ([]Node, int, error)
- func (s *Store) Replace(members []Node, version int) error
- func (s *Store) Self(address string) Node
- func (s *Store) SelfID() string
- func (s *Store) VerifyPeer(credential string) (Node, bool)
Constants ¶
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.
const InviteTTL = 15 * time.Minute
InviteTTL is how long a join code works.
Variables ¶
This section is empty.
Functions ¶
func Fingerprint ¶
Fingerprint is the SHA-256 of a certificate (DER), as hex.
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.
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 ¶
JoinRequest is what a joining node sends to the inviting node.
type MemberList ¶
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
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 ¶
As returns a copy of p that acts for a signed-in user: the member applies that user's role.
func (*Peer) Call ¶
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 ¶
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
WithCredentials returns a copy of p configured with peer authentication credentials.
type StatusError ¶
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 (*Store) Admit ¶
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) Credential ¶
Credential is the Authorization header value this node sends to peers.
func (*Store) InviteValid ¶ added in v1.2.1
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 ¶
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) 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) Remove ¶
Remove drops a member and returns the new list for pushing to the remaining members.
func (*Store) Replace ¶
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.