Skip to content

Commit

Permalink
deps: upgrade to go-set/v2 and fixup a few broken things
Browse files Browse the repository at this point in the history
No functional changes, just cleaning up deprecated usages that are
removed in v2, and replace one call of .Slice with .ForEach to avoid
making the intermediate copy.
  • Loading branch information
shoenig committed Oct 2, 2023
1 parent 3d62438 commit 52433e8
Show file tree
Hide file tree
Showing 53 changed files with 91 additions and 84 deletions.
2 changes: 1 addition & 1 deletion client/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/armon/go-metrics"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/acl"
"github.com/hashicorp/nomad/nomad/structs"
)
Expand Down
2 changes: 1 addition & 1 deletion client/lib/cgroupslib/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strconv"
"strings"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"golang.org/x/sys/unix"
)

Expand Down
2 changes: 1 addition & 1 deletion client/lib/cgroupslib/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"os"
"strings"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
)

func detect() Mode {
Expand Down
2 changes: 1 addition & 1 deletion client/lib/idset/idset.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strconv"
"strings"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
)

// An ID is representative of a non-negative identifier of something like
Expand Down
2 changes: 1 addition & 1 deletion client/serviceregistration/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/nomad/structs"
)
Expand Down
6 changes: 3 additions & 3 deletions command/acl_token_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/helper"
"github.com/posener/complete"
Expand Down Expand Up @@ -182,10 +182,10 @@ func (c *ACLTokenCreateCommand) Run(args []string) int {
func generateACLTokenRoleLinks(roleNames, roleIDs []string) []*api.ACLTokenRoleLink {
var tokenLinks []*api.ACLTokenRoleLink

roleNameSet := set.From[string](roleNames).List()
roleNameSet := set.From[string](roleNames).Slice()
roleNameFn := func(name string) *api.ACLTokenRoleLink { return &api.ACLTokenRoleLink{Name: name} }

roleIDsSet := set.From[string](roleIDs).List()
roleIDsSet := set.From[string](roleIDs).Slice()
roleIDFn := func(id string) *api.ACLTokenRoleLink { return &api.ACLTokenRoleLink{ID: id} }

tokenLinks = append(tokenLinks, helper.ConvertSlice(roleNameSet, roleNameFn)...)
Expand Down
2 changes: 1 addition & 1 deletion command/agent/consul/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/armon/go-metrics"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/client/serviceregistration"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/helper/envoy"
Expand Down
2 changes: 1 addition & 1 deletion command/job_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
humanize "github.com/dustin/go-humanize"
"github.com/dustin/go-humanize/english"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
Expand Down
2 changes: 1 addition & 1 deletion command/job_restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"time"

"github.com/google/go-cmp/cmp/cmpopts"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/command/agent"
Expand Down
2 changes: 1 addition & 1 deletion command/node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"strings"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/mitchellh/cli"
Expand Down
2 changes: 1 addition & 1 deletion command/node_pool_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"strings"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/api"
"github.com/posener/complete"
)
Expand Down
2 changes: 1 addition & 1 deletion command/node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"regexp"
"testing"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/ci"
"github.com/mitchellh/cli"
Expand Down
8 changes: 5 additions & 3 deletions command/var_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"strings"

multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/nomad/api"
Expand Down Expand Up @@ -581,10 +581,12 @@ func formatInvalidVarKeyChars(invalid []string) string {

// Sort the characters for output
charList := make([]string, 0, chars.Size())
for _, k := range chars.List() {
chars.ForEach(func(k string) bool {
// Use %s instead of %q to avoid escaping characters.
charList = append(charList, fmt.Sprintf(`"%s"`, k))
}
return true
})

slices.Sort(charList)

// Build string
Expand Down
4 changes: 2 additions & 2 deletions drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
hclog "github.com/hashicorp/go-hclog"
multierror "github.com/hashicorp/go-multierror"
plugin "github.com/hashicorp/go-plugin"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/client/lib/cgroupslib"
"github.com/hashicorp/nomad/client/lib/cpustats"
"github.com/hashicorp/nomad/client/taskenv"
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *pauseContainerStore) remove(id string) {
s.containerIDs.Remove(id)
}

func (s *pauseContainerStore) union(other *set.Set[string]) *set.Set[string] {
func (s *pauseContainerStore) union(other *set.Set[string]) set.Collection[string] {
s.lock.Lock()
defer s.lock.Unlock()
return other.Union(s.containerIDs)
Expand Down
8 changes: 4 additions & 4 deletions drivers/docker/reconcile_dangling.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

docker "github.com/fsouza/go-dockerclient"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
)

// containerReconciler detects and kills unexpectedly running containers.
Expand All @@ -28,7 +28,7 @@ type containerReconciler struct {
getClient func() (*docker.Client, error)

isDriverHealthy func() bool
trackedContainers func() *set.Set[string]
trackedContainers func() set.Collection[string]
isNomadContainer func(c docker.APIContainers) bool

once sync.Once
Expand Down Expand Up @@ -134,7 +134,7 @@ func (r *containerReconciler) removeDanglingContainersIteration() error {

// untrackedContainers returns the ids of containers that suspected
// to have been started by Nomad but aren't tracked by this driver
func (r *containerReconciler) untrackedContainers(tracked *set.Set[string], cutoffTime time.Time) (*set.Set[string], error) {
func (r *containerReconciler) untrackedContainers(tracked set.Collection[string], cutoffTime time.Time) (*set.Set[string], error) {
result := set.New[string](10)

ctx, cancel := r.dockerAPIQueryContext()
Expand Down Expand Up @@ -230,7 +230,7 @@ func hasNomadName(c docker.APIContainers) bool {
// trackedContainers returns the set of container IDs of containers that were
// started by Driver and are expected to be running. This includes both normal
// Task containers, as well as infra pause containers.
func (d *Driver) trackedContainers() *set.Set[string] {
func (d *Driver) trackedContainers() set.Collection[string] {
// collect the task containers
ids := d.tasks.IDs()
// now also accumulate pause containers
Expand Down
2 changes: 1 addition & 1 deletion drivers/docker/reconcile_dangling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

docker "github.com/fsouza/go-dockerclient"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/shoenig/test/must"
"github.com/shoenig/test/wait"

Expand Down
2 changes: 1 addition & 1 deletion drivers/docker/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package docker
import (
"sync"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
)

type taskStore struct {
Expand Down
2 changes: 1 addition & 1 deletion drivers/shared/executor/executor_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os/exec"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/client/lib/cpustats"
"github.com/hashicorp/nomad/drivers/shared/executor/procstats"
"github.com/hashicorp/nomad/plugins/drivers"
Expand Down
2 changes: 1 addition & 1 deletion drivers/shared/executor/executor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/armon/circbuf"
"github.com/hashicorp/consul-template/signals"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/lib/cgroupslib"
"github.com/hashicorp/nomad/client/lib/cpustats"
Expand Down
2 changes: 1 addition & 1 deletion drivers/shared/executor/executor_universal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"syscall"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/client/lib/cgroupslib"
"github.com/hashicorp/nomad/drivers/shared/executor/procstats"
"github.com/hashicorp/nomad/helper/users"
Expand Down
2 changes: 1 addition & 1 deletion drivers/shared/executor/procstats/list_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"context"
"time"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/lib/lang"
"github.com/shirou/gopsutil/v3/process"
)
Expand Down
2 changes: 1 addition & 1 deletion drivers/shared/executor/procstats/list_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package procstats

import (
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/client/lib/cgroupslib"
)

Expand Down
2 changes: 1 addition & 1 deletion drivers/shared/executor/procstats/procstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package procstats
import (
"time"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/client/lib/cpustats"
"github.com/hashicorp/nomad/plugins/drivers"
)
Expand Down
2 changes: 1 addition & 1 deletion e2e/acl/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package acl
import (
"testing"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/nomad/api"
"github.com/shoenig/test"
"github.com/shoenig/test/must"
Expand Down
2 changes: 1 addition & 1 deletion e2e/artifact/artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func artifactCheckLogContents(t *testing.T, nomad *api.Client, group, task strin
t.Run(task, func(t *testing.T) {
logs, err := e2eutil.AllocTaskLogs(allocID, task, e2eutil.LogsStdOut)
must.NoError(t, err)
must.StrContains(t, logs, "module github.com/hashicorp/go-set")
must.StrContains(t, logs, "module github.com/hashicorp/go-set/v2")
})
}

Expand Down
2 changes: 1 addition & 1 deletion e2e/nodedrain/node_drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"
"time"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/shoenig/test"
"github.com/shoenig/test/must"
"github.com/shoenig/test/wait"
Expand Down
2 changes: 1 addition & 1 deletion e2e/v3/jobs3/jobs3.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"
"time"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
nomadapi "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/e2e/v3/util3"
"github.com/hashicorp/nomad/helper/pointer"
Expand Down
2 changes: 1 addition & 1 deletion e2e/v3/namespaces3/namespaces3.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
nomadapi "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/e2e/v3/util3"
"github.com/hashicorp/nomad/helper"
Expand Down
2 changes: 1 addition & 1 deletion e2e/vaultcompat/vaultcompat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"time"

"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/go-version"
nomadapi "github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/helper/testlog"
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ require (
github.com/hashicorp/go-plugin v1.4.10
github.com/hashicorp/go-secure-stdlib/listenerutil v0.1.4
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2
github.com/hashicorp/go-set v0.1.14
github.com/hashicorp/go-set/v2 v2.0.0-alpha.1
github.com/hashicorp/go-sockaddr v1.0.2
github.com/hashicorp/go-syslog v1.0.0
github.com/hashicorp/go-uuid v1.0.3
Expand Down Expand Up @@ -213,6 +213,7 @@ require (
github.com/hashicorp/go-secure-stdlib/parseutil v0.1.7 // indirect
github.com/hashicorp/go-secure-stdlib/reloadutil v0.1.1 // indirect
github.com/hashicorp/go-secure-stdlib/tlsutil v0.1.2 // indirect
github.com/hashicorp/go-set v0.1.8 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/mdns v1.0.4 // indirect
github.com/hashicorp/vault/api/auth/kubernetes v0.3.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -888,8 +888,10 @@ github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25L
github.com/hashicorp/go-secure-stdlib/tlsutil v0.1.1/go.mod h1:l8slYwnJA26yBz+ErHpp2IRCLr0vuOMGBORIz4rRiAs=
github.com/hashicorp/go-secure-stdlib/tlsutil v0.1.2 h1:phcbL8urUzF/kxA/Oj6awENaRwfWsjP59GW7u2qlDyY=
github.com/hashicorp/go-secure-stdlib/tlsutil v0.1.2/go.mod h1:l8slYwnJA26yBz+ErHpp2IRCLr0vuOMGBORIz4rRiAs=
github.com/hashicorp/go-set v0.1.14 h1:ZU7JyS6QGueDuXYldjcuyKLR0XV14eOKcsQlGddXGgA=
github.com/hashicorp/go-set v0.1.14/go.mod h1:FH9zJxnQYHPlZ7j9JaoQjZOFPBStOrelKOE11Wjwirc=
github.com/hashicorp/go-set v0.1.8 h1:q2r58lFkJrikmC4I+vS3A+bn6QgR7EYeFD8kRiAIAnk=
github.com/hashicorp/go-set v0.1.8/go.mod h1:wedp+UE6HoxBywExd7mrdGdcXOo3awtiELmnRnpzsKI=
github.com/hashicorp/go-set/v2 v2.0.0-alpha.1 h1:vnuI6A2kou5+tcj5sF+cROU3mTeWPx2BGmsbBhBp/+E=
github.com/hashicorp/go-set/v2 v2.0.0-alpha.1/go.mod h1:6q4nh8UCVZODn2tJ5RbJi8+ki7pjZBsAEYGt6yaGeTo=
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0SyteCQc=
github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A=
Expand Down
12 changes: 6 additions & 6 deletions helper/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"time"

multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-set"
"github.com/hashicorp/go-set/v2"
"github.com/hashicorp/hcl/hcl/ast"
)

Expand Down Expand Up @@ -85,21 +85,21 @@ func HashUUID(input string) (output string, hashed bool) {
func UniqueMapSliceValues[K, V comparable](m map[K][]V) []V {
s := set.New[V](0)
for _, slice := range m {
s.InsertAll(slice)
s.InsertSlice(slice)
}
return s.List()
return s.Slice()
}

// IsSubset returns whether the smaller set of items is a subset of
// the larger. If the smaller set is not a subset, the offending elements are
// returned.
func IsSubset[T comparable](larger, smaller []T) (bool, []T) {
l := set.From(larger)
if l.ContainsAll(smaller) {
if l.ContainsSlice(smaller) {
return true, nil
}
s := set.From(smaller)
return false, s.Difference(l).List()
return false, s.Difference(l).Slice()
}

// StringHasPrefixInSlice returns true if s starts with any prefix in list.
Expand All @@ -118,7 +118,7 @@ func IsDisjoint[T comparable](first, second []T) (bool, []T) {
f, s := set.From(first), set.From(second)
intersection := f.Intersect(s)
if intersection.Size() > 0 {
return false, intersection.List()
return false, intersection.Slice()
}
return true, nil
}
Expand Down
Loading

0 comments on commit 52433e8

Please sign in to comment.