Skip to content

Commit

Permalink
optimize resource set mapping logic
Browse files Browse the repository at this point in the history
Signed-off-by: qicz <[email protected]>
  • Loading branch information
qicz committed Oct 22, 2024
1 parent 93bc44b commit e8fd517
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 49 deletions.
23 changes: 7 additions & 16 deletions internal/provider/kubernetes/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (r *gatewayAPIReconciler) Reconcile(ctx context.Context, _ reconcile.Reques
return reconcile.Result{}, err
}

if err = r.processBackends(ctx, gwcResource, resourceMappings); err != nil {
if err = r.processBackends(ctx, gwcResource); err != nil {
return reconcile.Result{}, err
}

Expand Down Expand Up @@ -394,12 +394,9 @@ func (r *gatewayAPIReconciler) processBackendRefs(ctx context.Context, gwcResour
} else {
resourceMappings.allAssociatedNamespaces[backend.Namespace] = struct{}{}
backend.Status = egv1a1.BackendStatus{}
if !resourceMappings.allAssociatedBackends.Has(utils.NamespacedName(backend).String()) {
resourceMappings.allAssociatedBackends.Insert(utils.NamespacedName(backend).String())
gwcResource.Backends = append(gwcResource.Backends, backend)
r.log.Info("added Backend to resource tree", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
}
gwcResource.Backends = append(gwcResource.Backends, backend)
r.log.Info("added Backend to resource tree", "namespace", string(*backendRef.Namespace),
"name", string(backendRef.Name))
}
}

Expand Down Expand Up @@ -1052,7 +1049,7 @@ func (r *gatewayAPIReconciler) processBackendTLSPolicies(
}

// processBackends adds Backends to the resourceTree
func (r *gatewayAPIReconciler) processBackends(ctx context.Context, resourceTree *resource.Resources, resourceMap *resourceMappings) error {
func (r *gatewayAPIReconciler) processBackends(ctx context.Context, resourceTree *resource.Resources) error {
backends := egv1a1.BackendList{}
if err := r.client.List(ctx, &backends); err != nil {
return fmt.Errorf("error listing Backends: %w", err)
Expand All @@ -1063,10 +1060,7 @@ func (r *gatewayAPIReconciler) processBackends(ctx context.Context, resourceTree
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
backend.Status = egv1a1.BackendStatus{}
if !resourceMap.allAssociatedBackends.Has(utils.NamespacedName(&backend).String()) {
resourceMap.allAssociatedBackends.Insert(utils.NamespacedName(&backend).String())
resourceTree.Backends = append(resourceTree.Backends, &backend)
}
resourceTree.Backends = append(resourceTree.Backends, &backend)
}
return nil
}
Expand Down Expand Up @@ -1722,10 +1716,7 @@ func (r *gatewayAPIReconciler) processGatewayParamsRef(ctx context.Context, gtw
}
}

if !resourceMap.allAssociatedEnvoyProxies.Has(utils.NamespacedName(ep).String()) {
resourceMap.allAssociatedEnvoyProxies.Insert(utils.NamespacedName(ep).String())
resourceTree.EnvoyProxiesForGateways = append(resourceTree.EnvoyProxiesForGateways, ep)
}
resourceTree.EnvoyProxiesForGateways = append(resourceTree.EnvoyProxiesForGateways, ep)
return nil
}

Expand Down
11 changes: 2 additions & 9 deletions internal/provider/kubernetes/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ type resourceMappings struct {
allAssociatedBackendTLSPolicies sets.Set[string]
// Map for storing EnvoyExtensionPolicies' NamespacedNames attaching to various Gateway objects.
allAssociatedEnvoyExtensionPolicies sets.Set[string]
// Map for storing Backends' NamespacedNames referred by various Route objects.
allAssociatedBackends sets.Set[string]
// Map for storing HTTPRouteFilters' NamespacedNames referred by various Route objects.
allAssociatedHTTPRouteFilters sets.Set[string]
// 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 Down Expand Up @@ -92,10 +88,7 @@ func newResourceMapping() *resourceMappings {
allAssociatedBackendTrafficPolicies: sets.New[string](),
allAssociatedSecurityPolicies: sets.New[string](),
allAssociatedBackendTLSPolicies: sets.New[string](),
allAssociatedEnvoyExtensionPolicies: sets.New[string](),
allAssociatedBackends: sets.New[string](),
allAssociatedHTTPRouteFilters: sets.New[string](),
extensionRefFilters: map[utils.NamespacedNameWithGroupKind]unstructured.Unstructured{},
httpRouteFilters: map[utils.NamespacedNameWithGroupKind]*egv1a1.HTTPRouteFilter{},
allAssociatedEnvoyExtensionPolicies: sets.New[string](), extensionRefFilters: map[utils.NamespacedNameWithGroupKind]unstructured.Unstructured{},
httpRouteFilters: map[utils.NamespacedNameWithGroupKind]*egv1a1.HTTPRouteFilter{},
}
}
30 changes: 6 additions & 24 deletions internal/provider/kubernetes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,7 @@ func (r *gatewayAPIReconciler) processTLSRoutes(ctx context.Context, gatewayName
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
tlsRoute.Status = gwapiv1a2.TLSRouteStatus{}
if !resourceMap.allAssociatedTLSRoutes.Has(utils.NamespacedName(&tlsRoute).String()) {
resourceMap.allAssociatedTLSRoutes.Insert(utils.NamespacedName(&tlsRoute).String())
resourceTree.TLSRoutes = append(resourceTree.TLSRoutes, &tlsRoute)
}
resourceTree.TLSRoutes = append(resourceTree.TLSRoutes, &tlsRoute)
}

return nil
Expand Down Expand Up @@ -229,10 +226,7 @@ func (r *gatewayAPIReconciler) processGRPCRoutes(ctx context.Context, gatewayNam
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
grpcRoute.Status = gwapiv1.GRPCRouteStatus{}
if !resourceMap.allAssociatedGRPCRoutes.Has(utils.NamespacedName(&grpcRoute).String()) {
resourceMap.allAssociatedGRPCRoutes.Insert(utils.NamespacedName(&grpcRoute).String())
resourceTree.GRPCRoutes = append(resourceTree.GRPCRoutes, &grpcRoute)
}
resourceTree.GRPCRoutes = append(resourceTree.GRPCRoutes, &grpcRoute)
}

return nil
Expand Down Expand Up @@ -424,10 +418,7 @@ func (r *gatewayAPIReconciler) processHTTPRoutes(ctx context.Context, gatewayNam
r.log.Error(err, "HTTPRouteFilters not found; bypassing rule", "index", i)
continue
}
if !resourceMap.allAssociatedHTTPRouteFilters.Has(key.NamespacedName.String()) {
resourceMap.allAssociatedHTTPRouteFilters.Insert(key.NamespacedName.String())
resourceTree.HTTPRouteFilters = append(resourceTree.HTTPRouteFilters, httpFilter)
}
resourceTree.HTTPRouteFilters = append(resourceTree.HTTPRouteFilters, httpFilter)
default:
extRefFilter, ok := resourceMap.extensionRefFilters[key]
if !ok {
Expand All @@ -450,10 +441,7 @@ func (r *gatewayAPIReconciler) processHTTPRoutes(ctx context.Context, gatewayNam
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
httpRoute.Status = gwapiv1.HTTPRouteStatus{}
if !resourceMap.allAssociatedHTTPRoutes.Has(utils.NamespacedName(&httpRoute).String()) {
resourceMap.allAssociatedHTTPRoutes.Insert(utils.NamespacedName(&httpRoute).String())
resourceTree.HTTPRoutes = append(resourceTree.HTTPRoutes, &httpRoute)
}
resourceTree.HTTPRoutes = append(resourceTree.HTTPRoutes, &httpRoute)
}

return nil
Expand Down Expand Up @@ -534,10 +522,7 @@ func (r *gatewayAPIReconciler) processTCPRoutes(ctx context.Context, gatewayName
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
tcpRoute.Status = gwapiv1a2.TCPRouteStatus{}
if !resourceMap.allAssociatedTCPRoutes.Has(utils.NamespacedName(&tcpRoute).String()) {
resourceMap.allAssociatedTCPRoutes.Insert(utils.NamespacedName(&tcpRoute).String())
resourceTree.TCPRoutes = append(resourceTree.TCPRoutes, &tcpRoute)
}
resourceTree.TCPRoutes = append(resourceTree.TCPRoutes, &tcpRoute)
}

return nil
Expand Down Expand Up @@ -617,10 +602,7 @@ func (r *gatewayAPIReconciler) processUDPRoutes(ctx context.Context, gatewayName
// Discard Status to reduce memory consumption in watchable
// It will be recomputed by the gateway-api layer
udpRoute.Status = gwapiv1a2.UDPRouteStatus{}
if !resourceMap.allAssociatedUDPRoutes.Has(utils.NamespacedName(&udpRoute).String()) {
resourceMap.allAssociatedUDPRoutes.Insert(utils.NamespacedName(&udpRoute).String())
resourceTree.UDPRoutes = append(resourceTree.UDPRoutes, &udpRoute)
}
resourceTree.UDPRoutes = append(resourceTree.UDPRoutes, &udpRoute)
}

return nil
Expand Down

0 comments on commit e8fd517

Please sign in to comment.