reformat code (#2019)

* reformat code

This is mostly an automated change with `make lint`.
I had to manually please golangci-lint in routes_test because of a short
variable name.

* fix start -> strategy which was wrongly corrected by linter
This commit is contained in:
greizgh 2024-07-22 08:56:00 +02:00 committed by GitHub
parent ca47d6f353
commit 8571513e3c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 56 additions and 65 deletions

View file

@ -4,10 +4,9 @@ import (
"io" "io"
"os" "os"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
) )
func init() { func init() {

View file

@ -7,16 +7,15 @@ import (
"fmt" "fmt"
"os" "os"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/credentials/insecure"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
) )
const ( const (

View file

@ -23,6 +23,16 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware" grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpcRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" grpcRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/juanfont/headscale"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol/db"
"github.com/juanfont/headscale/hscontrol/derp"
derpServer "github.com/juanfont/headscale/hscontrol/derp/server"
"github.com/juanfont/headscale/hscontrol/mapper"
"github.com/juanfont/headscale/hscontrol/notifier"
"github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
zerolog "github.com/philip-bui/grpc-zerolog" zerolog "github.com/philip-bui/grpc-zerolog"
"github.com/pkg/profile" "github.com/pkg/profile"
@ -47,17 +57,6 @@ import (
"tailscale.com/types/dnstype" "tailscale.com/types/dnstype"
"tailscale.com/types/key" "tailscale.com/types/key"
"tailscale.com/util/dnsname" "tailscale.com/util/dnsname"
"github.com/juanfont/headscale"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol/db"
"github.com/juanfont/headscale/hscontrol/derp"
derpServer "github.com/juanfont/headscale/hscontrol/derp/server"
"github.com/juanfont/headscale/hscontrol/mapper"
"github.com/juanfont/headscale/hscontrol/notifier"
"github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
) )
var ( var (
@ -680,7 +679,7 @@ func (h *Headscale) Serve() error {
Handler: router, Handler: router,
ReadTimeout: types.HTTPTimeout, ReadTimeout: types.HTTPTimeout,
// Long polling should not have any timeout, this is overriden // Long polling should not have any timeout, this is overridden
// further down the chain // further down the chain
WriteTimeout: types.HTTPTimeout, WriteTimeout: types.HTTPTimeout,
} }

View file

@ -337,7 +337,6 @@ func (h *Headscale) handleAuthKey(
if len(aclTags) > 0 { if len(aclTags) > 0 {
// This conditional preserves the existing behaviour, although SaaS would reset the tags on auth-key login // This conditional preserves the existing behaviour, although SaaS would reset the tags on auth-key login
err = h.db.SetTags(node.ID, aclTags) err = h.db.SetTags(node.ID, aclTags)
if err != nil { if err != nil {
log.Error(). log.Error().
Caller(). Caller().

View file

@ -13,13 +13,12 @@ import (
"github.com/glebarez/sqlite" "github.com/glebarez/sqlite"
"github.com/go-gormigrate/gormigrate/v2" "github.com/go-gormigrate/gormigrate/v2"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger" "gorm.io/gorm/logger"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
) )
var errDatabaseNotSupported = errors.New("database type not supported") var errDatabaseNotSupported = errors.New("database type not supported")
@ -331,7 +330,7 @@ func NewHeadscaleDatabase(
// IP v4 and v6 column. // IP v4 and v6 column.
// Note that previously, the list _could_ contain more // Note that previously, the list _could_ contain more
// than two addresses, which should not really happen. // than two addresses, which should not really happen.
// In that case, the first occurence of each type will // In that case, the first occurrence of each type will
// be kept. // be kept.
ID: "2024041121742", ID: "2024041121742",
Migrate: func(tx *gorm.DB) error { Migrate: func(tx *gorm.DB) error {

View file

@ -76,7 +76,6 @@ func NewIPAllocator(
if err != nil { if err != nil {
return nil, fmt.Errorf("reading IPv6 addresses from database: %w", err) return nil, fmt.Errorf("reading IPv6 addresses from database: %w", err)
} }
} }
var ips netipx.IPSetBuilder var ips netipx.IPSetBuilder

View file

@ -18,9 +18,11 @@ var mpp = func(pref string) *netip.Prefix {
p := netip.MustParsePrefix(pref) p := netip.MustParsePrefix(pref)
return &p return &p
} }
var na = func(pref string) netip.Addr { var na = func(pref string) netip.Addr {
return netip.MustParseAddr(pref) return netip.MustParseAddr(pref)
} }
var nap = func(pref string) *netip.Addr { var nap = func(pref string) *netip.Addr {
n := na(pref) n := na(pref)
return &n return &n

View file

@ -12,16 +12,15 @@ import (
"time" "time"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/puzpuzpuz/xsync/v3" "github.com/puzpuzpuz/xsync/v3"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/check.v1" "gopkg.in/check.v1"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/types/key" "tailscale.com/types/key"
"tailscale.com/types/ptr" "tailscale.com/types/ptr"
"github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
) )
func (s *Suite) TestGetNode(c *check.C) { func (s *Suite) TestGetNode(c *check.C) {

View file

@ -1,12 +1,11 @@
package db package db
import ( import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"errors" "errors"
"github.com/juanfont/headscale/hscontrol/types" "github.com/juanfont/headscale/hscontrol/types"
"gorm.io/gorm"
"gorm.io/gorm/clause"
) )
// SetPolicy sets the policy in the database. // SetPolicy sets the policy in the database.

View file

@ -542,7 +542,6 @@ func failoverRoute(
isLikelyConnected *xsync.MapOf[types.NodeID, bool], isLikelyConnected *xsync.MapOf[types.NodeID, bool],
routeToReplace *types.Route, routeToReplace *types.Route,
altRoutes types.Routes, altRoutes types.Routes,
) *failover { ) *failover {
if routeToReplace == nil { if routeToReplace == nil {
return nil return nil

View file

@ -285,25 +285,30 @@ func (s *Suite) TestDeleteRoutes(c *check.C) {
c.Assert(len(enabledRoutes1), check.Equals, 1) c.Assert(len(enabledRoutes1), check.Equals, 1)
} }
var ipp = func(s string) types.IPPrefix { return types.IPPrefix(netip.MustParsePrefix(s)) } var (
var n = func(nid types.NodeID) types.Node { ipp = func(s string) types.IPPrefix { return types.IPPrefix(netip.MustParsePrefix(s)) }
return types.Node{ID: nid} mkNode = func(nid types.NodeID) types.Node {
} return types.Node{ID: nid}
}
)
var np = func(nid types.NodeID) *types.Node { var np = func(nid types.NodeID) *types.Node {
no := n(nid) no := mkNode(nid)
return &no return &no
} }
var r = func(id uint, nid types.NodeID, prefix types.IPPrefix, enabled, primary bool) types.Route { var r = func(id uint, nid types.NodeID, prefix types.IPPrefix, enabled, primary bool) types.Route {
return types.Route{ return types.Route{
Model: gorm.Model{ Model: gorm.Model{
ID: id, ID: id,
}, },
Node: n(nid), Node: mkNode(nid),
Prefix: prefix, Prefix: prefix,
Enabled: enabled, Enabled: enabled,
IsPrimary: primary, IsPrimary: primary,
} }
} }
var rp = func(id uint, nid types.NodeID, prefix types.IPPrefix, enabled, primary bool) *types.Route { var rp = func(id uint, nid types.NodeID, prefix types.IPPrefix, enabled, primary bool) *types.Route {
ro := r(id, nid, prefix, enabled, primary) ro := r(id, nid, prefix, enabled, primary)
return &ro return &ro

View file

@ -541,7 +541,6 @@ func appendPeerChanges(
changed types.Nodes, changed types.Nodes,
cfg *types.Config, cfg *types.Config,
) error { ) error {
packetFilter, err := pol.CompileFilterRules(append(peers, node)) packetFilter, err := pol.CompileFilterRules(append(peers, node))
if err != nil { if err != nil {
return err return err

View file

@ -40,7 +40,7 @@ var (
mapResponseWriteUpdatesInStream = promauto.NewCounterVec(prometheus.CounterOpts{ mapResponseWriteUpdatesInStream = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: prometheusNamespace, Namespace: prometheusNamespace,
Name: "mapresponse_write_updates_in_stream_total", Name: "mapresponse_write_updates_in_stream_total",
Help: "total count of writes that occured in a stream session, pre-68 nodes", Help: "total count of writes that occurred in a stream session, pre-68 nodes",
}, []string{"status"}) }, []string{"status"})
mapResponseEndpointUpdates = promauto.NewCounterVec(prometheus.CounterOpts{ mapResponseEndpointUpdates = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: prometheusNamespace, Namespace: prometheusNamespace,

View file

@ -17,8 +17,10 @@ import (
"tailscale.com/util/set" "tailscale.com/util/set"
) )
var debugDeadlock = envknob.Bool("HEADSCALE_DEBUG_DEADLOCK") var (
var debugDeadlockTimeout = envknob.RegisterDuration("HEADSCALE_DEBUG_DEADLOCK_TIMEOUT") debugDeadlock = envknob.Bool("HEADSCALE_DEBUG_DEADLOCK")
debugDeadlockTimeout = envknob.RegisterDuration("HEADSCALE_DEBUG_DEADLOCK_TIMEOUT")
)
func init() { func init() {
deadlock.Opts.Disable = !debugDeadlock deadlock.Opts.Disable = !debugDeadlock
@ -291,7 +293,6 @@ func newBatcher(batchTime time.Duration, n *Notifier) *batcher {
patches: make(map[types.NodeID]tailcfg.PeerChange), patches: make(map[types.NodeID]tailcfg.PeerChange),
n: n, n: n,
} }
} }
func (b *batcher) close() { func (b *batcher) close() {
@ -393,7 +394,7 @@ func (b *batcher) doWork() {
} }
// overwritePatch takes the current patch and a newer patch // overwritePatch takes the current patch and a newer patch
// and override any field that has changed // and override any field that has changed.
func overwritePatch(currPatch, newPatch *tailcfg.PeerChange) { func overwritePatch(currPatch, newPatch *tailcfg.PeerChange) {
if newPatch.DERPRegion != 0 { if newPatch.DERPRegion != 0 {
currPatch.DERPRegion = newPatch.DERPRegion currPatch.DERPRegion = newPatch.DERPRegion

View file

@ -57,7 +57,6 @@ func (h *Headscale) initOIDC() error {
// grab oidc config if it hasn't been already // grab oidc config if it hasn't been already
if h.oauth2Config == nil { if h.oauth2Config == nil {
h.oidcProvider, err = oidc.NewProvider(context.Background(), h.cfg.OIDC.Issuer) h.oidcProvider, err = oidc.NewProvider(context.Background(), h.cfg.OIDC.Issuer)
if err != nil { if err != nil {
return fmt.Errorf("creating OIDC provider from issuer config: %w", err) return fmt.Errorf("creating OIDC provider from issuer config: %w", err)
} }

View file

@ -11,13 +11,12 @@ import (
"strings" "strings"
"time" "time"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/tailscale/hujson" "github.com/tailscale/hujson"
"go4.org/netipx" "go4.org/netipx"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
) )
var ( var (

View file

@ -6,15 +6,14 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"go4.org/netipx" "go4.org/netipx"
"gopkg.in/check.v1" "gopkg.in/check.v1"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
) )
var iap = func(ipStr string) *netip.Addr { var iap = func(ipStr string) *netip.Addr {
@ -1783,7 +1782,7 @@ var tsExitNodeDest = []tailcfg.NetPortRange{
} }
// hsExitNodeDest is the list of destination IP ranges that are allowed when // hsExitNodeDest is the list of destination IP ranges that are allowed when
// we use headscale "autogroup:internet" // we use headscale "autogroup:internet".
var hsExitNodeDest = []tailcfg.NetPortRange{ var hsExitNodeDest = []tailcfg.NetPortRange{
{IP: "0.0.0.0/5", Ports: tailcfg.PortRangeAny}, {IP: "0.0.0.0/5", Ports: tailcfg.PortRangeAny},
{IP: "8.0.0.0/7", Ports: tailcfg.PortRangeAny}, {IP: "8.0.0.0/7", Ports: tailcfg.PortRangeAny},
@ -1840,7 +1839,7 @@ func TestTheInternet(t *testing.T) {
internetPrefs := internetSet.Prefixes() internetPrefs := internetSet.Prefixes()
for i, _ := range internetPrefs { for i := range internetPrefs {
if internetPrefs[i].String() != hsExitNodeDest[i].IP { if internetPrefs[i].String() != hsExitNodeDest[i].IP {
t.Errorf("prefix from internet set %q != hsExit list %q", internetPrefs[i].String(), hsExitNodeDest[i].IP) t.Errorf("prefix from internet set %q != hsExit list %q", internetPrefs[i].String(), hsExitNodeDest[i].IP)
} }

View file

@ -10,7 +10,7 @@ import (
// ACLPolicy represents a Tailscale ACL Policy. // ACLPolicy represents a Tailscale ACL Policy.
type ACLPolicy struct { type ACLPolicy struct {
Groups Groups `json:"groups" ` Groups Groups `json:"groups"`
Hosts Hosts `json:"hosts"` Hosts Hosts `json:"hosts"`
TagOwners TagOwners `json:"tagOwners"` TagOwners TagOwners `json:"tagOwners"`
ACLs []ACL `json:"acls"` ACLs []ACL `json:"acls"`

View file

@ -184,7 +184,6 @@ func (m *mapSession) serve() {
return return
} }
} }
// serveLongPoll ensures the node gets the appropriate updates from either // serveLongPoll ensures the node gets the appropriate updates from either

View file

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/coreos/go-oidc/v3/oidc" "github.com/coreos/go-oidc/v3/oidc"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/prometheus/common/model" "github.com/prometheus/common/model"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -19,8 +20,6 @@ import (
"tailscale.com/net/tsaddr" "tailscale.com/net/tsaddr"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/types/dnstype" "tailscale.com/types/dnstype"
"github.com/juanfont/headscale/hscontrol/util"
) )
const ( const (

View file

@ -7,12 +7,11 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol/policy" "github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/integration/hsic" "github.com/juanfont/headscale/integration/hsic"
"github.com/juanfont/headscale/integration/tsic" "github.com/juanfont/headscale/integration/tsic"
"github.com/stretchr/testify/assert"
) )
func executeAndUnmarshal[T any](headscale ControlServer, command []string, result T) error { func executeAndUnmarshal[T any](headscale ControlServer, command []string, result T) error {
@ -481,7 +480,7 @@ func TestPreAuthKeyCorrectUserLoggedInCommand(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Len(t, listNodes, 1) assert.Len(t, listNodes, 1)
assert.Equal(t, "user2", listNodes[0].User.Name) assert.Equal(t, "user2", listNodes[0].GetUser().GetName())
} }
func TestApiKeyCommand(t *testing.T) { func TestApiKeyCommand(t *testing.T) {

View file

@ -1,9 +1,8 @@
package integration package integration
import ( import (
"github.com/ory/dockertest/v3"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1" v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/ory/dockertest/v3"
) )
type ControlServer interface { type ControlServer interface {

View file

@ -177,9 +177,9 @@ func WithPostgres() Option {
} }
// WithIPAllocationStrategy sets the tests IP Allocation strategy. // WithIPAllocationStrategy sets the tests IP Allocation strategy.
func WithIPAllocationStrategy(strat types.IPAllocationStrategy) Option { func WithIPAllocationStrategy(strategy types.IPAllocationStrategy) Option {
return func(hsic *HeadscaleInContainer) { return func(hsic *HeadscaleInContainer) {
hsic.env["HEADSCALE_PREFIXES_ALLOCATION"] = string(strat) hsic.env["HEADSCALE_PREFIXES_ALLOCATION"] = string(strategy)
} }
} }