forked from openshift/knative-eventing
/
controller.go
116 lines (95 loc) · 3.41 KB
/
controller.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
/*
Copyright 2020 The Knative 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 sinkbinding
import (
"context"
sbinformer "knative.dev/eventing/pkg/client/injection/informers/sources/v1alpha1/sinkbinding"
"knative.dev/pkg/client/injection/ducks/duck/v1/podspecable"
"knative.dev/pkg/resolver"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"
"knative.dev/eventing/pkg/apis/sources/v1alpha1"
"knative.dev/pkg/apis/duck"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection/clients/dynamicclient"
"knative.dev/pkg/logging"
"knative.dev/pkg/tracker"
"knative.dev/pkg/webhook/psbinding"
)
const (
controllerAgentName = "sinkbinding-controller"
)
// NewController returns a new SinkBinding reconciler.
func NewController(
ctx context.Context,
cmw configmap.Watcher,
) *controller.Impl {
logger := logging.FromContext(ctx)
sbInformer := sbinformer.Get(ctx)
dc := dynamicclient.Get(ctx)
psInformerFactory := podspecable.Get(ctx)
c := &psbinding.BaseReconciler{
GVR: v1alpha1.SchemeGroupVersion.WithResource("sinkbindings"),
Get: func(namespace string, name string) (psbinding.Bindable, error) {
return sbInformer.Lister().SinkBindings(namespace).Get(name)
},
DynamicClient: dc,
Recorder: record.NewBroadcaster().NewRecorder(
scheme.Scheme, corev1.EventSource{Component: controllerAgentName}),
}
impl := controller.NewImpl(c, logger, "SinkBindings")
logger.Info("Setting up event handlers")
sbInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue))
c.WithContext = WithContextFactory(ctx, impl.EnqueueKey)
c.Tracker = tracker.New(impl.EnqueueKey, controller.GetTrackerLease(ctx))
c.Factory = &duck.CachedInformerFactory{
Delegate: &duck.EnqueueInformerFactory{
Delegate: psInformerFactory,
EventHandler: controller.HandleAll(c.Tracker.OnChanged),
},
}
return impl
}
func ListAll(ctx context.Context, handler cache.ResourceEventHandler) psbinding.ListAll {
fbInformer := sbinformer.Get(ctx)
// Whenever a SinkBinding changes our webhook programming might change.
fbInformer.Informer().AddEventHandler(handler)
return func() ([]psbinding.Bindable, error) {
l, err := fbInformer.Lister().List(labels.Everything())
if err != nil {
return nil, err
}
bl := make([]psbinding.Bindable, 0, len(l))
for _, elt := range l {
bl = append(bl, elt)
}
return bl, nil
}
}
func WithContextFactory(ctx context.Context, handler func(types.NamespacedName)) psbinding.BindableContext {
r := resolver.NewURIResolver(ctx, handler)
return func(ctx context.Context, b psbinding.Bindable) (context.Context, error) {
sb := b.(*v1alpha1.SinkBinding)
uri, err := r.URIFromDestinationV1(sb.Spec.Sink, sb)
if err != nil {
return nil, err
}
sb.Status.SinkURI = uri
return v1alpha1.WithSinkURI(ctx, sb.Status.SinkURI), nil
}
}