github.com/Cloud-Foundations/Dominator@v0.3.4/fleetmanager/hypervisors/get.go (about) 1 package hypervisors 2 3 import ( 4 "errors" 5 "net" 6 7 "github.com/Cloud-Foundations/Dominator/fleetmanager/topology" 8 "github.com/Cloud-Foundations/Dominator/lib/tags/tagmatcher" 9 fm_proto "github.com/Cloud-Foundations/Dominator/proto/fleetmanager" 10 hyper_proto "github.com/Cloud-Foundations/Dominator/proto/hypervisor" 11 ) 12 13 func (h *hypervisorType) makeProtoHypervisor( 14 includeVMs bool) fm_proto.Hypervisor { 15 h.mutex.RLock() 16 defer h.mutex.RUnlock() 17 protoHypervisor := fm_proto.Hypervisor{ 18 Machine: *h.machine, 19 } 20 protoHypervisor.AllocatedMilliCPUs = h.allocatedMilliCPUs 21 protoHypervisor.AllocatedMemory = h.allocatedMemory 22 protoHypervisor.AllocatedVolumeBytes = h.allocatedVolumeBytes 23 protoHypervisor.Machine.MemoryInMiB = h.memoryInMiB 24 protoHypervisor.NumCPUs = h.numCPUs 25 protoHypervisor.TotalVolumeBytes = h.totalVolumeBytes 26 if includeVMs { 27 protoHypervisor.VMs = make([]hyper_proto.VmInfo, 0, len(h.vms)) 28 for _, vm := range h.vms { 29 protoHypervisor.VMs = append(protoHypervisor.VMs, vm.VmInfo) 30 } 31 } 32 return protoHypervisor 33 } 34 35 func (m *Manager) getLockedHypervisor(name string, 36 writeLock bool) (*hypervisorType, error) { 37 m.mutex.RLock() 38 defer m.mutex.RUnlock() 39 if hypervisor, ok := m.hypervisors[name]; !ok { 40 return nil, errors.New("Hypervisor not found") 41 } else { 42 if writeLock { 43 hypervisor.mutex.Lock() 44 } else { 45 hypervisor.mutex.RLock() 46 } 47 return hypervisor, nil 48 } 49 } 50 51 func (m *Manager) getHypervisorForVm(ipAddr net.IP) (string, error) { 52 addr := ipAddr.String() 53 m.mutex.RLock() 54 defer m.mutex.RUnlock() 55 if vm, ok := m.vms[addr]; !ok { 56 return "", errors.New("VM not found") 57 } else { 58 return vm.hypervisor.machine.Hostname, nil 59 } 60 } 61 62 func (m *Manager) getHypervisorsInLocation( 63 request fm_proto.GetHypervisorsInLocationRequest) ( 64 fm_proto.GetHypervisorsInLocationResponse, error) { 65 showFilter := showOK 66 if request.IncludeUnhealthy { 67 showFilter = showConnected 68 } 69 hypervisors, err := m.listHypervisors(request.Location, showFilter, 70 request.SubnetId, tagmatcher.New(request.HypervisorTagsToMatch, false)) 71 if err != nil { 72 return fm_proto.GetHypervisorsInLocationResponse{}, err 73 } 74 protoHypervisors := make([]fm_proto.Hypervisor, 0, len(hypervisors)) 75 for _, hypervisor := range hypervisors { 76 protoHypervisors = append(protoHypervisors, 77 hypervisor.makeProtoHypervisor(request.IncludeVMs)) 78 } 79 return fm_proto.GetHypervisorsInLocationResponse{ 80 Hypervisors: protoHypervisors, 81 }, nil 82 } 83 84 func (m *Manager) getMachineInfo(request fm_proto.GetMachineInfoRequest) ( 85 fm_proto.Machine, error) { 86 if !*manageHypervisors && !request.IgnoreMissingLocalTags { 87 return fm_proto.Machine{}, 88 errors.New("this is a read-only Fleet Manager: full machine information is not available") 89 } 90 hypervisor, err := m.getLockedHypervisor(request.Hostname, false) 91 if err != nil { 92 return fm_proto.Machine{}, err 93 } else { 94 defer hypervisor.mutex.RUnlock() 95 return *hypervisor.getMachineLocked(), nil 96 } 97 } 98 99 func (m *Manager) getTopology() (*topology.Topology, error) { 100 m.mutex.RLock() 101 defer m.mutex.RUnlock() 102 if m.topology == nil { 103 return nil, errors.New("no topology available") 104 } 105 return m.topology, nil 106 }