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

fix: store one copy of HTTPRoute Extension Filters #5002

Merged
merged 3 commits into from
Jan 7, 2025
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
11 changes: 5 additions & 6 deletions internal/provider/kubernetes/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@
return resourceItems, nil
}

func (r *gatewayAPIReconciler) getHTTPRouteFilters(ctx context.Context) ([]egv1a1.HTTPRouteFilter, error) {
httpFilterList := new(egv1a1.HTTPRouteFilterList)
if err := r.client.List(ctx, httpFilterList); err != nil {
return nil, fmt.Errorf("failed to list HTTPRouteFilters: %w", err)
func (r *gatewayAPIReconciler) getHTTPRouteFilter(ctx context.Context, name, namespace string) (*egv1a1.HTTPRouteFilter, error) {
hrf := new(egv1a1.HTTPRouteFilter)
if err := r.client.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, hrf); err != nil {
return nil, fmt.Errorf("failed to get HTTPRouteFilter: %w", err)

Check warning on line 56 in internal/provider/kubernetes/filters.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/filters.go#L56

Added line #L56 was not covered by tests
}

return httpFilterList.Items, nil
return hrf, nil
}

// processRouteFilterConfigMapRef adds the referenced ConfigMap in a HTTPRouteFilter
Expand Down
53 changes: 26 additions & 27 deletions internal/provider/kubernetes/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/utils"
)

Expand Down Expand Up @@ -61,35 +60,35 @@ type resourceMappings struct {
// The key is the namespaced name, group and kind of the filter and the value is the
// unstructured form of the resource.
extensionRefFilters map[utils.NamespacedNameWithGroupKind]unstructured.Unstructured
// httpRouteFilters is a map of HTTPRouteFilters, where the key is the namespaced name,
// group and kind of the HTTPFilter.
httpRouteFilters map[utils.NamespacedNameWithGroupKind]*egv1a1.HTTPRouteFilter
// Set for storing HTTPRouteExtensions (Envoy Gateway or Custom) NamespacedNames referenced by various
// route rules objects.
allAssociatedHTTPRouteExtensionFilters sets.Set[utils.NamespacedNameWithGroupKind]
}

func newResourceMapping() *resourceMappings {
return &resourceMappings{
allAssociatedGateways: sets.New[string](),
allAssociatedReferenceGrants: sets.New[string](),
allAssociatedServiceImports: sets.New[string](),
allAssociatedEndpointSlices: sets.New[string](),
allAssociatedBackends: sets.New[string](),
allAssociatedSecrets: sets.New[string](),
allAssociatedConfigMaps: sets.New[string](),
allAssociatedNamespaces: sets.New[string](),
allAssociatedEnvoyProxies: sets.New[string](),
allAssociatedEnvoyPatchPolicies: 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](),
allAssociatedClientTrafficPolicies: sets.New[string](),
allAssociatedBackendTrafficPolicies: sets.New[string](),
allAssociatedSecurityPolicies: sets.New[string](),
allAssociatedBackendTLSPolicies: sets.New[string](),
allAssociatedEnvoyExtensionPolicies: sets.New[string](),
extensionRefFilters: map[utils.NamespacedNameWithGroupKind]unstructured.Unstructured{},
httpRouteFilters: map[utils.NamespacedNameWithGroupKind]*egv1a1.HTTPRouteFilter{},
allAssociatedGateways: sets.New[string](),
allAssociatedReferenceGrants: sets.New[string](),
allAssociatedServiceImports: sets.New[string](),
allAssociatedEndpointSlices: sets.New[string](),
allAssociatedBackends: sets.New[string](),
allAssociatedSecrets: sets.New[string](),
allAssociatedConfigMaps: sets.New[string](),
allAssociatedNamespaces: sets.New[string](),
allAssociatedEnvoyProxies: sets.New[string](),
allAssociatedEnvoyPatchPolicies: 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](),
allAssociatedClientTrafficPolicies: sets.New[string](),
allAssociatedBackendTrafficPolicies: sets.New[string](),
allAssociatedSecurityPolicies: sets.New[string](),
allAssociatedBackendTLSPolicies: sets.New[string](),
allAssociatedEnvoyExtensionPolicies: sets.New[string](),
extensionRefFilters: map[utils.NamespacedNameWithGroupKind]unstructured.Unstructured{},
allAssociatedHTTPRouteExtensionFilters: sets.New[utils.NamespacedNameWithGroupKind](),
}
}
33 changes: 15 additions & 18 deletions internal/provider/kubernetes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,6 @@
resourceMap *resourceMappings, resourceTree *resource.Resources,
) error {
httpRouteList := &gwapiv1.HTTPRouteList{}
if r.hrfCRDExists {
httpFilters, err := r.getHTTPRouteFilters(ctx)
if err != nil {
return err
}

for i := range httpFilters {
filter := httpFilters[i]
resourceMap.httpRouteFilters[utils.GetNamespacedNameWithGroupKind(&filter)] = &filter
r.processRouteFilterConfigMapRef(ctx, &filter, resourceMap, resourceTree)
}
}

extensionRefFilters, err := r.getExtensionRefFilters(ctx)
if err != nil {
Expand Down Expand Up @@ -416,12 +404,18 @@

switch string(filter.ExtensionRef.Kind) {
case egv1a1.KindHTTPRouteFilter:
httpFilter, ok := resourceMap.httpRouteFilters[key]
if !ok {
r.log.Error(err, "HTTPRouteFilters not found; bypassing rule", "index", i)
continue
if r.hrfCRDExists {
httpFilter, err := r.getHTTPRouteFilter(ctx, key.Name, key.Namespace)
if err != nil {
r.log.Error(err, "HTTPRouteFilters not found; bypassing rule", "index", i)
continue

Check warning on line 411 in internal/provider/kubernetes/routes.go

View check run for this annotation

Codecov / codecov/patch

internal/provider/kubernetes/routes.go#L410-L411

Added lines #L410 - L411 were not covered by tests
}
if !resourceMap.allAssociatedHTTPRouteExtensionFilters.Has(key) {
r.processRouteFilterConfigMapRef(ctx, httpFilter, resourceMap, resourceTree)
resourceMap.allAssociatedHTTPRouteExtensionFilters.Insert(key)
resourceTree.HTTPRouteFilters = append(resourceTree.HTTPRouteFilters, httpFilter)
}
}
resourceTree.HTTPRouteFilters = append(resourceTree.HTTPRouteFilters, httpFilter)
default:
extRefFilter, ok := resourceMap.extensionRefFilters[key]
if !ok {
Expand All @@ -433,7 +427,10 @@
continue
}

resourceTree.ExtensionRefFilters = append(resourceTree.ExtensionRefFilters, extRefFilter)
if !resourceMap.allAssociatedHTTPRouteExtensionFilters.Has(key) {
resourceMap.allAssociatedHTTPRouteExtensionFilters.Insert(key)
resourceTree.ExtensionRefFilters = append(resourceTree.ExtensionRefFilters, extRefFilter)
}
}
}
}
Expand Down
Loading
Loading