mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-11 18:54:25 +00:00
A big bunch of golang-lint fixes
This commit is contained in:
parent
358ed64b98
commit
b5f1ae3d90
4
db.go
4
db.go
@ -106,6 +106,7 @@ func (h *Headscale) initDB() error {
|
|||||||
Err(err).
|
Err(err).
|
||||||
Str("enabled_route", prefix.String()).
|
Str("enabled_route", prefix.String()).
|
||||||
Msg("Error parsing enabled_route")
|
Msg("Error parsing enabled_route")
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,6 +115,7 @@ func (h *Headscale) initDB() error {
|
|||||||
log.Info().
|
log.Info().
|
||||||
Str("enabled_route", prefix.String()).
|
Str("enabled_route", prefix.String()).
|
||||||
Msg("Route already migrated to new table, skipping")
|
Msg("Route already migrated to new table, skipping")
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -335,6 +337,7 @@ func (i *IPPrefix) Scan(destination interface{}) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*i = IPPrefix(prefix)
|
*i = IPPrefix(prefix)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("%w: unexpected data type %T", ErrCannotParsePrefix, destination)
|
return fmt.Errorf("%w: unexpected data type %T", ErrCannotParsePrefix, destination)
|
||||||
@ -344,6 +347,7 @@ func (i *IPPrefix) Scan(destination interface{}) error {
|
|||||||
// Value return json value, implement driver.Valuer interface.
|
// Value return json value, implement driver.Valuer interface.
|
||||||
func (i IPPrefix) Value() (driver.Value, error) {
|
func (i IPPrefix) Value() (driver.Value, error) {
|
||||||
prefixStr := netip.Prefix(i).String()
|
prefixStr := netip.Prefix(i).String()
|
||||||
|
|
||||||
return prefixStr, nil
|
return prefixStr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -937,6 +937,7 @@ func (h *Headscale) GetAdvertisedRoutes(machine *Machine) ([]netip.Prefix, error
|
|||||||
Err(err).
|
Err(err).
|
||||||
Str("machine", machine.Hostname).
|
Str("machine", machine.Hostname).
|
||||||
Msg("Could not get advertised routes for machine")
|
Msg("Could not get advertised routes for machine")
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -962,6 +963,7 @@ func (h *Headscale) GetEnabledRoutes(machine *Machine) ([]netip.Prefix, error) {
|
|||||||
Err(err).
|
Err(err).
|
||||||
Str("machine", machine.Hostname).
|
Str("machine", machine.Hostname).
|
||||||
Msg("Could not get enabled routes for machine")
|
Msg("Could not get enabled routes for machine")
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -982,6 +984,7 @@ func (h *Headscale) IsRoutesEnabled(machine *Machine, routeStr string) bool {
|
|||||||
enabledRoutes, err := h.GetEnabledRoutes(machine)
|
enabledRoutes, err := h.GetEnabledRoutes(machine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Could not get enabled routes")
|
log.Error().Err(err).Msg("Could not get enabled routes")
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1122,12 +1125,14 @@ func (h *Headscale) RoutesToProto(machine *Machine) *v1.Routes {
|
|||||||
availableRoutes, err := h.GetAdvertisedRoutes(machine)
|
availableRoutes, err := h.GetAdvertisedRoutes(machine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Could not get advertised routes")
|
log.Error().Err(err).Msg("Could not get advertised routes")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
enabledRoutes, err := h.GetEnabledRoutes(machine)
|
enabledRoutes, err := h.GetEnabledRoutes(machine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Could not get enabled routes")
|
log.Error().Err(err).Msg("Could not get enabled routes")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1159,7 +1159,9 @@ func (s *Suite) TestAutoApproveRoutes(c *check.C) {
|
|||||||
machine0ByID, err := app.GetMachineByID(0)
|
machine0ByID, err := app.GetMachineByID(0)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
app.EnableAutoApprovedRoutes(machine0ByID)
|
err = app.EnableAutoApprovedRoutes(machine0ByID)
|
||||||
|
c.Assert(err, check.IsNil)
|
||||||
|
|
||||||
enabledRoutes, err := app.GetEnabledRoutes(machine0ByID)
|
enabledRoutes, err := app.GetEnabledRoutes(machine0ByID)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, check.IsNil)
|
||||||
c.Assert(enabledRoutes, check.HasLen, 3)
|
c.Assert(enabledRoutes, check.HasLen, 3)
|
||||||
|
@ -53,7 +53,15 @@ func (h *Headscale) handlePollCommon(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update routes with peer information
|
// update routes with peer information
|
||||||
h.EnableAutoApprovedRoutes(machine)
|
err = h.EnableAutoApprovedRoutes(machine)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().
|
||||||
|
Caller().
|
||||||
|
Bool("noise", isNoise).
|
||||||
|
Str("machine", machine.Hostname).
|
||||||
|
Err(err).
|
||||||
|
Msg("Error running auto approved routes")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// From Tailscale client:
|
// From Tailscale client:
|
||||||
|
24
routes.go
24
routes.go
@ -1,6 +1,7 @@
|
|||||||
package headscale
|
package headscale
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
|
||||||
@ -44,10 +45,11 @@ func (rs Routes) toPrefixes() []netip.Prefix {
|
|||||||
for i, r := range rs {
|
for i, r := range rs {
|
||||||
prefixes[i] = netip.Prefix(r.Prefix)
|
prefixes[i] = netip.Prefix(r.Prefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
return prefixes
|
return prefixes
|
||||||
}
|
}
|
||||||
|
|
||||||
// isUniquePrefix returns if there is another machine providing the same route already
|
// isUniquePrefix returns if there is another machine providing the same route already.
|
||||||
func (h *Headscale) isUniquePrefix(route Route) bool {
|
func (h *Headscale) isUniquePrefix(route Route) bool {
|
||||||
var count int64
|
var count int64
|
||||||
h.db.
|
h.db.
|
||||||
@ -56,6 +58,7 @@ func (h *Headscale) isUniquePrefix(route Route) bool {
|
|||||||
route.Prefix,
|
route.Prefix,
|
||||||
route.MachineID,
|
route.MachineID,
|
||||||
true, true).Count(&count)
|
true, true).Count(&count)
|
||||||
|
|
||||||
return count == 0
|
return count == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,11 +68,11 @@ func (h *Headscale) getPrimaryRoute(prefix netip.Prefix) (*Route, error) {
|
|||||||
Preload("Machine").
|
Preload("Machine").
|
||||||
Where("prefix = ? AND advertised = ? AND enabled = ? AND is_primary = ?", IPPrefix(prefix), true, true, true).
|
Where("prefix = ? AND advertised = ? AND enabled = ? AND is_primary = ?", IPPrefix(prefix), true, true, true).
|
||||||
First(&route).Error
|
First(&route).Error
|
||||||
if err != nil && err != gorm.ErrRecordNotFound {
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err == gorm.ErrRecordNotFound {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
return nil, gorm.ErrRecordNotFound
|
return nil, gorm.ErrRecordNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +80,7 @@ func (h *Headscale) getPrimaryRoute(prefix netip.Prefix) (*Route, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getMachinePrimaryRoutes returns the routes that are enabled and marked as primary (for subnet failover)
|
// getMachinePrimaryRoutes returns the routes that are enabled and marked as primary (for subnet failover)
|
||||||
// Exit nodes are not considered for this, as they are never marked as Primary
|
// Exit nodes are not considered for this, as they are never marked as Primary.
|
||||||
func (h *Headscale) getMachinePrimaryRoutes(m *Machine) ([]Route, error) {
|
func (h *Headscale) getMachinePrimaryRoutes(m *Machine) ([]Route, error) {
|
||||||
var routes []Route
|
var routes []Route
|
||||||
err := h.db.
|
err := h.db.
|
||||||
@ -113,8 +116,7 @@ func (h *Headscale) processMachineRoutes(machine *Machine) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
advertisedRoutes[netip.Prefix(route.Prefix)] = true
|
advertisedRoutes[netip.Prefix(route.Prefix)] = true
|
||||||
} else {
|
} else if route.Advertised {
|
||||||
if route.Advertised {
|
|
||||||
route.Advertised = false
|
route.Advertised = false
|
||||||
route.Enabled = false
|
route.Enabled = false
|
||||||
err := h.db.Save(&route).Error
|
err := h.db.Save(&route).Error
|
||||||
@ -123,7 +125,6 @@ func (h *Headscale) processMachineRoutes(machine *Machine) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for prefix, exists := range advertisedRoutes {
|
for prefix, exists := range advertisedRoutes {
|
||||||
if !exists {
|
if !exists {
|
||||||
@ -150,7 +151,7 @@ func (h *Headscale) handlePrimarySubnetFailover() error {
|
|||||||
Preload("Machine").
|
Preload("Machine").
|
||||||
Where("advertised = ? AND enabled = ?", true, true).
|
Where("advertised = ? AND enabled = ?", true, true).
|
||||||
Find(&routes).Error
|
Find(&routes).Error
|
||||||
if err != nil && err != gorm.ErrRecordNotFound {
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
log.Error().Err(err).Msg("error getting routes")
|
log.Error().Err(err).Msg("error getting routes")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,7 +162,7 @@ func (h *Headscale) handlePrimarySubnetFailover() error {
|
|||||||
|
|
||||||
if !route.IsPrimary {
|
if !route.IsPrimary {
|
||||||
_, err := h.getPrimaryRoute(netip.Prefix(route.Prefix))
|
_, err := h.getPrimaryRoute(netip.Prefix(route.Prefix))
|
||||||
if h.isUniquePrefix(route) || err == gorm.ErrRecordNotFound {
|
if h.isUniquePrefix(route) || errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
route.IsPrimary = true
|
route.IsPrimary = true
|
||||||
err := h.db.Save(&route).Error
|
err := h.db.Save(&route).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -169,6 +170,7 @@ func (h *Headscale) handlePrimarySubnetFailover() error {
|
|||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -193,7 +195,7 @@ func (h *Headscale) handlePrimarySubnetFailover() error {
|
|||||||
route.MachineID,
|
route.MachineID,
|
||||||
true, true).
|
true, true).
|
||||||
Find(&newPrimaryRoutes).Error
|
Find(&newPrimaryRoutes).Error
|
||||||
if err != nil && err != gorm.ErrRecordNotFound {
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
log.Error().Err(err).Msg("error finding new primary route")
|
log.Error().Err(err).Msg("error finding new primary route")
|
||||||
|
|
||||||
return err
|
return err
|
||||||
@ -203,6 +205,7 @@ func (h *Headscale) handlePrimarySubnetFailover() error {
|
|||||||
for _, r := range newPrimaryRoutes {
|
for _, r := range newPrimaryRoutes {
|
||||||
if r.Machine.isOnline() {
|
if r.Machine.isOnline() {
|
||||||
newPrimaryRoute = &r
|
newPrimaryRoute = &r
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -212,6 +215,7 @@ func (h *Headscale) handlePrimarySubnetFailover() error {
|
|||||||
Str("machine", route.Machine.Hostname).
|
Str("machine", route.Machine.Hostname).
|
||||||
Str("prefix", netip.Prefix(route.Prefix).String()).
|
Str("prefix", netip.Prefix(route.Prefix).String()).
|
||||||
Msgf("no alternative primary route found")
|
Msgf("no alternative primary route found")
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user