This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
resolver.go
412 lines (362 loc) · 12.1 KB
/
resolver.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package service
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/cenkalti/backoff"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/go-hclog"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
gw "sigs.k8s.io/gateway-api/apis/v1alpha2"
"github.com/hashicorp/consul-api-gateway/internal/common"
"github.com/hashicorp/consul-api-gateway/internal/k8s/gatewayclient"
apigwv1alpha1 "github.com/hashicorp/consul-api-gateway/pkg/apis/v1alpha1"
)
//go:generate mockgen -source ./resolver.go -destination ./mocks/resolver.go -package mocks BackendResolver
type ResolvedReferenceType int
type ServiceResolutionErrorType int
const (
K8sServiceResolutionErrorType ServiceResolutionErrorType = iota
ConsulServiceResolutionErrorType
GenericResolutionErrorType
NoResolutionErrorType
RefNotPermittedErrorType
)
var errorTypePrefixMap = map[ServiceResolutionErrorType]string{
K8sServiceResolutionErrorType: "k8s: ",
ConsulServiceResolutionErrorType: "consul: ",
}
type ResolutionError struct {
inner string
remote ServiceResolutionErrorType
}
func NewResolutionError(inner string) ResolutionError {
return ResolutionError{inner, GenericResolutionErrorType}
}
func NewK8sResolutionError(inner string) ResolutionError {
return ResolutionError{inner, K8sServiceResolutionErrorType}
}
func NewConsulResolutionError(inner string) ResolutionError {
return ResolutionError{inner, ConsulServiceResolutionErrorType}
}
func NewRefNotPermittedError(inner string) ResolutionError {
return ResolutionError{inner, RefNotPermittedErrorType}
}
func getErrorTypePrefix(errType ServiceResolutionErrorType) string {
return errorTypePrefixMap[errType]
}
func (r ResolutionError) Error() string {
return r.inner
}
type ResolutionErrors struct {
errors map[ServiceResolutionErrorType][]ResolutionError
}
func NewResolutionErrors() *ResolutionErrors {
return &ResolutionErrors{errors: make(map[ServiceResolutionErrorType][]ResolutionError)}
}
func (r *ResolutionErrors) Add(err ResolutionError) {
r.errors[err.remote] = append(r.errors[err.remote], err)
}
func (r *ResolutionErrors) String() string {
errs := []string{}
for errType, errors := range r.errors {
if len(errors) > 0 {
errorString := getErrorTypePrefix(errType)
for i, err := range errors {
if i != 0 {
errorString += ", "
}
errorString += err.Error()
}
errs = append(errs, errorString)
}
}
return strings.Join(errs, "; ")
}
func (r *ResolutionErrors) Flatten() (ServiceResolutionErrorType, error) {
if r.Empty() {
return NoResolutionErrorType, nil
}
//return generic error if there are multiple errors types, or if generic errors exist
if len(r.errors[GenericResolutionErrorType]) != 0 || (len(r.errors) > 1) {
return GenericResolutionErrorType, errors.New(r.String())
}
// we only have at most one error type at this point, so return the error type of the first set of errors we find
for errType := range r.errors {
return errType, errors.New(r.String())
}
//shouldn't be possible to get here
return GenericResolutionErrorType, errors.New(r.String())
}
func (r *ResolutionErrors) Empty() bool {
return len(r.errors) == 0
}
const (
HTTPRouteReference ResolvedReferenceType = iota
ConsulServiceReference
MetaKeyKubeServiceName = "k8s-service-name"
MetaKeyKubeNS = "k8s-namespace"
)
type ConsulService struct {
Namespace string
Name string
}
type BackendReference struct {
HTTPRef *gw.HTTPBackendRef
BackendRef *gw.BackendRef
}
func (b *BackendReference) Set(reference interface{}) {
switch ref := reference.(type) {
case *gw.HTTPBackendRef:
b.HTTPRef = ref
case *gw.BackendRef:
b.BackendRef = ref
}
}
type ResolvedReference struct {
Type ResolvedReferenceType
Reference *BackendReference
Consul *ConsulService
object client.Object
}
func NewConsulServiceReference(object client.Object, consul *ConsulService) *ResolvedReference {
return &ResolvedReference{
Type: ConsulServiceReference,
Reference: &BackendReference{},
Consul: consul,
object: object,
}
}
func (r *ResolvedReference) Item() client.Object {
return r.object
}
type BackendResolver interface {
Resolve(ctx context.Context, ref gw.BackendObjectReference) (*ResolvedReference, error)
}
type backendResolver struct {
namespace string
client gatewayclient.Client
consul *api.Client
logger hclog.Logger
mapper common.ConsulNamespaceMapper
}
func NewBackendResolver(logger hclog.Logger, namespace string, mapper common.ConsulNamespaceMapper, client gatewayclient.Client, consul *api.Client) BackendResolver {
return &backendResolver{
namespace: namespace,
client: client,
consul: consul,
mapper: mapper,
logger: logger,
}
}
func (r *backendResolver) Resolve(ctx context.Context, ref gw.BackendObjectReference) (*ResolvedReference, error) {
group := corev1.GroupName
kind := "Service"
namespace := r.namespace
if ref.Group != nil {
group = string(*ref.Group)
}
if ref.Kind != nil {
kind = string(*ref.Kind)
}
if ref.Namespace != nil {
namespace = string(*ref.Namespace)
}
namespacedName := types.NamespacedName{Name: string(ref.Name), Namespace: namespace}
switch {
case group == corev1.GroupName && kind == "Service":
if ref.Port == nil {
return nil, NewK8sResolutionError("service port must not be empty")
}
return r.consulServiceForK8SService(ctx, namespacedName)
case group == apigwv1alpha1.GroupVersion.Group && kind == apigwv1alpha1.MeshServiceKind:
return r.consulServiceForMeshService(ctx, namespacedName)
default:
return nil, NewResolutionError("unsupported reference type")
}
}
func (r *backendResolver) consulServiceForK8SService(ctx context.Context, namespacedName types.NamespacedName) (*ResolvedReference, error) {
var err error
var resolved *ResolvedReference
service, err := r.client.GetService(ctx, namespacedName)
if err != nil {
r.logger.Error("error resolving kubernetes service", "error", err)
return nil, err
}
if service == nil {
r.logger.Warn("kubernetes service not found")
return nil, NewK8sResolutionError(fmt.Sprintf("service %s not found", namespacedName))
}
// we do an inner retry since consul may take some time to sync
err = backoff.Retry(func() error {
r.logger.Trace("attempting to resolve global catalog service")
resolved, err = r.findGlobalCatalogService(service)
if err != nil {
r.logger.Trace("error resolving global catalog reference", "error", err)
return err
}
if resolved == nil {
return NewConsulResolutionError(fmt.Sprintf("consul service %s not found", namespacedName))
}
return nil
}, backoff.WithContext(backoff.WithMaxRetries(backoff.NewConstantBackOff(1*time.Second), 30), ctx))
if err != nil {
r.logger.Error("could not resolve consul service", "error", err)
return nil, err
}
return resolved, nil
}
func validateAgentConsulReference(services map[string]*api.AgentService, object client.Object) (*ResolvedReference, error) {
serviceName := ""
serviceNamespace := ""
for _, service := range services {
if serviceName == "" {
serviceName = service.Service
}
if serviceNamespace == "" {
serviceNamespace = service.Namespace
}
if service.Service != serviceName || service.Namespace != serviceNamespace {
return nil,
NewConsulResolutionError(fmt.Sprintf(
"must have a single service map to a kubernetes service, found - (%q, %q) and (%q, %q)",
serviceNamespace, serviceName, service.Namespace, service.Service,
))
}
}
return NewConsulServiceReference(object, &ConsulService{
Name: serviceName,
Namespace: serviceNamespace,
}), nil
}
// this acts as a brute-force mechanism for resolving a consul service if we can't find it registered
// in our local agent -- it checks all services based on their node with the same filtering mechanism
// we use in filtering the agent endpoint
func (r *backendResolver) findGlobalCatalogService(service *corev1.Service) (*ResolvedReference, error) {
nodes, _, err := r.consul.Catalog().Nodes(nil)
if err != nil {
r.logger.Trace("error retrieving nodes", "error", err)
return nil, err
}
var consulNamespaces []*api.Namespace
// the default namespace
namespaces := []string{""}
consulNamespaces, _, err = r.consul.Namespaces().List(nil)
if err != nil {
if !strings.Contains(err.Error(), "Unexpected response code: 404") {
r.logger.Trace("error retrieving namespaces", "error", err)
return nil, err
}
// we're dealing with an OSS version of Consul, skip namespaces other than the
// default namespace
} else {
for _, namespace := range consulNamespaces {
namespaces = append(namespaces, namespace.Name)
}
}
filter := fmt.Sprintf(`Meta[%q] == %q and Meta[%q] == %q and Kind != "connect-proxy"`, MetaKeyKubeServiceName, service.Name, MetaKeyKubeNS, service.Namespace)
for _, node := range nodes {
for _, namespace := range namespaces {
nodeWithServices, _, err := r.consul.Catalog().Node(node.Node, &api.QueryOptions{
Filter: filter,
Namespace: namespace,
})
if err != nil {
r.logger.Trace("error retrieving node services", "error", err, "node", node.Node)
return nil, err
}
if len(nodeWithServices.Services) == 0 {
continue
}
resolved, err := validateAgentConsulReference(nodeWithServices.Services, service)
if err != nil {
r.logger.Trace("error validating node services", "error", err, "node", node.Node)
return nil, err
}
if resolved != nil {
return resolved, nil
}
}
}
return nil, nil
}
func (r *backendResolver) consulServiceForMeshService(ctx context.Context, namespacedName types.NamespacedName) (*ResolvedReference, error) {
var err error
var resolved *ResolvedReference
service, err := r.client.GetMeshService(ctx, namespacedName)
if err != nil {
r.logger.Trace("error retrieving mesh service", "error", err, "name", namespacedName.Name, "namespace", namespacedName.Namespace)
return nil, err
}
if service == nil {
return nil,
NewConsulResolutionError(fmt.Sprintf(
"kubernetes mesh service object %s not found", namespacedName,
))
}
// we do an inner retry since consul may take some time to sync
err = backoff.Retry(func() error {
r.logger.Trace("attempting to resolve global catalog service")
resolved, err = r.findCatalogService(service)
if err != nil {
r.logger.Trace("error resolving global catalog reference", "error", err)
return err
}
if resolved == nil {
return NewConsulResolutionError(fmt.Sprintf("consul service %s not found", namespacedName))
}
return nil
}, backoff.WithContext(backoff.WithMaxRetries(backoff.NewConstantBackOff(1*time.Second), 30), ctx))
if err != nil {
r.logger.Error("could not resolve consul service", "error", err)
return nil, err
}
return resolved, nil
}
func (r *backendResolver) findCatalogService(service *apigwv1alpha1.MeshService) (*ResolvedReference, error) {
consulNamespace := r.mapper(service.Namespace)
consulName := service.Spec.Name
services, _, err := r.consul.Catalog().Service(consulName, "", &api.QueryOptions{
Namespace: consulNamespace,
})
if err != nil {
r.logger.Trace("error resolving consul service reference", "error", err)
return nil, err
}
if len(services) == 0 {
return nil, NewConsulResolutionError(fmt.Sprintf("consul service (%s, %s) not found", consulNamespace, consulName))
}
resolved, err := validateCatalogConsulReference(services, service)
if err != nil {
r.logger.Trace("error validating consul services", "error", err)
return nil, err
}
return resolved, nil
}
func validateCatalogConsulReference(services []*api.CatalogService, object client.Object) (*ResolvedReference, error) {
serviceName := ""
serviceNamespace := ""
for _, service := range services {
if serviceName == "" {
serviceName = service.ServiceName
}
if serviceNamespace == "" {
serviceNamespace = service.Namespace
}
if service.ServiceName != serviceName || service.Namespace != serviceNamespace {
return nil,
NewConsulResolutionError(fmt.Sprintf(
"must have a single service map to a kubernetes service, found - (%q, %q) and (%q, %q)",
serviceNamespace, serviceName, service.Namespace, service.ServiceName,
))
}
}
return NewConsulServiceReference(object, &ConsulService{
Name: serviceName,
Namespace: serviceNamespace,
}), nil
}