Skip to content

Commit

Permalink
refactor: use set library.
Browse files Browse the repository at this point in the history
  • Loading branch information
n6g7 committed Jan 18, 2024
1 parent 0ccecb9 commit a252ab0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 125 deletions.
5 changes: 3 additions & 2 deletions cmd/bingo/bingo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"runtime"
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/n6g7/bingo/internal/config"
"github.com/n6g7/bingo/internal/nameserver"
"github.com/n6g7/bingo/internal/proxy"
Expand Down Expand Up @@ -87,7 +88,7 @@ func bingo(logger *log.Logger, ns nameserver.Nameserver, prox proxy.Proxy, conf
logger.Error("error loading records from nameserver", "err", err)
return
}
newNSDomains := reconcile.NewDomainSet()
newNSDomains := mapset.NewSet[string]()
for _, record := range records {
// We only manage service domains
if !conf.IsServiceDomain(record.Name) {
Expand All @@ -109,7 +110,7 @@ func bingo(logger *log.Logger, ns nameserver.Nameserver, prox proxy.Proxy, conf
logger.Error("error loading services from proxy", "err", err)
return
}
newProxyDomains := reconcile.NewDomainSet()
newProxyDomains := mapset.NewSet[string]()
for _, service := range services {
// We only manage service domains
if !conf.IsServiceDomain(service.Domain) {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ toolchain go1.21.5
require (
github.com/aws/aws-sdk-go-v2/config v1.18.8
github.com/aws/aws-sdk-go-v2/service/route53 v1.26.0
github.com/deckarep/golang-set/v2 v2.6.0
github.com/lizongying/go-xpath v0.0.0-20221013102623-3e55fa3ceeed
github.com/mitchellh/mapstructure v1.5.0
github.com/n6g7/nomtail v0.2.0
github.com/prometheus/client_golang v1.17.0
github.com/spf13/viper v1.14.0
Expand Down Expand Up @@ -36,7 +38,6 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnht
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.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
10 changes: 5 additions & 5 deletions internal/proxy/traefik.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
"regexp"
"strings"

mapset "github.com/deckarep/golang-set/v2"
"github.com/n6g7/bingo/internal/config"
"github.com/n6g7/bingo/internal/set"
"golang.org/x/exp/slices"
)

type TraefikProxy struct {
hosts []string
adminPort uint16
scheme string
entryPoints *set.Set[string]
entryPoints mapset.Set[string]
regexp *regexp.Regexp
}

Expand All @@ -26,7 +26,7 @@ func NewTraefikProxy(conf config.TraefikConf) *TraefikProxy {
hosts: conf.Hosts,
adminPort: conf.AdminPort,
scheme: conf.Scheme,
entryPoints: set.NewSet(conf.EntryPoints),
entryPoints: mapset.NewSet[string](conf.EntryPoints...),
}
}

Expand Down Expand Up @@ -86,8 +86,8 @@ func (t *TraefikProxy) ListServices() ([]Service, error) {
continue
}
// Only track services on specified entrypoints
inter := set.NewSet(router.EntryPoints).Inter(t.entryPoints)
if inter.Length() == 0 {
inter := mapset.NewSet[string](router.EntryPoints...).Intersect(t.entryPoints)
if inter.Cardinality() == 0 {
continue
}

Expand Down
9 changes: 0 additions & 9 deletions internal/reconcile/domain_set.go

This file was deleted.

51 changes: 26 additions & 25 deletions internal/reconcile/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/n6g7/bingo/internal/config"
"github.com/n6g7/bingo/internal/nameserver"
"github.com/n6g7/bingo/internal/proxy"
Expand All @@ -30,15 +31,15 @@ var (

type Reconciler struct {
logger *log.Logger
nameserverDomains *DomainSet
proxyDomains *DomainSet
nameserverDomains mapset.Set[string]
proxyDomains mapset.Set[string]
needsDiff bool
proxyBackend proxy.Proxy
nsBackend nameserver.Nameserver
lastReconciliation time.Time
minimumWait time.Duration
loopTimeout time.Duration
deletionQueue *DomainSet
deletionQueue mapset.Set[string]
conf *config.Config
}

Expand All @@ -49,31 +50,31 @@ func NewReconciler(
conf *config.Config,
) *Reconciler {
return &Reconciler{
logger.With("component", "reconciler"),
nil,
nil,
false,
prox,
ns,
time.Unix(0, 0),
conf.ReconciliationTimeout,
conf.ReconcilerLoopTimeout,
NewDomainSet(),
conf,
logger: logger.With("component", "reconciler"),
nameserverDomains: nil,
proxyDomains: nil,
needsDiff: false,
proxyBackend: prox,
nsBackend: ns,
lastReconciliation: time.Unix(0, 0),
minimumWait: conf.ReconciliationTimeout,
loopTimeout: conf.ReconcilerLoopTimeout,
deletionQueue: mapset.NewSet[string](),
conf: conf,
}
}

func (r *Reconciler) SetNameserverDomains(nsDomains *DomainSet) {
r.logger.Trace("received NS domains", "domains", nsDomains.AsSlice())
func (r *Reconciler) SetNameserverDomains(nsDomains mapset.Set[string]) {
r.logger.Trace("received NS domains", "domains", nsDomains.ToSlice())
if reflect.DeepEqual(nsDomains, r.nameserverDomains) {
return
}
r.nameserverDomains = nsDomains
r.needsDiff = true
}

func (r *Reconciler) SetProxyDomains(proxyDomains *DomainSet) {
r.logger.Trace("received proxy domains", "domains", proxyDomains.AsSlice())
func (r *Reconciler) SetProxyDomains(proxyDomains mapset.Set[string]) {
r.logger.Trace("received proxy domains", "domains", proxyDomains.ToSlice())
if reflect.DeepEqual(proxyDomains, r.proxyDomains) {
return
}
Expand All @@ -86,7 +87,7 @@ func (r *Reconciler) MarkForDeletion(domain string) {
r.needsDiff = true
}

func (r *Reconciler) Diff() (toCreate *DomainSet, toDelete *DomainSet) {
func (r *Reconciler) Diff() (toCreate mapset.Set[string], toDelete mapset.Set[string]) {
if r.nameserverDomains == nil {
r.logger.Debug("reconciler not ready to diff, no nameserver domains yet")
return nil, nil
Expand All @@ -96,15 +97,15 @@ func (r *Reconciler) Diff() (toCreate *DomainSet, toDelete *DomainSet) {
return nil, nil
}

toDelete = r.nameserverDomains.Diff(r.proxyDomains).Union(r.deletionQueue) // NS - P + D
toCreate = r.proxyDomains.Diff(r.nameserverDomains).Union(r.deletionQueue.Inter(r.proxyDomains)) // P - NS + (D&P)
toDelete = r.nameserverDomains.Difference(r.proxyDomains).Union(r.deletionQueue) // NS - P + D
toCreate = r.proxyDomains.Difference(r.nameserverDomains).Union(r.deletionQueue.Intersect(r.proxyDomains)) // P - NS + (D&P)

managedGauge.Set(float64(r.proxyDomains.Length()))
managedGauge.Set(float64(r.proxyDomains.Cardinality()))

return
}

func (r *Reconciler) Reconcile(toCreate, toDelete *DomainSet) error {
func (r *Reconciler) Reconcile(toCreate, toDelete mapset.Set[string]) error {
r.lastReconciliation = time.Now()

// Start by deleting, gives us a chance to immediately recreate domains in
Expand Down Expand Up @@ -151,7 +152,7 @@ func (r *Reconciler) Run() error {
if r.needsDiff {
toCreate, toDelete := r.Diff()

if (toCreate == nil || toCreate.Length() == 0) && (toDelete == nil || toDelete.Length() == 0) {
if (toCreate == nil || toCreate.Cardinality() == 0) && (toDelete == nil || toDelete.Cardinality() == 0) {
if !previouslyInSync {
r.logger.Info("proxy and nameserver are in sync")
previouslyInSync = true
Expand All @@ -171,7 +172,7 @@ func (r *Reconciler) Run() error {
if err != nil {
r.logger.Error("error during reconciliation, will attempt again", "err", err)
} else {
r.deletionQueue = NewDomainSet()
r.deletionQueue = mapset.NewSet[string]()
r.needsDiff = false
}
tooEarlyWarningSent = false
Expand Down
83 changes: 0 additions & 83 deletions internal/set/set.go

This file was deleted.

0 comments on commit a252ab0

Please sign in to comment.