mirror of
https://github.com/rancher/norman.git
synced 2025-09-01 07:08:59 +00:00
Merge pull request #341 from ibuildthecloud/transaction
Add EnqueueAfter
This commit is contained in:
@@ -50,6 +50,7 @@ type GenericController interface {
|
||||
AddHandler(ctx context.Context, name string, handler HandlerFunc)
|
||||
HandlerCount() int
|
||||
Enqueue(namespace, name string)
|
||||
EnqueueAfter(namespace, name string, after time.Duration)
|
||||
Sync(ctx context.Context) error
|
||||
Start(ctx context.Context, threadiness int) error
|
||||
}
|
||||
@@ -122,6 +123,16 @@ func (g *genericController) Enqueue(namespace, name string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (g *genericController) EnqueueAfter(namespace, name string, after time.Duration) {
|
||||
key := name
|
||||
if namespace != "" {
|
||||
key = namespace + "/" + name
|
||||
}
|
||||
if g.queue != nil {
|
||||
g.queue.AddAfter(key, after)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *genericController) AddHandler(ctx context.Context, name string, handler HandlerFunc) {
|
||||
t := getHandlerTransaction(ctx)
|
||||
if t == nil {
|
||||
|
@@ -4,6 +4,7 @@ var controllerTemplate = `package {{.schema.Version.Version}}
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
{{.importPackage}}
|
||||
"github.com/rancher/norman/objectclient"
|
||||
@@ -78,6 +79,7 @@ type {{.schema.CodeName}}Controller interface {
|
||||
AddClusterScopedHandler(ctx context.Context, name, clusterName string, handler {{.schema.CodeName}}HandlerFunc)
|
||||
AddClusterScopedFeatureHandler(ctx context.Context, enabled func() bool, name, clusterName string, handler {{.schema.CodeName}}HandlerFunc)
|
||||
Enqueue(namespace, name string)
|
||||
EnqueueAfter(namespace, name string, after time.Duration)
|
||||
Sync(ctx context.Context) error
|
||||
Start(ctx context.Context, threadiness int) error
|
||||
}
|
||||
@@ -334,184 +336,4 @@ func (s *{{.schema.ID}}Client) AddClusterScopedFeatureLifecycle(ctx context.Cont
|
||||
sync := New{{.schema.CodeName}}LifecycleAdapter(name+"_"+clusterName, true, s, lifecycle)
|
||||
s.Controller().AddClusterScopedFeatureHandler(ctx, enabled, name, clusterName, sync)
|
||||
}
|
||||
|
||||
type {{.schema.CodeName}}Indexer func(obj *{{.prefix}}{{.schema.CodeName}}) ([]string, error)
|
||||
|
||||
type {{.schema.CodeName}}ClientCache interface {
|
||||
Get(namespace, name string) (*{{.prefix}}{{.schema.CodeName}}, error)
|
||||
List(namespace string, selector labels.Selector) ([]*{{.prefix}}{{.schema.CodeName}}, error)
|
||||
|
||||
Index(name string, indexer {{.schema.CodeName}}Indexer)
|
||||
GetIndexed(name, key string) ([]*{{.prefix}}{{.schema.CodeName}}, error)
|
||||
}
|
||||
|
||||
type {{.schema.CodeName}}Client interface {
|
||||
Create(*{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
|
||||
Get(namespace, name string, opts metav1.GetOptions) (*{{.prefix}}{{.schema.CodeName}}, error)
|
||||
Update(*{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
|
||||
Delete(namespace, name string, options *metav1.DeleteOptions) error
|
||||
List(namespace string, opts metav1.ListOptions) (*{{.schema.CodeName}}List, error)
|
||||
Watch(opts metav1.ListOptions) (watch.Interface, error)
|
||||
|
||||
Cache() {{.schema.CodeName}}ClientCache
|
||||
|
||||
OnCreate(ctx context.Context, name string, sync {{.schema.CodeName}}ChangeHandlerFunc)
|
||||
OnChange(ctx context.Context, name string, sync {{.schema.CodeName}}ChangeHandlerFunc)
|
||||
OnRemove(ctx context.Context, name string, sync {{.schema.CodeName}}ChangeHandlerFunc)
|
||||
Enqueue(namespace, name string)
|
||||
|
||||
Generic() controller.GenericController
|
||||
ObjectClient() *objectclient.ObjectClient
|
||||
Interface() {{.schema.CodeName}}Interface
|
||||
}
|
||||
|
||||
type {{.schema.ID}}ClientCache struct {
|
||||
client *{{.schema.ID}}Client2
|
||||
}
|
||||
|
||||
type {{.schema.ID}}Client2 struct {
|
||||
iface {{.schema.CodeName}}Interface
|
||||
controller {{.schema.CodeName}}Controller
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) Interface() {{.schema.CodeName}}Interface {
|
||||
return n.iface
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) Generic() controller.GenericController {
|
||||
return n.iface.Controller().Generic()
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) ObjectClient() *objectclient.ObjectClient {
|
||||
return n.Interface().ObjectClient()
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) Enqueue(namespace, name string) {
|
||||
n.iface.Controller().Enqueue(namespace, name)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) Create(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
||||
return n.iface.Create(obj)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) Get(namespace, name string, opts metav1.GetOptions) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
||||
return n.iface.GetNamespaced(namespace, name, opts)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) Update(obj *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
||||
return n.iface.Update(obj)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) Delete(namespace, name string, options *metav1.DeleteOptions) error {
|
||||
return n.iface.DeleteNamespaced(namespace, name, options)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) List(namespace string, opts metav1.ListOptions) (*{{.schema.CodeName}}List, error) {
|
||||
return n.iface.List(opts)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) Watch(opts metav1.ListOptions) (watch.Interface, error) {
|
||||
return n.iface.Watch(opts)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}ClientCache) Get(namespace, name string) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
||||
return n.client.controller.Lister().Get(namespace, name)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}ClientCache) List(namespace string, selector labels.Selector) ([]*{{.prefix}}{{.schema.CodeName}}, error) {
|
||||
return n.client.controller.Lister().List(namespace, selector)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) Cache() {{.schema.CodeName}}ClientCache {
|
||||
n.loadController()
|
||||
return &{{.schema.ID}}ClientCache{
|
||||
client: n,
|
||||
}
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) OnCreate(ctx context.Context, name string, sync {{.schema.CodeName}}ChangeHandlerFunc) {
|
||||
n.loadController()
|
||||
n.iface.AddLifecycle(ctx, name+"-create", &{{.schema.ID}}LifecycleDelegate{create: sync})
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) OnChange(ctx context.Context, name string, sync {{.schema.CodeName}}ChangeHandlerFunc) {
|
||||
n.loadController()
|
||||
n.iface.AddLifecycle(ctx, name+"-change", &{{.schema.ID}}LifecycleDelegate{update: sync})
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) OnRemove(ctx context.Context, name string, sync {{.schema.CodeName}}ChangeHandlerFunc) {
|
||||
n.loadController()
|
||||
n.iface.AddLifecycle(ctx, name, &{{.schema.ID}}LifecycleDelegate{remove: sync})
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}ClientCache) Index(name string, indexer {{.schema.CodeName}}Indexer) {
|
||||
err := n.client.controller.Informer().GetIndexer().AddIndexers(map[string]cache.IndexFunc{
|
||||
name: func(obj interface{}) ([]string, error) {
|
||||
if v, ok := obj.(*{{.prefix}}{{.schema.CodeName}}); ok {
|
||||
return indexer(v)
|
||||
}
|
||||
return nil, nil
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}ClientCache) GetIndexed(name, key string) ([]*{{.prefix}}{{.schema.CodeName}}, error) {
|
||||
var result []*{{.prefix}}{{.schema.CodeName}}
|
||||
objs, err := n.client.controller.Informer().GetIndexer().ByIndex(name, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, obj := range objs {
|
||||
if v, ok := obj.(*{{.prefix}}{{.schema.CodeName}}); ok {
|
||||
result = append(result, v)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}Client2) loadController() {
|
||||
if n.controller == nil {
|
||||
n.controller = n.iface.Controller()
|
||||
}
|
||||
}
|
||||
|
||||
type {{.schema.ID}}LifecycleDelegate struct {
|
||||
create {{.schema.CodeName}}ChangeHandlerFunc
|
||||
update {{.schema.CodeName}}ChangeHandlerFunc
|
||||
remove {{.schema.CodeName}}ChangeHandlerFunc
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}LifecycleDelegate) HasCreate() bool {
|
||||
return n.create != nil
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}LifecycleDelegate) Create(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error) {
|
||||
if n.create == nil {
|
||||
return obj, nil
|
||||
}
|
||||
return n.create(obj)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}LifecycleDelegate) HasFinalize() bool {
|
||||
return n.remove != nil
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}LifecycleDelegate) Remove(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error) {
|
||||
if n.remove == nil {
|
||||
return obj, nil
|
||||
}
|
||||
return n.remove(obj)
|
||||
}
|
||||
|
||||
func (n *{{.schema.ID}}LifecycleDelegate) Updated(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error) {
|
||||
if n.update == nil {
|
||||
return obj, nil
|
||||
}
|
||||
return n.update(obj)
|
||||
}
|
||||
`
|
||||
|
@@ -25,12 +25,6 @@ type Interface interface {
|
||||
{{.CodeNamePlural}}Getter{{end}}
|
||||
}
|
||||
|
||||
type Clients struct {
|
||||
Interface Interface
|
||||
{{range .schemas}}
|
||||
{{.CodeName}} {{.CodeName}}Client{{end}}
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
sync.Mutex
|
||||
restClient rest.Interface
|
||||
@@ -39,45 +33,6 @@ type Client struct {
|
||||
{{.ID}}Controllers map[string]{{.CodeName}}Controller{{end}}
|
||||
}
|
||||
|
||||
func Factory(ctx context.Context, config rest.Config) (context.Context, controller.Starter, error) {
|
||||
c, err := NewForConfig(config)
|
||||
if err != nil {
|
||||
return ctx, nil, err
|
||||
}
|
||||
|
||||
cs := NewClientsFromInterface(c)
|
||||
|
||||
ctx = context.WithValue(ctx, contextKeyType{}, c)
|
||||
ctx = context.WithValue(ctx, contextClientsKeyType{}, cs)
|
||||
return ctx, c, nil
|
||||
}
|
||||
|
||||
func ClientsFrom(ctx context.Context) *Clients {
|
||||
return ctx.Value(contextClientsKeyType{}).(*Clients)
|
||||
}
|
||||
|
||||
func From(ctx context.Context) Interface {
|
||||
return ctx.Value(contextKeyType{}).(Interface)
|
||||
}
|
||||
|
||||
func NewClients(config rest.Config) (*Clients, error) {
|
||||
iface, err := NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewClientsFromInterface(iface), nil
|
||||
}
|
||||
|
||||
func NewClientsFromInterface(iface Interface) *Clients {
|
||||
return &Clients{
|
||||
Interface: iface,
|
||||
{{range .schemas}}
|
||||
{{.CodeName}}: &{{.ID}}Client2{
|
||||
iface: iface.{{.CodeNamePlural}}(""),
|
||||
},{{end}}
|
||||
}
|
||||
}
|
||||
|
||||
func NewForConfig(config rest.Config) (Interface, error) {
|
||||
if config.NegotiatedSerializer == nil {
|
||||
config.NegotiatedSerializer = dynamic.NegotiatedSerializer
|
||||
|
Reference in New Issue
Block a user