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
109 lines (88 loc) · 3.05 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
package gfunc
import (
envoyapi "github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoyauth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
envoycore "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
envoyroute "github.com/envoyproxy/go-control-plane/envoy/api/v2/route"
envoyhttp "github.com/envoyproxy/go-control-plane/envoy/config/filter/network/http_connection_manager/v2"
"github.com/gogo/protobuf/types"
"github.com/pkg/errors"
"github.com/solo-io/gloo-api/pkg/api/types/v1"
"github.com/solo-io/gloo/pkg/coreplugins/common"
"github.com/solo-io/gloo/pkg/plugin"
)
func init() {
plugin.Register(&Plugin{}, nil)
}
type Plugin struct {
isNeeded bool
}
const (
// define Upstream type name
UpstreamTypeGoogle = "google"
// generic plugin info
filterName = "io.solo.gcloudfunc"
pluginStage = plugin.OutAuth
googleRegion = "region"
// function-specific metadata
functionHost = "host"
functionPath = "path"
)
func (p *Plugin) GetDependencies(cfg *v1.Config) *plugin.Dependencies {
return nil
}
func (p *Plugin) HttpFilters(params *plugin.FilterPluginParams) []plugin.StagedFilter {
defer func() { p.isNeeded = false }()
if p.isNeeded {
return []plugin.StagedFilter{{HttpFilter: &envoyhttp.HttpFilter{Name: filterName}, Stage: pluginStage}}
}
return nil
}
func (p *Plugin) ProcessRoute(_ *plugin.RoutePluginParams, in *v1.Route, out *envoyroute.Route) error {
return nil
}
func (p *Plugin) ProcessUpstream(params *plugin.UpstreamPluginParams, in *v1.Upstream, out *envoyapi.Cluster) error {
if in.Type != UpstreamTypeGoogle {
return nil
}
p.isNeeded = true
out.Type = envoyapi.Cluster_LOGICAL_DNS
// need to make sure we use ipv4 only dns
out.DnsLookupFamily = envoyapi.Cluster_V4_ONLY
googleUpstream, err := DecodeUpstreamSpec(in.Spec)
if err != nil {
return errors.Wrap(err, "invalid Google upstream spec")
}
out.Hosts = append(out.Hosts, &envoycore.Address{Address: &envoycore.Address_SocketAddress{SocketAddress: &envoycore.SocketAddress{
Address: googleUpstream.GetGFuncHostname(),
PortSpecifier: &envoycore.SocketAddress_PortValue{PortValue: 443},
}}})
out.TlsContext = &envoyauth.UpstreamTlsContext{
Sni: googleUpstream.GetGFuncHostname(),
}
if out.Metadata == nil {
out.Metadata = &envoycore.Metadata{}
}
common.InitFilterMetadata(filterName, out.Metadata)
out.Metadata.FilterMetadata[filterName] = &types.Struct{
Fields: map[string]*types.Value{
googleRegion: {Kind: &types.Value_StringValue{StringValue: googleUpstream.Region}},
},
}
return nil
}
func (p *Plugin) ParseFunctionSpec(params *plugin.FunctionPluginParams, in v1.FunctionSpec) (*types.Struct, error) {
if params.UpstreamType != UpstreamTypeGoogle {
return nil, nil
}
functionSpec, err := DecodeFunctionSpec(in)
if err != nil {
return nil, errors.Wrap(err, "invalid google function spec")
}
return &types.Struct{
Fields: map[string]*types.Value{
functionHost: {Kind: &types.Value_StringValue{StringValue: functionSpec.host}},
functionPath: {Kind: &types.Value_StringValue{StringValue: functionSpec.path}},
},
}, nil
}