Skip to content

Commit 1eff23c

Browse files
committed
Create commonclient to abstract controller-runtime versions
commonclient contains wrappers around functions we use from controller-runtime which have changed between 0.11 and 0.15 (the version range we are supporting currently). This allows for buildtags to be used to select a previous version of controller-runtime, without needing to carry patches.
1 parent 48db027 commit 1eff23c

File tree

15 files changed

+198
-41
lines changed

15 files changed

+198
-41
lines changed

commonclient/doc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package commonclient provides version-independent wrappers over controller-runtime and client-go.
2+
// This enables one codebase to work with multiple versions of controller-runtime.
3+
package commonclient

commonclient/factory_cr11.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//go:build controllerruntime_11 || controllerruntime_12 || controllerruntime_13 || controllerruntime_14
2+
3+
package commonclient
4+
5+
import (
6+
"context"
7+
8+
"k8s.io/client-go/util/workqueue"
9+
"sigs.k8s.io/controller-runtime/pkg/cache"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
"sigs.k8s.io/controller-runtime/pkg/event"
12+
"sigs.k8s.io/controller-runtime/pkg/handler"
13+
"sigs.k8s.io/controller-runtime/pkg/source"
14+
)
15+
16+
// SourceKind is a version-indendenent abstraction over calling source.Kind
17+
func SourceKind(cache cache.Cache, obj client.Object) source.SyncingSource {
18+
return source.NewKindWithCache(obj, cache)
19+
}
20+
21+
// WrapEventHandler is a version-indendenent abstraction over handler.EventHandler
22+
func WrapEventHandler(h EventHandler) handler.EventHandler {
23+
return &eventHandlerWithoutContext{h: h}
24+
}
25+
26+
type eventHandlerWithoutContext struct {
27+
h EventHandler
28+
}
29+
30+
func (h *eventHandlerWithoutContext) Create(ev event.CreateEvent, q workqueue.RateLimitingInterface) {
31+
h.h.Create(context.TODO(), ev, q)
32+
}
33+
func (h *eventHandlerWithoutContext) Update(ev event.UpdateEvent, q workqueue.RateLimitingInterface) {
34+
h.h.Update(context.TODO(), ev, q)
35+
}
36+
func (h *eventHandlerWithoutContext) Delete(ev event.DeleteEvent, q workqueue.RateLimitingInterface) {
37+
h.h.Delete(context.TODO(), ev, q)
38+
}
39+
func (h *eventHandlerWithoutContext) Generic(ev event.GenericEvent, q workqueue.RateLimitingInterface) {
40+
h.h.Generic(context.TODO(), ev, q)
41+
}
42+
43+
// EventHandler is the controller-runtime 0.15 version of EventHandler (with a context argument)
44+
type EventHandler interface {
45+
// Create is called in response to an create event - e.g. Pod Creation.
46+
Create(context.Context, event.CreateEvent, workqueue.RateLimitingInterface)
47+
48+
// Update is called in response to an update event - e.g. Pod Updated.
49+
Update(context.Context, event.UpdateEvent, workqueue.RateLimitingInterface)
50+
51+
// Delete is called in response to a delete event - e.g. Pod Deleted.
52+
Delete(context.Context, event.DeleteEvent, workqueue.RateLimitingInterface)
53+
54+
// Generic is called in response to an event of an unknown type or a synthetic event triggered as a cron or
55+
// external trigger request - e.g. reconcile Autoscaling, or a Webhook.
56+
Generic(context.Context, event.GenericEvent, workqueue.RateLimitingInterface)
57+
}

commonclient/factory_cr15.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build !(controllerruntime_11 || controllerruntime_12 || controllerruntime_13 || controllerruntime_14)
2+
3+
package commonclient
4+
5+
import (
6+
"sigs.k8s.io/controller-runtime/pkg/cache"
7+
"sigs.k8s.io/controller-runtime/pkg/client"
8+
"sigs.k8s.io/controller-runtime/pkg/handler"
9+
"sigs.k8s.io/controller-runtime/pkg/source"
10+
)
11+
12+
// SourceKind is a version-indendenent abstraction over calling source.Kind
13+
func SourceKind(cache cache.Cache, obj client.Object) source.Source {
14+
return source.Kind(cache, obj)
15+
}
16+
17+
// WrapEventHandler is a version-indendenent abstraction over handler.EventHandler
18+
func WrapEventHandler(h handler.EventHandler) handler.EventHandler {
19+
return h
20+
}
21+
22+
type EventHandler = handler.EventHandler

commonclient/restmapper_cr11.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build controllerruntime_11 || controllerruntime_12 || controllerruntime_13 || controllerruntime_14
2+
3+
package commonclient
4+
5+
import (
6+
"net/http"
7+
8+
"k8s.io/apimachinery/pkg/api/meta"
9+
"k8s.io/client-go/rest"
10+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
11+
)
12+
13+
// NewDiscoveryRESTMapper is a version-independent wrapper around apiutil.NewDiscoveryRESTMapper
14+
func NewDiscoveryRESTMapper(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) {
15+
return apiutil.NewDiscoveryRESTMapper(c)
16+
}

commonclient/restmapper_cr15.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build !(controllerruntime_11 || controllerruntime_12 || controllerruntime_13 || controllerruntime_14)
2+
3+
package commonclient
4+
5+
import (
6+
"net/http"
7+
8+
"k8s.io/apimachinery/pkg/api/meta"
9+
"k8s.io/client-go/rest"
10+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
11+
)
12+
13+
// NewDiscoveryRESTMapper is a version-independent wrapper around apiutil.NewDiscoveryRESTMapper
14+
func NewDiscoveryRESTMapper(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) {
15+
return apiutil.NewDiscoveryRESTMapper(c, httpClient)
16+
}

pkg/patterns/declarative/metrics.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"sigs.k8s.io/controller-runtime/pkg/predicate"
3434
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3535
"sigs.k8s.io/controller-runtime/pkg/source"
36+
"sigs.k8s.io/kubebuilder-declarative-pattern/commonclient"
3637
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest"
3738
)
3839

@@ -351,9 +352,9 @@ func newGVKTracker(mgr manager.Manager, obj *unstructured.Unstructured, namespac
351352
gvkt = &gvkTracker{}
352353
gvkt.list = newItems()
353354
gvkt.recorder = objectRecorderFor(obj.GroupVersionKind())
354-
gvkt.src = source.Kind(mgr.GetCache(), obj)
355+
gvkt.src = commonclient.SourceKind(mgr.GetCache(), obj)
355356
gvkt.predicate = predicate.Funcs{}
356-
gvkt.eventHandler = recordTrigger{gvkt.list, namespaced, gvkt.recorder}
357+
gvkt.eventHandler = commonclient.WrapEventHandler(recordTrigger{gvkt.list, namespaced, gvkt.recorder})
357358

358359
return
359360
}
@@ -531,7 +532,7 @@ func (r record) DeleteIfNeeded(name string, limit int) bool {
531532
return false
532533
}
533534

534-
var _ handler.EventHandler = recordTrigger{}
535+
var _ commonclient.EventHandler = recordTrigger{}
535536

536537
type recordTrigger struct {
537538
list *items

pkg/patterns/declarative/pkg/applier/applylib_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"k8s.io/apimachinery/pkg/runtime"
1515
"k8s.io/apimachinery/pkg/runtime/schema"
1616
"k8s.io/client-go/rest"
17-
restclient "k8s.io/client-go/rest"
1817
"k8s.io/klog/v2"
1918
"sigs.k8s.io/controller-runtime/pkg/client/fake"
2019
"sigs.k8s.io/kubebuilder-declarative-pattern/applylib/applyset"
@@ -91,14 +90,9 @@ func runApplierGoldenTests(t *testing.T, testDir string, interceptHTTPServer boo
9190
t.Errorf("error parsing manifest %q: %v", p, err)
9291
}
9392

94-
c, err := restclient.HTTPClientFor(restConfig)
93+
restMapper, err := restmapper.NewForTest(restConfig)
9594
if err != nil {
96-
t.Fatalf("error from HTTClientFor: %v", err)
97-
}
98-
99-
restMapper, err := restmapper.NewControllerRESTMapper(restConfig, c)
100-
if err != nil {
101-
t.Fatalf("error from controllerrestmapper.NewControllerRESTMapper: %v", err)
95+
t.Fatalf("error from controllerrestmapper.NewForTest: %v", err)
10296
}
10397

10498
parent := fakeParent()

pkg/patterns/declarative/pkg/applier/global.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build !without_exec_applier || !without_direct_applier
2-
// +build !without_exec_applier !without_direct_applier
1+
//go:build !without_direct_applier
2+
// +build !without_direct_applier
33

44
package applier
55

pkg/patterns/declarative/pkg/applier/global_without_kubectl.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//go:build without_exec_applier && without_direct_applier
2-
// +build without_exec_applier,without_direct_applier
1+
//go:build without_direct_applier
2+
// +build without_direct_applier
33

44
package applier
55

pkg/patterns/declarative/watch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ import (
2929
"k8s.io/client-go/rest"
3030
restclient "k8s.io/client-go/rest"
3131
ctrl "sigs.k8s.io/controller-runtime"
32-
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3332
"sigs.k8s.io/controller-runtime/pkg/controller"
3433
"sigs.k8s.io/controller-runtime/pkg/handler"
3534
"sigs.k8s.io/controller-runtime/pkg/log"
3635
"sigs.k8s.io/controller-runtime/pkg/source"
36+
"sigs.k8s.io/kubebuilder-declarative-pattern/commonclient"
3737
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/watch"
3838
)
3939

@@ -105,7 +105,7 @@ func WatchChildren(options WatchChildrenOptions) error {
105105
if err != nil {
106106
return err
107107
}
108-
rm, err := apiutil.NewDiscoveryRESTMapper(options.RESTConfig, client)
108+
rm, err := commonclient.NewDiscoveryRESTMapper(options.RESTConfig, client)
109109
if err != nil {
110110
return err
111111
}

0 commit comments

Comments
 (0)