-
Notifications
You must be signed in to change notification settings - Fork 665
/
cache.go
670 lines (603 loc) · 20.7 KB
/
cache.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// Copyright Project Contour 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 dag
import (
"context"
"errors"
"fmt"
"sync"
contour_api_v1 "github.com/projectcontour/contour/apis/projectcontour/v1"
contour_api_v1alpha1 "github.com/projectcontour/contour/apis/projectcontour/v1alpha1"
"github.com/projectcontour/contour/internal/annotation"
"github.com/projectcontour/contour/internal/gatewayapi"
"github.com/projectcontour/contour/internal/ingressclass"
"github.com/projectcontour/contour/internal/k8s"
"github.com/projectcontour/contour/internal/ref"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
networking_v1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/tools/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayapi_v1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayapi_v1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)
// A KubernetesCache holds Kubernetes objects and associated configuration and produces
// DAG values.
type KubernetesCache struct {
// RootNamespaces specifies the namespaces where root
// HTTPProxies can be defined. If empty, roots can be defined in any
// namespace.
RootNamespaces []string
// Names of ingress classes to cache HTTPProxies/Ingresses for. If not
// set, objects with no ingress class or DEFAULT_INGRESS_CLASS will be
// cached.
IngressClassNames []string
// ConfiguredGatewayToCache is the optional name of the specific Gateway to cache.
// If set, only the Gateway with this namespace/name will be kept.
ConfiguredGatewayToCache *types.NamespacedName
// Secrets that are referred from the configuration file.
ConfiguredSecretRefs []*types.NamespacedName
ingresses map[types.NamespacedName]*networking_v1.Ingress
httpproxies map[types.NamespacedName]*contour_api_v1.HTTPProxy
secrets map[types.NamespacedName]*Secret
tlscertificatedelegations map[types.NamespacedName]*contour_api_v1.TLSCertificateDelegation
services map[types.NamespacedName]*v1.Service
namespaces map[string]*v1.Namespace
gatewayclass *gatewayapi_v1beta1.GatewayClass
gateway *gatewayapi_v1beta1.Gateway
httproutes map[types.NamespacedName]*gatewayapi_v1beta1.HTTPRoute
tlsroutes map[types.NamespacedName]*gatewayapi_v1alpha2.TLSRoute
referencegrants map[types.NamespacedName]*gatewayapi_v1beta1.ReferenceGrant
extensions map[types.NamespacedName]*contour_api_v1alpha1.ExtensionService
Client client.Reader
initialize sync.Once
logrus.FieldLogger
}
// init creates the internal cache storage. It is called implicitly from the public API.
func (kc *KubernetesCache) init() {
kc.ingresses = make(map[types.NamespacedName]*networking_v1.Ingress)
kc.httpproxies = make(map[types.NamespacedName]*contour_api_v1.HTTPProxy)
kc.secrets = make(map[types.NamespacedName]*Secret)
kc.tlscertificatedelegations = make(map[types.NamespacedName]*contour_api_v1.TLSCertificateDelegation)
kc.services = make(map[types.NamespacedName]*v1.Service)
kc.namespaces = make(map[string]*v1.Namespace)
kc.httproutes = make(map[types.NamespacedName]*gatewayapi_v1beta1.HTTPRoute)
kc.referencegrants = make(map[types.NamespacedName]*gatewayapi_v1beta1.ReferenceGrant)
kc.tlsroutes = make(map[types.NamespacedName]*gatewayapi_v1alpha2.TLSRoute)
kc.extensions = make(map[types.NamespacedName]*contour_api_v1alpha1.ExtensionService)
}
// Insert inserts obj into the KubernetesCache.
// Insert returns true if the cache accepted the object, or false if the value
// is not interesting to the cache. If an object with a matching type, name,
// and namespace exists, it will be overwritten.
func (kc *KubernetesCache) Insert(obj interface{}) bool {
kc.initialize.Do(kc.init)
maybeInsert := func(obj interface{}) bool {
switch obj := obj.(type) {
case *v1.Secret:
// Secret validation status is intentionally cleared, it needs
// to be re-validated after an insert.
kc.secrets[k8s.NamespacedNameOf(obj)] = &Secret{Object: obj}
return kc.secretTriggersRebuild(obj)
case *v1.Service:
kc.services[k8s.NamespacedNameOf(obj)] = obj
return kc.serviceTriggersRebuild(obj)
case *v1.Namespace:
kc.namespaces[obj.Name] = obj
return true
case *networking_v1.Ingress:
if !ingressclass.MatchesIngress(obj, kc.IngressClassNames) {
// We didn't get a match so report this object is being ignored.
kc.WithField("name", obj.GetName()).
WithField("namespace", obj.GetNamespace()).
WithField("kind", k8s.KindOf(obj)).
WithField("ingress-class-annotation", annotation.IngressClass(obj)).
WithField("ingress-class-name", ref.Val(obj.Spec.IngressClassName, "")).
WithField("target-ingress-classes", kc.IngressClassNames).
Debug("ignoring Ingress with unmatched ingress class")
return false
}
kc.ingresses[k8s.NamespacedNameOf(obj)] = obj
return true
case *contour_api_v1.HTTPProxy:
if !ingressclass.MatchesHTTPProxy(obj, kc.IngressClassNames) {
// We didn't get a match so report this object is being ignored.
kc.WithField("name", obj.GetName()).
WithField("namespace", obj.GetNamespace()).
WithField("kind", k8s.KindOf(obj)).
WithField("ingress-class-annotation", annotation.IngressClass(obj)).
WithField("ingress-class-name", obj.Spec.IngressClassName).
WithField("target-ingress-classes", kc.IngressClassNames).
Debug("ignoring HTTPProxy with unmatched ingress class")
return false
}
kc.httpproxies[k8s.NamespacedNameOf(obj)] = obj
return true
case *contour_api_v1.TLSCertificateDelegation:
kc.tlscertificatedelegations[k8s.NamespacedNameOf(obj)] = obj
return true
case *gatewayapi_v1beta1.GatewayClass:
switch {
// Specific gateway configured: make sure the incoming gateway class
// matches that gateway's.
case kc.ConfiguredGatewayToCache != nil:
if kc.gateway == nil || obj.Name != string(kc.gateway.Spec.GatewayClassName) {
return false
}
kc.gatewayclass = obj
return true
// Otherwise, take whatever we're given.
default:
kc.gatewayclass = obj
return true
}
case *gatewayapi_v1beta1.Gateway:
switch {
// Specific gateway configured: make sure the incoming gateway
// matches, and get its gateway class.
case kc.ConfiguredGatewayToCache != nil:
if k8s.NamespacedNameOf(obj) != *kc.ConfiguredGatewayToCache {
return false
}
kc.gateway = obj
gatewayClass := &gatewayapi_v1beta1.GatewayClass{}
if err := kc.Client.Get(context.Background(), client.ObjectKey{Name: string(kc.gateway.Spec.GatewayClassName)}, gatewayClass); err != nil {
kc.WithError(err).Errorf("error getting gatewayclass for gateway %s/%s", kc.gateway.Namespace, kc.gateway.Name)
} else {
kc.gatewayclass = gatewayClass
}
return true
// Otherwise, take whatever we're given.
default:
kc.gateway = obj
return true
}
case *gatewayapi_v1beta1.HTTPRoute:
kc.httproutes[k8s.NamespacedNameOf(obj)] = obj
return kc.routeTriggersRebuild(obj.Spec.ParentRefs)
case *gatewayapi_v1alpha2.TLSRoute:
kc.tlsroutes[k8s.NamespacedNameOf(obj)] = obj
return kc.routeTriggersRebuild(obj.Spec.ParentRefs)
case *gatewayapi_v1beta1.ReferenceGrant:
kc.referencegrants[k8s.NamespacedNameOf(obj)] = obj
return true
case *contour_api_v1alpha1.ExtensionService:
kc.extensions[k8s.NamespacedNameOf(obj)] = obj
return true
case *contour_api_v1alpha1.ContourConfiguration:
return false
default:
// not an interesting object
kc.WithField("object", obj).Error("insert unknown object")
return false
}
}
if maybeInsert(obj) {
// Only check annotations if we actually inserted
// the object in our cache; uninteresting objects
// should not be checked.
if obj, ok := obj.(metav1.Object); ok {
kind := k8s.KindOf(obj)
for key := range obj.GetAnnotations() {
// Emit a warning if this is a known annotation that has
// been applied to an invalid object kind. Note that we
// only warn for known annotations because we want to
// allow users to add arbitrary orthogonal annotations
// to objects that we inspect.
if annotation.IsKnown(key) && !annotation.ValidForKind(kind, key) {
// TODO(jpeach): this should be exposed
// to the user as a status condition.
kc.WithField("name", obj.GetName()).
WithField("namespace", obj.GetNamespace()).
WithField("kind", kind).
WithField("version", k8s.VersionOf(obj)).
WithField("annotation", key).
Error("ignoring invalid or unsupported annotation")
}
}
}
return true
}
return false
}
// Remove removes obj from the KubernetesCache.
// Remove returns a boolean indicating if the cache changed after the remove operation.
func (kc *KubernetesCache) Remove(obj interface{}) bool {
kc.initialize.Do(kc.init)
switch obj := obj.(type) {
default:
return kc.remove(obj)
case cache.DeletedFinalStateUnknown:
return kc.Remove(obj.Obj) // recurse into ourselves with the tombstoned value
}
}
func (kc *KubernetesCache) remove(obj interface{}) bool {
switch obj := obj.(type) {
case *v1.Secret:
m := k8s.NamespacedNameOf(obj)
delete(kc.secrets, m)
return kc.secretTriggersRebuild(obj)
case *v1.Service:
m := k8s.NamespacedNameOf(obj)
delete(kc.services, m)
return kc.serviceTriggersRebuild(obj)
case *v1.Namespace:
_, ok := kc.namespaces[obj.Name]
delete(kc.namespaces, obj.Name)
return ok
case *networking_v1.Ingress:
m := k8s.NamespacedNameOf(obj)
_, ok := kc.ingresses[m]
delete(kc.ingresses, m)
return ok
case *contour_api_v1.HTTPProxy:
m := k8s.NamespacedNameOf(obj)
_, ok := kc.httpproxies[m]
delete(kc.httpproxies, m)
return ok
case *contour_api_v1.TLSCertificateDelegation:
m := k8s.NamespacedNameOf(obj)
_, ok := kc.tlscertificatedelegations[m]
delete(kc.tlscertificatedelegations, m)
return ok
case *gatewayapi_v1beta1.GatewayClass:
switch {
case kc.ConfiguredGatewayToCache != nil:
if kc.gatewayclass == nil || obj.Name != kc.gatewayclass.Name {
return false
}
kc.gatewayclass = nil
return true
default:
kc.gatewayclass = nil
return true
}
case *gatewayapi_v1beta1.Gateway:
switch {
case kc.ConfiguredGatewayToCache != nil:
if kc.gateway == nil || k8s.NamespacedNameOf(obj) != k8s.NamespacedNameOf(kc.gateway) {
return false
}
kc.gateway = nil
return true
default:
kc.gateway = nil
return true
}
case *gatewayapi_v1beta1.HTTPRoute:
m := k8s.NamespacedNameOf(obj)
delete(kc.httproutes, m)
return kc.routeTriggersRebuild(obj.Spec.ParentRefs)
case *gatewayapi_v1alpha2.TLSRoute:
m := k8s.NamespacedNameOf(obj)
delete(kc.tlsroutes, m)
return kc.routeTriggersRebuild(obj.Spec.ParentRefs)
case *gatewayapi_v1beta1.ReferenceGrant:
m := k8s.NamespacedNameOf(obj)
_, ok := kc.referencegrants[m]
delete(kc.referencegrants, m)
return ok
case *contour_api_v1alpha1.ExtensionService:
m := k8s.NamespacedNameOf(obj)
_, ok := kc.extensions[m]
delete(kc.extensions, m)
return ok
case *contour_api_v1alpha1.ContourConfiguration:
return false
default:
// not interesting
kc.WithField("object", obj).Error("remove unknown object")
return false
}
}
// serviceTriggersRebuild returns true if this service is referenced
// by an Ingress or HTTPProxy in this cache.
func (kc *KubernetesCache) serviceTriggersRebuild(service *v1.Service) bool {
for _, ingress := range kc.ingresses {
if ingress.Namespace != service.Namespace {
continue
}
if backend := ingress.Spec.DefaultBackend; backend != nil {
if backend.Service.Name == service.Name {
return true
}
}
for _, rule := range ingress.Spec.Rules {
http := rule.IngressRuleValue.HTTP
if http == nil {
continue
}
for _, path := range http.Paths {
if path.Backend.Service.Name == service.Name {
return true
}
}
}
}
for _, proxy := range kc.httpproxies {
if proxy.Namespace != service.Namespace {
continue
}
for _, route := range proxy.Spec.Routes {
for _, s := range route.Services {
if s.Name == service.Name {
return true
}
}
}
if tcpproxy := proxy.Spec.TCPProxy; tcpproxy != nil {
for _, s := range tcpproxy.Services {
if s.Name == service.Name {
return true
}
}
}
}
for _, route := range kc.httproutes {
for _, rule := range route.Spec.Rules {
for _, backend := range rule.BackendRefs {
if isRefToService(backend.BackendObjectReference, service, route.Namespace) {
return true
}
}
}
}
for _, route := range kc.tlsroutes {
for _, rule := range route.Spec.Rules {
for _, backend := range rule.BackendRefs {
if isRefToService(backend.BackendObjectReference, service, route.Namespace) {
return true
}
}
}
}
return false
}
func isRefToService(ref gatewayapi_v1beta1.BackendObjectReference, service *v1.Service, routeNamespace string) bool {
return ref.Group != nil && *ref.Group == "" &&
ref.Kind != nil && *ref.Kind == "Service" &&
((ref.Namespace != nil && string(*ref.Namespace) == service.Namespace) || (ref.Namespace == nil && routeNamespace == service.Namespace)) &&
string(ref.Name) == service.Name
}
// secretTriggersRebuild returns true if this secret is referenced by an Ingress
// or HTTPProxy object, or by the configuration file. If the secret is not in the same namespace
// it must be mentioned by a TLSCertificateDelegation.
func (kc *KubernetesCache) secretTriggersRebuild(secret *v1.Secret) bool {
if _, isCA := secret.Data[CACertificateKey]; isCA {
// locating a secret validation usage involves traversing each
// proxy object, determining if there is a valid delegation,
// and if the reference the secret as a certificate. The DAG already
// does this so don't reproduce the logic and just assume for the moment
// that any change to a CA secret will trigger a rebuild.
return true
}
delegations := make(map[string]bool) // targetnamespace/secretname to bool
// TODO(youngnick): Check if this is required.
for _, d := range kc.tlscertificatedelegations {
for _, cd := range d.Spec.Delegations {
for _, n := range cd.TargetNamespaces {
delegations[n+"/"+cd.SecretName] = true
}
}
}
for _, ingress := range kc.ingresses {
if ingress.Namespace == secret.Namespace {
for _, tls := range ingress.Spec.TLS {
if tls.SecretName == secret.Name {
return true
}
}
}
if delegations[ingress.Namespace+"/"+secret.Name] {
for _, tls := range ingress.Spec.TLS {
if tls.SecretName == secret.Namespace+"/"+secret.Name {
return true
}
}
}
if delegations["*/"+secret.Name] {
for _, tls := range ingress.Spec.TLS {
if tls.SecretName == secret.Namespace+"/"+secret.Name {
return true
}
}
}
}
for _, proxy := range kc.httpproxies {
vh := proxy.Spec.VirtualHost
if vh == nil {
// not a root ingress
continue
}
tls := vh.TLS
if tls == nil {
// no tls spec
continue
}
if proxy.Namespace == secret.Namespace && tls.SecretName == secret.Name {
return true
}
if delegations[proxy.Namespace+"/"+secret.Name] {
if tls.SecretName == secret.Namespace+"/"+secret.Name {
return true
}
}
if delegations["*/"+secret.Name] {
if tls.SecretName == secret.Namespace+"/"+secret.Name {
return true
}
}
}
// Secrets referred by the configuration file shall also trigger rebuild.
for _, s := range kc.ConfiguredSecretRefs {
if s.Namespace == secret.Namespace && s.Name == secret.Name {
return true
}
}
if kc.gateway != nil {
for _, listener := range kc.gateway.Spec.Listeners {
if listener.TLS == nil {
continue
}
for _, certificateRef := range listener.TLS.CertificateRefs {
if isRefToSecret(certificateRef, secret, kc.gateway.Namespace) {
return true
}
}
}
}
return false
}
func isRefToSecret(ref gatewayapi_v1beta1.SecretObjectReference, secret *v1.Secret, gatewayNamespace string) bool {
return ref.Group != nil && *ref.Group == "" &&
ref.Kind != nil && *ref.Kind == "Secret" &&
((ref.Namespace != nil && *ref.Namespace == gatewayapi_v1beta1.Namespace(secret.Namespace)) || (ref.Namespace == nil && gatewayNamespace == secret.Namespace)) &&
string(ref.Name) == secret.Name
}
// routesTriggersRebuild returns true if this route references gateway in this cache.
func (kc *KubernetesCache) routeTriggersRebuild(parentRefs []gatewayapi_v1beta1.ParentReference) bool {
if kc.gateway == nil {
return false
}
for _, parentRef := range parentRefs {
if gatewayapi.IsRefToGateway(parentRef, k8s.NamespacedNameOf(kc.gateway)) {
return true
}
}
return false
}
func (kc *KubernetesCache) LookupTLSSecret(name types.NamespacedName) (*Secret, error) {
sec, ok := kc.secrets[name]
if !ok {
return nil, fmt.Errorf("Secret not found")
}
// Compute and store the validation result if not
// already stored.
if sec.ValidTLSSecret == nil {
sec.ValidTLSSecret = &SecretValidationStatus{
Error: validTLSSecret(sec.Object),
}
}
if err := sec.ValidTLSSecret.Error; err != nil {
return nil, err
}
return sec, nil
}
func (kc *KubernetesCache) LookupCASecret(name types.NamespacedName) (*Secret, error) {
sec, ok := kc.secrets[name]
if !ok {
return nil, fmt.Errorf("Secret not found")
}
// Compute and store the validation result if not
// already stored.
if sec.ValidCASecret == nil {
sec.ValidCASecret = &SecretValidationStatus{
Error: validCASecret(sec.Object),
}
}
if err := sec.ValidCASecret.Error; err != nil {
return nil, err
}
return sec, nil
}
func (kc *KubernetesCache) LookupCRLSecret(name types.NamespacedName) (*Secret, error) {
sec, ok := kc.secrets[name]
if !ok {
return nil, fmt.Errorf("Secret not found")
}
// Compute and store the validation result if not
// already stored.
if sec.ValidCRLSecret == nil {
sec.ValidCRLSecret = &SecretValidationStatus{
Error: validCRLSecret(sec.Object),
}
}
if err := sec.ValidCRLSecret.Error; err != nil {
return nil, err
}
return sec, nil
}
func (kc *KubernetesCache) LookupUpstreamValidation(uv *contour_api_v1.UpstreamValidation, caCertificate types.NamespacedName) (*PeerValidationContext, error) {
if uv == nil {
// no upstream validation requested, nothing to do
return nil, nil
}
cacert, err := kc.LookupCASecret(caCertificate)
if err != nil {
// UpstreamValidation is requested, but cert is missing or not configured
return nil, fmt.Errorf("invalid CA Secret %q: %s", caCertificate, err)
}
if uv.SubjectName == "" {
// UpstreamValidation is requested, but SAN is not provided
return nil, errors.New("missing subject alternative name")
}
return &PeerValidationContext{
CACertificate: cacert,
SubjectName: uv.SubjectName,
}, nil
}
// DelegationPermitted returns true if the referenced secret has been delegated
// to the namespace where the ingress object is located.
func (kc *KubernetesCache) DelegationPermitted(secret types.NamespacedName, targetNamespace string) bool {
contains := func(haystack []string, needle string) bool {
if len(haystack) == 1 && haystack[0] == "*" {
return true
}
for _, h := range haystack {
if h == needle {
return true
}
}
return false
}
if secret.Namespace == targetNamespace {
// secret is in the same namespace as target
return true
}
for _, d := range kc.tlscertificatedelegations {
if d.Namespace != secret.Namespace {
continue
}
for _, d := range d.Spec.Delegations {
if contains(d.TargetNamespaces, targetNamespace) {
if secret.Name == d.SecretName {
return true
}
}
}
}
return false
}
// LookupService returns the Kubernetes service and port matching the provided parameters,
// or an error if a match can't be found.
func (kc *KubernetesCache) LookupService(meta types.NamespacedName, port intstr.IntOrString) (*v1.Service, v1.ServicePort, error) {
svc, ok := kc.services[meta]
if !ok {
return nil, v1.ServicePort{}, fmt.Errorf("service %q not found", meta)
}
for i := range svc.Spec.Ports {
p := svc.Spec.Ports[i]
if int(p.Port) == port.IntValue() || port.String() == p.Name {
switch p.Protocol {
case "", v1.ProtocolTCP:
return svc, p, nil
default:
return nil, v1.ServicePort{}, fmt.Errorf("unsupported service protocol %q", p.Protocol)
}
}
}
return nil, v1.ServicePort{}, fmt.Errorf("port %q on service %q not matched", port.String(), meta)
}