Skip to content

Modernize Go map and slice operations #38126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2025
Merged

Conversation

tklauser
Copy link
Member

@tklauser tklauser commented Mar 11, 2025

Use the convenience functions from the maps and slices standard library packages, added in Go 1.21 and extended in the successive releases.

Generated using modernize by running:

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

and committing the relevant changes with some minor manual edits (e.g. reordering added imports).


Sorry for the large PR touching many pieces of the code base. Best reviewed by filtering for "Only files owned by you" so you only see changes for your own review groups:

image

@tklauser tklauser added kind/cleanup This includes no functional changes. release-note/misc This PR makes changes that have no direct user impact. labels Mar 11, 2025
@tklauser tklauser requested review from a team as code owners March 11, 2025 15:09
@tklauser
Copy link
Member Author

tklauser commented Mar 12, 2025

Incremental diff to address first round of reviews:

diff --git a/pkg/debug/subsystem.go b/pkg/debug/subsystem.go
index 32402279db8f..9013cf1487d5 100644
--- a/pkg/debug/subsystem.go
+++ b/pkg/debug/subsystem.go
@@ -57,11 +57,9 @@ func (s *statusFunctions) registerStatusObject(name string, obj StatusObject) er
 }
 
 func (s *statusFunctions) collectStatus() StatusMap {
-       fnCopy := functionMap{}
-
        // Make a copy to not hold the mutex while collecting the status
        s.mutex.RLock()
-       maps.Copy(fnCopy, s.functions)
+       fnCopy := maps.Clone(s.functions)
        s.mutex.RUnlock()
 
        status := StatusMap{}
diff --git a/pkg/identity/cache/local.go b/pkg/identity/cache/local.go
index cc2aff6c7a88..70f05cedf719 100644
--- a/pkg/identity/cache/local.go
+++ b/pkg/identity/cache/local.go
@@ -238,14 +238,9 @@ func (l *localIdentityCache) lookupByID(id identity.NumericIdentity) *identity.I
 
 // GetIdentities returns all local identities
 func (l *localIdentityCache) GetIdentities() map[identity.NumericIdentity]*identity.Identity {
-       cache := map[identity.NumericIdentity]*identity.Identity{}
-
        l.mutex.RLock()
        defer l.mutex.RUnlock()
-
-       maps.Copy(cache, l.identitiesByID)
-
-       return cache
+       return maps.Clone(l.identitiesByID)
 }
 
 func (l *localIdentityCache) checkpoint(dst []*identity.Identity) []*identity.Identity {
diff --git a/pkg/node/manager/rest_api.go b/pkg/node/manager/rest_api.go
index e8fa02508ed3..a37b805a3863 100644
--- a/pkg/node/manager/rest_api.go
+++ b/pkg/node/manager/rest_api.go
@@ -160,12 +160,9 @@ func (c *clusterNodesClient) NodeDelete(node nodeTypes.Node) error {
        // If the node was added/updated and removed before the clusterNodesClient
        // was aware of it then we can safely remove it from the list of added
        // nodes and not set it in the list of removed nodes.
-       found := -1
-       for i, added := range c.NodesAdded {
-               if added.Name == node.Fullname() {
-                       found = i
-               }
-       }
+       found := slices.IndexFunc(c.NodesAdded, func(added *models.NodeElement) bool {
+               return added.Name == node.Fullname()
+       })
        if found != -1 {
                c.NodesAdded = slices.Delete(c.NodesAdded, found, found+1)
        } else {

@tklauser tklauser force-pushed the pr/tklauser/modernize-maps-slices branch from faa050b to 6ead8ac Compare March 12, 2025 09:22
@tklauser
Copy link
Member Author

LGTM, can we add a linter for it?

Yes. I'll add modernize to the CI once all issues are address. I plan to follow-up with PRs addressing all the other issues that modernize fixes separately (to ease review) and then add the linter in a final step.

@tklauser
Copy link
Member Author

Looks good for the file I owned 👍

Out of curiosity, is there any way to run the same check in CI ?

Yes, see my comment above. I plan to add the linter to CI once all issues it reports are resolved.

@tklauser
Copy link
Member Author

/test

@tklauser tklauser force-pushed the pr/tklauser/modernize-maps-slices branch from 6ead8ac to a620b81 Compare March 12, 2025 09:49
@tklauser
Copy link
Member Author

/test

Copy link
Contributor

@doniacld doniacld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neaaat! 🚀

Copy link
Member

@kaworu kaworu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hubble changes LGTM, thanks @tklauser 🎉

@tklauser tklauser force-pushed the pr/tklauser/modernize-maps-slices branch from a620b81 to db9c865 Compare March 14, 2025 08:46
@tklauser
Copy link
Member Author

tklauser commented Mar 14, 2025

/test

Rebased to resolve merge conflict

Use the Copy and Clone functions from the maps standard library package,
added in Go 1.21.

Generated using modernize by running:

    go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

and committing the relevant changes with some minor manual edits (e.g.
reordering added imports).

Signed-off-by: Tobias Klauser <tobias@cilium.io>
Use the convenience functions from the slices standard library package,
added in Go 1.21.

Generated using modernize by running:

    go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

and committing the relevant changes with some minor manual edits (e.g.
reordering added imports).

Signed-off-by: Tobias Klauser <tobias@cilium.io>
@tklauser tklauser force-pushed the pr/tklauser/modernize-maps-slices branch from db9c865 to 9981d34 Compare March 14, 2025 12:59
@tklauser
Copy link
Member Author

tklauser commented Mar 14, 2025

/test

...and another rebase to resolve merge conflicts

@tklauser tklauser added this pull request to the merge queue Mar 14, 2025
@tklauser tklauser removed the request for review from aditighag March 14, 2025 15:19
Merged via the queue into main with commit c512109 Mar 14, 2025
284 of 287 checks passed
@tklauser tklauser deleted the pr/tklauser/modernize-maps-slices branch March 14, 2025 15:20
@maintainer-s-little-helper maintainer-s-little-helper bot added ready-to-merge This PR has passed all tests and received consensus from code owners to merge. labels Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cilium-cli This PR contains changes related with cilium-cli hubble-cli PRs or GitHub issues related with hubble-cli kind/cleanup This includes no functional changes. ready-to-merge This PR has passed all tests and received consensus from code owners to merge. release-note/misc This PR makes changes that have no direct user impact. sig/policy Impacts whether traffic is allowed or denied based on user-defined policies.
Projects
None yet
Development

Successfully merging this pull request may close these issues.