From a779c2c2e5b9e89920a5e2ae52e4268e84bb7079 Mon Sep 17 00:00:00 2001 From: pastequo Date: Thu, 13 Jul 2023 17:39:43 +0200 Subject: [PATCH] chore: Migrate to sets.Set[string] when possible --- pkg/reconciler/ingress/ingress.go | 20 ++--- pkg/reconciler/ingress/ingress_test.go | 20 ++--- pkg/reconciler/ingress/lister.go | 12 ++- pkg/reconciler/ingress/resources/gateway.go | 2 +- .../ingress/resources/virtual_service.go | 61 +++++++------ .../ingress/resources/virtual_service_test.go | 85 +++++++++---------- 6 files changed, 108 insertions(+), 92 deletions(-) diff --git a/pkg/reconciler/ingress/ingress.go b/pkg/reconciler/ingress/ingress.go index 30bb17aa33..f6d981adc5 100644 --- a/pkg/reconciler/ingress/ingress.go +++ b/pkg/reconciler/ingress/ingress.go @@ -111,9 +111,9 @@ func (r *Reconciler) reconcileIngress(ctx context.Context, ing *v1alpha1.Ingress ing.Status.InitializeConditions() logger.Infof("Reconciling ingress: %#v", ing) - gatewayNames := map[v1alpha1.IngressVisibility]sets.String{} + gatewayNames := map[v1alpha1.IngressVisibility]sets.Set[string]{} gatewayNames[v1alpha1.IngressVisibilityClusterLocal] = qualifiedGatewayNamesFromContext(ctx)[v1alpha1.IngressVisibilityClusterLocal] - gatewayNames[v1alpha1.IngressVisibilityExternalIP] = sets.String{} + gatewayNames[v1alpha1.IngressVisibilityExternalIP] = sets.New[string]() ingressGateways := []*v1beta1.Gateway{} if shouldReconcileTLS(ing) { @@ -178,7 +178,7 @@ func (r *Reconciler) reconcileIngress(ctx context.Context, ing *v1alpha1.Ingress // Otherwise, we fall back to the default global Gateways for HTTP behavior. // We need this for the backward compatibility. defaultGlobalHTTPGateways := qualifiedGatewayNamesFromContext(ctx)[v1alpha1.IngressVisibilityExternalIP] - gatewayNames[v1alpha1.IngressVisibilityExternalIP].Insert(defaultGlobalHTTPGateways.List()...) + gatewayNames[v1alpha1.IngressVisibilityExternalIP].Insert(sets.List(defaultGlobalHTTPGateways)...) } if err := r.reconcileIngressGateways(ctx, ingressGateways); err != nil { @@ -235,13 +235,13 @@ func (r *Reconciler) reconcileIngress(ctx context.Context, ing *v1alpha1.Ingress } func getPublicHosts(ing *v1alpha1.Ingress) []string { - hosts := sets.String{} + hosts := sets.New[string]() for _, rule := range ing.Spec.Rules { if rule.Visibility == v1alpha1.IngressVisibilityExternalIP { hosts.Insert(rule.Hosts...) } } - return hosts.List() + return sets.List(hosts) } func (r *Reconciler) reconcileCertSecrets(ctx context.Context, ing *v1alpha1.Ingress, desiredSecrets []*corev1.Secret) error { @@ -297,7 +297,7 @@ func (r *Reconciler) reconcileSystemGeneratedGateway(ctx context.Context, desire func (r *Reconciler) reconcileVirtualServices(ctx context.Context, ing *v1alpha1.Ingress, desired []*v1beta1.VirtualService) error { // First, create all needed VirtualServices. - kept := sets.NewString() + kept := sets.New[string]() for _, d := range desired { if d.GetAnnotations()[networking.IngressClassAnnotationKey] != netconfig.IstioIngressClassName { // We do not create resources that do not have istio ingress class annotation. @@ -438,19 +438,19 @@ func (r *Reconciler) GetVirtualServiceLister() istiolisters.VirtualServiceLister } // qualifiedGatewayNamesFromContext get gateway names from context -func qualifiedGatewayNamesFromContext(ctx context.Context) map[v1alpha1.IngressVisibility]sets.String { +func qualifiedGatewayNamesFromContext(ctx context.Context) map[v1alpha1.IngressVisibility]sets.Set[string] { ci := config.FromContext(ctx).Istio - publicGateways := make(sets.String, len(ci.IngressGateways)) + publicGateways := sets.New[string]() for _, gw := range ci.IngressGateways { publicGateways.Insert(gw.QualifiedName()) } - privateGateways := make(sets.String, len(ci.LocalGateways)) + privateGateways := sets.New[string]() for _, gw := range ci.LocalGateways { privateGateways.Insert(gw.QualifiedName()) } - return map[v1alpha1.IngressVisibility]sets.String{ + return map[v1alpha1.IngressVisibility]sets.Set[string]{ v1alpha1.IngressVisibilityExternalIP: publicGateways, v1alpha1.IngressVisibilityClusterLocal: privateGateways, } diff --git a/pkg/reconciler/ingress/ingress_test.go b/pkg/reconciler/ingress/ingress_test.go index 8ec78577e8..e75d433288 100644 --- a/pkg/reconciler/ingress/ingress_test.go +++ b/pkg/reconciler/ingress/ingress_test.go @@ -140,11 +140,11 @@ var ( "gateway." + config.KnativeIngressGateway: newDomainInternal, "gateway.knative-test-gateway": originDomainInternal, } - ingressGateway = map[v1alpha1.IngressVisibility]sets.String{ - v1alpha1.IngressVisibilityExternalIP: sets.NewString(config.KnativeIngressGateway), + ingressGateway = map[v1alpha1.IngressVisibility]sets.Set[string]{ + v1alpha1.IngressVisibilityExternalIP: sets.New(config.KnativeIngressGateway), } - gateways = map[v1alpha1.IngressVisibility]sets.String{ - v1alpha1.IngressVisibilityExternalIP: sets.NewString("knative-test-gateway", config.KnativeIngressGateway), + gateways = map[v1alpha1.IngressVisibility]sets.Set[string]{ + v1alpha1.IngressVisibilityExternalIP: sets.New("knative-test-gateway", config.KnativeIngressGateway), } perIngressGatewayName = resources.GatewayName(ingressWithTLS("reconciling-ingress", ingressTLS), ingressService) ) @@ -1256,7 +1256,7 @@ func ingressWithTLSAndStatusClusterLocal(name string, tls []v1alpha1.IngressTLS, return ci } -func meshVirtualServiceWithStatus(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.String, status *istiov1alpha1.IstioStatus, generation int64, observedGeneration int64) *v1beta1.VirtualService { +func meshVirtualServiceWithStatus(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.Set[string], status *istiov1alpha1.IstioStatus, generation int64, observedGeneration int64) *v1beta1.VirtualService { vs := resources.MakeMeshVirtualService(ing, gateways) vs.Status = *status.DeepCopy() vs.ObjectMeta.Generation = generation @@ -1265,7 +1265,7 @@ func meshVirtualServiceWithStatus(ing *v1alpha1.Ingress, gateways map[v1alpha1.I return vs } -func ingressVirtualServiceWithStatus(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.String, status *istiov1alpha1.IstioStatus, generation int64, observedGeneration int64) *v1beta1.VirtualService { +func ingressVirtualServiceWithStatus(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.Set[string], status *istiov1alpha1.IstioStatus, generation int64, observedGeneration int64) *v1beta1.VirtualService { vs := resources.MakeIngressVirtualService(ing, gateways) vs.Status = *status.DeepCopy() vs.ObjectMeta.Generation = generation @@ -1536,10 +1536,10 @@ func TestGlobalResyncOnUpdateNetwork(t *testing.T) { } } -func makeGatewayMap(publicGateways []string, privateGateways []string) map[v1alpha1.IngressVisibility]sets.String { - return map[v1alpha1.IngressVisibility]sets.String{ - v1alpha1.IngressVisibilityExternalIP: sets.NewString(publicGateways...), - v1alpha1.IngressVisibilityClusterLocal: sets.NewString(privateGateways...), +func makeGatewayMap(publicGateways []string, privateGateways []string) map[v1alpha1.IngressVisibility]sets.Set[string] { + return map[v1alpha1.IngressVisibility]sets.Set[string]{ + v1alpha1.IngressVisibilityExternalIP: sets.New(publicGateways...), + v1alpha1.IngressVisibilityClusterLocal: sets.New(privateGateways...), } } diff --git a/pkg/reconciler/ingress/lister.go b/pkg/reconciler/ingress/lister.go index 04f162de08..c8689299f4 100644 --- a/pkg/reconciler/ingress/lister.go +++ b/pkg/reconciler/ingress/lister.go @@ -60,7 +60,7 @@ type gatewayPodTargetLister struct { func (l *gatewayPodTargetLister) ListProbeTargets(ctx context.Context, ing *v1alpha1.Ingress) ([]status.ProbeTarget, error) { results := []status.ProbeTarget{} - hostsByGateway := ingress.HostsPerVisibility(ing, qualifiedGatewayNamesFromContext(ctx)) + hostsByGateway := ingress.HostsPerVisibility(ing, convertVisibilityMap(qualifiedGatewayNamesFromContext(ctx))) gatewayNames := make([]string, 0, len(hostsByGateway)) for gatewayName := range hostsByGateway { gatewayNames = append(gatewayNames, gatewayName) @@ -129,7 +129,7 @@ func (l *gatewayPodTargetLister) listGatewayTargets(gateway *v1beta1.Gateway) ([ return nil, fmt.Errorf("failed to get Endpoints: %w", err) } - seen := sets.NewString() + seen := sets.New[string]() targets := []status.ProbeTarget{} for _, server := range gateway.Spec.Servers { tURL := &url.URL{} @@ -187,3 +187,11 @@ func (l *gatewayPodTargetLister) listGatewayTargets(gateway *v1beta1.Gateway) ([ } return targets, nil } + +// Remove me when ingress.HostsPerVisibility uses sets.Set[string] +func convertVisibilityMap(input map[v1alpha1.IngressVisibility]sets.Set[string]) map[v1alpha1.IngressVisibility]sets.String { + return map[v1alpha1.IngressVisibility]sets.String{ + v1alpha1.IngressVisibilityExternalIP: sets.NewString(sets.List(input[v1alpha1.IngressVisibilityExternalIP])...), + v1alpha1.IngressVisibilityClusterLocal: sets.NewString(sets.List(input[v1alpha1.IngressVisibilityClusterLocal])...), + } +} diff --git a/pkg/reconciler/ingress/resources/gateway.go b/pkg/reconciler/ingress/resources/gateway.go index e5fa560df9..a693b36911 100644 --- a/pkg/reconciler/ingress/resources/gateway.go +++ b/pkg/reconciler/ingress/resources/gateway.go @@ -398,7 +398,7 @@ func GetIngressGatewaySvcNameNamespaces(ctx context.Context) ([]metav1.ObjectMet // UpdateGateway replaces the existing servers with the wanted servers. func UpdateGateway(gateway *v1beta1.Gateway, want []*istiov1beta1.Server, existing []*istiov1beta1.Server) *v1beta1.Gateway { - existingServers := sets.String{} + existingServers := sets.New[string]() for i := range existing { existingServers.Insert(existing[i].Port.Name) } diff --git a/pkg/reconciler/ingress/resources/virtual_service.go b/pkg/reconciler/ingress/resources/virtual_service.go index c970f899f2..7f929aef97 100644 --- a/pkg/reconciler/ingress/resources/virtual_service.go +++ b/pkg/reconciler/ingress/resources/virtual_service.go @@ -45,7 +45,7 @@ func VirtualServiceNamespace(ing *v1alpha1.Ingress) string { // MakeIngressVirtualService creates Istio VirtualService as network // programming for Istio Gateways other than 'mesh'. -func MakeIngressVirtualService(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.String) *v1beta1.VirtualService { +func MakeIngressVirtualService(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.Set[string]) *v1beta1.VirtualService { vs := &v1beta1.VirtualService{ ObjectMeta: metav1.ObjectMeta{ Name: names.IngressVirtualService(ing), @@ -53,7 +53,7 @@ func MakeIngressVirtualService(ing *v1alpha1.Ingress, gateways map[v1alpha1.Ingr OwnerReferences: []metav1.OwnerReference{*kmeta.NewControllerRef(ing)}, Annotations: ing.GetAnnotations(), }, - Spec: *makeVirtualServiceSpec(ing, gateways, ingress.ExpandedHosts(getHosts(ing))), + Spec: *makeVirtualServiceSpec(ing, gateways, expandedHosts(getHosts(ing))), } // Populate the Ingress labels. @@ -65,12 +65,12 @@ func MakeIngressVirtualService(ing *v1alpha1.Ingress, gateways map[v1alpha1.Ingr } // MakeMeshVirtualService creates a mesh Virtual Service -func MakeMeshVirtualService(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.String) *v1beta1.VirtualService { +func MakeMeshVirtualService(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.Set[string]) *v1beta1.VirtualService { hosts := keepLocalHostnames(getHosts(ing)) // If cluster local gateway is configured, we need to expand hosts because of // https://github.com/knative/serving/issues/6488#issuecomment-573513768. if len(gateways[v1alpha1.IngressVisibilityClusterLocal]) != 0 { - hosts = ingress.ExpandedHosts(hosts) + hosts = expandedHosts(hosts) } if len(hosts) == 0 { return nil @@ -82,9 +82,9 @@ func MakeMeshVirtualService(ing *v1alpha1.Ingress, gateways map[v1alpha1.Ingress OwnerReferences: []metav1.OwnerReference{*kmeta.NewControllerRef(ing)}, Annotations: ing.GetAnnotations(), }, - Spec: *makeVirtualServiceSpec(ing, map[v1alpha1.IngressVisibility]sets.String{ - v1alpha1.IngressVisibilityExternalIP: sets.NewString("mesh"), - v1alpha1.IngressVisibilityClusterLocal: sets.NewString("mesh"), + Spec: *makeVirtualServiceSpec(ing, map[v1alpha1.IngressVisibility]sets.Set[string]{ + v1alpha1.IngressVisibilityExternalIP: sets.New("mesh"), + v1alpha1.IngressVisibilityClusterLocal: sets.New("mesh"), }, hosts), } // Populate the Ingress labels. @@ -96,7 +96,7 @@ func MakeMeshVirtualService(ing *v1alpha1.Ingress, gateways map[v1alpha1.Ingress } // MakeVirtualServices creates a mesh VirtualService and a virtual service for each gateway -func MakeVirtualServices(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.String) ([]*v1beta1.VirtualService, error) { +func MakeVirtualServices(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.Set[string]) ([]*v1beta1.VirtualService, error) { // Insert probe header ing = ing.DeepCopy() if _, err := ingress.InsertProbe(ing); err != nil { @@ -122,16 +122,16 @@ func MakeVirtualServices(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVis return vss, nil } -func makeVirtualServiceSpec(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.String, hosts sets.String) *istiov1beta1.VirtualService { +func makeVirtualServiceSpec(ing *v1alpha1.Ingress, gateways map[v1alpha1.IngressVisibility]sets.Set[string], hosts sets.Set[string]) *istiov1beta1.VirtualService { spec := istiov1beta1.VirtualService{ - Hosts: hosts.List(), + Hosts: sets.List(hosts), } - gw := sets.String{} + gw := sets.New[string]() for _, rule := range ing.Spec.Rules { for i := range rule.HTTP.Paths { p := rule.HTTP.Paths[i] - hosts := hosts.Intersection(sets.NewString(rule.Hosts...)) + hosts := hosts.Intersection(sets.New(rule.Hosts...)) if hosts.Len() != 0 { http := makeVirtualServiceRoute(hosts, &p, gateways, rule.Visibility) // Add all the Gateways that exist inside the http.match section of @@ -139,22 +139,22 @@ func makeVirtualServiceSpec(ing *v1alpha1.Ingress, gateways map[v1alpha1.Ingress // This ensures that we are only using the Gateways that actually appear // in VirtualService routes. for _, m := range http.Match { - gw = gw.Union(sets.NewString(m.Gateways...)) + gw = gw.Union(sets.New(m.Gateways...)) } spec.Http = append(spec.Http, http) } } } - spec.Gateways = gw.List() + spec.Gateways = sets.List(gw) return &spec } -func makeVirtualServiceRoute(hosts sets.String, http *v1alpha1.HTTPIngressPath, gateways map[v1alpha1.IngressVisibility]sets.String, visibility v1alpha1.IngressVisibility) *istiov1beta1.HTTPRoute { +func makeVirtualServiceRoute(hosts sets.Set[string], http *v1alpha1.HTTPIngressPath, gateways map[v1alpha1.IngressVisibility]sets.Set[string], visibility v1alpha1.IngressVisibility) *istiov1beta1.HTTPRoute { matches := []*istiov1beta1.HTTPMatchRequest{} // Deduplicate hosts to avoid excessive matches, which cause a combinatorial expansion in Istio distinctHosts := getDistinctHostPrefixes(hosts) - for _, host := range distinctHosts.List() { + for _, host := range sets.List(distinctHosts) { matches = append(matches, makeMatch(host, http.Path, http.Headers, gateways[visibility])) } @@ -210,11 +210,11 @@ func makeVirtualServiceRoute(hosts sets.String, http *v1alpha1.HTTPIngressPath, // getDistinctHostPrefixes deduplicate a set of prefix matches. For example, the set {a, aabb} can be // reduced to {a}, as a prefix match on {a} accepts all the same inputs as {a, aabb}. -func getDistinctHostPrefixes(hosts sets.String) sets.String { +func getDistinctHostPrefixes(hosts sets.Set[string]) sets.Set[string] { // First we sort the list. This ensures that we always process the smallest elements (which match against // the most patterns, as they are less specific) first. - all := hosts.List() - ns := sets.NewString() + all := sets.List(hosts) + ns := sets.New[string]() for _, h := range all { prefixExists := false h = hostPrefix(h) @@ -233,10 +233,10 @@ func getDistinctHostPrefixes(hosts sets.String) sets.String { return ns } -func keepLocalHostnames(hosts sets.String) sets.String { +func keepLocalHostnames(hosts sets.Set[string]) sets.Set[string] { localSvcSuffix := ".svc." + network.GetClusterDomainName() - retained := sets.NewString() - for _, h := range hosts.List() { + retained := sets.New[string]() + for _, h := range sets.List(hosts) { if strings.HasSuffix(h, localSvcSuffix) { retained.Insert(h) } @@ -244,9 +244,9 @@ func keepLocalHostnames(hosts sets.String) sets.String { return retained } -func makeMatch(host, path string, headers map[string]v1alpha1.HeaderMatch, gateways sets.String) *istiov1beta1.HTTPMatchRequest { +func makeMatch(host, path string, headers map[string]v1alpha1.HeaderMatch, gateways sets.Set[string]) *istiov1beta1.HTTPMatchRequest { match := &istiov1beta1.HTTPMatchRequest{ - Gateways: gateways.List(), + Gateways: sets.List(gateways), Authority: &istiov1beta1.StringMatch{ // Do not use Regex as Istio 1.4 or later has 100 bytes limitation. MatchType: &istiov1beta1.StringMatch_Prefix{Prefix: host}, @@ -283,8 +283,8 @@ func hostPrefix(host string) string { return strings.TrimSuffix(host, localDomainSuffix) } -func getHosts(ing *v1alpha1.Ingress) sets.String { - hosts := sets.NewString() +func getHosts(ing *v1alpha1.Ingress) sets.Set[string] { + hosts := sets.New[string]() for _, rule := range ing.Spec.Rules { hosts.Insert(rule.Hosts...) } @@ -312,3 +312,12 @@ func getPublicIngressRules(i *v1alpha1.Ingress) []v1alpha1.IngressRule { return result } + +// Keep me until ingress.ExpandedHosts uses sets.Set[string] +func expandedHosts(hosts sets.Set[string]) sets.Set[string] { + tmp := sets.NewString(sets.List(hosts)...) + + ret := ingress.ExpandedHosts(tmp) + + return sets.New(ret.List()...) +} diff --git a/pkg/reconciler/ingress/resources/virtual_service_test.go b/pkg/reconciler/ingress/resources/virtual_service_test.go index 5e4bf47ab9..4d61c0c557 100644 --- a/pkg/reconciler/ingress/resources/virtual_service_test.go +++ b/pkg/reconciler/ingress/resources/virtual_service_test.go @@ -27,7 +27,6 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "knative.dev/networking/pkg/apis/networking" "knative.dev/networking/pkg/apis/networking/v1alpha1" - "knative.dev/networking/pkg/ingress" "knative.dev/pkg/kmeta" "knative.dev/pkg/system" _ "knative.dev/pkg/system/testing" @@ -65,7 +64,7 @@ var ( func TestMakeVirtualServices_CorrectMetadata(t *testing.T) { for _, tc := range []struct { name string - gateways map[v1alpha1.IngressVisibility]sets.String + gateways map[v1alpha1.IngressVisibility]sets.Set[string] ci *v1alpha1.Ingress expected []metav1.ObjectMeta }{{ @@ -217,8 +216,8 @@ func TestMakeVirtualServicesSpec_CorrectGateways(t *testing.T) { tests := []struct { name string ingress *v1alpha1.Ingress - gateways map[v1alpha1.IngressVisibility]sets.String - expectedGateways sets.String + gateways map[v1alpha1.IngressVisibility]sets.Set[string] + expectedGateways sets.Set[string] }{ { name: "local visibility", @@ -233,11 +232,11 @@ func TestMakeVirtualServicesSpec_CorrectGateways(t *testing.T) { }, }, }, - gateways: map[v1alpha1.IngressVisibility]sets.String{ - v1alpha1.IngressVisibilityClusterLocal: sets.NewString("knative-local-gateway/knative-serving"), - v1alpha1.IngressVisibilityExternalIP: sets.NewString("knative-ingress-gateway/knative-serving"), + gateways: map[v1alpha1.IngressVisibility]sets.Set[string]{ + v1alpha1.IngressVisibilityClusterLocal: sets.New("knative-local-gateway/knative-serving"), + v1alpha1.IngressVisibilityExternalIP: sets.New("knative-ingress-gateway/knative-serving"), }, - expectedGateways: sets.NewString("knative-local-gateway/knative-serving"), + expectedGateways: sets.New("knative-local-gateway/knative-serving"), }, { name: "public visibility", @@ -252,11 +251,11 @@ func TestMakeVirtualServicesSpec_CorrectGateways(t *testing.T) { }, }, }, - gateways: map[v1alpha1.IngressVisibility]sets.String{ - v1alpha1.IngressVisibilityClusterLocal: sets.NewString("knative-local-gateway/knative-serving"), - v1alpha1.IngressVisibilityExternalIP: sets.NewString("knative-ingress-gateway/knative-serving"), + gateways: map[v1alpha1.IngressVisibility]sets.Set[string]{ + v1alpha1.IngressVisibilityClusterLocal: sets.New("knative-local-gateway/knative-serving"), + v1alpha1.IngressVisibilityExternalIP: sets.New("knative-ingress-gateway/knative-serving"), }, - expectedGateways: sets.NewString("knative-ingress-gateway/knative-serving"), + expectedGateways: sets.New("knative-ingress-gateway/knative-serving"), }, { name: "local and public visibility", @@ -276,21 +275,21 @@ func TestMakeVirtualServicesSpec_CorrectGateways(t *testing.T) { }, }, }, - gateways: map[v1alpha1.IngressVisibility]sets.String{ - v1alpha1.IngressVisibilityClusterLocal: sets.NewString("knative-local-gateway/knative-serving"), - v1alpha1.IngressVisibilityExternalIP: sets.NewString("knative-ingress-gateway/knative-serving"), + gateways: map[v1alpha1.IngressVisibility]sets.Set[string]{ + v1alpha1.IngressVisibilityClusterLocal: sets.New("knative-local-gateway/knative-serving"), + v1alpha1.IngressVisibilityExternalIP: sets.New("knative-ingress-gateway/knative-serving"), }, - expectedGateways: sets.NewString("knative-ingress-gateway/knative-serving", + expectedGateways: sets.New("knative-ingress-gateway/knative-serving", "knative-local-gateway/knative-serving"), }, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - vs := makeVirtualServiceSpec(tc.ingress, tc.gateways, ingress.ExpandedHosts(getHosts(tc.ingress))) - actualGateways := sets.NewString(vs.Gateways...) + vs := makeVirtualServiceSpec(tc.ingress, tc.gateways, expandedHosts(getHosts(tc.ingress))) + actualGateways := sets.New(vs.Gateways...) if !actualGateways.Equal(tc.expectedGateways) { - t.Fatalf("Got gateways %v, expected %v", actualGateways.List(), tc.expectedGateways.List()) + t.Fatalf("Got gateways %v, expected %v", sets.List(actualGateways), sets.List(tc.expectedGateways)) } }) } @@ -325,26 +324,26 @@ func TestMakeMeshVirtualServiceSpec_CorrectGateways(t *testing.T) { func TestMakeMeshVirtualServiceSpecCorrectHosts(t *testing.T) { for _, tc := range []struct { name string - gateways map[v1alpha1.IngressVisibility]sets.String - expectedHosts sets.String + gateways map[v1alpha1.IngressVisibility]sets.Set[string] + expectedHosts sets.Set[string] }{{ name: "with cluster local gateway: expanding hosts", - gateways: map[v1alpha1.IngressVisibility]sets.String{ - v1alpha1.IngressVisibilityClusterLocal: sets.NewString("cluster-local"), + gateways: map[v1alpha1.IngressVisibility]sets.Set[string]{ + v1alpha1.IngressVisibilityClusterLocal: sets.New("cluster-local"), }, - expectedHosts: sets.NewString( + expectedHosts: sets.New( "test-route.test-ns.svc.cluster.local", "test-route.test-ns.svc", "test-route.test-ns", ), }, { name: "with mesh: no exapnding hosts", - gateways: map[v1alpha1.IngressVisibility]sets.String{}, - expectedHosts: sets.NewString("test-route.test-ns.svc.cluster.local"), + gateways: map[v1alpha1.IngressVisibility]sets.Set[string]{}, + expectedHosts: sets.New("test-route.test-ns.svc.cluster.local"), }} { t.Run(tc.name, func(t *testing.T) { vs := MakeMeshVirtualService(&defaultIngress, tc.gateways) - vsHosts := sets.NewString(vs.Spec.Hosts...) + vsHosts := sets.New(vs.Spec.Hosts...) if !vsHosts.Equal(tc.expectedHosts) { t.Errorf("Unexpected hosts want %v; got %v", tc.expectedHosts, vsHosts) } @@ -602,7 +601,7 @@ func TestMakeVirtualServiceRoute_RewriteHost(t *testing.T) { }, }}, } - route := makeVirtualServiceRoute(sets.NewString("a.vanity.url", "another.vanity.url"), ingressPath, makeGatewayMap([]string{"gateway-1"}, nil), v1alpha1.IngressVisibilityExternalIP) + route := makeVirtualServiceRoute(sets.New("a.vanity.url", "another.vanity.url"), ingressPath, makeGatewayMap([]string{"gateway-1"}, nil), v1alpha1.IngressVisibilityExternalIP) expected := &istiov1beta1.HTTPRoute{ Retries: &istiov1beta1.HTTPRetry{}, Match: []*istiov1beta1.HTTPMatchRequest{{ @@ -651,7 +650,7 @@ func TestMakeVirtualServiceRoute_Vanilla(t *testing.T) { Percent: 100, }}, } - route := makeVirtualServiceRoute(sets.NewString("a.com", "b.org"), ingressPath, makeGatewayMap([]string{"gateway-1"}, nil), v1alpha1.IngressVisibilityExternalIP) + route := makeVirtualServiceRoute(sets.New("a.com", "b.org"), ingressPath, makeGatewayMap([]string{"gateway-1"}, nil), v1alpha1.IngressVisibilityExternalIP) expected := &istiov1beta1.HTTPRoute{ Retries: &istiov1beta1.HTTPRetry{}, Match: []*istiov1beta1.HTTPMatchRequest{{ @@ -704,7 +703,7 @@ func TestMakeVirtualServiceRoute_Internal(t *testing.T) { Percent: 100, }}, } - route := makeVirtualServiceRoute(sets.NewString("a.default", "a.default.svc", "a.default.svc.cluster.local"), + route := makeVirtualServiceRoute(sets.New("a.default", "a.default.svc", "a.default.svc.cluster.local"), ingressPath, makeGatewayMap([]string{"gateway-1"}, nil), v1alpha1.IngressVisibilityExternalIP) expected := &istiov1beta1.HTTPRoute{ Retries: &istiov1beta1.HTTPRetry{}, @@ -746,7 +745,7 @@ func TestMakeVirtualServiceRoute_TwoTargets(t *testing.T) { Percent: 10, }}, } - route := makeVirtualServiceRoute(sets.NewString("test.org"), ingressPath, makeGatewayMap([]string{"knative-testing/gateway-1"}, nil), v1alpha1.IngressVisibilityExternalIP) + route := makeVirtualServiceRoute(sets.New("test.org"), ingressPath, makeGatewayMap([]string{"knative-testing/gateway-1"}, nil), v1alpha1.IngressVisibilityExternalIP) expected := &istiov1beta1.HTTPRoute{ Retries: &istiov1beta1.HTTPRetry{}, Match: []*istiov1beta1.HTTPMatchRequest{{ @@ -785,30 +784,30 @@ func TestGetHosts_Duplicate(t *testing.T) { }, } hosts := getHosts(ci) - expected := sets.NewString("test-route1", "test-route2", "test-route3") + expected := sets.New("test-route1", "test-route2", "test-route3") if diff := cmp.Diff(expected, hosts); diff != "" { t.Error("Unexpected hosts (-want +got):", diff) } } -func makeGatewayMap(publicGateways []string, privateGateways []string) map[v1alpha1.IngressVisibility]sets.String { - return map[v1alpha1.IngressVisibility]sets.String{ - v1alpha1.IngressVisibilityExternalIP: sets.NewString(publicGateways...), - v1alpha1.IngressVisibilityClusterLocal: sets.NewString(privateGateways...), +func makeGatewayMap(publicGateways []string, privateGateways []string) map[v1alpha1.IngressVisibility]sets.Set[string] { + return map[v1alpha1.IngressVisibility]sets.Set[string]{ + v1alpha1.IngressVisibilityExternalIP: sets.New(publicGateways...), + v1alpha1.IngressVisibilityClusterLocal: sets.New(privateGateways...), } } func TestGetDistinctHostPrefixes(t *testing.T) { cases := []struct { name string - in sets.String - out sets.String + in sets.Set[string] + out sets.Set[string] }{ - {"empty", sets.NewString(), sets.NewString()}, - {"single element", sets.NewString("a"), sets.NewString("a")}, - {"no overlap", sets.NewString("a", "b"), sets.NewString("a", "b")}, - {"overlap", sets.NewString("a", "ab", "abc"), sets.NewString("a")}, - {"multiple overlaps", sets.NewString("a", "ab", "abc", "xyz", "xy", "m"), sets.NewString("a", "xy", "m")}, + {"empty", sets.New[string](), sets.New[string]()}, + {"single element", sets.New("a"), sets.New("a")}, + {"no overlap", sets.New("a", "b"), sets.New("a", "b")}, + {"overlap", sets.New("a", "ab", "abc"), sets.New("a")}, + {"multiple overlaps", sets.New("a", "ab", "abc", "xyz", "xy", "m"), sets.New("a", "xy", "m")}, } for _, tt := range cases { t.Run(tt.name, func(t *testing.T) {