You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
205 lines
7.8 KiB
Go
205 lines
7.8 KiB
Go
//go:build libvirt
|
|
|
|
package hypervisor
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.luoli.live/root/virt/agent/operation"
|
|
"git.luoli.live/root/virt/agent/publicguard"
|
|
"git.luoli.live/root/virt/agent/publicnetwork"
|
|
"libvirt.org/go/libvirt"
|
|
"libvirt.org/go/libvirtxml"
|
|
)
|
|
|
|
func (l *Libvirt) acquirePublicGuardTransition(ctx context.Context) (*publicguard.Lease, error) {
|
|
if l == nil || l.publicGuard == nil {
|
|
return nil, NewError(ErrUnavailable, "public_guard_unavailable", "public network guard is unavailable", nil)
|
|
}
|
|
lease, err := l.publicGuard.Acquire(ctx)
|
|
if err != nil {
|
|
return nil, NewError(ErrUnavailable, "public_guard_unavailable", "public network guard transition lease is unavailable", err)
|
|
}
|
|
return lease, nil
|
|
}
|
|
|
|
func (l *Libvirt) reconcilePublicGuardLease(ctx context.Context, lease *publicguard.Lease,
|
|
override *publicguard.Binding) error {
|
|
if lease == nil {
|
|
return NewError(ErrUnavailable, "public_guard_unavailable", "public network guard transition lease is unavailable", nil)
|
|
}
|
|
return lease.Reconcile(ctx, func(projectionCtx context.Context) ([]publicguard.Binding, error) {
|
|
return l.publicGuardProjection(projectionCtx, override)
|
|
})
|
|
}
|
|
|
|
// startManagedDomain is the only production path allowed to call
|
|
// virDomainCreate. It projects the exact managed-domain inventory before the
|
|
// link can come up and converges it again under the same transition lease after
|
|
// the mutation, including an ambiguous start response.
|
|
func (l *Libvirt) startManagedDomain(ctx context.Context, domain *libvirt.Domain,
|
|
action string) (resultErr error) {
|
|
lease, err := l.acquirePublicGuardTransition(ctx)
|
|
if err != nil {
|
|
// The transition lease is acquired before inspecting or starting the
|
|
// domain. Failure here proves Domain.Create was never dispatched.
|
|
return operation.MarkNotApplied(err)
|
|
}
|
|
defer lease.Release()
|
|
defer func() {
|
|
reconcileCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), publicGuardTransitionReconcileTimeout)
|
|
defer cancel()
|
|
if reconcileErr := l.reconcilePublicGuardLease(reconcileCtx, lease, nil); reconcileErr != nil {
|
|
resultErr = finishManagedDomainStart(resultErr, reconcileErr)
|
|
}
|
|
}()
|
|
return l.startManagedDomainWithLease(ctx, lease, domain, action)
|
|
}
|
|
|
|
func finishManagedDomainStart(resultErr, reconcileErr error) error {
|
|
if reconcileErr == nil {
|
|
return resultErr
|
|
}
|
|
public := NewError(
|
|
ErrUnavailable, "public_guard_reconcile_failed",
|
|
"public network guard could not be reconciled after domain start", reconcileErr,
|
|
)
|
|
if operation.IsNotApplied(resultErr) && !operation.IsUncertain(resultErr) {
|
|
// Domain.Create was never dispatched, so a failed best-effort guard
|
|
// convergence cannot make the power effect ambiguous. Do not expose a
|
|
// cleanup deadline/transport marker through the error chain: Journal
|
|
// intentionally gives uncertainty precedence over NotApplied.
|
|
diagnostic := errors.New(public.Error() + ": " + reconcileErr.Error())
|
|
return operation.MarkNotApplied(errors.Join(resultErr, diagnostic))
|
|
}
|
|
return operation.MarkUncertain(errors.Join(resultErr, public))
|
|
}
|
|
|
|
const publicGuardTransitionReconcileTimeout = 30 * time.Second
|
|
|
|
func (l *Libvirt) startManagedDomainWithLease(ctx context.Context, lease *publicguard.Lease,
|
|
domain *libvirt.Domain, action string) error {
|
|
if domain == nil {
|
|
return operation.MarkNotApplied(NewError(ErrInvalid, "invalid_domain", "managed domain handle is required", nil))
|
|
}
|
|
document, err := domain.GetXMLDesc(libvirt.DOMAIN_XML_INACTIVE)
|
|
if err != nil {
|
|
return operation.MarkNotApplied(translateLibvirtError("read managed domain before "+action, err))
|
|
}
|
|
if _, _, managed, inspectErr := inspectManagedDomain(document); inspectErr != nil || !managed {
|
|
if inspectErr != nil {
|
|
return operation.MarkNotApplied(inspectErr)
|
|
}
|
|
return operation.MarkNotApplied(NewError(ErrConflict, "unmanaged_domain", "domain is not managed by this panel", nil))
|
|
}
|
|
if err := l.reconcilePublicGuardLease(ctx, lease, nil); err != nil {
|
|
return operation.MarkNotApplied(NewError(ErrUnavailable, "public_guard_apply_failed", "public network guard is not ready for domain start", err))
|
|
}
|
|
if err := domain.Create(); err != nil {
|
|
return operation.MarkUncertain(mutationLibvirtError(action, err))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func publicGuardBinding(uuid, mac string, public publicnetwork.Intent) (publicguard.Binding, error) {
|
|
ipv6, err := publicAddress(public.IPv6.AddressCIDR, 6)
|
|
if err != nil {
|
|
return publicguard.Binding{}, err
|
|
}
|
|
linkLocal, err := publicnetwork.LinkLocalIPv6(mac)
|
|
if err != nil {
|
|
return publicguard.Binding{}, NewError(ErrConflict, "public_guard_identity_invalid", "managed VM public link-local guard identity is invalid", err)
|
|
}
|
|
value, err := publicguard.Normalize(publicguard.Binding{
|
|
Interface: publicInterfaceTarget(uuid), MAC: strings.ToLower(mac), IPv6: ipv6,
|
|
LinkLocalIPv6: linkLocal.String(),
|
|
})
|
|
if err != nil {
|
|
return publicguard.Binding{}, NewError(ErrConflict, "public_guard_identity_invalid", "managed VM public network guard identity is invalid", err)
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func publicGuardBindingFromDomain(definition *libvirtxml.Domain,
|
|
metadata *domainMetadataXML) (publicguard.Binding, error) {
|
|
if !managedNetworkLayoutMatches(definition, metadata) {
|
|
return publicguard.Binding{}, NewError(ErrConflict, "network_layout_changed", "managed VM network layout no longer matches its metadata", nil)
|
|
}
|
|
public := metadata.PublicNetwork.publicMust()
|
|
index, found := managedPublicInterfaceIndex(definition, networkFilterName(definition.UUID), public)
|
|
if !found {
|
|
return publicguard.Binding{}, NewError(ErrConflict, "network_layout_changed", "managed VM public interface is missing", nil)
|
|
}
|
|
iface := definition.Devices.Interfaces[index]
|
|
if iface.MAC == nil || iface.Target == nil || iface.Target.Dev != publicInterfaceTarget(definition.UUID) {
|
|
return publicguard.Binding{}, NewError(ErrConflict, "public_guard_identity_changed", "managed VM public guard binding no longer matches its domain", nil)
|
|
}
|
|
return publicGuardBinding(definition.UUID, iface.MAC.Address, public)
|
|
}
|
|
|
|
func (l *Libvirt) reconcilePublicGuard(ctx context.Context, override *publicguard.Binding) error {
|
|
if l == nil || l.publicGuard == nil {
|
|
return NewError(ErrUnavailable, "public_guard_unavailable", "public network guard is unavailable", nil)
|
|
}
|
|
return l.publicGuard.Reconcile(ctx, func(projectionCtx context.Context) ([]publicguard.Binding, error) {
|
|
return l.publicGuardProjection(projectionCtx, override)
|
|
})
|
|
}
|
|
|
|
func (l *Libvirt) publicGuardProjection(ctx context.Context,
|
|
override *publicguard.Binding) ([]publicguard.Binding, error) {
|
|
domains, err := l.conn.ListAllDomains(0)
|
|
if err != nil {
|
|
return nil, translateLibvirtError("list domains for public network guard", err)
|
|
}
|
|
defer func() {
|
|
for index := range domains {
|
|
_ = domains[index].Free()
|
|
}
|
|
}()
|
|
result := make([]publicguard.Binding, 0, len(domains)+1)
|
|
for index := range domains {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
domain := &domains[index]
|
|
document, xmlErr := domain.GetXMLDesc(libvirt.DOMAIN_XML_INACTIVE)
|
|
if xmlErr != nil {
|
|
return nil, translateLibvirtError("read domain for public network guard", xmlErr)
|
|
}
|
|
definition, metadata, managed, inspectErr := inspectManagedDomain(document)
|
|
if inspectErr != nil {
|
|
return nil, inspectErr
|
|
}
|
|
if !managed {
|
|
continue
|
|
}
|
|
binding, bindingErr := publicGuardBindingFromDomain(definition, metadata)
|
|
if bindingErr != nil {
|
|
return nil, bindingErr
|
|
}
|
|
result = append(result, binding)
|
|
}
|
|
if override == nil {
|
|
return result, nil
|
|
}
|
|
normalized, err := publicguard.Normalize(*override)
|
|
if err != nil {
|
|
return nil, NewError(ErrConflict, "public_guard_identity_invalid", "public network guard override is invalid", err)
|
|
}
|
|
replaced := false
|
|
for index := range result {
|
|
if result[index].Interface == normalized.Interface {
|
|
result[index] = normalized
|
|
replaced = true
|
|
}
|
|
}
|
|
if !replaced {
|
|
result = append(result, normalized)
|
|
}
|
|
return result, nil
|
|
}
|