Skip to content

Commit

Permalink
Use sets.String replace sets.Set[string] (istio#47841)
Browse files Browse the repository at this point in the history
* Use sets.String replace sets.Set[string]

* Use %s print set

* Address the problems from CR
  • Loading branch information
sword-jin authored Nov 16, 2023
1 parent b39c066 commit 7e24bf9
Show file tree
Hide file tree
Showing 17 changed files with 36 additions and 21 deletions.
4 changes: 2 additions & 2 deletions cni/pkg/ambient/informers.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *Server) Run(stop <-chan struct{}) {
<-stop
}

func (s *Server) ReconcileNamespaces() sets.Set[string] {
func (s *Server) ReconcileNamespaces() sets.String {
processed := sets.New[string]()
for _, ns := range s.namespaces.List(metav1.NamespaceAll, klabels.Everything()) {
processed.Merge(s.enqueueNamespace(ns))
Expand All @@ -73,7 +73,7 @@ func (s *Server) EnqueueNamespace(o controllers.Object) {
s.enqueueNamespace(o)
}

func (s *Server) enqueueNamespace(o controllers.Object) sets.Set[string] {
func (s *Server) enqueueNamespace(o controllers.Object) sets.String {
namespace := o.GetName()
matchAmbient := o.GetLabels()[constants.DataplaneMode] == constants.DataplaneModeAmbient
processed := sets.New[string]()
Expand Down
2 changes: 1 addition & 1 deletion cni/pkg/ambient/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func SetProc(path string, value string) error {
return os.WriteFile(path, []byte(value), 0o644)
}

func (s *Server) cleanStaleIPs(stales sets.Set[string]) {
func (s *Server) cleanStaleIPs(stales sets.String) {
log.Infof("Ambient stale Pod IPs to be cleaned: %s", stales)
switch s.redirectMode {
case IptablesMode:
Expand Down
2 changes: 1 addition & 1 deletion cni/pkg/ambient/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ func (s *Server) cleanupNode() {
}
}

func (s *Server) getEnrolledIPSets() sets.Set[string] {
func (s *Server) getEnrolledIPSets() sets.String {
pods := sets.New[string]()
switch s.redirectMode {
case IptablesMode:
Expand Down
2 changes: 1 addition & 1 deletion cni/pkg/ambient/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func NewServer(ctx context.Context, args AmbientArgs) (*Server, error) {
s.ebpfServer.Start(ctx.Done())
}

log.Infof("Ambient enrolled IPs before reconciling: %+v", s.getEnrolledIPSets())
log.Infof("Ambient enrolled IPs before reconciling: %s", s.getEnrolledIPSets())

s.setupHandlers()

Expand Down
2 changes: 1 addition & 1 deletion cni/pkg/ebpf/server/redirectionServer_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ func (r *RedirectServer) dumpZtunnelInfo() (*mapInfo, error) {
return &info, nil
}

func (r *RedirectServer) DumpAppIPs() sets.Set[string] {
func (r *RedirectServer) DumpAppIPs() sets.String {
var keyOut [4]byte
var valueOut mapInfo
m := sets.New[string]()
Expand Down
4 changes: 2 additions & 2 deletions cni/pkg/install/binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (

// Copies/mirrors any files present in a single source dir to N number of target dirs
// and returns a set of the filenames copied.
func copyBinaries(srcDir string, targetDirs []string) (sets.Set[string], error) {
copiedFilenames := sets.Set[string]{}
func copyBinaries(srcDir string, targetDirs []string) (sets.String, error) {
copiedFilenames := sets.String{}
srcFiles, err := os.ReadDir(srcDir)
if err != nil {
return copiedFilenames, err
Expand Down
4 changes: 2 additions & 2 deletions cni/pkg/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewInstaller(cfg *config.InstallConfig, isReady *atomic.Value) *Installer {
}
}

func (in *Installer) installAll(ctx context.Context) (sets.Set[string], error) {
func (in *Installer) installAll(ctx context.Context) (sets.String, error) {
// Install binaries
// Currently we _always_ do this, since the binaries do not live in a shared location
// and we harm no one by doing so.
Expand Down Expand Up @@ -179,7 +179,7 @@ func (in *Installer) Cleanup() error {
// sleepWatchInstall blocks until any file change for the binaries or config are detected.
// At that point, the func yields so the caller can recheck the validity of the install.
// If an error occurs or context is canceled, the function will return an error.
func (in *Installer) sleepWatchInstall(ctx context.Context, installedBinFiles sets.Set[string]) error {
func (in *Installer) sleepWatchInstall(ctx context.Context, installedBinFiles sets.String) error {
// Watch our specific binaries, in each configured binary dir.
// We may or may not be the only CNI plugin in play, and if we are not
// we shouldn't fire events for binaries that are not ours.
Expand Down
6 changes: 3 additions & 3 deletions cni/pkg/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestSleepCheckInstall(t *testing.T) {
}

t.Log("Expecting an invalid configuration log:")
err := in.sleepWatchInstall(ctx, sets.Set[string]{})
err := in.sleepWatchInstall(ctx, sets.String{})
if err != nil {
t.Fatalf("error should be nil due to invalid config, got: %v", err)
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestSleepCheckInstall(t *testing.T) {
// Should detect a valid configuration and wait indefinitely for a file modification
errChan := make(chan error)
go func(ctx context.Context) {
errChan <- in.sleepWatchInstall(ctx, sets.Set[string]{})
errChan <- in.sleepWatchInstall(ctx, sets.String{})
}(ctx)

select {
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestSleepCheckInstall(t *testing.T) {

// Run sleepWatchInstall
go func(ctx context.Context, in *Installer) {
errChan <- in.sleepWatchInstall(ctx, sets.Set[string]{})
errChan <- in.sleepWatchInstall(ctx, sets.String{})
}(ctx, in)
}

Expand Down
2 changes: 1 addition & 1 deletion pilot/pkg/config/kube/gateway/deploymentcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ func (d *DeploymentController) apply(controller string, yml string) error {
return nil
}

func (d *DeploymentController) HandleTagChange(newTags sets.Set[string]) {
func (d *DeploymentController) HandleTagChange(newTags sets.String) {
for _, gw := range d.gateways.List(metav1.NamespaceAll, klabels.Everything()) {
d.queue.AddObject(gw)
}
Expand Down
2 changes: 1 addition & 1 deletion pilot/pkg/model/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestGatewayHostnames(t *testing.T) {
xdsUpdater.WaitOrFail(t, "xds full")
})

workingDNSServer.setHosts(make(sets.Set[string]))
workingDNSServer.setHosts(make(sets.String))
t.Run("no answer", func(t *testing.T) {
assert.EventuallyEqual(t, func() int {
return len(env.NetworkManager.AllGateways())
Expand Down
2 changes: 1 addition & 1 deletion pilot/pkg/model/push_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ func (ps *PushContext) initVirtualServices(env *Environment) {
sets.InsertOrNew(ps.virtualServiceIndex.destinationsByGateway, gw, host)
}
if _, exists := ps.virtualServiceIndex.destinationsByGateway[gw]; !exists {
ps.virtualServiceIndex.destinationsByGateway[gw] = sets.Set[string]{}
ps.virtualServiceIndex.destinationsByGateway[gw] = sets.String{}
}
addHostsFromMeshConfig(ps, ps.virtualServiceIndex.destinationsByGateway[gw])
}
Expand Down
2 changes: 1 addition & 1 deletion pilot/pkg/model/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ type AmbientIndexes interface {
proxy *Proxy,
allAddresses sets.String,
currentSubs sets.String,
) sets.Set[string]
) sets.String
Policies(requested sets.Set[ConfigKey]) []*security.Authorization
Waypoint(scope WaypointScope) []netip.Addr
WorkloadsForWaypoint(scope WaypointScope) []*WorkloadInfo
Expand Down
4 changes: 2 additions & 2 deletions pkg/revisions/tag_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ type TagWatcher interface {
Run(stopCh <-chan struct{})
HasSynced() bool
AddHandler(handler TagHandler)
GetMyTags() sets.Set[string]
GetMyTags() sets.String
}

// TagHandler is a callback for when the tags revision change.
type TagHandler func(sets.Set[string])
type TagHandler func(sets.String)

type tagWatcher struct {
revision string
Expand Down
2 changes: 1 addition & 1 deletion pkg/revisions/tag_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestTagWatcher(t *testing.T) {
stop := test.NewStop(t)
c.RunAndWait(stop)
track := assert.NewTracker[string](t)
tw.AddHandler(func(s sets.Set[string]) {
tw.AddHandler(func(s sets.String) {
track.Record(strings.Join(sets.SortedList(s), ","))
})
go tw.Run(stop)
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/sets/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package sets

import (
"fmt"

"golang.org/x/exp/constraints"

"istio.io/istio/pkg/slices"
Expand Down Expand Up @@ -233,6 +235,13 @@ func (s Set[T]) IsEmpty() bool {
return len(s) == 0
}

// String returns a string representation of the set.
// Be aware that the order of elements is random so the string representation may vary.
// Use it only for debugging and logging.
func (s Set[T]) String() string {
return fmt.Sprintf("%v", s.UnsortedList())
}

// InsertOrNew inserts t into the set if the set exists, or returns a new set with t if not.
// Works well with DeleteCleanupLast.
// Example:
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/sets/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,9 @@ func TestMapOfSet(t *testing.T) {
DeleteCleanupLast(m, 1, "not found")
assert.Equal(t, m, map[int]String{2: New("c")})
}

func TestSetString(t *testing.T) {
elements := []string{"a"}
set := New(elements...)
assert.Equal(t, "[a]", set.String())
}
2 changes: 1 addition & 1 deletion tools/bug-report/pkg/kubectlcmd/kubectlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Runner struct {
taskSem chan struct{}

// runningTasks tracks the in-flight fetch operations for user feedback.
runningTasks sets.Set[string]
runningTasks sets.String
runningTasksMu sync.RWMutex

// runningTasksTicker is the report interval for running tasks.
Expand Down

0 comments on commit 7e24bf9

Please sign in to comment.