Skip to content
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

Switch to Go 1.23+ stdlib maps/slices packages #3080

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ linters:
- gosimple
- makezero
- dupword
- gomodguard

linters-settings:
goheader:
Expand All @@ -35,6 +36,19 @@ linters-settings:
Copyright Authors of {{ PROJECT }}
dupword:
keywords: ["the", "and", "a", "for", "to", "as", "in", "of", "with", "by", "on", "at", "from"]
gomodguard:
blocked:
modules:
- golang.org/x/exp/maps:
recommendations:
- iter
- maps
- slices
reason: "Go 1.23+ supports iterating over maps and slices, see https://go.dev/doc/go1.23#iterators"
- golang.org/x/exp/slices:
recommendations:
- slices
reason: "Go 1.21+ provides many common operations for slices using generic functions, see https://go.dev/doc/go1.21#slices"

issues:
max-issues-per-linter: 0
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ require (
github.com/vishvananda/netlink v1.3.1-0.20241022031324-976bd8de7d81
go.uber.org/atomic v1.11.0
go.uber.org/multierr v1.11.0
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6
golang.org/x/sync v0.8.0
golang.org/x/sys v0.26.0
golang.org/x/time v0.7.0
Expand Down Expand Up @@ -152,6 +151,7 @@ require (
go.opentelemetry.io/otel/metric v1.31.0 // indirect
go.opentelemetry.io/otel/trace v1.31.0 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
Expand Down
16 changes: 9 additions & 7 deletions pkg/bugtool/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (
"errors"
"fmt"
"io/fs"
"iter"
"maps"
"os"
"path/filepath"
"slices"
"sort"
"syscall"

"github.com/cilium/ebpf"
"github.com/cilium/tetragon/pkg/bpf"
"golang.org/x/exp/maps"
)

// TotalMemlockBytes iterates over the extend map info and sums the memlock field.
Expand All @@ -35,7 +37,7 @@ func FindMapsUsedByPinnedProgs(path string) ([]bpf.ExtendedMapInfo, error) {
return nil, fmt.Errorf("failed retrieving map IDs: %w", err)
}
mapInfos := []bpf.ExtendedMapInfo{}
for _, mapID := range mapIDs {
for mapID := range mapIDs {
memlockInfo, err := bpf.ExtendedInfoFromMapID(mapID)
if err != nil {
return nil, fmt.Errorf("failed retrieving map memlock from ID: %w", err)
Expand Down Expand Up @@ -125,7 +127,7 @@ func FindPinnedMaps(path string) ([]bpf.ExtendedMapInfo, error) {
}

// mapIDsFromProgs retrieves all map IDs used inside a prog.
func mapIDsFromProgs(prog *ebpf.Program) ([]int, error) {
func mapIDsFromProgs(prog *ebpf.Program) (iter.Seq[int], error) {
if prog == nil {
return nil, fmt.Errorf("prog is nil")
}
Expand All @@ -149,7 +151,7 @@ func mapIDsFromProgs(prog *ebpf.Program) ([]int, error) {
// prog pinned under the path. It also retrieves the map IDs used by the prog
// referenced by program array maps (tail calls). This should only work from
// kernel 4.15.
func mapIDsFromPinnedProgs(path string) ([]int, error) {
func mapIDsFromPinnedProgs(path string) (iter.Seq[int], error) {
mapSet := map[int]bool{}
progArrays := []*ebpf.Map{}
err := filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
Expand Down Expand Up @@ -200,7 +202,7 @@ func mapIDsFromPinnedProgs(path string) ([]int, error) {
if err != nil {
return fmt.Errorf("failed to retrieve map IDs from prog: %w", err)
}
for _, id := range newIDs {
for id := range newIDs {
mapSet[id] = true
}
return nil
Expand Down Expand Up @@ -238,7 +240,7 @@ func mapIDsFromPinnedProgs(path string) ([]int, error) {
if err != nil {
return nil, fmt.Errorf("failed to retrieve map IDs from prog: %w", err)
}
for _, id := range newIDs {
for id := range newIDs {
mapSet[id] = true
}
}
Expand Down Expand Up @@ -399,7 +401,7 @@ func RunMapsChecks(path string) (*MapsChecksOutput, error) {
}{m, 1}
}
}
aggregatedMaps := maps.Values(aggregatedMapsSet)
aggregatedMaps := slices.Collect(maps.Values(aggregatedMapsSet))
sort.Slice(aggregatedMaps, func(i, j int) bool {
return aggregatedMaps[i].Memlock > aggregatedMaps[j].Memlock
})
Expand Down
7 changes: 4 additions & 3 deletions pkg/eventcache/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package eventcache

import (
"golang.org/x/exp/maps"
"maps"
"slices"

"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/pkg/metrics"
Expand Down Expand Up @@ -51,11 +52,11 @@ func (e CacheError) String() string {
var (
entryTypeLabel = metrics.ConstrainedLabel{
Name: "entry_type",
Values: maps.Values(cacheEntryTypeLabelValues),
Values: slices.Collect(maps.Values(cacheEntryTypeLabelValues)),
}
errorLabel = metrics.ConstrainedLabel{
Name: "error",
Values: maps.Values(cacheErrorLabelValues),
Values: slices.Collect(maps.Values(cacheErrorLabelValues)),
}
)

Expand Down
5 changes: 3 additions & 2 deletions pkg/metrics/eventmetrics/eventmetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package eventmetrics

import (
"golang.org/x/exp/maps"
"maps"
"slices"

v1 "github.com/cilium/cilium/pkg/hubble/api/v1"
"github.com/cilium/tetragon/api/v1/tetragon"
Expand Down Expand Up @@ -32,7 +33,7 @@ var (
}
perfEventErrorLabel = metrics.ConstrainedLabel{
Name: "error",
Values: maps.Values(perfEventErrors),
Values: slices.Collect(maps.Values(perfEventErrors)),
}
)

Expand Down
9 changes: 5 additions & 4 deletions pkg/metrics/policyfiltermetrics/policyfiltermetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package policyfiltermetrics

import (
"golang.org/x/exp/maps"
"maps"
"slices"

"github.com/cilium/tetragon/pkg/metrics"
"github.com/cilium/tetragon/pkg/metrics/consts"
Expand Down Expand Up @@ -68,17 +69,17 @@ func (s OperationErr) String() string {
var (
subsysLabel = metrics.ConstrainedLabel{
Name: "subsys",
Values: maps.Values(subsysLabelValues),
Values: slices.Collect(maps.Values(subsysLabelValues)),
}

operationLabel = metrics.ConstrainedLabel{
Name: "operation",
Values: maps.Values(operationLabelValues),
Values: slices.Collect(maps.Values(operationLabelValues)),
}

errorLabel = metrics.ConstrainedLabel{
Name: "error",
Values: maps.Values(operationErrLabels),
Values: slices.Collect(maps.Values(operationErrLabels)),
}
)

Expand Down
Loading