-
Notifications
You must be signed in to change notification settings - Fork 12
/
resources.go
155 lines (131 loc) · 5.34 KB
/
resources.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package envoy
import (
envoy "github.com/3scale-ops/marin3r/pkg/envoy"
envoy_config_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
envoy_config_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
envoy_config_endpoint_v3 "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
envoy_config_listener_v3 "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
envoy_config_route_v3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
envoy_extensions_transport_sockets_tls_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
envoy_service_runtime_v3 "github.com/envoyproxy/go-control-plane/envoy/service/runtime/v3"
)
// Generator returns a strcut that implements the envoy_resources.Generator
// interface for v3 resources
type Generator struct{}
// New returns an empty resource of the given type
func (g Generator) New(rType envoy.Type) envoy.Resource {
switch rType {
case envoy.Endpoint:
return &envoy_config_endpoint_v3.ClusterLoadAssignment{}
case envoy.Cluster:
return &envoy_config_cluster_v3.Cluster{}
case envoy.Route:
return &envoy_config_route_v3.RouteConfiguration{}
case envoy.ScopedRoute:
return &envoy_config_route_v3.ScopedRouteConfiguration{}
case envoy.Listener:
return &envoy_config_listener_v3.Listener{}
case envoy.Runtime:
return &envoy_service_runtime_v3.Runtime{}
case envoy.Secret:
return &envoy_extensions_transport_sockets_tls_v3.Secret{}
case envoy.ExtensionConfig:
return &envoy_config_core_v3.TypedExtensionConfig{}
}
return nil
}
// NewTlsCertificateSecret generates a new envoy secret given the certificate and key.
func (g Generator) NewTlsCertificateSecret(name, privateKey, certificateChain string) envoy.Resource {
return &envoy_extensions_transport_sockets_tls_v3.Secret{
Name: name,
Type: &envoy_extensions_transport_sockets_tls_v3.Secret_TlsCertificate{
TlsCertificate: &envoy_extensions_transport_sockets_tls_v3.TlsCertificate{
PrivateKey: &envoy_config_core_v3.DataSource{
Specifier: &envoy_config_core_v3.DataSource_InlineBytes{InlineBytes: []byte(privateKey)},
},
CertificateChain: &envoy_config_core_v3.DataSource{
Specifier: &envoy_config_core_v3.DataSource_InlineBytes{InlineBytes: []byte(certificateChain)},
},
},
},
}
}
// NewValidationContextSecret generates a new envoy validation context given the certificate and key.
func (g Generator) NewValidationContextSecret(name, certificateChain string) envoy.Resource {
return &envoy_extensions_transport_sockets_tls_v3.Secret{
Name: name,
Type: &envoy_extensions_transport_sockets_tls_v3.Secret_ValidationContext{
ValidationContext: &envoy_extensions_transport_sockets_tls_v3.CertificateValidationContext{
TrustedCa: &envoy_config_core_v3.DataSource{
Specifier: &envoy_config_core_v3.DataSource_InlineBytes{InlineBytes: []byte(certificateChain)},
},
},
},
}
}
func (g Generator) NewGenericSecret(name string, value string) envoy.Resource {
return &envoy_extensions_transport_sockets_tls_v3.Secret{
Name: name,
Type: &envoy_extensions_transport_sockets_tls_v3.Secret_GenericSecret{
GenericSecret: &envoy_extensions_transport_sockets_tls_v3.GenericSecret{
Secret: &envoy_config_core_v3.DataSource{
Specifier: &envoy_config_core_v3.DataSource_InlineBytes{InlineBytes: []byte(value)}},
},
},
}
}
// NewSecretFromPath returns an envoy secret that uses path sds to get the certificate from
// a path and reload it whenever the certificate files change
func (g Generator) NewTlsSecretFromPath(name, certificateChainPath, privateKeyPath string) envoy.Resource {
return &envoy_extensions_transport_sockets_tls_v3.Secret{
Name: name,
Type: &envoy_extensions_transport_sockets_tls_v3.Secret_TlsCertificate{
TlsCertificate: &envoy_extensions_transport_sockets_tls_v3.TlsCertificate{
CertificateChain: &envoy_config_core_v3.DataSource{
Specifier: &envoy_config_core_v3.DataSource_Filename{
Filename: certificateChainPath,
},
},
PrivateKey: &envoy_config_core_v3.DataSource{
Specifier: &envoy_config_core_v3.DataSource_Filename{
Filename: privateKeyPath,
}},
},
},
}
}
func (g Generator) NewClusterLoadAssignment(clusterName string, hosts ...envoy.UpstreamHost) envoy.Resource {
return &envoy_config_endpoint_v3.ClusterLoadAssignment{
ClusterName: clusterName,
Endpoints: []*envoy_config_endpoint_v3.LocalityLbEndpoints{
{
LbEndpoints: func() []*envoy_config_endpoint_v3.LbEndpoint {
endpoints := make([]*envoy_config_endpoint_v3.LbEndpoint, len(hosts))
for idx, host := range hosts {
endpoints[idx] = LbEndpoint(host).(*envoy_config_endpoint_v3.LbEndpoint)
}
return endpoints
}(),
},
},
}
}
func LbEndpoint(host envoy.UpstreamHost) envoy.Resource {
return &envoy_config_endpoint_v3.LbEndpoint{
HostIdentifier: &envoy_config_endpoint_v3.LbEndpoint_Endpoint{
Endpoint: &envoy_config_endpoint_v3.Endpoint{
Address: &envoy_config_core_v3.Address{
Address: &envoy_config_core_v3.Address_SocketAddress{
SocketAddress: &envoy_config_core_v3.SocketAddress{
Address: host.IP.String(),
PortSpecifier: &envoy_config_core_v3.SocketAddress_PortValue{
PortValue: host.Port,
},
},
},
},
},
},
HealthStatus: envoy_config_core_v3.HealthStatus(host.Health),
}
}