From 59a1a85a2b57c674074926738893d11fe444be88 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Mon, 30 May 2022 11:49:35 +0200 Subject: [PATCH] Change to a go generics set implementation, no more casting :tada: --- dns.go | 12 +++--------- flake.nix | 2 +- go.mod | 2 +- go.sum | 4 ++-- machine.go | 13 +++---------- 5 files changed, 10 insertions(+), 23 deletions(-) diff --git a/dns.go b/dns.go index 9a91cff..be7c87c 100644 --- a/dns.go +++ b/dns.go @@ -4,7 +4,7 @@ import ( "fmt" "strings" - "github.com/fatih/set" + mapset "github.com/deckarep/golang-set/v2" "inet.af/netaddr" "tailscale.com/tailcfg" "tailscale.com/util/dnsname" @@ -170,18 +170,12 @@ func getMapResponseDNSConfig( ), ) - namespaceSet := set.New(set.ThreadSafe) + namespaceSet := mapset.NewSet[Namespace]() namespaceSet.Add(machine.Namespace) for _, p := range peers { namespaceSet.Add(p.Namespace) } - for _, ns := range namespaceSet.List() { - namespace, ok := ns.(Namespace) - if !ok { - dnsConfig = dnsConfigOrig - - continue - } + for _, namespace := range namespaceSet.ToSlice() { dnsRoute := fmt.Sprintf("%v.%v", namespace.Name, baseDomain) dnsConfig.Routes[dnsRoute] = nil } diff --git a/flake.nix b/flake.nix index 4818ba0..5d45535 100644 --- a/flake.nix +++ b/flake.nix @@ -63,7 +63,7 @@ # When updating go.mod or go.sum, a new sha will need to be calculated, # update this if you have a mismatch after doing a change to thos files. - vendorSha256 = "sha256-hZXNbbPy1XcCd3t2jBRMcvrMnCDcUpROK+NZYCXx6XY="; + vendorSha256 = "sha256-1yFvqQvsJLmjQn2RLDjwvyiZVDXv0dg/V+UUDvwk6Hg="; ldflags = [ "-s" "-w" "-X github.com/juanfont/headscale/cmd/headscale/cli.Version=v${version}" ]; }; diff --git a/go.mod b/go.mod index 4d2f9b1..69510a5 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ require ( github.com/AlecAivazis/survey/v2 v2.3.4 github.com/ccding/go-stun/stun v0.0.0-20200514191101-4dc67bcdb029 github.com/coreos/go-oidc/v3 v3.1.0 + github.com/deckarep/golang-set/v2 v2.1.0 github.com/efekarakus/termcolor v1.0.1 - github.com/fatih/set v0.2.1 github.com/gin-gonic/gin v1.7.7 github.com/glebarez/sqlite v1.4.3 github.com/gofrs/uuid v4.2.0+incompatible diff --git a/go.sum b/go.sum index 7ebdcd6..85c6034 100644 --- a/go.sum +++ b/go.sum @@ -121,6 +121,8 @@ github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI= +github.com/deckarep/golang-set/v2 v2.1.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4= github.com/docker/cli v20.10.11+incompatible h1:tXU1ezXcruZQRrMP8RN2z9N91h+6egZTS1gsPsKantc= github.com/docker/cli v20.10.11+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= github.com/docker/docker v20.10.7+incompatible h1:Z6O9Nhsjv+ayUEeI1IojKbYcsGdgYSNqxe1s2MYzUhQ= @@ -141,8 +143,6 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= -github.com/fatih/set v0.2.1 h1:nn2CaJyknWE/6txyUDGwysr3G5QC6xWB/PtVjPBbeaA= -github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss= github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= diff --git a/machine.go b/machine.go index 28a49a3..3f37909 100644 --- a/machine.go +++ b/machine.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/fatih/set" + mapset "github.com/deckarep/golang-set/v2" v1 "github.com/juanfont/headscale/gen/go/headscale/v1" "github.com/rs/zerolog/log" "google.golang.org/protobuf/types/known/timestamppb" @@ -469,17 +469,10 @@ func (h *Headscale) isOutdated(machine *Machine) bool { return true } - namespaceSet := set.New(set.ThreadSafe) + namespaceSet := mapset.NewSet[string]() namespaceSet.Add(machine.Namespace.Name) - namespaces := make([]string, namespaceSet.Size()) - for index, namespace := range namespaceSet.List() { - if name, ok := namespace.(string); ok { - namespaces[index] = name - } - } - - lastChange := h.getLastStateChange(namespaces...) + lastChange := h.getLastStateChange(namespaceSet.ToSlice()...) lastUpdate := machine.CreatedAt if machine.LastSuccessfulUpdate != nil { lastUpdate = *machine.LastSuccessfulUpdate