-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdeps.go
More file actions
91 lines (81 loc) · 2.81 KB
/
Copy pathdeps.go
File metadata and controls
91 lines (81 loc) · 2.81 KB
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
package ingress
import (
"context"
"fmt"
"reflect"
discoveryv1 "k8s.io/api/discovery/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/pomerium/ingress-controller/model"
)
// getDependantIngressFn returns for a given object kind (i.e. a secret) a function
// that would return ingress objects keys that depend from this object
func (r *ingressController) getDependantIngressFn(kind string) handler.MapFunc {
return func(ctx context.Context, a client.Object) []reconcile.Request {
if !r.isWatching(a) {
return nil
}
lookupKind := kind
name := types.NamespacedName{Name: a.GetName(), Namespace: a.GetNamespace()}
// For EndpointSlice, look up dependencies by the associated Service name.
// EndpointSlices have a label "kubernetes.io/service-name" that identifies
// which Service they belong to. Dependencies are registered under the Service,
// not the EndpointSlice, so we need to translate the lookup.
if kind == r.endpointSliceKind {
serviceName := a.GetLabels()[discoveryv1.LabelServiceName]
if serviceName == "" {
log.FromContext(ctx).V(1).Info("EndpointSlice missing service-name label",
"endpointSlice", name)
return nil
}
name.Name = serviceName
lookupKind = r.serviceKind
}
deps := r.DepsOfKind(model.Key{Kind: lookupKind, NamespacedName: name}, r.ingressKind)
reqs := make([]reconcile.Request, 0, len(deps))
for _, k := range deps {
reqs = append(reqs, reconcile.Request{NamespacedName: k.NamespacedName})
}
log.FromContext(ctx).
WithValues("kind", kind).V(5).
Info("watch", "name", fmt.Sprintf("%s/%s", a.GetNamespace(), a.GetName()), "deps", reqs)
return reqs
}
}
func (r *ingressController) watchIngressClass() handler.MapFunc {
return func(ctx context.Context, a client.Object) []reconcile.Request {
logger := log.FromContext(ctx)
ctx, cancel := context.WithTimeout(ctx, initialReconciliationTimeout)
defer cancel()
_ = r.initComplete.yield(ctx)
ic, ok := a.(*networkingv1.IngressClass)
if !ok {
logger.Error(fmt.Errorf("got %s", reflect.TypeOf(a)), "expected IngressClass")
return nil
}
if ic.Spec.Controller != r.controllerName {
return nil
}
il := new(networkingv1.IngressList)
err := r.Client.List(ctx, il)
if err != nil {
logger.Error(err, "list")
return nil
}
deps := make([]reconcile.Request, 0, len(il.Items))
for i := range il.Items {
deps = append(deps, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: il.Items[i].Name,
Namespace: il.Items[i].Namespace,
},
})
}
logger.V(5).Info("watch", "deps", deps, "ingressClass", a.GetName())
return deps
}
}