1
0
mirror of https://github.com/rancher/norman.git synced 2025-08-31 14:51:57 +00:00
Files
norman/controller/generic_controller.go

78 lines
2.2 KiB
Go
Raw Permalink Normal View History

2017-11-10 21:44:02 -07:00
package controller
import (
"context"
"time"
"github.com/rancher/lasso/pkg/controller"
2017-11-10 21:44:02 -07:00
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
2017-11-10 21:44:02 -07:00
"k8s.io/client-go/tools/cache"
)
2018-10-30 09:54:49 -07:00
type HandlerFunc func(key string, obj interface{}) (interface{}, error)
2017-11-10 21:44:02 -07:00
type GenericController interface {
Informer() cache.SharedIndexInformer
2018-10-30 09:54:49 -07:00
AddHandler(ctx context.Context, name string, handler HandlerFunc)
2017-11-10 21:44:02 -07:00
Enqueue(namespace, name string)
2020-02-05 21:15:26 -07:00
EnqueueAfter(namespace, name string, after time.Duration)
2018-01-15 22:08:36 -07:00
}
2017-11-10 21:44:02 -07:00
type genericController struct {
2020-05-15 17:38:28 -07:00
controller controller.SharedController
informer cache.SharedIndexInformer
name string
namespace string
2017-11-10 21:44:02 -07:00
}
func NewGenericController(namespace, name string, controller controller.SharedController) GenericController {
2017-11-10 21:44:02 -07:00
return &genericController{
2020-05-15 17:38:28 -07:00
controller: controller,
informer: controller.Informer(),
name: name,
namespace: namespace,
2017-11-13 12:50:25 -07:00
}
2017-11-10 21:44:02 -07:00
}
func (g *genericController) Informer() cache.SharedIndexInformer {
return g.informer
}
func (g *genericController) Enqueue(namespace, name string) {
2020-05-15 17:38:28 -07:00
g.controller.Enqueue(namespace, name)
2017-11-10 21:44:02 -07:00
}
2020-02-05 21:15:26 -07:00
func (g *genericController) EnqueueAfter(namespace, name string, after time.Duration) {
2020-05-15 17:38:28 -07:00
g.controller.EnqueueAfter(namespace, name, after)
2020-02-05 21:15:26 -07:00
}
2018-10-30 09:54:49 -07:00
func (g *genericController) AddHandler(ctx context.Context, name string, handler HandlerFunc) {
2020-05-15 17:38:28 -07:00
g.controller.RegisterHandler(ctx, name, controller.SharedControllerHandlerFunc(func(key string, obj runtime.Object) (runtime.Object, error) {
if !isNamespace(g.namespace, obj) {
return obj, nil
}
2020-05-15 17:38:28 -07:00
logrus.Tracef("%s calling handler %s %s", g.name, name, key)
result, err := handler(key, obj)
runtimeObject, _ := result.(runtime.Object)
if _, ok := err.(*ForgetError); ok {
2020-03-11 22:48:52 +01:00
logrus.Tracef("%v %v completed with dropped err: %v", g.name, key, err)
2020-05-15 17:38:28 -07:00
return runtimeObject, controller.ErrIgnore
2018-01-15 22:08:36 -07:00
}
2020-05-15 17:38:28 -07:00
return runtimeObject, err
}))
2017-11-10 21:44:02 -07:00
}
func isNamespace(namespace string, obj runtime.Object) bool {
2020-08-26 21:25:00 -07:00
if namespace == "" || obj == nil {
return true
}
meta, err := meta.Accessor(obj)
if err != nil {
2020-08-26 21:25:00 -07:00
// if you can't figure out the namespace, just let it through
return true
}
return meta.GetNamespace() == namespace
}