-
Notifications
You must be signed in to change notification settings - Fork 690
/
Copy pathroute.go
92 lines (83 loc) · 2.8 KB
/
route.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
// Copyright Project Contour 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 (
"google.golang.org/protobuf/types/known/durationpb"
"github.com/projectcontour/contour/internal/dag"
"github.com/projectcontour/contour/internal/timeout"
)
func HostRewriteLiteral(hp *dag.HeadersPolicy) string {
if hp == nil {
return ""
}
return hp.HostRewrite
}
func HostRewriteHeader(hp *dag.HeadersPolicy) string {
if hp == nil {
return ""
}
return hp.HostRewriteHeader
}
// Timeout converts a timeout.Setting to a protobuf.Duration
// that's appropriate for Envoy. In general (though there are
// exceptions), Envoy uses the following semantics:
// - not passing a value means "use Envoy default"
// - explicitly passing a 0 means "disable this timeout"
// - passing a positive value uses that value
func Timeout(d timeout.Setting) *durationpb.Duration {
switch {
case d.UseDefault():
// Don't pass a value to Envoy.
return nil
case d.IsDisabled():
// Explicitly pass a 0.
return durationpb.New(0)
default:
// Pass the duration value.
return durationpb.New(d.Duration())
}
}
// SingleSimpleCluster determines whether we can use a RouteAction_Cluster
// or must use a RouteAction_WeighedCluster to encode additional routing data.
func SingleSimpleCluster(route *dag.Route) bool {
// If there are multiple clusters, than we cannot simply dispatch
// to it by name.
if len(route.Clusters) != 1 {
return false
}
// If there are route cookie rewrite policies, we need to add a Lua
// filter configuration and cannot simply dispatch to it by name.
if len(route.CookieRewritePolicies) > 0 {
return false
}
cluster := route.Clusters[0]
// If the target cluster performs any kind of header manipulation,
// then we should use a WeightedCluster to encode the additional
// configuration.
if cluster.RequestHeadersPolicy != nil &&
(len(cluster.RequestHeadersPolicy.Set) != 0 ||
len(cluster.RequestHeadersPolicy.Add) != 0 ||
len(cluster.RequestHeadersPolicy.Remove) != 0 ||
len(cluster.RequestHeadersPolicy.HostRewrite) != 0) {
return false
}
if cluster.ResponseHeadersPolicy != nil &&
(len(cluster.ResponseHeadersPolicy.Set) != 0 ||
len(cluster.ResponseHeadersPolicy.Remove) != 0) {
return false
}
if len(cluster.CookieRewritePolicies) > 0 {
return false
}
return true
}