-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
17 changed files
with
210 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package commonclient provides version-independent wrappers over controller-runtime and client-go. | ||
// This enables one codebase to work with multiple versions of controller-runtime. | ||
package commonclient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package commonclient | ||
|
||
import "sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
||
// Factory is a helper type used for version-independent operations. | ||
type Factory struct { | ||
mgr manager.Manager | ||
} | ||
|
||
// NewFactory constructs a new Factory. | ||
func NewFactory(mgr manager.Manager) *Factory { | ||
return &Factory{ | ||
mgr: mgr, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//go:build controllerruntime_11 || controllerruntime_12 || controllerruntime_13 || controllerruntime_14 | ||
|
||
package commonclient | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/client-go/util/workqueue" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
// SourceKind is a version-indendenent abstraction over calling source.Kind | ||
func (f *Factory) SourceKind(obj client.Object) *source.Kind { | ||
return &source.Kind{Type: obj} | ||
} | ||
|
||
// EventHandler is a version-indendenent abstraction over handler.EventHandler | ||
func (f *Factory) EventHandler(h EventHandler) handler.EventHandler { | ||
return &eventHandlerWithoutContext{h: h} | ||
} | ||
|
||
type eventHandlerWithoutContext struct { | ||
h EventHandler | ||
} | ||
|
||
func (h *eventHandlerWithoutContext) Create(ev event.CreateEvent, q workqueue.RateLimitingInterface) { | ||
h.h.Create(context.TODO(), ev, q) | ||
} | ||
func (h *eventHandlerWithoutContext) Update(ev event.UpdateEvent, q workqueue.RateLimitingInterface) { | ||
h.h.Update(context.TODO(), ev, q) | ||
} | ||
func (h *eventHandlerWithoutContext) Delete(ev event.DeleteEvent, q workqueue.RateLimitingInterface) { | ||
h.h.Delete(context.TODO(), ev, q) | ||
} | ||
func (h *eventHandlerWithoutContext) Generic(ev event.GenericEvent, q workqueue.RateLimitingInterface) { | ||
h.h.Generic(context.TODO(), ev, q) | ||
} | ||
|
||
// EventHandler is the controller-runtime 0.15 version of EventHandler (with a context argument) | ||
type EventHandler interface { | ||
// Create is called in response to an create event - e.g. Pod Creation. | ||
Create(context.Context, event.CreateEvent, workqueue.RateLimitingInterface) | ||
|
||
// Update is called in response to an update event - e.g. Pod Updated. | ||
Update(context.Context, event.UpdateEvent, workqueue.RateLimitingInterface) | ||
|
||
// Delete is called in response to a delete event - e.g. Pod Deleted. | ||
Delete(context.Context, event.DeleteEvent, workqueue.RateLimitingInterface) | ||
|
||
// Generic is called in response to an event of an unknown type or a synthetic event triggered as a cron or | ||
// external trigger request - e.g. reconcile Autoscaling, or a Webhook. | ||
Generic(context.Context, event.GenericEvent, workqueue.RateLimitingInterface) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//go:build !(controllerruntime_11 || controllerruntime_12 || controllerruntime_13 || controllerruntime_14) | ||
|
||
package commonclient | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
// SourceKind is a version-indendenent abstraction over calling source.Kind | ||
func (f *Factory) SourceKind(obj client.Object) source.Source { | ||
return source.Kind(f.mgr.GetCache(), obj) | ||
} | ||
|
||
// EventHandler is a version-indendenent abstraction over handler.EventHandler | ||
func (f *Factory) EventHandler(h handler.EventHandler) handler.EventHandler { | ||
return h | ||
} | ||
|
||
type EventHandler = handler.EventHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build controllerruntime_11 || controllerruntime_12 || controllerruntime_13 || controllerruntime_14 | ||
|
||
package commonclient | ||
|
||
import ( | ||
"net/http" | ||
|
||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
) | ||
|
||
// NewDiscoveryRESTMapper is a version-independent wrapper around apiutil.NewDiscoveryRESTMapper | ||
func NewDiscoveryRESTMapper(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) { | ||
return apiutil.NewDiscoveryRESTMapper(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build !(controllerruntime_11 || controllerruntime_12 || controllerruntime_13 || controllerruntime_14) | ||
|
||
package commonclient | ||
|
||
import ( | ||
"net/http" | ||
|
||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/client-go/rest" | ||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
) | ||
|
||
// NewDiscoveryRESTMapper is a version-independent wrapper around apiutil.NewDiscoveryRESTMapper | ||
func NewDiscoveryRESTMapper(c *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) { | ||
return apiutil.NewDiscoveryRESTMapper(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
pkg/patterns/declarative/pkg/applier/global_without_kubectl.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//go:build controllerruntime_11 || controllerruntime_12 || controllerruntime_13 || controllerruntime_14 | ||
|
||
package restmapper | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/client-go/discovery" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
// NewControllerRESTMapper is the constructor for a ControllerRESTMapper. | ||
func NewControllerRESTMapper(cfg *rest.Config) (meta.RESTMapper, error) { | ||
discoveryClient, err := discovery.NewDiscoveryClientForConfig(cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ControllerRESTMapper{ | ||
uncached: discoveryClient, | ||
cache: newCache(), | ||
}, nil | ||
} | ||
|
||
// NewForTest creates a ControllerRESTMapper, but is intended to be a common interface for use by tests. | ||
func NewForTest(cfg *rest.Config) (meta.RESTMapper, error) { | ||
return NewControllerRESTMapper(cfg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//go:build !(controllerruntime_11 || controllerruntime_12 || controllerruntime_13 || controllerruntime_14) | ||
|
||
package restmapper | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"k8s.io/apimachinery/pkg/api/meta" | ||
"k8s.io/client-go/discovery" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
// NewControllerRESTMapper is the constructor for a ControllerRESTMapper | ||
func NewControllerRESTMapper(cfg *rest.Config, httpClient *http.Client) (meta.RESTMapper, error) { | ||
discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(cfg, httpClient) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ControllerRESTMapper{ | ||
uncached: discoveryClient, | ||
cache: newCache(), | ||
}, nil | ||
} | ||
|
||
// NewForTest creates a ControllerRESTMapper, but is intended to be a common interface for use by tests. | ||
func NewForTest(restConfig *rest.Config) (meta.RESTMapper, error) { | ||
client, err := restclient.HTTPClientFor(restConfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("error from restclient.HTTPClientFor: %w", err) | ||
} | ||
return NewControllerRESTMapper(restConfig, client) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters