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
/
Copy pathresolver.go
315 lines (274 loc) · 8.28 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
package service
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/cenkalti/backoff"
"github.com/hashicorp/consul-api-gateway/internal/k8s/gatewayclient"
"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"
)
//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
)
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 (r ResolutionError) Error() string {
return r.inner
}
type ResolutionErrors struct {
k8sErrors []ResolutionError
consulErrors []ResolutionError
genericErrors []ResolutionError
}
func NewResolutionErrors() *ResolutionErrors {
return &ResolutionErrors{}
}
func (r *ResolutionErrors) Add(err ResolutionError) {
switch err.remote {
case K8sServiceResolutionErrorType:
r.k8sErrors = append(r.k8sErrors, err)
case ConsulServiceResolutionErrorType:
r.consulErrors = append(r.consulErrors, err)
default:
r.genericErrors = append(r.genericErrors, err)
}
}
func (r *ResolutionErrors) String() string {
errs := []string{}
if len(r.k8sErrors) > 0 {
k8sErrs := "k8s: "
for i, err := range r.k8sErrors {
if i != 0 {
k8sErrs += ", "
}
k8sErrs += err.Error()
}
errs = append(errs, k8sErrs)
}
if len(r.consulErrors) > 0 {
consulErrs := "consul: "
for i, err := range r.consulErrors {
if i != 0 {
consulErrs += ", "
}
consulErrs += err.Error()
}
errs = append(errs, consulErrs)
}
if len(r.genericErrors) > 0 {
genericErrs := ""
for i, err := range r.genericErrors {
if i != 0 {
genericErrs += ", "
}
genericErrs += err.Error()
}
errs = append(errs, genericErrs)
}
return strings.Join(errs, "; ")
}
func (r *ResolutionErrors) Flatten() (ServiceResolutionErrorType, error) {
if r.Empty() {
return NoResolutionErrorType, nil
}
if len(r.genericErrors) != 0 || (len(r.consulErrors) != 0 && len(r.k8sErrors) != 0) {
return GenericResolutionErrorType, errors.New(r.String())
}
if len(r.consulErrors) != 0 {
return ConsulServiceResolutionErrorType, errors.New(r.String())
}
return K8sServiceResolutionErrorType, errors.New(r.String())
}
func (r *ResolutionErrors) Empty() bool {
return len(r.k8sErrors) == 0 && len(r.consulErrors) == 0 && len(r.genericErrors) == 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
}
func NewBackendResolver(logger hclog.Logger, namespace string, client gatewayclient.Client, consul *api.Client) BackendResolver {
return &backendResolver{
namespace: namespace,
client: client,
consul: consul,
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)
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("service not found")
}
// 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("consul service not found")
}
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
}
filter := fmt.Sprintf(`Meta[%q] == %q and Meta[%q] == %q and Kind != "connect-proxy"`, MetaKeyKubeServiceName, service.Name, MetaKeyKubeNS, service.Namespace)
for _, node := range nodes {
nodeWithServices, _, err := r.consul.Catalog().Node(node.Node, &api.QueryOptions{
Filter: filter,
})
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
}