/
http_connection_manager.go
130 lines (111 loc) · 4.65 KB
/
http_connection_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package envoy
import (
"time"
accesslog_v3 "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v3"
envoy_api_v3_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
envoy_config_trace_v3 "github.com/envoyproxy/go-control-plane/envoy/config/trace/v3"
accesslog_file_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/access_loggers/file/v3"
hcm "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
"github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/wrapperspb"
"knative.dev/net-kourier/pkg/config"
)
// NewHTTPConnectionManager creates a new HttpConnectionManager that points to the given
// RouteConfig for further configuration.
func NewHTTPConnectionManager(routeConfigName string, kourierConfig *config.Kourier) *hcm.HttpConnectionManager {
filters := make([]*hcm.HttpFilter, 0, 1)
if config.ExternalAuthz.Enabled {
filters = append(filters, config.ExternalAuthz.HTTPFilter)
}
// Append the Router filter at the end.
filters = append(filters, &hcm.HttpFilter{
Name: wellknown.Router,
ConfigType: &hcm.HttpFilter_TypedConfig{TypedConfig: &anypb.Any{
TypeUrl: "type.googleapis.com/envoy.extensions.filters.http.router.v3.Router",
}},
})
enableAccessLog := kourierConfig.EnableServiceAccessLogging
enableProxyProtocol := kourierConfig.EnableProxyProtocol
idleTimeout := kourierConfig.IdleTimeout
mgr := &hcm.HttpConnectionManager{
CodecType: hcm.HttpConnectionManager_AUTO,
StatPrefix: "ingress_http",
HttpFilters: filters,
RouteSpecifier: &hcm.HttpConnectionManager_Rds{
Rds: &hcm.Rds{
ConfigSource: &envoy_api_v3_core.ConfigSource{
ResourceApiVersion: resource.DefaultAPIVersion,
ConfigSourceSpecifier: &envoy_api_v3_core.ConfigSource_Ads{
Ads: &envoy_api_v3_core.AggregatedConfigSource{},
},
InitialFetchTimeout: durationpb.New(10 * time.Second),
},
RouteConfigName: routeConfigName,
},
},
StreamIdleTimeout: durationpb.New(idleTimeout),
XffNumTrustedHops: kourierConfig.TrustedHopsCount,
}
if enableProxyProtocol {
//Force the connection manager to use the real remote address of the client connection.
mgr.UseRemoteAddress = &wrapperspb.BoolValue{Value: true}
}
if enableAccessLog {
// Write access logs to stdout by default.
accessLog, _ := anypb.New(&accesslog_file_v3.FileAccessLog{
Path: "/dev/stdout",
})
mgr.AccessLog = []*accesslog_v3.AccessLog{{
Name: "envoy.file_access_log",
ConfigType: &accesslog_v3.AccessLog_TypedConfig{
TypedConfig: accessLog,
},
}}
}
if kourierConfig.Tracing.Enabled {
mgr.GenerateRequestId = wrapperspb.Bool(true)
zipkinConfig, _ := anypb.New(&envoy_config_trace_v3.ZipkinConfig{
CollectorCluster: "tracing-collector",
CollectorEndpoint: kourierConfig.Tracing.CollectorEndpoint,
SharedSpanContext: wrapperspb.Bool(false),
CollectorEndpointVersion: envoy_config_trace_v3.ZipkinConfig_HTTP_JSON,
})
mgr.Tracing = &hcm.HttpConnectionManager_Tracing{
Provider: &envoy_config_trace_v3.Tracing_Http{
Name: wellknown.Zipkin,
ConfigType: &envoy_config_trace_v3.Tracing_Http_TypedConfig{
TypedConfig: zipkinConfig,
},
},
}
}
return mgr
}
// NewRouteConfig create a new RouteConfiguration with the given name and hosts.
func NewRouteConfig(name string, virtualHosts []*route.VirtualHost) *route.RouteConfiguration {
return &route.RouteConfiguration{
Name: name,
VirtualHosts: virtualHosts,
// Without this validation we can generate routes that point to non-existing clusters
// That causes some "no_cluster" errors in Envoy and the "TestUpdate"
// in the Knative serving test suite fails sometimes.
// Ref: https://github.com/knative/serving/blob/f6da03e5dfed78593c4f239c3c7d67c5d7c55267/test/conformance/ingress/update_test.go#L37
ValidateClusters: wrapperspb.Bool(true),
}
}