-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
router.go
114 lines (98 loc) · 3.55 KB
/
router.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
/*
*
* Copyright 2021 gRPC 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 router implements the Envoy Router HTTP filter.
package router
import (
"fmt"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
iresolver "google.golang.org/grpc/internal/resolver"
"google.golang.org/grpc/xds/internal/httpfilter"
"google.golang.org/protobuf/types/known/anypb"
pb "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/http/router/v3"
)
// TypeURL is the message type for the Router configuration.
const TypeURL = "type.googleapis.com/envoy.extensions.filters.http.router.v3.Router"
func init() {
httpfilter.Register(builder{})
}
// IsRouterFilter returns true iff a HTTP filter is a Router filter.
func IsRouterFilter(b httpfilter.Filter) bool {
_, ok := b.(builder)
return ok
}
type builder struct {
}
func (builder) TypeURLs() []string { return []string{TypeURL} }
func (builder) ParseFilterConfig(cfg proto.Message) (httpfilter.FilterConfig, error) {
// The gRPC router filter does not currently use any fields from the
// config. Verify type only.
if cfg == nil {
return nil, fmt.Errorf("router: nil configuration message provided")
}
any, ok := cfg.(*anypb.Any)
if !ok {
return nil, fmt.Errorf("router: error parsing config %v: unknown type %T", cfg, cfg)
}
msg := new(pb.Router)
if err := ptypes.UnmarshalAny(any, msg); err != nil {
return nil, fmt.Errorf("router: error parsing config %v: %v", cfg, err)
}
return config{}, nil
}
func (builder) ParseFilterConfigOverride(override proto.Message) (httpfilter.FilterConfig, error) {
if override != nil {
return nil, fmt.Errorf("router: unexpected config override specified: %v", override)
}
return config{}, nil
}
func (builder) IsTerminal() bool {
return true
}
var (
_ httpfilter.ClientInterceptorBuilder = builder{}
_ httpfilter.ServerInterceptorBuilder = builder{}
)
func (builder) BuildClientInterceptor(cfg, override httpfilter.FilterConfig) (iresolver.ClientInterceptor, error) {
if _, ok := cfg.(config); !ok {
return nil, fmt.Errorf("router: incorrect config type provided (%T): %v", cfg, cfg)
}
if override != nil {
return nil, fmt.Errorf("router: unexpected override configuration specified: %v", override)
}
// The gRPC router is implemented within the xds resolver's config
// selector, not as a separate plugin. So we return a nil HTTPFilter,
// which will not be invoked.
return nil, nil
}
func (builder) BuildServerInterceptor(cfg, override httpfilter.FilterConfig) (iresolver.ServerInterceptor, error) {
if _, ok := cfg.(config); !ok {
return nil, fmt.Errorf("router: incorrect config type provided (%T): %v", cfg, cfg)
}
if override != nil {
return nil, fmt.Errorf("router: unexpected override configuration specified: %v", override)
}
// The gRPC router is currently unimplemented on the server side. So we
// return a nil HTTPFilter, which will not be invoked.
return nil, nil
}
// The gRPC router filter does not currently support any configuration. Verify
// type only.
type config struct {
httpfilter.FilterConfig
}