-
Notifications
You must be signed in to change notification settings - Fork 18
/
manager.go
295 lines (254 loc) · 9 KB
/
manager.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2023 The ClusterLink 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 xds
import (
"fmt"
"time"
cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
listener "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
tcpproxy "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/tcp_proxy/v3"
getaddrinfo "github.com/envoyproxy/go-control-plane/envoy/extensions/network/dns_resolver/getaddrinfo/v3"
tls "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
"github.com/envoyproxy/go-control-plane/pkg/cache/v3"
"github.com/envoyproxy/go-control-plane/pkg/resource/v3"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/durationpb"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/types"
"github.com/clusterlink-net/clusterlink/pkg/apis/clusterlink.net/v1alpha1"
cpapi "github.com/clusterlink-net/clusterlink/pkg/controlplane/api"
dpapi "github.com/clusterlink-net/clusterlink/pkg/dataplane/api"
)
// Manager manages the core routing components of the dataplane.
// It maps the following controlplane types to xDS types:
// - Peer -> Cluster (whose name starts with a designated prefix)
// - Export -> Cluster (whose name starts with a designated prefix)
// - Import -> Listener (whose name starts with a designated prefix)
// Note that imported service bindings are handled by the egress authz server.
type Manager struct {
crdMode bool
clusters *cache.LinearCache
listeners *cache.LinearCache
logger *logrus.Entry
}
// AddPeer defines a new route target for egress dataplane connections.
func (m *Manager) AddPeer(peer *v1alpha1.Peer) error {
m.logger.Infof("Adding peer '%s'.", peer.Name)
clusterName := cpapi.RemotePeerClusterName(peer.Name)
dataplaneSNI := dpapi.DataplaneSNI(peer.Name)
epc, err := makeEndpointsCluster(clusterName, peer.Spec.Gateways, dataplaneSNI)
if err != nil {
return err
}
tlsConfig := &tls.UpstreamTlsContext{
Sni: dataplaneSNI,
CommonTlsContext: &tls.CommonTlsContext{
TlsCertificateSdsSecretConfigs: []*tls.SdsSecretConfig{{
Name: cpapi.CertificateSecret,
}},
ValidationContextType: &tls.CommonTlsContext_ValidationContextSdsSecretConfig{
ValidationContextSdsSecretConfig: &tls.SdsSecretConfig{
Name: cpapi.ValidationSecret,
},
},
},
}
pb, err := anypb.New(tlsConfig)
if err != nil {
return err
}
epc.TransportSocket = &core.TransportSocket{
Name: wellknown.TransportSocketTLS,
ConfigType: &core.TransportSocket_TypedConfig{TypedConfig: pb},
}
return m.clusters.UpdateResource(clusterName, epc)
}
// DeletePeer removes the possibility for egress dataplane connections to be routed to a given peer.
func (m *Manager) DeletePeer(name string) error {
m.logger.Infof("Deleting peer '%s'.", name)
clusterName := cpapi.RemotePeerClusterName(name)
return m.clusters.DeleteResource(clusterName)
}
// AddExport defines a new route target for ingress dataplane connections.
func (m *Manager) AddExport(export *v1alpha1.Export) error {
m.logger.Infof("Adding export '%s/%s'.", export.Namespace, export.Name)
host := export.Spec.Host
if host == "" {
host = fmt.Sprintf("%s.%s.svc.cluster.local", export.Name, export.Namespace)
}
clusterName := cpapi.ExportClusterName(export.Name, export.Namespace)
cc, err := makeAddressCluster(
clusterName,
host,
export.Spec.Port, "")
if err != nil {
return err
}
return m.clusters.UpdateResource(clusterName, cc)
}
// DeleteExport removes the possibility for ingress dataplane connections to access a given service.
func (m *Manager) DeleteExport(name types.NamespacedName) error {
m.logger.Infof("Deleting export '%v'.", name)
clusterName := cpapi.ExportClusterName(name.Name, name.Namespace)
return m.clusters.DeleteResource(clusterName)
}
// AddImport adds a listening socket for an imported remote service.
func (m *Manager) AddImport(imp *v1alpha1.Import) error {
m.logger.Infof("Adding import '%s/%s'.", imp.Namespace, imp.Name)
if m.crdMode && !meta.IsStatusConditionTrue(imp.Status.Conditions, v1alpha1.ImportTargetPortValid) {
// target port not yet allocated, skip
m.logger.Infof("Skipping import with no valid target port '%s/%s'.", imp.Namespace, imp.Name)
return nil
}
listenerName := cpapi.ImportListenerName(imp.Name, imp.Namespace)
egressRouterHostname := "egress-router:443"
tunnelingConfig := &tcpproxy.TcpProxy_TunnelingConfig{
Hostname: egressRouterHostname,
UsePost: true,
HeadersToAdd: []*core.HeaderValueOption{
{
Header: &core.HeaderValue{
Key: cpapi.ImportNameHeader,
Value: imp.Name,
},
KeepEmptyValue: true,
},
{
Header: &core.HeaderValue{
Key: cpapi.ImportNamespaceHeader,
Value: imp.Namespace,
},
KeepEmptyValue: true,
},
{
Header: &core.HeaderValue{
Key: cpapi.ClientIPHeader,
Value: "%DOWNSTREAM_REMOTE_ADDRESS_WITHOUT_PORT%",
},
KeepEmptyValue: true,
},
},
}
tcpProxyFilter, err := makeTCPProxyFilter(
cpapi.EgressRouterCluster, imp.Name, tunnelingConfig)
if err != nil {
return err
}
// TODO: listen on a more specific address (i.e. not 0.0.0.0)
ln := &listener.Listener{
Name: listenerName,
Address: &core.Address{
Address: &core.Address_SocketAddress{
SocketAddress: &core.SocketAddress{
Address: "0.0.0.0",
PortSpecifier: &core.SocketAddress_PortValue{
PortValue: uint32(imp.Spec.TargetPort),
},
},
},
},
FilterChains: []*listener.FilterChain{{
Filters: []*listener.Filter{tcpProxyFilter},
}},
}
return m.listeners.UpdateResource(listenerName, ln)
}
// DeleteImport removes the listening socket of a previously imported service.
func (m *Manager) DeleteImport(name types.NamespacedName) error {
m.logger.Infof("Deleting import '%v'.", name)
listenerName := cpapi.ImportListenerName(name.Name, name.Namespace)
return m.listeners.DeleteResource(listenerName)
}
func makeAddressCluster(name, addr string, port uint16, hostname string) (*cluster.Cluster, error) {
return makeEndpointsCluster(name, []v1alpha1.Endpoint{{Host: addr, Port: port}}, hostname)
}
func makeEndpointsCluster(name string, endpoints []v1alpha1.Endpoint, hostname string) (*cluster.Cluster, error) {
lbEndpoints := make([]*endpoint.LbEndpoint, len(endpoints))
for i, ep := range endpoints {
lbEndpoints[i] = &endpoint.LbEndpoint{
HostIdentifier: &endpoint.LbEndpoint_Endpoint{
Endpoint: &endpoint.Endpoint{
Address: &core.Address{
Address: &core.Address_SocketAddress{
SocketAddress: &core.SocketAddress{
Address: ep.Host,
PortSpecifier: &core.SocketAddress_PortValue{
PortValue: uint32(ep.Port),
},
},
},
},
Hostname: hostname,
},
},
}
}
pb, err := anypb.New(&getaddrinfo.GetAddrInfoDnsResolverConfig{})
if err != nil {
return nil, err
}
cc := &cluster.Cluster{
Name: name,
ConnectTimeout: durationpb.New(time.Second),
DnsRefreshRate: durationpb.New(time.Second),
ClusterDiscoveryType: &cluster.Cluster_Type{
Type: cluster.Cluster_STRICT_DNS,
},
TypedDnsResolverConfig: &core.TypedExtensionConfig{
Name: "envoy.network.dns_resolver.getaddrinfo",
TypedConfig: pb,
},
LoadAssignment: &endpoint.ClusterLoadAssignment{
ClusterName: name,
Endpoints: []*endpoint.LocalityLbEndpoints{{
LbEndpoints: lbEndpoints,
}},
},
}
return cc, nil
}
func makeTCPProxyFilter(clusterName, statPrefix string,
tunnelingConfig *tcpproxy.TcpProxy_TunnelingConfig,
) (*listener.Filter, error) {
tcpProxyConfig := &tcpproxy.TcpProxy{
StatPrefix: "tcp-proxy-" + statPrefix,
ClusterSpecifier: &tcpproxy.TcpProxy_Cluster{
Cluster: clusterName,
},
TunnelingConfig: tunnelingConfig,
}
pb, err := anypb.New(tcpProxyConfig)
if err != nil {
return nil, err
}
return &listener.Filter{
Name: wellknown.TCPProxy,
ConfigType: &listener.Filter_TypedConfig{
TypedConfig: pb,
},
}, nil
}
// NewManager creates an uninitialized, non-registered xDS manager.
func NewManager(crdMode bool) *Manager {
logger := logrus.WithField("component", "controlplane.xds.manager")
return &Manager{
crdMode: crdMode,
clusters: cache.NewLinearCache(resource.ClusterType, cache.WithLogger(logger)),
listeners: cache.NewLinearCache(resource.ListenerType, cache.WithLogger(logger)),
logger: logger,
}
}