mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-20 19:09:07 +01:00
Apply suggestions from code review
Removed one letter variables Co-authored-by: Kristoffer Dalby <kradalby@kradalby.no>
This commit is contained in:
parent
2780623076
commit
55f3e07bd4
@ -25,7 +25,7 @@ func init() {
|
|||||||
nodeCmd.AddCommand(listNodesCmd)
|
nodeCmd.AddCommand(listNodesCmd)
|
||||||
nodeCmd.AddCommand(registerNodeCmd)
|
nodeCmd.AddCommand(registerNodeCmd)
|
||||||
nodeCmd.AddCommand(deleteNodeCmd)
|
nodeCmd.AddCommand(deleteNodeCmd)
|
||||||
nodeCmd.AddCommand(shareNodeCmd)
|
nodeCmd.AddCommand(shareMachineCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
var nodeCmd = &cobra.Command{
|
var nodeCmd = &cobra.Command{
|
||||||
@ -81,7 +81,7 @@ var listNodesCmd = &cobra.Command{
|
|||||||
log.Fatalf("Error initializing: %s", err)
|
log.Fatalf("Error initializing: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ns, err := h.GetNamespace(n)
|
namespace, err := h.GetNamespace(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error fetching namespace: %s", err)
|
log.Fatalf("Error fetching namespace: %s", err)
|
||||||
}
|
}
|
||||||
@ -107,7 +107,7 @@ var listNodesCmd = &cobra.Command{
|
|||||||
log.Fatalf("Error getting nodes: %s", err)
|
log.Fatalf("Error getting nodes: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
d, err := nodesToPtables(*ns, allMachines)
|
d, err := nodesToPtables(*namespace, allMachines)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error converting to table: %s", err)
|
log.Fatalf("Error converting to table: %s", err)
|
||||||
}
|
}
|
||||||
@ -163,7 +163,7 @@ var deleteNodeCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var shareNodeCmd = &cobra.Command{
|
var shareMachineCmd = &cobra.Command{
|
||||||
Use: "share ID namespace",
|
Use: "share ID namespace",
|
||||||
Short: "Shares a node from the current namespace to the specified one",
|
Short: "Shares a node from the current namespace to the specified one",
|
||||||
Args: func(cmd *cobra.Command, args []string) error {
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
@ -173,23 +173,23 @@ var shareNodeCmd = &cobra.Command{
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
n, err := cmd.Flags().GetString("namespace")
|
namespace, err := cmd.Flags().GetString("namespace")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error getting namespace: %s", err)
|
log.Fatalf("Error getting namespace: %s", err)
|
||||||
}
|
}
|
||||||
o, _ := cmd.Flags().GetString("output")
|
output, _ := cmd.Flags().GetString("output")
|
||||||
|
|
||||||
h, err := getHeadscaleApp()
|
h, err := getHeadscaleApp()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error initializing: %s", err)
|
log.Fatalf("Error initializing: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = h.GetNamespace(n)
|
_, err = h.GetNamespace(namespace)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error fetching origin namespace: %s", err)
|
log.Fatalf("Error fetching origin namespace: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
destNs, err := h.GetNamespace(args[1])
|
destinationNamespace, err := h.GetNamespace(args[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error fetching destination namespace: %s", err)
|
log.Fatalf("Error fetching destination namespace: %s", err)
|
||||||
}
|
}
|
||||||
@ -198,12 +198,12 @@ var shareNodeCmd = &cobra.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error converting ID to integer: %s", err)
|
log.Fatalf("Error converting ID to integer: %s", err)
|
||||||
}
|
}
|
||||||
m, err := h.GetMachineByID(uint64(id))
|
machine, err := h.GetMachineByID(uint64(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error getting node: %s", err)
|
log.Fatalf("Error getting node: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.AddSharedMachineToNamespace(m, destNs)
|
err = h.AddSharedMachineToNamespace(machine, destinationNamespace)
|
||||||
if strings.HasPrefix(o, "json") {
|
if strings.HasPrefix(o, "json") {
|
||||||
JsonOutput(map[string]string{"Result": "Node shared"}, err, o)
|
JsonOutput(map[string]string{"Result": "Node shared"}, err, o)
|
||||||
return
|
return
|
||||||
@ -217,10 +217,10 @@ var shareNodeCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func nodesToPtables(currNs headscale.Namespace, m []headscale.Machine) (pterm.TableData, error) {
|
func nodesToPtables(currentNamespace headscale.Namespace, machines []headscale.Machine) (pterm.TableData, error) {
|
||||||
d := pterm.TableData{{"ID", "Name", "NodeKey", "Namespace", "IP address", "Ephemeral", "Last seen", "Online"}}
|
d := pterm.TableData{{"ID", "Name", "NodeKey", "Namespace", "IP address", "Ephemeral", "Last seen", "Online"}}
|
||||||
|
|
||||||
for _, m := range m {
|
for _, machine := range machines {
|
||||||
var ephemeral bool
|
var ephemeral bool
|
||||||
if m.AuthKey != nil && m.AuthKey.Ephemeral {
|
if m.AuthKey != nil && m.AuthKey.Ephemeral {
|
||||||
ephemeral = true
|
ephemeral = true
|
||||||
@ -243,12 +243,12 @@ func nodesToPtables(currNs headscale.Namespace, m []headscale.Machine) (pterm.Ta
|
|||||||
}
|
}
|
||||||
|
|
||||||
var namespace string
|
var namespace string
|
||||||
if currNs.ID == m.NamespaceID {
|
if currentNamespace.ID == machine.NamespaceID {
|
||||||
namespace = pterm.LightMagenta(m.Namespace.Name)
|
namespace = pterm.LightMagenta(machine.Namespace.Name)
|
||||||
} else {
|
} else {
|
||||||
namespace = pterm.LightYellow(m.Namespace.Name)
|
namespace = pterm.LightYellow(machine.Namespace.Name)
|
||||||
}
|
}
|
||||||
d = append(d, []string{strconv.FormatUint(m.ID, 10), m.Name, nodeKey.ShortString(), namespace, m.IPAddress, strconv.FormatBool(ephemeral), lastSeen.Format("2006-01-02 15:04:05"), online})
|
d = append(d, []string{strconv.FormatUint(machine.ID, 10), machine.Name, nodeKey.ShortString(), namespace, machine.IPAddress, strconv.FormatBool(ephemeral), lastSeen.Format("2006-01-02 15:04:05"), online})
|
||||||
}
|
}
|
||||||
return d, nil
|
return d, nil
|
||||||
}
|
}
|
||||||
|
12
machine.go
12
machine.go
@ -98,8 +98,8 @@ func (m Machine) toNode(includeRoutes bool) (*tailcfg.Node, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, aip := range routesStr {
|
for _, routeStr := range routesStr {
|
||||||
ip, err := netaddr.ParseIPPrefix(aip)
|
ip, err := netaddr.ParseIPPrefix(routeStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -183,9 +183,9 @@ func (h *Headscale) getPeers(m Machine) (*[]*tailcfg.Node, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// We fetch here machines that are shared to the `Namespace` of the machine we are getting peers for
|
// We fetch here machines that are shared to the `Namespace` of the machine we are getting peers for
|
||||||
sharedNodes := []SharedMachine{}
|
sharedMachines := []SharedMachine{}
|
||||||
if err := h.db.Preload("Namespace").Preload("Machine").Where("namespace_id = ?",
|
if err := h.db.Preload("Namespace").Preload("Machine").Where("namespace_id = ?",
|
||||||
m.NamespaceID).Find(&sharedNodes).Error; err != nil {
|
m.NamespaceID).Find(&sharedMachines).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,8 +197,8 @@ func (h *Headscale) getPeers(m Machine) (*[]*tailcfg.Node, error) {
|
|||||||
}
|
}
|
||||||
peers = append(peers, peer)
|
peers = append(peers, peer)
|
||||||
}
|
}
|
||||||
for _, sn := range sharedNodes {
|
for _, sharedMachine := range sharedMachines {
|
||||||
peer, err := sn.Machine.toNode(false) // shared nodes do not expose their routes
|
peer, err := sharedMachine.Machine.toNode(false) // shared nodes do not expose their routes
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -99,22 +99,22 @@ func (h *Headscale) ListMachinesInNamespace(name string) (*[]Machine, error) {
|
|||||||
|
|
||||||
// ListSharedMachinesInNamespace returns all the machines that are shared to the specified namespace
|
// ListSharedMachinesInNamespace returns all the machines that are shared to the specified namespace
|
||||||
func (h *Headscale) ListSharedMachinesInNamespace(name string) (*[]Machine, error) {
|
func (h *Headscale) ListSharedMachinesInNamespace(name string) (*[]Machine, error) {
|
||||||
n, err := h.GetNamespace(name)
|
namespace, err := h.GetNamespace(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
sharedNodes := []SharedMachine{}
|
sharedMachines := []SharedMachine{}
|
||||||
if err := h.db.Preload("Namespace").Where(&SharedMachine{NamespaceID: n.ID}).Find(&sharedNodes).Error; err != nil {
|
if err := h.db.Preload("Namespace").Where(&SharedMachine{NamespaceID: namespace.ID}).Find(&sharedMachines).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
machines := []Machine{}
|
machines := []Machine{}
|
||||||
for _, sn := range sharedNodes {
|
for _, sharedMachine := range sharedMachines {
|
||||||
m, err := h.GetMachineByID(sn.MachineID) // otherwise not everything comes filled
|
machine, err := h.GetMachineByID(sharedMachine.MachineID) // otherwise not everything comes filled
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
machines = append(machines, *m)
|
machines = append(machines, *machine)
|
||||||
}
|
}
|
||||||
return &machines, nil
|
return &machines, nil
|
||||||
}
|
}
|
||||||
|
@ -21,17 +21,17 @@ func (h *Headscale) AddSharedMachineToNamespace(m *Machine, ns *Namespace) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
sn := SharedMachine{}
|
sn := SharedMachine{}
|
||||||
if err := h.db.Where("machine_id = ? AND namespace_id", m.ID, ns.ID).First(&sn).Error; err == nil {
|
if err := h.db.Where("machine_id = ? AND namespace_id", m.ID, ns.ID).First(&sharedMachine).Error; err == nil {
|
||||||
return errorNodeAlreadyShared
|
return errorMachineAlreadyShared
|
||||||
}
|
}
|
||||||
|
|
||||||
sn = SharedMachine{
|
sharedMachine = SharedMachine{
|
||||||
MachineID: m.ID,
|
MachineID: m.ID,
|
||||||
Machine: *m,
|
Machine: *m,
|
||||||
NamespaceID: ns.ID,
|
NamespaceID: ns.ID,
|
||||||
Namespace: *ns,
|
Namespace: *ns,
|
||||||
}
|
}
|
||||||
h.db.Save(&sn)
|
h.db.Save(&sharedMachine)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -183,7 +183,7 @@ func (s *Suite) TestAlreadyShared(c *check.C) {
|
|||||||
err = h.AddSharedMachineToNamespace(&m2, n1)
|
err = h.AddSharedMachineToNamespace(&m2, n1)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
err = h.AddSharedMachineToNamespace(&m2, n1)
|
err = h.AddSharedMachineToNamespace(&m2, n1)
|
||||||
c.Assert(err, check.Equals, errorNodeAlreadyShared)
|
c.Assert(err, check.Equals, errorMachineAlreadyShared)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Suite) TestDoNotIncludeRoutesOnShared(c *check.C) {
|
func (s *Suite) TestDoNotIncludeRoutesOnShared(c *check.C) {
|
||||||
|
Loading…
Reference in New Issue
Block a user