Skip to content

Commit

Permalink
fix: add missing http filters to the http filter chain
Browse files Browse the repository at this point in the history
Signed-off-by: huabing zhao <[email protected]>
  • Loading branch information
zhaohuabing committed Mar 18, 2024
1 parent 36717d7 commit 1b57329
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
15 changes: 12 additions & 3 deletions internal/xds/translator/httpfilters.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,18 @@ func (t *Translator) patchHCMWithFilters(
// rate limit server configuration.
t.patchHCMWithRateLimit(mgr, irListener)

// Add the router filter
headerSettings := ptr.Deref(irListener.Headers, ir.HeaderSettings{})
mgr.HttpFilters = append(mgr.HttpFilters, filters.GenerateRouterFilter(headerSettings.EnableEnvoyHeaders))
// Add the router filter if it doesn't exist.
hasRouter := false
for _, filter := range mgr.HttpFilters {
if filter.Name == wellknown.Router {
hasRouter = true
break
}
}
if !hasRouter {
headerSettings := ptr.Deref(irListener.Headers, ir.HeaderSettings{})
mgr.HttpFilters = append(mgr.HttpFilters, filters.GenerateRouterFilter(headerSettings.EnableEnvoyHeaders))
}

// Sort the filters in the correct order.
mgr.HttpFilters = sortHTTPFilters(mgr.HttpFilters)
Expand Down
26 changes: 26 additions & 0 deletions internal/xds/translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
endpointv3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
listenerv3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
routev3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
hcmv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
tlsv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
matcherv3 "github.com/envoyproxy/go-control-plane/envoy/type/matcher/v3"
resourcev3 "github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/wrapperspb"

Expand Down Expand Up @@ -171,6 +173,17 @@ func (t *Translator) processHTTPListenerXdsTranslation(
return err
}
}
} else {
// Add HTTP filters to the HCM if they have not yet been added,
// the filters have already been sorted in the correct order in the
// patchHCMWithFilters function.
hcm, err := findHCM(xdsListener.DefaultFilterChain)
if err != nil {
return err // should not happen
}

Check warning on line 183 in internal/xds/translator/translator.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/translator/translator.go#L182-L183

Added lines #L182 - L183 were not covered by tests
if err = t.patchHCMWithFilters(hcm, httpListener); err != nil {
return err
}

Check warning on line 186 in internal/xds/translator/translator.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/translator/translator.go#L185-L186

Added lines #L185 - L186 were not covered by tests
}

// Create a route config if we have not found one yet
Expand Down Expand Up @@ -320,6 +333,19 @@ func (t *Translator) processHTTPListenerXdsTranslation(
return errs
}

func findHCM(filterChain *listenerv3.FilterChain) (*hcmv3.HttpConnectionManager, error) {
for _, filter := range filterChain.Filters {
if filter.Name == wellknown.HTTPConnectionManager {
hcm := &hcmv3.HttpConnectionManager{}
if err := anypb.UnmarshalTo(filter.GetTypedConfig(), hcm, proto.UnmarshalOptions{}); err != nil {
return nil, err
}

Check warning on line 342 in internal/xds/translator/translator.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/translator/translator.go#L341-L342

Added lines #L341 - L342 were not covered by tests
return hcm, nil
}
}
return nil, errors.New("http connection manager not found")

Check warning on line 346 in internal/xds/translator/translator.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/translator/translator.go#L346

Added line #L346 was not covered by tests
}

func buildHTTP3AltSvcHeader(port int) *corev3.HeaderValueOption {
return &corev3.HeaderValueOption{
Append: &wrapperspb.BoolValue{Value: true},
Expand Down

0 comments on commit 1b57329

Please sign in to comment.