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

chore: use sets.Set[T] instead of map[T]struct{} #3471

Merged
merged 1 commit into from
May 24, 2024
Merged
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: 7 additions & 7 deletions internal/cmd/egctl/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,8 @@ func addMissingServices(requiredServices map[string]*v1.Service, obj interface{}
func kubernetesYAMLToResources(str string, addMissingResources bool) (*gatewayapi.Resources, error) {
resources := gatewayapi.NewResources()
var useDefaultNamespace bool
providedNamespaceMap := map[string]struct{}{}
requiredNamespaceMap := map[string]struct{}{}
providedNamespaceMap := sets.New[string]()
requiredNamespaceMap := sets.New[string]()
yamls := strings.Split(str, "\n---")
combinedScheme := envoygateway.GetScheme()
for _, y := range yamls {
Expand All @@ -655,7 +655,7 @@ func kubernetesYAMLToResources(str string, addMissingResources bool) (*gatewayap
useDefaultNamespace = true
namespace = config.DefaultNamespace
}
requiredNamespaceMap[namespace] = struct{}{}
requiredNamespaceMap.Insert(namespace)
kobj, err := combinedScheme.New(gvk)
if err != nil {
return nil, err
Expand Down Expand Up @@ -779,7 +779,7 @@ func kubernetesYAMLToResources(str string, addMissingResources bool) (*gatewayap
},
}
resources.Namespaces = append(resources.Namespaces, namespace)
providedNamespaceMap[name] = struct{}{}
providedNamespaceMap.Insert(name)
case gatewayapi.KindService:
typedSpec := spec.Interface()
service := &v1.Service{
Expand Down Expand Up @@ -850,20 +850,20 @@ func kubernetesYAMLToResources(str string, addMissingResources bool) (*gatewayap
}

if useDefaultNamespace {
if _, found := providedNamespaceMap[config.DefaultNamespace]; !found {
if !providedNamespaceMap.Has(config.DefaultNamespace) {
namespace := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: config.DefaultNamespace,
},
}
resources.Namespaces = append(resources.Namespaces, namespace)
providedNamespaceMap[config.DefaultNamespace] = struct{}{}
providedNamespaceMap.Insert(config.DefaultNamespace)
}
}

if addMissingResources {
for ns := range requiredNamespaceMap {
if _, found := providedNamespaceMap[ns]; !found {
if !providedNamespaceMap.Has(ns) {
namespace := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns,
Expand Down
58 changes: 29 additions & 29 deletions internal/provider/kubernetes/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@

type resourceMappings struct {
// Map for storing namespaces for Route, Service and Gateway objects.
allAssociatedNamespaces map[string]struct{}
allAssociatedNamespaces sets.Set[string]
// Map for storing TLSRoutes' NamespacedNames attaching to various Gateway objects.
allAssociatedTLSRoutes map[string]struct{}
allAssociatedTLSRoutes sets.Set[string]
// Map for storing HTTPRoutes' NamespacedNames attaching to various Gateway objects.
allAssociatedHTTPRoutes map[string]struct{}
allAssociatedHTTPRoutes sets.Set[string]
// Map for storing GRPCRoutes' NamespacedNames attaching to various Gateway objects.
allAssociatedGRPCRoutes map[string]struct{}
allAssociatedGRPCRoutes sets.Set[string]
// Map for storing TCPRoutes' NamespacedNames attaching to various Gateway objects.
allAssociatedTCPRoutes map[string]struct{}
allAssociatedTCPRoutes sets.Set[string]
// Map for storing UDPRoutes' NamespacedNames attaching to various Gateway objects.
allAssociatedUDPRoutes map[string]struct{}
allAssociatedUDPRoutes sets.Set[string]
// Map for storing backendRefs' NamespaceNames referred by various Route objects.
allAssociatedBackendRefs map[gwapiv1.BackendObjectReference]struct{}
allAssociatedBackendRefs sets.Set[gwapiv1.BackendObjectReference]
// extensionRefFilters is a map of filters managed by an extension.
// The key is the namespaced name, group and kind of the filter and the value is the
// unstructured form of the resource.
Expand All @@ -136,13 +136,13 @@

func newResourceMapping() *resourceMappings {
return &resourceMappings{
allAssociatedNamespaces: map[string]struct{}{},
allAssociatedTLSRoutes: map[string]struct{}{},
allAssociatedHTTPRoutes: map[string]struct{}{},
allAssociatedGRPCRoutes: map[string]struct{}{},
allAssociatedTCPRoutes: map[string]struct{}{},
allAssociatedUDPRoutes: map[string]struct{}{},
allAssociatedBackendRefs: map[gwapiv1.BackendObjectReference]struct{}{},
allAssociatedNamespaces: sets.New[string](),
allAssociatedTLSRoutes: sets.New[string](),
allAssociatedHTTPRoutes: sets.New[string](),
allAssociatedGRPCRoutes: sets.New[string](),
allAssociatedTCPRoutes: sets.New[string](),
allAssociatedUDPRoutes: sets.New[string](),
allAssociatedBackendRefs: sets.New[gwapiv1.BackendObjectReference](),
extensionRefFilters: map[utils.NamespacedNameWithGroupKind]unstructured.Unstructured{},
}
}
Expand Down Expand Up @@ -367,7 +367,7 @@
r.log.Error(err, "failed to get Service", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
} else {
resourceMappings.allAssociatedNamespaces[service.Namespace] = struct{}{}
resourceMappings.allAssociatedNamespaces.Insert(service.Namespace)
gwcResource.Services = append(gwcResource.Services, service)
r.log.Info("added Service to resource tree", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
Expand All @@ -381,7 +381,7 @@
r.log.Error(err, "failed to get ServiceImport", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
} else {
resourceMappings.allAssociatedNamespaces[serviceImport.Namespace] = struct{}{}
resourceMappings.allAssociatedNamespaces.Insert(serviceImport.Namespace)

Check warning on line 384 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L384

Added line #L384 was not covered by tests
gwcResource.ServiceImports = append(gwcResource.ServiceImports, serviceImport)
r.log.Info("added ServiceImport to resource tree", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
Expand Down Expand Up @@ -472,12 +472,12 @@
}

backendNamespace := gatewayapi.NamespaceDerefOr(backendRef.Namespace, policy.Namespace)
resourceMap.allAssociatedBackendRefs[gwapiv1.BackendObjectReference{
resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{

Check warning on line 475 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L475

Added line #L475 was not covered by tests
Group: backendRef.Group,
Kind: backendRef.Kind,
Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace),
Name: backendRef.Name,
}] = struct{}{}
})

Check warning on line 480 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L480

Added line #L480 was not covered by tests

if backendNamespace != policy.Namespace {
from := ObjectKindNamespacedName{
Expand Down Expand Up @@ -583,7 +583,7 @@
"name", refGrant.Name)
}
}
resourceMap.allAssociatedNamespaces[secretNS] = struct{}{} // TODO Zhaohuabing do we need this line?
resourceMap.allAssociatedNamespaces.Insert(secretNS) // TODO Zhaohuabing do we need this line?

Check warning on line 586 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L586

Added line #L586 was not covered by tests
resourceTree.Secrets = append(resourceTree.Secrets, secret)
r.log.Info("processing Secret", "namespace", secretNS, "name", string(secretRef.Name))
return nil
Expand Down Expand Up @@ -685,7 +685,7 @@
"name", refGrant.Name)
}
}
resourceMap.allAssociatedNamespaces[configMapNS] = struct{}{} // TODO Zhaohuabing do we need this line?
resourceMap.allAssociatedNamespaces.Insert(configMapNS) // TODO Zhaohuabing do we need this line?

Check warning on line 688 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L688

Added line #L688 was not covered by tests
resourceTree.ConfigMaps = append(resourceTree.ConfigMaps, configMap)
r.log.Info("processing ConfigMap", "namespace", configMapNS, "name", string(configMapRef.Name))
return nil
Expand Down Expand Up @@ -760,7 +760,7 @@
}
}
r.log.Info("processing Gateway", "namespace", gtw.Namespace, "name", gtw.Name)
resourceMap.allAssociatedNamespaces[gtw.Namespace] = struct{}{}
resourceMap.allAssociatedNamespaces.Insert(gtw.Namespace)

for _, listener := range gtw.Spec.Listeners {
listener := listener
Expand Down Expand Up @@ -1517,12 +1517,12 @@
}
for _, backendRef := range sink.OpenTelemetry.BackendRefs {
backendNamespace := gatewayapi.NamespaceDerefOrAlpha(backendRef.Namespace, ep.Namespace)
resourceMap.allAssociatedBackendRefs[gwapiv1.BackendObjectReference{
resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{

Check warning on line 1520 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L1520

Added line #L1520 was not covered by tests
Group: backendRef.BackendObjectReference.Group,
Kind: backendRef.BackendObjectReference.Kind,
Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace),
Name: backendRef.Name,
}] = struct{}{}
})

Check warning on line 1525 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L1525

Added line #L1525 was not covered by tests
}
}
}
Expand All @@ -1535,25 +1535,25 @@
}
for _, backendRef := range sink.OpenTelemetry.BackendRefs {
backendNamespace := gatewayapi.NamespaceDerefOrAlpha(backendRef.Namespace, ep.Namespace)
resourceMap.allAssociatedBackendRefs[gwapiv1.BackendObjectReference{
resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{

Check warning on line 1538 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L1538

Added line #L1538 was not covered by tests
Group: backendRef.BackendObjectReference.Group,
Kind: backendRef.BackendObjectReference.Kind,
Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace),
Name: backendRef.Name,
}] = struct{}{}
})

Check warning on line 1543 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L1543

Added line #L1543 was not covered by tests
}
}
}

if telemetry.Tracing != nil {
for _, backendRef := range telemetry.Tracing.Provider.BackendRefs {
backendNamespace := gatewayapi.NamespaceDerefOrAlpha(backendRef.Namespace, ep.Namespace)
resourceMap.allAssociatedBackendRefs[gwapiv1.BackendObjectReference{
resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{

Check warning on line 1551 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L1551

Added line #L1551 was not covered by tests
Group: backendRef.BackendObjectReference.Group,
Kind: backendRef.BackendObjectReference.Kind,
Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace),
Name: backendRef.Name,
}] = struct{}{}
})

Check warning on line 1556 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L1556

Added line #L1556 was not covered by tests

}
}
Expand Down Expand Up @@ -1701,12 +1701,12 @@
backendRef := br.BackendObjectReference

backendNamespace := gatewayapi.NamespaceDerefOr(backendRef.Namespace, policy.Namespace)
resourceMap.allAssociatedBackendRefs[gwapiv1.BackendObjectReference{
resourceMap.allAssociatedBackendRefs.Insert(gwapiv1.BackendObjectReference{

Check warning on line 1704 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L1704

Added line #L1704 was not covered by tests
Group: backendRef.Group,
Kind: backendRef.Kind,
Namespace: gatewayapi.NamespacePtrV1Alpha2(backendNamespace),
Name: backendRef.Name,
}] = struct{}{}
})

Check warning on line 1709 in internal/provider/kubernetes/controller.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/controller.go#L1709

Added line #L1709 was not covered by tests

if backendNamespace != policy.Namespace {
from := ObjectKindNamespacedName{
Expand Down
Loading