This repository has been archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
plugin.go
139 lines (115 loc) · 3.92 KB
/
plugin.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
131
132
133
134
135
136
137
138
139
package rest
import (
envoyapi "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoyroute "github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
"github.com/pkg/errors"
"github.com/solo-io/gloo-api/pkg/api/types/v1"
"github.com/solo-io/gloo-plugins/common/transformation"
"github.com/solo-io/gloo/pkg/log"
"github.com/solo-io/gloo/pkg/plugin"
)
const (
ServiceTypeREST = "REST"
)
func init() {
plugin.Register(NewPlugin(), nil)
}
func NewPlugin() *Plugin {
return &Plugin{transformation: transformation.NewTransformationPlugin()}
}
type Plugin struct {
transformation transformation.Plugin
}
func (p *Plugin) GetDependencies(_ *v1.Config) *plugin.Dependencies {
return nil
}
func isOurs(in *v1.Upstream) bool {
return in.ServiceInfo != nil && in.ServiceInfo.Type == ServiceTypeREST
}
func (p *Plugin) ProcessUpstream(params *plugin.UpstreamPluginParams, in *v1.Upstream, out *envoyapi.Cluster) error {
if !isOurs(in) {
return nil
}
p.transformation.ActivateFilterForCluster(out)
return nil
}
func (p *Plugin) ProcessRoute(pluginParams *plugin.RoutePluginParams, in *v1.Route, out *envoyroute.Route) error {
getTransformationFunction := createTransformationForRestFunction(pluginParams.Upstreams)
if err := p.transformation.AddRequestTransformationsToRoute(getTransformationFunction, in, out); err != nil {
return errors.Wrap(err, "failed to process request transformation")
}
if err := p.transformation.AddResponseTransformationsToRoute(in, out); err != nil {
return errors.Wrap(err, "failed to process response transformation")
}
return nil
}
func createTransformationForRestFunction(upstreams []*v1.Upstream) transformation.GetTransformationFunction {
return func(fnDestination *v1.Destination_Function) (*transformation.TransformationTemplate, error) {
fn, err := findFunction(upstreams, fnDestination.Function.UpstreamName, fnDestination.Function.FunctionName)
if err != nil {
return nil, errors.Wrap(err, "finding function")
}
if fn == nil {
return nil, nil
}
outputTemplates, err := DecodeFunctionSpec(fn.Spec)
if err != nil {
return nil, errors.Wrap(err, "decoding function spec")
}
// if the the function doesn't need a transformation, also return nil
needsTransformation := outputTemplates.Body != nil
// create templates
// right now it's just a no-op, user writes inja directly
headerTemplates := make(map[string]*transformation.InjaTemplate)
for k, v := range outputTemplates.Header {
needsTransformation = true
headerTemplates[k] = &transformation.InjaTemplate{Text: v}
}
if outputTemplates.Path != "" {
needsTransformation = true
headerTemplates[":path"] = &transformation.InjaTemplate{Text: outputTemplates.Path}
}
// this function doesn't request any kind of transformation
if !needsTransformation {
log.Debugf("does not need transformation: %v", outputTemplates)
return nil, nil
}
template := &transformation.TransformationTemplate{
Headers: headerTemplates,
}
if outputTemplates.Body != nil {
template.BodyTransformation = &transformation.TransformationTemplate_Body{
Body: &transformation.InjaTemplate{
Text: *outputTemplates.Body,
},
}
} else {
template.BodyTransformation = &transformation.TransformationTemplate_Passthrough{
Passthrough : &transformation.Passthrough{},
}
}
return template, nil
}
}
func findFunction(upstreams []*v1.Upstream, upstreamName, functionName string) (*v1.Function, error) {
for _, us := range upstreams {
if us.Name == upstreamName {
if !isOurs(us) {
return nil, nil
}
for _, fn := range us.Functions {
if fn.Name == functionName {
return fn, nil
}
}
}
}
return nil, errors.Errorf("function %v/%v not found", upstreamName, functionName)
}
func (p *Plugin) HttpFilters(_ *plugin.FilterPluginParams) []plugin.StagedFilter {
filter := p.transformation.GetTransformationFilter()
if filter == nil {
return nil
}
return []plugin.StagedFilter{*filter}
}