Skip to content

Commit 003c19c

Browse files
authored
Merge pull request #171 from justinsb/reuse_restmapper
Re-use the RESTMapper from the ctrl.Manager in the dynamic watcher
2 parents b27195f + 69eaa29 commit 003c19c

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

docs/addon/walkthrough/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (r *GuestbookReconciler) SetupWithManager(mgr ctrl.Manager) error {
176176
}
177177

178178
// Watch for changes to deployed objects
179-
_, err = declarative.WatchAll(mgr.GetConfig(), c, r, r.watchLabels)
179+
_, err = declarative.WatchChildren(declarative.WatchChildrenOptions{Manager: mgr, Controller: c, Reconciler: r, LabelMaker: r.watchLabels})
180180
if err != nil {
181181
return err
182182
}

examples/guestbook-operator/controllers/guestbook_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (r *GuestbookReconciler) SetupWithManager(mgr ctrl.Manager) error {
7979
}
8080

8181
// Watch for changes to deployed objects
82-
_, err = declarative.WatchAll(mgr.GetConfig(), c, r, r.watchLabels)
82+
_, err = declarative.WatchChildren(declarative.WatchChildrenOptions{Manager: mgr, Controller: c, Reconciler: r, LabelMaker: r.watchLabels})
8383
if err != nil {
8484
return err
8585
}

pkg/patterns/declarative/pkg/watch/dynamic.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,27 @@ import (
2727
"k8s.io/apimachinery/pkg/runtime/schema"
2828
"k8s.io/apimachinery/pkg/watch"
2929
"k8s.io/client-go/dynamic"
30-
"k8s.io/client-go/rest"
3130
"sigs.k8s.io/controller-runtime/pkg/client"
32-
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3331
"sigs.k8s.io/controller-runtime/pkg/event"
3432
"sigs.k8s.io/controller-runtime/pkg/log"
3533
)
3634

3735
// WatchDelay is the time between a Watch being dropped and attempting to resume it
3836
const WatchDelay = 30 * time.Second
3937

40-
func NewDynamicWatch(config rest.Config) (*dynamicWatch, chan event.GenericEvent, error) {
41-
dw := &dynamicWatch{events: make(chan event.GenericEvent)}
42-
43-
restMapper, err := apiutil.NewDiscoveryRESTMapper(&config)
44-
if err != nil {
45-
return nil, nil, err
46-
}
47-
48-
client, err := dynamic.NewForConfig(&config)
49-
if err != nil {
50-
return nil, nil, err
38+
// NewDynamicWatch constructs a watcher for unstructured objects.
39+
// Deprecated: avoid using directly; will move to internal in future.
40+
func NewDynamicWatch(restMapper meta.RESTMapper, client dynamic.Interface) (*dynamicWatch, chan event.GenericEvent, error) {
41+
dw := &dynamicWatch{
42+
events: make(chan event.GenericEvent),
43+
restMapper: restMapper,
44+
client: client,
5145
}
5246

53-
dw.restMapper = restMapper
54-
dw.config = config
55-
dw.client = client
5647
return dw, dw.events, nil
5748
}
5849

5950
type dynamicWatch struct {
60-
config rest.Config
6151
client dynamic.Interface
6252
restMapper meta.RESTMapper
6353
events chan event.GenericEvent

pkg/patterns/declarative/watch.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ import (
2121
"fmt"
2222
"sync"
2323

24+
"k8s.io/apimachinery/pkg/api/meta"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
"k8s.io/apimachinery/pkg/labels"
2627
"k8s.io/apimachinery/pkg/runtime/schema"
28+
"k8s.io/client-go/dynamic"
2729
"k8s.io/client-go/rest"
30+
ctrl "sigs.k8s.io/controller-runtime"
31+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2832
"sigs.k8s.io/controller-runtime/pkg/controller"
2933
"sigs.k8s.io/controller-runtime/pkg/handler"
3034
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -45,7 +49,10 @@ type DynamicWatch interface {
4549

4650
// WatchChildrenOptions configures how we want to watch children.
4751
type WatchChildrenOptions struct {
48-
// RESTConfig is the configuration for connecting to the cluster
52+
// Manager is used as a factory for the default RESTConfig and the RESTMapper.
53+
Manager ctrl.Manager
54+
55+
// RESTConfig is the configuration for connecting to the cluster.
4956
RESTConfig *rest.Config
5057

5158
// LabelMaker is used to build the labels we should watch on.
@@ -81,7 +88,31 @@ func WatchChildren(options WatchChildrenOptions) (chan struct{}, error) {
8188
return nil, fmt.Errorf("labelMaker is required to scope watches")
8289
}
8390

84-
dw, events, err := watch.NewDynamicWatch(*options.RESTConfig)
91+
if options.RESTConfig == nil {
92+
if options.Manager != nil {
93+
options.RESTConfig = options.Manager.GetConfig()
94+
} else {
95+
return nil, fmt.Errorf("RESTConfig or Manager should be set")
96+
}
97+
}
98+
99+
var restMapper meta.RESTMapper
100+
if options.Manager != nil {
101+
restMapper = options.Manager.GetRESTMapper()
102+
} else {
103+
rm, err := apiutil.NewDiscoveryRESTMapper(options.RESTConfig)
104+
if err != nil {
105+
return nil, err
106+
}
107+
restMapper = rm
108+
}
109+
110+
client, err := dynamic.NewForConfig(options.RESTConfig)
111+
if err != nil {
112+
return nil, err
113+
}
114+
115+
dw, events, err := watch.NewDynamicWatch(restMapper, client)
85116
if err != nil {
86117
return nil, fmt.Errorf("creating dynamic watch: %v", err)
87118
}

0 commit comments

Comments
 (0)