From 5b9ae8fbf488cc3c0e9ed863df475fadde102c81 Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Sat, 30 Nov 2024 21:10:36 +0800 Subject: [PATCH] refactor(*): replace experimental `maps` and `slices` with stdlib 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 --- api/common/v1alpha1/ref.go | 10 ++-- go.mod | 5 +- pkg/config/loader_test.go | 4 +- pkg/defaults/mesh/mesh.go | 2 +- .../vips/tag_first_virtual_outbound_view.go | 2 +- pkg/kds/util/meta.go | 3 +- pkg/kds/v2/reconcile/reconciler.go | 5 +- pkg/kds/v2/server/event_based_watchdog.go | 2 +- .../core/xds/meshroute/gateway/gateway.go | 5 +- .../plugin/v1alpha1/gateway_routes.go | 4 +- .../plugin/v1alpha1/listeners.go | 3 +- .../plugin/v1alpha1/locality_aware.go | 13 +---- .../plugin/v1alpha1/plugin.go | 4 +- .../backends/reachable_backend_refs_graph.go | 2 +- .../services/reachable_services_graph.go | 2 +- pkg/plugins/resources/postgres/pgx_store.go | 2 +- pkg/plugins/runtime/gateway/generator.go | 4 +- .../runtime/gateway/route/configurers.go | 6 +- .../k8s/controllers/cni_taint_controller.go | 2 +- .../gatewayapi/http_route_conversion.go | 2 +- pkg/plugins/runtime/k8s/plugin_gateway.go | 4 +- pkg/plugins/runtime/k8s/util/util.go | 2 +- .../k8s/webhooks/resourceadmissionchecker.go | 2 +- .../config/config_executables.go | 5 +- pkg/util/maps/maps.go | 22 ++++++++ pkg/util/maps/maps_test.go | 55 +++++++++++++++++++ pkg/util/maps/sorted_keys.go | 13 ----- pkg/util/maps/sorted_keys_test.go | 25 --------- pkg/xds/generator/ingress_generator.go | 4 -- pkg/xds/generator/zoneproxy/destinations.go | 3 +- pkg/xds/ingress/dataplane.go | 3 +- pkg/xds/topology/outbound.go | 4 +- .../deployments/observability/kubernetes.go | 2 +- test/framework/universal_cluster.go | 4 +- 34 files changed, 124 insertions(+), 106 deletions(-) create mode 100644 pkg/util/maps/maps.go create mode 100644 pkg/util/maps/maps_test.go delete mode 100644 pkg/util/maps/sorted_keys.go delete mode 100644 pkg/util/maps/sorted_keys_test.go diff --git a/api/common/v1alpha1/ref.go b/api/common/v1alpha1/ref.go index 83df5bea54e4..1a40f0001134 100644 --- a/api/common/v1alpha1/ref.go +++ b/api/common/v1alpha1/ref.go @@ -7,7 +7,7 @@ import ( "sort" "strings" - "golang.org/x/exp/maps" + util_maps "github.com/kumahq/kuma/pkg/util/maps" ) type TargetRefKind string @@ -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 } @@ -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])) diff --git a/go.mod b/go.mod index d1f6919c0119..737253380f02 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 diff --git a/pkg/config/loader_test.go b/pkg/config/loader_test.go index 458830065c5d..0c24cb43a539 100644 --- a/pkg/config/loader_test.go +++ b/pkg/config/loader_test.go @@ -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" ) @@ -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))) diff --git a/pkg/defaults/mesh/mesh.go b/pkg/defaults/mesh/mesh.go index e353bf318ece..e946c287f68f 100644 --- a/pkg/defaults/mesh/mesh.go +++ b/pkg/defaults/mesh/mesh.go @@ -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" diff --git a/pkg/dns/vips/tag_first_virtual_outbound_view.go b/pkg/dns/vips/tag_first_virtual_outbound_view.go index b5fe2e2a277d..737555f225a2 100644 --- a/pkg/dns/vips/tag_first_virtual_outbound_view.go +++ b/pkg/dns/vips/tag_first_virtual_outbound_view.go @@ -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" diff --git a/pkg/kds/util/meta.go b/pkg/kds/util/meta.go index 201624d4697c..9ee3c7f86e1c 100644 --- a/pkg/kds/util/meta.go +++ b/pkg/kds/util/meta.go @@ -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" diff --git a/pkg/kds/v2/reconcile/reconciler.go b/pkg/kds/v2/reconcile/reconciler.go index 936abbd18fa4..a27f6ad36c6f 100644 --- a/pkg/kds/v2/reconcile/reconciler.go +++ b/pkg/kds/v2/reconcile/reconciler.go @@ -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" ) @@ -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)) } } } diff --git a/pkg/kds/v2/server/event_based_watchdog.go b/pkg/kds/v2/server/event_based_watchdog.go index 328aafcd8cba..7e717aa03e79 100644 --- a/pkg/kds/v2/server/event_based_watchdog.go +++ b/pkg/kds/v2/server/event_based_watchdog.go @@ -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" diff --git a/pkg/plugins/policies/core/xds/meshroute/gateway/gateway.go b/pkg/plugins/policies/core/xds/meshroute/gateway/gateway.go index 9eed8d5e9ebe..13aa83bab8f4 100644 --- a/pkg/plugins/policies/core/xds/meshroute/gateway/gateway.go +++ b/pkg/plugins/policies/core/xds/meshroute/gateway/gateway.go @@ -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" @@ -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" @@ -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]) } diff --git a/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/gateway_routes.go b/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/gateway_routes.go index e97ef49cad20..2e595878b30a 100644 --- a/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/gateway_routes.go +++ b/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/gateway_routes.go @@ -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" @@ -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" @@ -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, diff --git a/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/listeners.go b/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/listeners.go index 54e1b04a182a..9e0d693c38fd 100644 --- a/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/listeners.go +++ b/pkg/plugins/policies/meshhttproute/plugin/v1alpha1/listeners.go @@ -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" diff --git a/pkg/plugins/policies/meshloadbalancingstrategy/plugin/v1alpha1/locality_aware.go b/pkg/plugins/policies/meshloadbalancingstrategy/plugin/v1alpha1/locality_aware.go index 3ffde922094b..756115457f8e 100644 --- a/pkg/plugins/policies/meshloadbalancingstrategy/plugin/v1alpha1/locality_aware.go +++ b/pkg/plugins/policies/meshloadbalancingstrategy/plugin/v1alpha1/locality_aware.go @@ -2,12 +2,10 @@ 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" @@ -15,6 +13,7 @@ import ( 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" @@ -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 } @@ -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 -} diff --git a/pkg/plugins/policies/meshloadbalancingstrategy/plugin/v1alpha1/plugin.go b/pkg/plugins/policies/meshloadbalancingstrategy/plugin/v1alpha1/plugin.go index 99ab9e3c8719..e6ea48031f2d 100644 --- a/pkg/plugins/policies/meshloadbalancingstrategy/plugin/v1alpha1/plugin.go +++ b/pkg/plugins/policies/meshloadbalancingstrategy/plugin/v1alpha1/plugin.go @@ -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" @@ -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" @@ -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 } diff --git a/pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go b/pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go index c3655a12c94f..fd035b861822 100644 --- a/pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go +++ b/pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go @@ -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" diff --git a/pkg/plugins/policies/meshtrafficpermission/graph/services/reachable_services_graph.go b/pkg/plugins/policies/meshtrafficpermission/graph/services/reachable_services_graph.go index 33565184bca3..51ac8e422a55 100644 --- a/pkg/plugins/policies/meshtrafficpermission/graph/services/reachable_services_graph.go +++ b/pkg/plugins/policies/meshtrafficpermission/graph/services/reachable_services_graph.go @@ -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" diff --git a/pkg/plugins/resources/postgres/pgx_store.go b/pkg/plugins/resources/postgres/pgx_store.go index 61805a415675..88b64ae49ce0 100644 --- a/pkg/plugins/resources/postgres/pgx_store.go +++ b/pkg/plugins/resources/postgres/pgx_store.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "maps" "math/rand" "strconv" "strings" @@ -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" diff --git a/pkg/plugins/runtime/gateway/generator.go b/pkg/plugins/runtime/gateway/generator.go index bfd12dd56d44..ba297687fb08 100644 --- a/pkg/plugins/runtime/gateway/generator.go +++ b/pkg/plugins/runtime/gateway/generator.go @@ -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" @@ -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" @@ -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) diff --git a/pkg/plugins/runtime/gateway/route/configurers.go b/pkg/plugins/runtime/gateway/route/configurers.go index 96f1bcf235a9..1e387b44580a 100644 --- a/pkg/plugins/runtime/gateway/route/configurers.go +++ b/pkg/plugins/runtime/gateway/route/configurers.go @@ -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" @@ -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 diff --git a/pkg/plugins/runtime/k8s/controllers/cni_taint_controller.go b/pkg/plugins/runtime/k8s/controllers/cni_taint_controller.go index 9c41192e358f..92e790d6c930 100644 --- a/pkg/plugins/runtime/k8s/controllers/cni_taint_controller.go +++ b/pkg/plugins/runtime/k8s/controllers/cni_taint_controller.go @@ -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" diff --git a/pkg/plugins/runtime/k8s/controllers/gatewayapi/http_route_conversion.go b/pkg/plugins/runtime/k8s/controllers/gatewayapi/http_route_conversion.go index 27d5403e4c9e..6ea90ba6661c 100644 --- a/pkg/plugins/runtime/k8s/controllers/gatewayapi/http_route_conversion.go +++ b/pkg/plugins/runtime/k8s/controllers/gatewayapi/http_route_conversion.go @@ -3,10 +3,10 @@ package gatewayapi import ( "context" "fmt" + "slices" "strings" "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_apimeta "k8s.io/apimachinery/pkg/api/meta" diff --git a/pkg/plugins/runtime/k8s/plugin_gateway.go b/pkg/plugins/runtime/k8s/plugin_gateway.go index d285d8fbd706..575ee2f9a61b 100644 --- a/pkg/plugins/runtime/k8s/plugin_gateway.go +++ b/pkg/plugins/runtime/k8s/plugin_gateway.go @@ -5,7 +5,6 @@ import ( "os" "github.com/pkg/errors" - "golang.org/x/exp/maps" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/discovery" kube_ctrl "sigs.k8s.io/controller-runtime" @@ -21,6 +20,7 @@ import ( "github.com/kumahq/kuma/pkg/plugins/runtime/k8s/controllers" gatewayapi_controllers "github.com/kumahq/kuma/pkg/plugins/runtime/k8s/controllers/gatewayapi" k8s_webhooks "github.com/kumahq/kuma/pkg/plugins/runtime/k8s/webhooks" + util_maps "github.com/kumahq/kuma/pkg/util/maps" ) var requiredGatewayCRDs = map[string]string{ @@ -139,7 +139,7 @@ func addGatewayAPIReconcilers(mgr kube_ctrl.Manager, rt core_runtime.Runtime, pr log.Error( errors.New("only subset of required GatewayAPI CRDs registered"), "disabling support for GatewayAPI", - "required", maps.Values(requiredGatewayCRDs), + "required", util_maps.AllValues(requiredGatewayCRDs), "missing", missingGatewayCRDs, ) } else { diff --git a/pkg/plugins/runtime/k8s/util/util.go b/pkg/plugins/runtime/k8s/util/util.go index 26636ddb7723..db9424c78b52 100644 --- a/pkg/plugins/runtime/k8s/util/util.go +++ b/pkg/plugins/runtime/k8s/util/util.go @@ -2,10 +2,10 @@ package util import ( "fmt" + "maps" "strings" "github.com/go-logr/logr" - "golang.org/x/exp/maps" kube_core "k8s.io/api/core/v1" kube_labels "k8s.io/apimachinery/pkg/labels" kube_types "k8s.io/apimachinery/pkg/types" diff --git a/pkg/plugins/runtime/k8s/webhooks/resourceadmissionchecker.go b/pkg/plugins/runtime/k8s/webhooks/resourceadmissionchecker.go index 1b4136c0bb65..5df26d0a1088 100644 --- a/pkg/plugins/runtime/k8s/webhooks/resourceadmissionchecker.go +++ b/pkg/plugins/runtime/k8s/webhooks/resourceadmissionchecker.go @@ -2,9 +2,9 @@ package webhooks import ( "fmt" + "slices" "strings" - "golang.org/x/exp/slices" v1 "k8s.io/api/admission/v1" authenticationv1 "k8s.io/api/authentication/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" diff --git a/pkg/transparentproxy/config/config_executables.go b/pkg/transparentproxy/config/config_executables.go index bf201ff25032..cb972784c5f7 100644 --- a/pkg/transparentproxy/config/config_executables.go +++ b/pkg/transparentproxy/config/config_executables.go @@ -14,7 +14,6 @@ import ( "time" "github.com/pkg/errors" - "golang.org/x/exp/maps" k8s_version "k8s.io/apimachinery/pkg/util/version" "github.com/kumahq/kuma/pkg/transparentproxy/consts" @@ -691,7 +690,7 @@ func inferIptablesMode(executables ...InitializedExecutable) (consts.IptablesMod modesSet[executable.version.Mode] = struct{}{} } - modes := maps.Keys(modesSet) + modes := util_maps.AllKeys(modesSet) if len(modes) != 1 { return consts.IptablesModeUnknown, errors.Errorf( @@ -740,7 +739,7 @@ func getNonEmptyPaths(eps ...executablesPaths) []string { var result []string for _, ep := range eps { - result = slices.Concat(result, removeEmptyStrings(maps.Values(ep.getPathsMap()))) + result = slices.Concat(result, removeEmptyStrings(util_maps.AllValues(ep.getPathsMap()))) } return result diff --git a/pkg/util/maps/maps.go b/pkg/util/maps/maps.go new file mode 100644 index 000000000000..721ae073abb9 --- /dev/null +++ b/pkg/util/maps/maps.go @@ -0,0 +1,22 @@ +package maps + +import ( + "cmp" + "maps" + "slices" +) + +func SortedKeys[M ~map[K]V, K cmp.Ordered, V any](m M) []K { + keys := maps.Keys(m) + return slices.Sorted(keys) +} + +func AllKeys[M ~map[K]V, K comparable, V any](m M) []K { + keys := maps.Keys(m) + return slices.Collect(keys) +} + +func AllValues[M ~map[K]V, K comparable, V any](m M) []V { + values := maps.Values(m) + return slices.Collect(values) +} diff --git a/pkg/util/maps/maps_test.go b/pkg/util/maps/maps_test.go new file mode 100644 index 000000000000..5a32ba2cacf6 --- /dev/null +++ b/pkg/util/maps/maps_test.go @@ -0,0 +1,55 @@ +package maps_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/kumahq/kuma/pkg/util/maps" +) + +var _ = Describe("Maps", func() { + It("SortedKeys should return sorted keys", func() { + // given + m := map[string]string{ + "c": "x", + "b": "y", + "a": "z", + } + + // when + keys := maps.SortedKeys(m) + + // then + Expect(keys).To(Equal([]string{"a", "b", "c"})) + }) + + It("AllKeys should return all keys", func() { + // given + m := map[string]string{ + "c": "x", + "b": "y", + "a": "z", + } + + // when + keys := maps.AllKeys(m) + + // then + Expect(keys).To(ConsistOf([]string{"c", "b", "a"})) + }) + + It("AllValues should return all values", func() { + // given + m := map[string]string{ + "c": "x", + "b": "y", + "a": "z", + } + + // when + keys := maps.AllValues(m) + + // then + Expect(keys).To(ConsistOf([]string{"x", "y", "z"})) + }) +}) diff --git a/pkg/util/maps/sorted_keys.go b/pkg/util/maps/sorted_keys.go deleted file mode 100644 index ccf860249683..000000000000 --- a/pkg/util/maps/sorted_keys.go +++ /dev/null @@ -1,13 +0,0 @@ -package maps - -import ( - "golang.org/x/exp/constraints" - "golang.org/x/exp/maps" - "golang.org/x/exp/slices" -) - -func SortedKeys[M ~map[K]V, K constraints.Ordered, V any](m M) []K { - keys := maps.Keys(m) - slices.Sort(keys) - return keys -} diff --git a/pkg/util/maps/sorted_keys_test.go b/pkg/util/maps/sorted_keys_test.go deleted file mode 100644 index d16f67e4a072..000000000000 --- a/pkg/util/maps/sorted_keys_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package maps_test - -import ( - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/kumahq/kuma/pkg/util/maps" -) - -var _ = Describe("SortedKeys", func() { - It("should return sorted keys", func() { - // given - m := map[string]string{ - "c": "x", - "b": "y", - "a": "z", - } - - // when - keys := maps.SortedKeys(m) - - // then - Expect(keys).To(Equal([]string{"a", "b", "c"})) - }) -}) diff --git a/pkg/xds/generator/ingress_generator.go b/pkg/xds/generator/ingress_generator.go index 2fab4cc2cdd5..27af1fedb244 100644 --- a/pkg/xds/generator/ingress_generator.go +++ b/pkg/xds/generator/ingress_generator.go @@ -2,10 +2,8 @@ package generator import ( "context" - "sort" envoy_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3" - "golang.org/x/exp/maps" mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1" meshservice_api "github.com/kumahq/kuma/pkg/core/resources/apis/meshservice/api/v1alpha1" @@ -45,8 +43,6 @@ func (i IngressGenerator) Generate( for _, mr := range proxy.ZoneIngressProxy.MeshResourceList { meshName := mr.Mesh.GetMeta().GetName() - serviceList := maps.Keys(mr.EndpointMap) - sort.Strings(serviceList) meshResources := xds_context.Resources{MeshLocalResources: mr.Resources} // we only want to expose local mesh services diff --git a/pkg/xds/generator/zoneproxy/destinations.go b/pkg/xds/generator/zoneproxy/destinations.go index b5d17e211319..2bc57459ae37 100644 --- a/pkg/xds/generator/zoneproxy/destinations.go +++ b/pkg/xds/generator/zoneproxy/destinations.go @@ -2,8 +2,7 @@ package zoneproxy import ( "reflect" - - "golang.org/x/exp/slices" + "slices" mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1" core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh" diff --git a/pkg/xds/ingress/dataplane.go b/pkg/xds/ingress/dataplane.go index d4b7fa9ed1ee..354e6aa49b5f 100644 --- a/pkg/xds/ingress/dataplane.go +++ b/pkg/xds/ingress/dataplane.go @@ -2,11 +2,10 @@ package ingress import ( "fmt" + "slices" "sort" "strings" - "golang.org/x/exp/slices" - mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1" core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh" "github.com/kumahq/kuma/pkg/core/xds" diff --git a/pkg/xds/topology/outbound.go b/pkg/xds/topology/outbound.go index cce127ea64bd..bb76ba7baf54 100644 --- a/pkg/xds/topology/outbound.go +++ b/pkg/xds/topology/outbound.go @@ -8,7 +8,6 @@ import ( "github.com/asaskevich/govalidator" "github.com/pkg/errors" - exp_maps "golang.org/x/exp/maps" "k8s.io/apimachinery/pkg/util/intstr" common_tls "github.com/kumahq/kuma/api/common/v1alpha1/tls" @@ -24,6 +23,7 @@ import ( "github.com/kumahq/kuma/pkg/core/resources/model" core_xds "github.com/kumahq/kuma/pkg/core/xds" core_rules "github.com/kumahq/kuma/pkg/plugins/policies/core/rules" + util_maps "github.com/kumahq/kuma/pkg/util/maps" "github.com/kumahq/kuma/pkg/util/pointer" envoy_tags "github.com/kumahq/kuma/pkg/xds/envoy/tags" ) @@ -107,7 +107,7 @@ func BuildEdsEndpointMap( ) core_xds.EndpointMap { outbound := core_xds.EndpointMap{} - meshServices := exp_maps.Values(meshServicesByName) + meshServices := util_maps.AllValues(meshServicesByName) fillLocalMeshServices(outbound, meshServices, dataplanes, mesh, localZone) // we want to prefer endpoints build by MeshService diff --git a/test/framework/deployments/observability/kubernetes.go b/test/framework/deployments/observability/kubernetes.go index c0897632966d..258b4616a138 100644 --- a/test/framework/deployments/observability/kubernetes.go +++ b/test/framework/deployments/observability/kubernetes.go @@ -2,9 +2,9 @@ package observability import ( "fmt" + "slices" "github.com/gruntwork-io/terratest/modules/k8s" - "golang.org/x/exp/slices" "github.com/kumahq/kuma/test/framework" ) diff --git a/test/framework/universal_cluster.go b/test/framework/universal_cluster.go index 0f5930b135e4..8aaf48727e88 100644 --- a/test/framework/universal_cluster.go +++ b/test/framework/universal_cluster.go @@ -13,11 +13,11 @@ import ( "github.com/gruntwork-io/terratest/modules/testing" "github.com/pkg/errors" "go.uber.org/multierr" - "golang.org/x/exp/maps" "github.com/kumahq/kuma/pkg/config/core" core_mesh "github.com/kumahq/kuma/pkg/core/resources/apis/mesh" core_model "github.com/kumahq/kuma/pkg/core/resources/model" + util_maps "github.com/kumahq/kuma/pkg/util/maps" "github.com/kumahq/kuma/pkg/util/pointer" "github.com/kumahq/kuma/pkg/util/template" "github.com/kumahq/kuma/test/framework/envoy_admin" @@ -450,7 +450,7 @@ func (c *UniversalCluster) DeleteMesh(mesh string) error { func (c *UniversalCluster) DeleteMeshApps(mesh string) error { c.mutex.RLock() - apps := maps.Keys(c.apps) + apps := util_maps.AllKeys(c.apps) c.mutex.RUnlock() for _, name := range apps { if c.GetApp(name).mesh == mesh {