-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Improve UX for using client-go generated Informers #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,6 +135,14 @@ func (r RunnableFunc) Start(s <-chan struct{}) error { | |
return r(s) | ||
} | ||
|
||
// StartAdapter wraps a Start function to make it implement Runnable | ||
func StartAdapter(s func(<-chan struct{})) Runnable { | ||
return RunnableFunc(func(c <-chan struct{}) error { | ||
s(c) | ||
return nil | ||
}) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like Backward compatibility contracts with core methods/functions are going to be more stricter than these utils/helpers pkgs. WDYT ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SGTM |
||
|
||
// New returns a new Manager for creating Controllers. | ||
func New(config *rest.Config, options Options) (Manager, error) { | ||
// Initialize a rest.config if none was specified | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,14 +17,22 @@ limitations under the License. | |
package source_test | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/golang/glog" | ||
"k8s.io/api/core/v1" | ||
kubeinformers "k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes" | ||
"sigs.k8s.io/controller-runtime/pkg/controller" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/runtime/signals" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
) | ||
|
||
var ctrl controller.Controller | ||
var mgr manager.Manager | ||
|
||
// This example Watches for Pod Events (e.g. Create / Update / Delete) and enqueues a reconcile.Request | ||
// with the Name and Namespace of the Pod. | ||
|
@@ -42,3 +50,27 @@ func ExampleChannel() { | |
&handler.EnqueueRequestForObject{}, | ||
) | ||
} | ||
|
||
// This example Watches for Service Events (e.g. Create / Update / Delete) and enqueues a reconcile.Request | ||
// with the Name and Namespace of the Service. It uses the client-go generated Service Informer instead of the | ||
// Generic Informer. | ||
func ExampleInformer() { | ||
generatedClient := kubernetes.NewForConfigOrDie(mgr.GetConfig()) | ||
generatedInformers := kubeinformers.NewSharedInformerFactory(generatedClient, time.Minute*30) | ||
|
||
// Add it to the Manager | ||
if err := mgr.Add(manager.StartAdapter(generatedInformers.Start)); err != nil { | ||
glog.Fatalf("error Adding InformerFactory to the Manager: %v", err) | ||
} | ||
|
||
// Setup Watch using the client-go generated Informer | ||
if err := ctrl.Watch( | ||
&source.Informer{InformerProvider: generatedInformers.Core().V1().Services()}, | ||
&handler.EnqueueRequestForObject{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I am reading correctly, there is an order requirement here. Informer source should only start after the informer Runnable has started and sync'ed ? Not sure how that is enforced. Will look into the code. I may be wrong also about the ordering requirement. |
||
); err != nil { | ||
glog.Fatalf("error Watching Services: %v", err) | ||
} | ||
|
||
// Start the Manager | ||
mgr.Start(signals.SetupSignalHandler()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,10 +241,18 @@ func (cs *Channel) syncLoop() { | |
} | ||
} | ||
|
||
// InformerProvider provides an Informer | ||
type InformerProvider interface { | ||
Informer() toolscache.SharedIndexInformer | ||
} | ||
|
||
// Informer is used to provide a source of events originating inside the cluster from Watches (e.g. Pod Create) | ||
type Informer struct { | ||
// Informer is the generated client-go Informer | ||
// Informer is the generated client-go Informer. Mutually exclusive with InformerProvider. | ||
Informer toolscache.SharedIndexInformer | ||
|
||
// InformerProvider provides a generated client-go Informer. Mutually exclusive with Informer. | ||
InformerProvider InformerProvider | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
var _ Source = &Informer{} | ||
|
@@ -255,11 +263,23 @@ func (is *Informer) Start(handler handler.EventHandler, queue workqueue.RateLimi | |
prct ...predicate.Predicate) error { | ||
|
||
// Informer should have been specified by the user. | ||
if is.Informer == nil { | ||
return fmt.Errorf("must specify Informer.Informer") | ||
if is.Informer == nil && is.InformerProvider == nil { | ||
return fmt.Errorf("must specify Informer.Informer or Informer.InformerProvider") | ||
} | ||
|
||
if is.Informer != nil && is.InformerProvider != nil { | ||
return fmt.Errorf("must specify only one of Informer.Informer and Informer.InformerProvider") | ||
} | ||
|
||
if is.Informer != nil { | ||
is.Informer.AddEventHandler(internal.EventHandler{Queue: queue, EventHandler: handler, Predicates: prct}) | ||
} | ||
|
||
if is.InformerProvider != nil { | ||
is.InformerProvider.Informer().AddEventHandler( | ||
internal.EventHandler{Queue: queue, EventHandler: handler, Predicates: prct}) | ||
} | ||
|
||
is.Informer.AddEventHandler(internal.EventHandler{Queue: queue, EventHandler: handler, Predicates: prct}) | ||
return nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To a new user it will read as --->
This starts some sort of an adapter
. Other names that comes to mind are:MakeRunnable
,StartFnAdapter
.