diff --git a/.golangci.yml b/.golangci.yml index d45d103ea8c..d14407e3672 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -24,6 +24,7 @@ linters: - gosimple - makezero - dupword + - gomodguard linters-settings: goheader: @@ -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 diff --git a/go.mod b/go.mod index 294c61e6e46..da2d96ccc38 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/pkg/bugtool/maps.go b/pkg/bugtool/maps.go index 697e2e9e029..dc3ed4242bc 100644 --- a/pkg/bugtool/maps.go +++ b/pkg/bugtool/maps.go @@ -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. @@ -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) @@ -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") } @@ -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 { @@ -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 @@ -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 } } @@ -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 }) diff --git a/pkg/eventcache/metrics.go b/pkg/eventcache/metrics.go index e13090cd1db..530b0ee8202 100644 --- a/pkg/eventcache/metrics.go +++ b/pkg/eventcache/metrics.go @@ -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" @@ -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)), } ) diff --git a/pkg/metrics/eventmetrics/eventmetrics.go b/pkg/metrics/eventmetrics/eventmetrics.go index b26503259b5..06314ecb567 100644 --- a/pkg/metrics/eventmetrics/eventmetrics.go +++ b/pkg/metrics/eventmetrics/eventmetrics.go @@ -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" @@ -32,7 +33,7 @@ var ( } perfEventErrorLabel = metrics.ConstrainedLabel{ Name: "error", - Values: maps.Values(perfEventErrors), + Values: slices.Collect(maps.Values(perfEventErrors)), } ) diff --git a/pkg/metrics/policyfiltermetrics/policyfiltermetrics.go b/pkg/metrics/policyfiltermetrics/policyfiltermetrics.go index 39c005eb2b9..8369bacb7bf 100644 --- a/pkg/metrics/policyfiltermetrics/policyfiltermetrics.go +++ b/pkg/metrics/policyfiltermetrics/policyfiltermetrics.go @@ -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" @@ -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)), } )