Skip to content

Commit

Permalink
reformat code (#2019)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
greizgh authored Jul 22, 2024
1 parent ca47d6f commit 8571513
Show file tree
Hide file tree
Showing 23 changed files with 56 additions and 65 deletions.
3 changes: 1 addition & 2 deletions cmd/headscale/cli/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"io"
"os"

v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"

v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
)

func init() {
Expand Down
9 changes: 4 additions & 5 deletions cmd/headscale/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import (
"fmt"
"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"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"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 (
Expand Down
23 changes: 11 additions & 12 deletions hscontrol/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ import (
"github.com/gorilla/mux"
grpcMiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
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"
zerolog "github.com/philip-bui/grpc-zerolog"
"github.com/pkg/profile"
Expand All @@ -47,17 +57,6 @@ import (
"tailscale.com/types/dnstype"
"tailscale.com/types/key"
"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 (
Expand Down Expand Up @@ -680,7 +679,7 @@ func (h *Headscale) Serve() error {
Handler: router,
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
WriteTimeout: types.HTTPTimeout,
}
Expand Down
1 change: 0 additions & 1 deletion hscontrol/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ func (h *Headscale) handleAuthKey(
if len(aclTags) > 0 {
// This conditional preserves the existing behaviour, although SaaS would reset the tags on auth-key login
err = h.db.SetTags(node.ID, aclTags)

if err != nil {
log.Error().
Caller().
Expand Down
7 changes: 3 additions & 4 deletions hscontrol/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import (

"github.com/glebarez/sqlite"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog/log"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"

"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
)

var errDatabaseNotSupported = errors.New("database type not supported")
Expand Down Expand Up @@ -331,7 +330,7 @@ func NewHeadscaleDatabase(
// IP v4 and v6 column.
// Note that previously, the list _could_ contain more
// 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.
ID: "2024041121742",
Migrate: func(tx *gorm.DB) error {
Expand Down
1 change: 0 additions & 1 deletion hscontrol/db/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func NewIPAllocator(
if err != nil {
return nil, fmt.Errorf("reading IPv6 addresses from database: %w", err)
}

}

var ips netipx.IPSetBuilder
Expand Down
2 changes: 2 additions & 0 deletions hscontrol/db/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ var mpp = func(pref string) *netip.Prefix {
p := netip.MustParsePrefix(pref)
return &p
}

var na = func(pref string) netip.Addr {
return netip.MustParseAddr(pref)
}

var nap = func(pref string) *netip.Addr {
n := na(pref)
return &n
Expand Down
7 changes: 3 additions & 4 deletions hscontrol/db/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ import (
"time"

"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/stretchr/testify/assert"
"gopkg.in/check.v1"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
"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) {
Expand Down
5 changes: 2 additions & 3 deletions hscontrol/db/policy.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package db

import (
"gorm.io/gorm"
"gorm.io/gorm/clause"

"errors"

"github.com/juanfont/headscale/hscontrol/types"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

// SetPolicy sets the policy in the database.
Expand Down
1 change: 0 additions & 1 deletion hscontrol/db/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ func failoverRoute(
isLikelyConnected *xsync.MapOf[types.NodeID, bool],
routeToReplace *types.Route,
altRoutes types.Routes,

) *failover {
if routeToReplace == nil {
return nil
Expand Down
17 changes: 11 additions & 6 deletions hscontrol/db/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,25 +285,30 @@ func (s *Suite) TestDeleteRoutes(c *check.C) {
c.Assert(len(enabledRoutes1), check.Equals, 1)
}

var ipp = func(s string) types.IPPrefix { return types.IPPrefix(netip.MustParsePrefix(s)) }
var n = func(nid types.NodeID) types.Node {
return types.Node{ID: nid}
}
var (
ipp = func(s string) types.IPPrefix { return types.IPPrefix(netip.MustParsePrefix(s)) }
mkNode = func(nid types.NodeID) types.Node {
return types.Node{ID: nid}
}
)

var np = func(nid types.NodeID) *types.Node {
no := n(nid)
no := mkNode(nid)
return &no
}

var r = func(id uint, nid types.NodeID, prefix types.IPPrefix, enabled, primary bool) types.Route {
return types.Route{
Model: gorm.Model{
ID: id,
},
Node: n(nid),
Node: mkNode(nid),
Prefix: prefix,
Enabled: enabled,
IsPrimary: primary,
}
}

var rp = func(id uint, nid types.NodeID, prefix types.IPPrefix, enabled, primary bool) *types.Route {
ro := r(id, nid, prefix, enabled, primary)
return &ro
Expand Down
1 change: 0 additions & 1 deletion hscontrol/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,6 @@ func appendPeerChanges(
changed types.Nodes,
cfg *types.Config,
) error {

packetFilter, err := pol.CompileFilterRules(append(peers, node))
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion hscontrol/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
mapResponseWriteUpdatesInStream = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: prometheusNamespace,
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"})
mapResponseEndpointUpdates = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: prometheusNamespace,
Expand Down
9 changes: 5 additions & 4 deletions hscontrol/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ import (
"tailscale.com/util/set"
)

var debugDeadlock = envknob.Bool("HEADSCALE_DEBUG_DEADLOCK")
var debugDeadlockTimeout = envknob.RegisterDuration("HEADSCALE_DEBUG_DEADLOCK_TIMEOUT")
var (
debugDeadlock = envknob.Bool("HEADSCALE_DEBUG_DEADLOCK")
debugDeadlockTimeout = envknob.RegisterDuration("HEADSCALE_DEBUG_DEADLOCK_TIMEOUT")
)

func init() {
deadlock.Opts.Disable = !debugDeadlock
Expand Down Expand Up @@ -291,7 +293,6 @@ func newBatcher(batchTime time.Duration, n *Notifier) *batcher {
patches: make(map[types.NodeID]tailcfg.PeerChange),
n: n,
}

}

func (b *batcher) close() {
Expand Down Expand Up @@ -393,7 +394,7 @@ func (b *batcher) doWork() {
}

// 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) {
if newPatch.DERPRegion != 0 {
currPatch.DERPRegion = newPatch.DERPRegion
Expand Down
1 change: 0 additions & 1 deletion hscontrol/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (h *Headscale) initOIDC() error {
// grab oidc config if it hasn't been already
if h.oauth2Config == nil {
h.oidcProvider, err = oidc.NewProvider(context.Background(), h.cfg.OIDC.Issuer)

if err != nil {
return fmt.Errorf("creating OIDC provider from issuer config: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions hscontrol/policy/acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ import (
"strings"
"time"

"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
"github.com/rs/zerolog/log"
"github.com/tailscale/hujson"
"go4.org/netipx"
"tailscale.com/tailcfg"

"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
)

var (
Expand Down
9 changes: 4 additions & 5 deletions hscontrol/policy/acls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"testing"

"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/spf13/viper"
"github.com/stretchr/testify/assert"
"go4.org/netipx"
"gopkg.in/check.v1"
"tailscale.com/tailcfg"

"github.com/juanfont/headscale/hscontrol/types"
"github.com/juanfont/headscale/hscontrol/util"
)

var iap = func(ipStr string) *netip.Addr {
Expand Down Expand Up @@ -1783,7 +1782,7 @@ var tsExitNodeDest = []tailcfg.NetPortRange{
}

// 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{
{IP: "0.0.0.0/5", Ports: tailcfg.PortRangeAny},
{IP: "8.0.0.0/7", Ports: tailcfg.PortRangeAny},
Expand Down Expand Up @@ -1840,7 +1839,7 @@ func TestTheInternet(t *testing.T) {

internetPrefs := internetSet.Prefixes()

for i, _ := range internetPrefs {
for i := range internetPrefs {
if internetPrefs[i].String() != hsExitNodeDest[i].IP {
t.Errorf("prefix from internet set %q != hsExit list %q", internetPrefs[i].String(), hsExitNodeDest[i].IP)
}
Expand Down
2 changes: 1 addition & 1 deletion hscontrol/policy/acls_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// ACLPolicy represents a Tailscale ACL Policy.
type ACLPolicy struct {
Groups Groups `json:"groups" `
Groups Groups `json:"groups"`
Hosts Hosts `json:"hosts"`
TagOwners TagOwners `json:"tagOwners"`
ACLs []ACL `json:"acls"`
Expand Down
1 change: 0 additions & 1 deletion hscontrol/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func (m *mapSession) serve() {

return
}

}

// serveLongPoll ensures the node gets the appropriate updates from either
Expand Down
3 changes: 1 addition & 2 deletions hscontrol/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"time"

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

"github.com/juanfont/headscale/hscontrol/util"
)

const (
Expand Down
5 changes: 2 additions & 3 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"

v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/juanfont/headscale/hscontrol/policy"
"github.com/juanfont/headscale/integration/hsic"
"github.com/juanfont/headscale/integration/tsic"
"github.com/stretchr/testify/assert"
)

func executeAndUnmarshal[T any](headscale ControlServer, command []string, result T) error {
Expand Down Expand Up @@ -481,7 +480,7 @@ func TestPreAuthKeyCorrectUserLoggedInCommand(t *testing.T) {
assert.Nil(t, err)
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) {
Expand Down
3 changes: 1 addition & 2 deletions integration/control.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package integration

import (
"github.com/ory/dockertest/v3"

v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
"github.com/ory/dockertest/v3"
)

type ControlServer interface {
Expand Down
4 changes: 2 additions & 2 deletions integration/hsic/hsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ func WithPostgres() Option {
}

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

Expand Down

0 comments on commit 8571513

Please sign in to comment.