mirror of
https://github.com/juanfont/headscale.git
synced 2024-11-27 20:15:25 +00:00
Remove sharing references across the code
This commit is contained in:
parent
9411ec47c3
commit
24a8e198a1
@ -232,15 +232,6 @@ func (api headscaleV1APIServer) ListMachines(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sharedMachines, err := api.h.ListSharedMachinesInNamespace(
|
|
||||||
request.GetNamespace(),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
machines = append(machines, sharedMachines...)
|
|
||||||
|
|
||||||
response := make([]*v1.Machine, len(machines))
|
response := make([]*v1.Machine, len(machines))
|
||||||
for index, machine := range machines {
|
for index, machine := range machines {
|
||||||
response[index] = machine.toProto()
|
response[index] = machine.toProto()
|
||||||
|
107
machine.go
107
machine.go
@ -3,7 +3,6 @@ package headscale
|
|||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -255,68 +254,6 @@ func (h *Headscale) getDirectPeers(machine *Machine) (Machines, error) {
|
|||||||
return machines, nil
|
return machines, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getShared fetches machines that are shared to the `Namespace` of the machine we are getting peers for.
|
|
||||||
func (h *Headscale) getShared(machine *Machine) (Machines, error) {
|
|
||||||
log.Trace().
|
|
||||||
Caller().
|
|
||||||
Str("machine", machine.Name).
|
|
||||||
Msg("Finding shared peers")
|
|
||||||
|
|
||||||
sharedMachines := []SharedMachine{}
|
|
||||||
if err := h.db.Preload("Namespace").Preload("Machine").Preload("Machine.Namespace").Where("namespace_id = ?",
|
|
||||||
machine.NamespaceID).Find(&sharedMachines).Error; err != nil {
|
|
||||||
return Machines{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
peers := make(Machines, 0)
|
|
||||||
for _, sharedMachine := range sharedMachines {
|
|
||||||
peers = append(peers, sharedMachine.Machine)
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(peers, func(i, j int) bool { return peers[i].ID < peers[j].ID })
|
|
||||||
|
|
||||||
log.Trace().
|
|
||||||
Caller().
|
|
||||||
Str("machine", machine.Name).
|
|
||||||
Msgf("Found shared peers: %s", peers.String())
|
|
||||||
|
|
||||||
return peers, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// getSharedTo fetches the machines of the namespaces this machine is shared in.
|
|
||||||
func (h *Headscale) getSharedTo(machine *Machine) (Machines, error) {
|
|
||||||
log.Trace().
|
|
||||||
Caller().
|
|
||||||
Str("machine", machine.Name).
|
|
||||||
Msg("Finding peers in namespaces this machine is shared with")
|
|
||||||
|
|
||||||
sharedMachines := []SharedMachine{}
|
|
||||||
if err := h.db.Preload("Namespace").Preload("Machine").Preload("Machine.Namespace").Where("machine_id = ?",
|
|
||||||
machine.ID).Find(&sharedMachines).Error; err != nil {
|
|
||||||
return Machines{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
peers := make(Machines, 0)
|
|
||||||
for _, sharedMachine := range sharedMachines {
|
|
||||||
namespaceMachines, err := h.ListMachinesInNamespace(
|
|
||||||
sharedMachine.Namespace.Name,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return Machines{}, err
|
|
||||||
}
|
|
||||||
peers = append(peers, namespaceMachines...)
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(peers, func(i, j int) bool { return peers[i].ID < peers[j].ID })
|
|
||||||
|
|
||||||
log.Trace().
|
|
||||||
Caller().
|
|
||||||
Str("machine", machine.Name).
|
|
||||||
Msgf("Found peers we are shared with: %s", peers.String())
|
|
||||||
|
|
||||||
return peers, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Headscale) getPeers(machine *Machine) (Machines, error) {
|
func (h *Headscale) getPeers(machine *Machine) (Machines, error) {
|
||||||
var peers Machines
|
var peers Machines
|
||||||
var err error
|
var err error
|
||||||
@ -333,7 +270,7 @@ func (h *Headscale) getPeers(machine *Machine) (Machines, error) {
|
|||||||
}
|
}
|
||||||
peers = getFilteredByACLPeers(machines, h.aclRules, machine)
|
peers = getFilteredByACLPeers(machines, h.aclRules, machine)
|
||||||
} else {
|
} else {
|
||||||
direct, err := h.getDirectPeers(machine)
|
peers, err = h.getDirectPeers(machine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().
|
log.Error().
|
||||||
Caller().
|
Caller().
|
||||||
@ -342,28 +279,6 @@ func (h *Headscale) getPeers(machine *Machine) (Machines, error) {
|
|||||||
|
|
||||||
return Machines{}, err
|
return Machines{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
shared, err := h.getShared(machine)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().
|
|
||||||
Caller().
|
|
||||||
Err(err).
|
|
||||||
Msg("Cannot fetch peers")
|
|
||||||
|
|
||||||
return Machines{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
sharedTo, err := h.getSharedTo(machine)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().
|
|
||||||
Caller().
|
|
||||||
Err(err).
|
|
||||||
Msg("Cannot fetch peers")
|
|
||||||
|
|
||||||
return Machines{}, err
|
|
||||||
}
|
|
||||||
peers = append(direct, shared...)
|
|
||||||
peers = append(peers, sharedTo...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(peers, func(i, j int) bool { return peers[i].ID < peers[j].ID })
|
sort.Slice(peers, func(i, j int) bool { return peers[i].ID < peers[j].ID })
|
||||||
@ -474,11 +389,6 @@ func (h *Headscale) RefreshMachine(machine *Machine, expiry time.Time) {
|
|||||||
|
|
||||||
// DeleteMachine softs deletes a Machine from the database.
|
// DeleteMachine softs deletes a Machine from the database.
|
||||||
func (h *Headscale) DeleteMachine(machine *Machine) error {
|
func (h *Headscale) DeleteMachine(machine *Machine) error {
|
||||||
err := h.RemoveSharedMachineFromAllNamespaces(machine)
|
|
||||||
if err != nil && errors.Is(err, errMachineNotShared) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
machine.Registered = false
|
machine.Registered = false
|
||||||
h.db.Save(&machine) // we mark it as unregistered, just in case
|
h.db.Save(&machine) // we mark it as unregistered, just in case
|
||||||
if err := h.db.Delete(&machine).Error; err != nil {
|
if err := h.db.Delete(&machine).Error; err != nil {
|
||||||
@ -498,11 +408,6 @@ func (h *Headscale) TouchMachine(machine *Machine) error {
|
|||||||
|
|
||||||
// HardDeleteMachine hard deletes a Machine from the database.
|
// HardDeleteMachine hard deletes a Machine from the database.
|
||||||
func (h *Headscale) HardDeleteMachine(machine *Machine) error {
|
func (h *Headscale) HardDeleteMachine(machine *Machine) error {
|
||||||
err := h.RemoveSharedMachineFromAllNamespaces(machine)
|
|
||||||
if err != nil && errors.Is(err, errMachineNotShared) {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := h.db.Unscoped().Delete(&machine).Error; err != nil {
|
if err := h.db.Unscoped().Delete(&machine).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -534,17 +439,9 @@ func (h *Headscale) isOutdated(machine *Machine) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
sharedMachines, _ := h.getShared(machine)
|
|
||||||
|
|
||||||
namespaceSet := set.New(set.ThreadSafe)
|
namespaceSet := set.New(set.ThreadSafe)
|
||||||
namespaceSet.Add(machine.Namespace.Name)
|
namespaceSet.Add(machine.Namespace.Name)
|
||||||
|
|
||||||
// Check if any of our shared namespaces has updates that we have
|
|
||||||
// not propagated.
|
|
||||||
for _, sharedMachine := range sharedMachines {
|
|
||||||
namespaceSet.Add(sharedMachine.Namespace.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
namespaces := make([]string, namespaceSet.Size())
|
namespaces := make([]string, namespaceSet.Size())
|
||||||
for index, namespace := range namespaceSet.List() {
|
for index, namespace := range namespaceSet.List() {
|
||||||
if name, ok := namespace.(string); ok {
|
if name, ok := namespace.(string); ok {
|
||||||
@ -659,6 +556,8 @@ func (machine Machine) toNode(
|
|||||||
[]netaddr.IPPrefix{},
|
[]netaddr.IPPrefix{},
|
||||||
addrs...) // we append the node own IP, as it is required by the clients
|
addrs...) // we append the node own IP, as it is required by the clients
|
||||||
|
|
||||||
|
// TODO(kradalby): Needs investigation, We probably dont need this condition
|
||||||
|
// now that we dont have shared nodes
|
||||||
if includeRoutes {
|
if includeRoutes {
|
||||||
routesStr := []string{}
|
routesStr := []string{}
|
||||||
if len(machine.EnabledRoutes) != 0 {
|
if len(machine.EnabledRoutes) != 0 {
|
||||||
|
@ -143,31 +143,6 @@ func (h *Headscale) ListMachinesInNamespace(name string) ([]Machine, error) {
|
|||||||
return machines, nil
|
return machines, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListSharedMachinesInNamespace returns all the machines that are shared to the specified namespace.
|
|
||||||
func (h *Headscale) ListSharedMachinesInNamespace(name string) ([]Machine, error) {
|
|
||||||
namespace, err := h.GetNamespace(name)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
sharedMachines := []SharedMachine{}
|
|
||||||
if err := h.db.Preload("Namespace").Where(&SharedMachine{NamespaceID: namespace.ID}).Find(&sharedMachines).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
machines := []Machine{}
|
|
||||||
for _, sharedMachine := range sharedMachines {
|
|
||||||
machine, err := h.GetMachineByID(
|
|
||||||
sharedMachine.MachineID,
|
|
||||||
) // otherwise not everything comes filled
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
machines = append(machines, *machine)
|
|
||||||
}
|
|
||||||
|
|
||||||
return machines, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetMachineNamespace assigns a Machine to a namespace.
|
// SetMachineNamespace assigns a Machine to a namespace.
|
||||||
func (h *Headscale) SetMachineNamespace(machine *Machine, namespaceName string) error {
|
func (h *Headscale) SetMachineNamespace(machine *Machine, namespaceName string) error {
|
||||||
namespace, err := h.GetNamespace(namespaceName)
|
namespace, err := h.GetNamespace(namespaceName)
|
||||||
|
Loading…
Reference in New Issue
Block a user