@@ -33,12 +33,12 @@ import (
33
33
"sigs.k8s.io/controller-runtime/pkg/handler"
34
34
"sigs.k8s.io/controller-runtime/pkg/log"
35
35
"sigs.k8s.io/controller-runtime/pkg/source"
36
- "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
37
36
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/watch"
38
37
)
39
38
40
- type eventsSource interface {
41
- SetSink (sink Sink )
39
+ // hookableReconciler is implemented by a reconciler that we can hook
40
+ type hookableReconciler interface {
41
+ AddHook (hook Hook )
42
42
}
43
43
44
44
type DynamicWatch interface {
@@ -62,7 +62,7 @@ type WatchChildrenOptions struct {
62
62
Controller controller.Controller
63
63
64
64
// Reconciler lets us hook into the post-apply lifecycle event.
65
- Reconciler eventsSource
65
+ Reconciler hookableReconciler
66
66
67
67
// ScopeWatchesToNamespace controls whether watches are per-namespace.
68
68
// This allows for more narrowly scoped RBAC permissions, at the cost of more watches.
@@ -71,7 +71,7 @@ type WatchChildrenOptions struct {
71
71
72
72
// WatchAll creates a Watch on ctrl for all objects reconciled by recnl.
73
73
// Deprecated: prefer WatchChildren (and consider setting ScopeWatchesToNamespace)
74
- func WatchAll (config * rest.Config , ctrl controller.Controller , reconciler eventsSource , labelMaker LabelMaker ) (chan struct {}, error ) {
74
+ func WatchAll (config * rest.Config , ctrl controller.Controller , reconciler hookableReconciler , labelMaker LabelMaker ) (chan struct {}, error ) {
75
75
options := WatchChildrenOptions {
76
76
RESTConfig : config ,
77
77
Controller : ctrl ,
@@ -126,47 +126,54 @@ func WatchChildren(options WatchChildrenOptions) (chan struct{}, error) {
126
126
return nil , fmt .Errorf ("setting up dynamic watch on the controller: %w" , err )
127
127
}
128
128
129
- options .Reconciler .SetSink (& watchAll {
130
- dw : dw ,
131
- options : options ,
132
- registered : make (map [string ]struct {})})
129
+ afterApplyHook := & clusterWatch {
130
+ dw : dw ,
131
+ scopeWatchesToNamespace : options .ScopeWatchesToNamespace ,
132
+ labelMaker : options .LabelMaker ,
133
+ registered : make (map [string ]struct {}),
134
+ }
135
+
136
+ options .Reconciler .AddHook (afterApplyHook )
133
137
134
138
return stopCh , nil
135
139
}
136
140
137
- type watchAll struct {
141
+ // clusterWatch watches the objects in one cluster
142
+ type clusterWatch struct {
138
143
dw DynamicWatch
139
144
140
- options WatchChildrenOptions
145
+ scopeWatchesToNamespace bool
146
+ labelMaker LabelMaker
141
147
142
148
mutex sync.Mutex
143
- // registered tracks what we are currently watching, avoid duplicate watches.
149
+
150
+ // registered tracks the objects we are currently watching, avoid duplicate watches.
144
151
registered map [string ]struct {}
145
152
}
146
153
147
- // Notify is called by the controller when the object changes. We establish any new watches.
148
- func (w * watchAll ) Notify (ctx context.Context , dest DeclarativeObject , objs * manifest. Objects ) error {
154
+ // AfterApply is called by the controller after an apply. We establish any new watches.
155
+ func (w * clusterWatch ) AfterApply (ctx context.Context , op * ApplyOperation ) error {
149
156
log := log .FromContext (ctx )
150
157
151
- labelSelector , err := labels .ValidatedSelectorFromSet (w .options . LabelMaker (ctx , dest ))
158
+ labelSelector , err := labels .ValidatedSelectorFromSet (w .labelMaker (ctx , op . Subject ))
152
159
if err != nil {
153
160
return fmt .Errorf ("failed to build label selector: %w" , err )
154
161
}
155
162
156
- notify := metav1.ObjectMeta {Name : dest . GetName (), Namespace : dest .GetNamespace ()}
163
+ notify := metav1.ObjectMeta {Name : op . Subject . GetName (), Namespace : op . Subject .GetNamespace ()}
157
164
filter := metav1.ListOptions {LabelSelector : labelSelector .String ()}
158
165
159
166
// Protect against concurrent invocation
160
167
w .mutex .Lock ()
161
168
defer w .mutex .Unlock ()
162
169
163
- for _ , obj := range objs .Items {
170
+ for _ , obj := range op . Objects .Items {
164
171
gvk := obj .GroupVersionKind ()
165
172
166
173
key := fmt .Sprintf ("gvk=%s:%s:%s;labels=%s" , gvk .Group , gvk .Version , gvk .Kind , filter .LabelSelector )
167
174
168
175
filterNamespace := ""
169
- if w .options . ScopeWatchesToNamespace && obj .GetNamespace () != "" {
176
+ if w .scopeWatchesToNamespace && obj .GetNamespace () != "" {
170
177
filterNamespace = obj .GetNamespace ()
171
178
key += ";namespace=" + filterNamespace
172
179
}
0 commit comments