github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/fleetmanager/hypervisors/api.go (about)

     1  package hypervisors
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  	"sync"
     7  	"time"
     8  
     9  	"github.com/Cloud-Foundations/Dominator/fleetmanager/topology"
    10  	"github.com/Cloud-Foundations/Dominator/lib/log"
    11  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    12  	"github.com/Cloud-Foundations/Dominator/lib/tags"
    13  	fm_proto "github.com/Cloud-Foundations/Dominator/proto/fleetmanager"
    14  	hyper_proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor"
    15  )
    16  
    17  const (
    18  	probeStatusNotYetProbed probeStatus = iota
    19  	probeStatusConnected
    20  	probeStatusAccessDenied
    21  	probeStatusNoSrpc
    22  	probeStatusNoService
    23  	probeStatusConnectionRefused
    24  	probeStatusUnreachable
    25  	probeStatusOff
    26  )
    27  
    28  type hypervisorType struct {
    29  	logger             log.DebugLogger
    30  	receiveChannel     chan struct{}
    31  	mutex              sync.RWMutex
    32  	cachedSerialNumber string
    33  	conn               *srpc.Conn
    34  	deleteScheduled    bool
    35  	healthStatus       string
    36  	lastIpmiProbe      time.Time
    37  	localTags          tags.Tags
    38  	location           string
    39  	machine            *fm_proto.Machine
    40  	migratingVms       map[string]*vmInfoType // Key: VM IP address.
    41  	ownerUsers         map[string]struct{}
    42  	probeStatus        probeStatus
    43  	serialNumber       string
    44  	subnets            []hyper_proto.Subnet
    45  	vms                map[string]*vmInfoType // Key: VM IP address.
    46  }
    47  
    48  type ipStorer interface {
    49  	AddIPsForHypervisor(hypervisor net.IP, addrs []net.IP) error
    50  	CheckIpIsRegistered(addr net.IP) (bool, error)
    51  	GetHypervisorForIp(addr net.IP) (net.IP, error)
    52  	GetIPsForHypervisor(hypervisor net.IP) ([]net.IP, error)
    53  	SetIPsForHypervisor(hypervisor net.IP, addrs []net.IP) error
    54  	UnregisterHypervisor(hypervisor net.IP) error
    55  }
    56  
    57  type locationType struct {
    58  	notifiers map[<-chan fm_proto.Update]chan<- fm_proto.Update
    59  }
    60  
    61  type Manager struct {
    62  	ipmiPasswordFile string
    63  	ipmiUsername     string
    64  	logger           log.DebugLogger
    65  	storer           Storer
    66  	mutex            sync.RWMutex               // Protect everything below.
    67  	allocatingIPs    map[string]struct{}        // Key: VM IP address.
    68  	hypervisors      map[string]*hypervisorType // Key: hypervisor machine name.
    69  	locations        map[string]*locationType   // Key: location.
    70  	migratingIPs     map[string]struct{}        // Key: VM IP address.
    71  	notifiers        map[<-chan fm_proto.Update]*locationType
    72  	topology         *topology.Topology
    73  	subnets          map[string]*subnetType // Key: Gateway IP.
    74  	vms              map[string]*vmInfoType // Key: VM IP address.
    75  }
    76  
    77  type probeStatus uint
    78  
    79  type serialStorer interface {
    80  	ReadMachineSerialNumber(hypervisor net.IP) (string, error)
    81  	WriteMachineSerialNumber(hypervisor net.IP, serialNumber string) error
    82  }
    83  
    84  type StartOptions struct {
    85  	IpmiPasswordFile string
    86  	IpmiUsername     string
    87  	Logger           log.DebugLogger
    88  	Storer           Storer
    89  }
    90  
    91  type Storer interface {
    92  	ipStorer
    93  	serialStorer
    94  	tagsStorer
    95  	vmStorer
    96  }
    97  
    98  type subnetType struct {
    99  	subnet  *topology.Subnet
   100  	startIp net.IP
   101  	stopIp  net.IP
   102  	nextIp  net.IP
   103  }
   104  
   105  type tagsStorer interface {
   106  	ReadMachineTags(hypervisor net.IP) (tags.Tags, error)
   107  	WriteMachineTags(hypervisor net.IP, tgs tags.Tags) error
   108  }
   109  
   110  type vmInfoType struct {
   111  	ipAddr string
   112  	hyper_proto.VmInfo
   113  	hypervisor *hypervisorType
   114  }
   115  
   116  type vmStorer interface {
   117  	DeleteVm(hypervisor net.IP, ipAddr string) error
   118  	ListVMs(hypervisor net.IP) ([]string, error)
   119  	ReadVm(hypervisor net.IP, ipAddr string) (*hyper_proto.VmInfo, error)
   120  	WriteVm(hypervisor net.IP, ipAddr string, vmInfo hyper_proto.VmInfo) error
   121  }
   122  
   123  func New(startOptions StartOptions) (*Manager, error) {
   124  	return newManager(startOptions)
   125  }
   126  
   127  func (m *Manager) ChangeMachineTags(hostname string,
   128  	authInfo *srpc.AuthInformation, tgs tags.Tags) error {
   129  	return m.changeMachineTags(hostname, authInfo, tgs)
   130  }
   131  
   132  func (m *Manager) CloseUpdateChannel(channel <-chan fm_proto.Update) {
   133  	m.closeUpdateChannel(channel)
   134  }
   135  
   136  func (m *Manager) GetHypervisorForVm(ipAddr net.IP) (string, error) {
   137  	return m.getHypervisorForVm(ipAddr)
   138  }
   139  
   140  func (m *Manager) GetMachineInfo(hostname string) (fm_proto.Machine, error) {
   141  	return m.getMachineInfo(hostname)
   142  }
   143  
   144  func (m *Manager) GetTopology() (*topology.Topology, error) {
   145  	return m.getTopology()
   146  }
   147  
   148  func (m *Manager) ListHypervisorsInLocation(
   149  	request fm_proto.ListHypervisorsInLocationRequest) ([]string, error) {
   150  	return m.listHypervisorsInLocation(request)
   151  }
   152  
   153  func (m *Manager) ListLocations(dirname string) ([]string, error) {
   154  	return m.listLocations(dirname)
   155  }
   156  
   157  func (m *Manager) ListVMsInLocation(dirname string) ([]net.IP, error) {
   158  	return m.listVMsInLocation(dirname)
   159  }
   160  
   161  func (m *Manager) MakeUpdateChannel(location string) <-chan fm_proto.Update {
   162  	return m.makeUpdateChannel(location)
   163  }
   164  
   165  func (m *Manager) MoveIpAddresses(hostname string, ipAddresses []net.IP) error {
   166  	return m.moveIpAddresses(hostname, ipAddresses)
   167  }
   168  
   169  func (m *Manager) PowerOnMachine(hostname string,
   170  	authInfo *srpc.AuthInformation) error {
   171  	return m.powerOnMachine(hostname, authInfo)
   172  }
   173  
   174  func (m *Manager) WriteHtml(writer io.Writer) {
   175  	m.writeHtml(writer)
   176  }
   177  
   178  func (m *Manager) UpdateTopology(t *topology.Topology) {
   179  	m.updateTopology(t)
   180  }