Skip to content

Commit

Permalink
refactor(*): replace experimental maps and slices with stdlib
Browse files Browse the repository at this point in the history
The experimental functions are now available in the standard library in
Go 1.21 [1] and Go 1.23 [2].

[1]: https://go.dev/doc/go1.21#slices
[2]: https://go.dev/doc/go1.23#iterators

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee committed Nov 30, 2024
1 parent d318db1 commit 5b9ae8f
Show file tree
Hide file tree
Showing 34 changed files with 124 additions and 106 deletions.
10 changes: 4 additions & 6 deletions api/common/v1alpha1/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sort"
"strings"

"golang.org/x/exp/maps"
util_maps "github.com/kumahq/kuma/pkg/util/maps"
)

type TargetRefKind string
Expand Down Expand Up @@ -67,7 +67,7 @@ func (k TargetRefKind) IsOldKind() bool {
}

func AllTargetRefKinds() []TargetRefKind {
keys := maps.Keys(order)
keys := util_maps.AllKeys(order)
sort.Sort(TargetRefKindSlice(keys))
return keys
}
Expand Down Expand Up @@ -152,15 +152,13 @@ type BackendRefHash string

// Hash returns a hash of the BackendRef
func (in BackendRef) Hash() BackendRefHash {
keys := maps.Keys(in.Tags)
sort.Strings(keys)
keys := util_maps.SortedKeys(in.Tags)
orderedTags := make([]string, 0, len(keys))
for _, k := range keys {
orderedTags = append(orderedTags, fmt.Sprintf("%s=%s", k, in.Tags[k]))
}

keys = maps.Keys(in.Labels)
sort.Strings(keys)
keys = util_maps.SortedKeys(in.Labels)
orderedLabels := make([]string, 0, len(in.Labels))
for _, k := range keys {
orderedLabels = append(orderedLabels, fmt.Sprintf("%s=%s", k, in.Labels[k]))
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ require (
go.opentelemetry.io/proto/otlp v1.3.1
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e
golang.org/x/net v0.31.0
golang.org/x/sync v0.9.0
golang.org/x/sys v0.27.0
golang.org/x/text v0.20.0
gonum.org/v1/gonum v0.15.1
Expand All @@ -91,8 +91,6 @@ require (
sigs.k8s.io/yaml v1.4.0
)

require golang.org/x/sync v0.9.0

require (
cel.dev/expr v0.16.1 // indirect
dario.cat/mergo v1.0.1 // indirect
Expand Down Expand Up @@ -216,6 +214,7 @@ require (
go.opentelemetry.io/otel/metric v1.32.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/term v0.26.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"golang.org/x/exp/maps"

"github.com/kumahq/kuma/pkg/config"
kuma_cp "github.com/kumahq/kuma/pkg/config/app/kuma-cp"
config_core "github.com/kumahq/kuma/pkg/config/core"
"github.com/kumahq/kuma/pkg/config/core/resources/store"
"github.com/kumahq/kuma/pkg/config/plugins/resources/postgres"
util_maps "github.com/kumahq/kuma/pkg/util/maps"
"github.com/kumahq/kuma/test/testenvconfig"
)

Expand Down Expand Up @@ -77,7 +77,7 @@ var _ = Describe("Config loader", func() {
testEnvs[key] = struct{}{}
}

Expect(maps.Keys(testEnvs)).To(ConsistOf(maps.Keys(configEnvs)), "config values are not overridden in the test. Add overrides for them with a value that is different than default.")
Expect(util_maps.AllKeys(testEnvs)).To(ConsistOf(util_maps.AllKeys(configEnvs)), "config values are not overridden in the test. Add overrides for them with a value that is different than default.")
}

Expect(cfg.BootstrapServer.Params.AdminPort).To(Equal(uint32(1234)))
Expand Down
2 changes: 1 addition & 1 deletion pkg/defaults/mesh/mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package mesh
import (
"context"
"fmt"
"slices"
"strings"
"sync"

"github.com/pkg/errors"
"golang.org/x/exp/slices"

"github.com/kumahq/kuma/pkg/core"
"github.com/kumahq/kuma/pkg/core/resources/manager"
Expand Down
2 changes: 1 addition & 1 deletion pkg/dns/vips/tag_first_virtual_outbound_view.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package vips

import (
"maps"
"reflect"
"strings"

"github.com/asaskevich/govalidator"
"golang.org/x/exp/maps"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
"github.com/kumahq/kuma/pkg/util/pointer"
Expand Down
3 changes: 1 addition & 2 deletions pkg/kds/util/meta.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package util

import (
"maps"
"strings"
"time"

"golang.org/x/exp/maps"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
config_store "github.com/kumahq/kuma/pkg/config/core/resources/store"
"github.com/kumahq/kuma/pkg/core/resources/model"
Expand Down
5 changes: 3 additions & 2 deletions pkg/kds/v2/reconcile/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package reconcile

import (
"context"
"maps"
"sync"

envoy_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_cache "github.com/envoyproxy/go-control-plane/pkg/cache/v3"
"github.com/go-logr/logr"
"github.com/pkg/errors"
"golang.org/x/exp/maps"

config_core "github.com/kumahq/kuma/pkg/config/core"
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
cache_v2 "github.com/kumahq/kuma/pkg/kds/v2/cache"
util_kds_v2 "github.com/kumahq/kuma/pkg/kds/v2/util"
"github.com/kumahq/kuma/pkg/multitenant"
util_maps "github.com/kumahq/kuma/pkg/util/maps"
"github.com/kumahq/kuma/pkg/util/xds"
)

Expand Down Expand Up @@ -73,7 +74,7 @@ func (r *reconciler) Reconcile(ctx context.Context, node *envoy_core.Node, chang

oldRes := old.GetResources(typ)
if len(oldRes) > 0 {
builder = builder.With(resType, maps.Values(oldRes))
builder = builder.With(resType, util_maps.AllValues(oldRes))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kds/v2/server/event_based_watchdog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package server
import (
"context"
"errors"
"maps"
"strings"
"time"

envoy_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
"github.com/go-logr/logr"
"golang.org/x/exp/maps"

"github.com/kumahq/kuma/pkg/core"
"github.com/kumahq/kuma/pkg/core/resources/model"
Expand Down
5 changes: 2 additions & 3 deletions pkg/plugins/policies/core/xds/meshroute/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"slices"

"golang.org/x/exp/maps"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
"github.com/kumahq/kuma/pkg/core/permissions"
core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh"
Expand All @@ -14,6 +12,7 @@ import (
"github.com/kumahq/kuma/pkg/plugins/policies/core/rules"
plugin_gateway "github.com/kumahq/kuma/pkg/plugins/runtime/gateway"
"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/match"
util_maps "github.com/kumahq/kuma/pkg/util/maps"
xds_context "github.com/kumahq/kuma/pkg/xds/context"
envoy_names "github.com/kumahq/kuma/pkg/xds/envoy/names"
xds_topology "github.com/kumahq/kuma/pkg/xds/topology"
Expand Down Expand Up @@ -159,7 +158,7 @@ func SortByHostname(listenersByHostname map[string]plugin_gateway.GatewayListene
}

var listenerHostnames []plugin_gateway.GatewayListenerHostname
for _, hostname := range match.SortHostnamesByExactnessDec(maps.Keys(listenersByHostname)) {
for _, hostname := range match.SortHostnamesByExactnessDec(util_maps.AllKeys(listenersByHostname)) {
listenerHostnames = append(listenerHostnames, listenersByHostname[hostname])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"slices"
"strings"

"golang.org/x/exp/maps"
"k8s.io/apimachinery/pkg/util/intstr"

common_api "github.com/kumahq/kuma/api/common/v1alpha1"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/match"
"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/metadata"
"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/route"
util_maps "github.com/kumahq/kuma/pkg/util/maps"
"github.com/kumahq/kuma/pkg/util/pointer"
xds_context "github.com/kumahq/kuma/pkg/xds/context"
"github.com/kumahq/kuma/pkg/xds/envoy/tags"
Expand Down Expand Up @@ -52,7 +52,7 @@ func sortRulesToHosts(
// under the same route config
var observedHostnames []string

for _, hostname := range match.SortHostnamesByExactnessDec(maps.Keys(sublistenersByHostname)) {
for _, hostname := range match.SortHostnamesByExactnessDec(util_maps.AllKeys(sublistenersByHostname)) {
hostnameTag := sublistenersByHostname[hostname]
inboundListener := rules.NewInboundListenerHostname(
address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package v1alpha1

import (
"reflect"

"golang.org/x/exp/slices"
"slices"

common_api "github.com/kumahq/kuma/api/common/v1alpha1"
mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package v1alpha1

import (
"fmt"
"sort"
"strings"

envoy_cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
envoy_endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/wrapperspb"

common_api "github.com/kumahq/kuma/api/common/v1alpha1"
mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
core_xds "github.com/kumahq/kuma/pkg/core/xds"
api "github.com/kumahq/kuma/pkg/plugins/policies/meshloadbalancingstrategy/api/v1alpha1"
util_maps "github.com/kumahq/kuma/pkg/util/maps"
"github.com/kumahq/kuma/pkg/xds/cache/sha256"
envoy_endpoints "github.com/kumahq/kuma/pkg/xds/envoy/endpoints"
envoy_metadata "github.com/kumahq/kuma/pkg/xds/envoy/metadata/v3"
Expand Down Expand Up @@ -200,11 +199,11 @@ func egressLocality(crossZoneGroups []CrossZoneLbGroup) *core_xds.Locality {
for _, group := range crossZoneGroups {
switch group.Type {
case api.Only:
builder.WriteString(fmt.Sprintf("%d:%s", group.Priority, strings.Join(sortedZones(group.Zones), ",")))
builder.WriteString(fmt.Sprintf("%d:%s", group.Priority, strings.Join(util_maps.SortedKeys(group.Zones), ",")))
case api.Any:
builder.WriteString(fmt.Sprintf("%d:%s", group.Priority, group.Type))
case api.AnyExcept:
builder.WriteString(fmt.Sprintf("%d:%s:%s", group.Priority, group.Type, strings.Join(sortedZones(group.Zones), ",")))
builder.WriteString(fmt.Sprintf("%d:%s:%s", group.Priority, group.Type, strings.Join(util_maps.SortedKeys(group.Zones), ",")))
default:
continue
}
Expand All @@ -216,9 +215,3 @@ func egressLocality(crossZoneGroups []CrossZoneLbGroup) *core_xds.Locality {
Priority: 1,
}
}

func sortedZones(zones map[string]bool) []string {
keys := maps.Keys(zones)
sort.Strings(keys)
return keys
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
envoy_hcm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
envoy_resource "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"github.com/pkg/errors"
"golang.org/x/exp/maps"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
core_plugins "github.com/kumahq/kuma/pkg/core/plugins"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/kumahq/kuma/pkg/plugins/policies/meshloadbalancingstrategy/plugin/xds"
gateway_plugin "github.com/kumahq/kuma/pkg/plugins/runtime/gateway"
"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/metadata"
util_maps "github.com/kumahq/kuma/pkg/util/maps"
"github.com/kumahq/kuma/pkg/util/pointer"
xds_context "github.com/kumahq/kuma/pkg/xds/context"
v3 "github.com/kumahq/kuma/pkg/xds/envoy/listeners/v3"
Expand Down Expand Up @@ -408,7 +408,7 @@ func (p plugin) configureEgress(rs *core_xds.ResourceSet, proxy *core_xds.Proxy)
// Zone egress is a single point for multiple clients. At this moment we don't support different
// configurations based on the client. That's why we are computing rules for MeshSubset
func (p plugin) computeFrom(fr core_rules.FromRules) *core_rules.Rule {
rules := maps.Values(fr.Rules)
rules := util_maps.AllValues(fr.Rules)
if len(rules) == 0 {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package backends

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

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
"github.com/kumahq/kuma/pkg/core"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package services

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

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
"github.com/kumahq/kuma/pkg/core"
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/resources/postgres/pgx_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"math/rand"
"strconv"
"strings"
Expand All @@ -15,7 +16,6 @@ import (
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/exp/maps"

config "github.com/kumahq/kuma/pkg/config/plugins/resources/postgres"
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/runtime/gateway/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

envoy_service_runtime_v3 "github.com/envoyproxy/go-control-plane/envoy/service/runtime/v3"
"github.com/pkg/errors"
"golang.org/x/exp/maps"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
"github.com/kumahq/kuma/pkg/core/permissions"
Expand All @@ -21,6 +20,7 @@ import (
"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/merge"
"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/metadata"
"github.com/kumahq/kuma/pkg/plugins/runtime/gateway/route"
util_maps "github.com/kumahq/kuma/pkg/util/maps"
util_proto "github.com/kumahq/kuma/pkg/util/proto"
xds_context "github.com/kumahq/kuma/pkg/xds/context"
envoy_listeners "github.com/kumahq/kuma/pkg/xds/envoy/listeners"
Expand Down Expand Up @@ -430,7 +430,7 @@ func MakeGatewayListener(
}

var listenerHostnames []GatewayListenerHostname
for _, hostname := range match.SortHostnamesByExactnessDec(maps.Keys(hostsByName)) {
for _, hostname := range match.SortHostnamesByExactnessDec(util_maps.AllKeys(hostsByName)) {
hostAcc := hostsByName[hostname]
hosts := RedistributeWildcardRoutes(hostAcc.hosts)

Expand Down
6 changes: 2 additions & 4 deletions pkg/plugins/runtime/gateway/route/configurers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
envoy_config_route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
envoy_type_matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
"github.com/pkg/errors"
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
core_xds "github.com/kumahq/kuma/pkg/core/xds"
util_maps "github.com/kumahq/kuma/pkg/util/maps"
util_proto "github.com/kumahq/kuma/pkg/util/proto"
xds_context "github.com/kumahq/kuma/pkg/xds/context"
envoy_listeners "github.com/kumahq/kuma/pkg/xds/envoy/listeners/v3"
Expand Down Expand Up @@ -172,8 +171,7 @@ func RouteActionForward(xdsCtx xds_context.Context, endpoints core_xds.EndpointM

var weights []*envoy_config_route.WeightedCluster_ClusterWeight

names := maps.Keys(byName)
slices.Sort(names)
names := util_maps.SortedKeys(byName)
for _, name := range names {
destination := byName[name]
var requestHeadersToAdd []*envoy_config_core.HeaderValueOption
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package controllers

import (
"context"
"slices"

"github.com/go-logr/logr"
"github.com/pkg/errors"
"golang.org/x/exp/slices"
kube_core "k8s.io/api/core/v1"
kube_apierrs "k8s.io/apimachinery/pkg/api/errors"
kube_types "k8s.io/apimachinery/pkg/types"
Expand Down
Loading

0 comments on commit 5b9ae8f

Please sign in to comment.