2017-11-11 04:44:02 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
var controllerTemplate = `package {{.schema.Version.Version}}
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2017-11-28 21:28:25 +00:00
|
|
|
{{.importPackage}}
|
2018-04-03 19:49:20 +00:00
|
|
|
"github.com/rancher/norman/objectclient"
|
2017-11-11 04:44:02 +00:00
|
|
|
"github.com/rancher/norman/controller"
|
2017-11-28 22:08:31 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
2017-11-11 04:44:02 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2017-11-28 22:08:31 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2017-11-11 04:44:02 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
{{.schema.CodeName}}GroupVersionKind = schema.GroupVersionKind{
|
2017-12-16 08:22:59 +00:00
|
|
|
Version: Version,
|
|
|
|
Group: GroupName,
|
2017-11-11 04:44:02 +00:00
|
|
|
Kind: "{{.schema.CodeName}}",
|
|
|
|
}
|
|
|
|
{{.schema.CodeName}}Resource = metav1.APIResource{
|
|
|
|
Name: "{{.schema.PluralName | toLower}}",
|
|
|
|
SingularName: "{{.schema.ID | toLower}}",
|
2017-11-28 21:28:25 +00:00
|
|
|
{{- if eq .schema.Scope "namespace" }}
|
|
|
|
Namespaced: true,
|
|
|
|
{{ else }}
|
2017-11-11 04:44:02 +00:00
|
|
|
Namespaced: false,
|
2017-11-28 21:28:25 +00:00
|
|
|
{{- end }}
|
2017-11-11 04:44:02 +00:00
|
|
|
Kind: {{.schema.CodeName}}GroupVersionKind.Kind,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-11-12 00:07:09 +00:00
|
|
|
type {{.schema.CodeName}}List struct {
|
|
|
|
metav1.TypeMeta %BACK%json:",inline"%BACK%
|
2017-11-13 21:27:53 +00:00
|
|
|
metav1.ListMeta %BACK%json:"metadata,omitempty"%BACK%
|
2017-11-28 21:28:25 +00:00
|
|
|
Items []{{.prefix}}{{.schema.CodeName}}
|
2017-11-12 00:07:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 16:55:18 +00:00
|
|
|
type {{.schema.CodeName}}HandlerFunc func(key string, obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error)
|
2017-11-28 21:28:25 +00:00
|
|
|
|
2018-11-19 23:30:00 +00:00
|
|
|
type {{.schema.CodeName}}ChangeHandlerFunc func(obj *{{.prefix}}{{.schema.CodeName}}) (runtime.Object, error)
|
|
|
|
|
2017-11-28 21:28:25 +00:00
|
|
|
type {{.schema.CodeName}}Lister interface {
|
|
|
|
List(namespace string, selector labels.Selector) (ret []*{{.prefix}}{{.schema.CodeName}}, err error)
|
|
|
|
Get(namespace, name string) (*{{.prefix}}{{.schema.CodeName}}, error)
|
|
|
|
}
|
2017-11-11 04:44:02 +00:00
|
|
|
|
|
|
|
type {{.schema.CodeName}}Controller interface {
|
2018-09-24 15:58:30 +00:00
|
|
|
Generic() controller.GenericController
|
2017-11-11 04:44:02 +00:00
|
|
|
Informer() cache.SharedIndexInformer
|
2017-11-28 21:28:25 +00:00
|
|
|
Lister() {{.schema.CodeName}}Lister
|
2018-10-30 16:54:49 +00:00
|
|
|
AddHandler(ctx context.Context, name string, handler {{.schema.CodeName}}HandlerFunc)
|
|
|
|
AddClusterScopedHandler(ctx context.Context, name, clusterName string, handler {{.schema.CodeName}}HandlerFunc)
|
2017-11-11 04:44:02 +00:00
|
|
|
Enqueue(namespace, name string)
|
2017-11-28 21:28:25 +00:00
|
|
|
Sync(ctx context.Context) error
|
2017-11-21 23:16:45 +00:00
|
|
|
Start(ctx context.Context, threadiness int) error
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type {{.schema.CodeName}}Interface interface {
|
2018-04-03 19:49:20 +00:00
|
|
|
ObjectClient() *objectclient.ObjectClient
|
2017-11-28 21:28:25 +00:00
|
|
|
Create(*{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
|
2018-01-18 23:22:36 +00:00
|
|
|
GetNamespaced(namespace, name string, opts metav1.GetOptions) (*{{.prefix}}{{.schema.CodeName}}, error)
|
2017-11-28 21:28:25 +00:00
|
|
|
Get(name string, opts metav1.GetOptions) (*{{.prefix}}{{.schema.CodeName}}, error)
|
|
|
|
Update(*{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error)
|
2017-11-11 04:44:02 +00:00
|
|
|
Delete(name string, options *metav1.DeleteOptions) error
|
2018-01-18 23:22:36 +00:00
|
|
|
DeleteNamespaced(namespace, name string, options *metav1.DeleteOptions) error
|
2017-11-29 21:27:02 +00:00
|
|
|
List(opts metav1.ListOptions) (*{{.schema.CodeName}}List, error)
|
2017-11-11 04:44:02 +00:00
|
|
|
Watch(opts metav1.ListOptions) (watch.Interface, error)
|
|
|
|
DeleteCollection(deleteOpts *metav1.DeleteOptions, listOpts metav1.ListOptions) error
|
2017-11-13 19:50:25 +00:00
|
|
|
Controller() {{.schema.CodeName}}Controller
|
2018-10-30 16:54:49 +00:00
|
|
|
AddHandler(ctx context.Context, name string, sync {{.schema.CodeName}}HandlerFunc)
|
|
|
|
AddLifecycle(ctx context.Context, name string, lifecycle {{.schema.CodeName}}Lifecycle)
|
|
|
|
AddClusterScopedHandler(ctx context.Context, name, clusterName string, sync {{.schema.CodeName}}HandlerFunc)
|
|
|
|
AddClusterScopedLifecycle(ctx context.Context, name, clusterName string, lifecycle {{.schema.CodeName}}Lifecycle)
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 21:28:25 +00:00
|
|
|
type {{.schema.ID}}Lister struct {
|
|
|
|
controller *{{.schema.ID}}Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *{{.schema.ID}}Lister) List(namespace string, selector labels.Selector) (ret []*{{.prefix}}{{.schema.CodeName}}, err error) {
|
|
|
|
err = cache.ListAllByNamespace(l.controller.Informer().GetIndexer(), namespace, selector, func(obj interface{}) {
|
|
|
|
ret = append(ret, obj.(*{{.prefix}}{{.schema.CodeName}}))
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *{{.schema.ID}}Lister) Get(namespace, name string) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
2017-12-12 03:25:05 +00:00
|
|
|
var key string
|
|
|
|
if namespace != "" {
|
|
|
|
key = namespace + "/" + name
|
|
|
|
} else {
|
|
|
|
key = name
|
|
|
|
}
|
|
|
|
obj, exists, err := l.controller.Informer().GetIndexer().GetByKey(key)
|
2017-11-28 21:28:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !exists {
|
2017-11-28 22:08:31 +00:00
|
|
|
return nil, errors.NewNotFound(schema.GroupResource{
|
|
|
|
Group: {{.schema.CodeName}}GroupVersionKind.Group,
|
|
|
|
Resource: "{{.schema.ID}}",
|
2018-06-21 23:06:20 +00:00
|
|
|
}, key)
|
2017-11-28 21:28:25 +00:00
|
|
|
}
|
|
|
|
return obj.(*{{.prefix}}{{.schema.CodeName}}), nil
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:44:02 +00:00
|
|
|
type {{.schema.ID}}Controller struct {
|
|
|
|
controller.GenericController
|
|
|
|
}
|
|
|
|
|
2018-09-24 15:58:30 +00:00
|
|
|
func (c *{{.schema.ID}}Controller) Generic() controller.GenericController {
|
|
|
|
return c.GenericController
|
|
|
|
}
|
|
|
|
|
2017-11-28 21:28:25 +00:00
|
|
|
func (c *{{.schema.ID}}Controller) Lister() {{.schema.CodeName}}Lister {
|
|
|
|
return &{{.schema.ID}}Lister{
|
|
|
|
controller: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-30 16:54:49 +00:00
|
|
|
func (c *{{.schema.ID}}Controller) AddHandler(ctx context.Context, name string, handler {{.schema.CodeName}}HandlerFunc) {
|
|
|
|
c.GenericController.AddHandler(ctx, name, func(key string, obj interface{}) (interface{}, error) {
|
|
|
|
if obj == nil {
|
2017-11-11 04:44:02 +00:00
|
|
|
return handler(key, nil)
|
2018-10-30 16:54:49 +00:00
|
|
|
} else if v, ok := obj.(*{{.prefix}}{{.schema.CodeName}}); ok {
|
|
|
|
return handler(key, v)
|
|
|
|
} else {
|
|
|
|
return nil, nil
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-30 16:54:49 +00:00
|
|
|
func (c *{{.schema.ID}}Controller) AddClusterScopedHandler(ctx context.Context, name, cluster string, handler {{.schema.CodeName}}HandlerFunc) {
|
|
|
|
c.GenericController.AddHandler(ctx, name, func(key string, obj interface{}) (interface{}, error) {
|
|
|
|
if obj == nil {
|
2018-01-15 20:38:10 +00:00
|
|
|
return handler(key, nil)
|
2018-10-30 16:54:49 +00:00
|
|
|
} else if v, ok := obj.(*{{.prefix}}{{.schema.CodeName}}); ok && controller.ObjectInCluster(cluster, obj) {
|
|
|
|
return handler(key, v)
|
|
|
|
} else {
|
|
|
|
return nil, nil
|
2018-01-15 20:38:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:44:02 +00:00
|
|
|
type {{.schema.ID}}Factory struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c {{.schema.ID}}Factory) Object() runtime.Object {
|
2017-11-28 21:28:25 +00:00
|
|
|
return &{{.prefix}}{{.schema.CodeName}}{}
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c {{.schema.ID}}Factory) List() runtime.Object {
|
|
|
|
return &{{.schema.CodeName}}List{}
|
|
|
|
}
|
|
|
|
|
2017-11-13 19:50:25 +00:00
|
|
|
func (s *{{.schema.ID}}Client) Controller() {{.schema.CodeName}}Controller {
|
|
|
|
s.client.Lock()
|
|
|
|
defer s.client.Unlock()
|
2017-11-11 04:44:02 +00:00
|
|
|
|
2017-11-13 19:50:25 +00:00
|
|
|
c, ok := s.client.{{.schema.ID}}Controllers[s.ns]
|
|
|
|
if ok {
|
|
|
|
return c
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 19:50:25 +00:00
|
|
|
genericController := controller.NewGenericController({{.schema.CodeName}}GroupVersionKind.Kind+"Controller",
|
2017-11-11 04:44:02 +00:00
|
|
|
s.objectClient)
|
|
|
|
|
2017-11-13 19:50:25 +00:00
|
|
|
c = &{{.schema.ID}}Controller{
|
|
|
|
GenericController: genericController,
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
2017-11-13 19:50:25 +00:00
|
|
|
|
|
|
|
s.client.{{.schema.ID}}Controllers[s.ns] = c
|
2017-11-28 21:28:25 +00:00
|
|
|
s.client.starters = append(s.client.starters, c)
|
2017-11-13 19:50:25 +00:00
|
|
|
|
|
|
|
return c
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type {{.schema.ID}}Client struct {
|
2017-11-13 19:50:25 +00:00
|
|
|
client *Client
|
|
|
|
ns string
|
2018-04-03 19:49:20 +00:00
|
|
|
objectClient *objectclient.ObjectClient
|
2017-11-11 04:44:02 +00:00
|
|
|
controller {{.schema.CodeName}}Controller
|
|
|
|
}
|
|
|
|
|
2018-04-03 19:49:20 +00:00
|
|
|
func (s *{{.schema.ID}}Client) ObjectClient() *objectclient.ObjectClient {
|
2017-11-28 21:28:25 +00:00
|
|
|
return s.objectClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *{{.schema.ID}}Client) Create(o *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
2017-11-11 04:44:02 +00:00
|
|
|
obj, err := s.objectClient.Create(o)
|
2017-11-28 21:28:25 +00:00
|
|
|
return obj.(*{{.prefix}}{{.schema.CodeName}}), err
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 21:28:25 +00:00
|
|
|
func (s *{{.schema.ID}}Client) Get(name string, opts metav1.GetOptions) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
2017-11-11 04:44:02 +00:00
|
|
|
obj, err := s.objectClient.Get(name, opts)
|
2017-11-28 21:28:25 +00:00
|
|
|
return obj.(*{{.prefix}}{{.schema.CodeName}}), err
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 23:22:36 +00:00
|
|
|
func (s *{{.schema.ID}}Client) GetNamespaced(namespace, name string, opts metav1.GetOptions) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
|
|
|
obj, err := s.objectClient.GetNamespaced(namespace, name, opts)
|
2017-12-29 22:05:27 +00:00
|
|
|
return obj.(*{{.prefix}}{{.schema.CodeName}}), err
|
|
|
|
}
|
|
|
|
|
2017-11-28 21:28:25 +00:00
|
|
|
func (s *{{.schema.ID}}Client) Update(o *{{.prefix}}{{.schema.CodeName}}) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
2017-11-11 04:44:02 +00:00
|
|
|
obj, err := s.objectClient.Update(o.Name, o)
|
2017-11-28 21:28:25 +00:00
|
|
|
return obj.(*{{.prefix}}{{.schema.CodeName}}), err
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *{{.schema.ID}}Client) Delete(name string, options *metav1.DeleteOptions) error {
|
|
|
|
return s.objectClient.Delete(name, options)
|
|
|
|
}
|
|
|
|
|
2018-01-18 23:22:36 +00:00
|
|
|
func (s *{{.schema.ID}}Client) DeleteNamespaced(namespace, name string, options *metav1.DeleteOptions) error {
|
|
|
|
return s.objectClient.DeleteNamespaced(namespace, name, options)
|
2017-12-29 22:05:27 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 21:27:02 +00:00
|
|
|
func (s *{{.schema.ID}}Client) List(opts metav1.ListOptions) (*{{.schema.CodeName}}List, error) {
|
2017-11-11 04:44:02 +00:00
|
|
|
obj, err := s.objectClient.List(opts)
|
2017-11-29 21:27:02 +00:00
|
|
|
return obj.(*{{.schema.CodeName}}List), err
|
2017-11-11 04:44:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *{{.schema.ID}}Client) Watch(opts metav1.ListOptions) (watch.Interface, error) {
|
|
|
|
return s.objectClient.Watch(opts)
|
|
|
|
}
|
|
|
|
|
2017-12-23 06:09:40 +00:00
|
|
|
// Patch applies the patch and returns the patched deployment.
|
|
|
|
func (s *{{.schema.ID}}Client) Patch(o *{{.prefix}}{{.schema.CodeName}}, data []byte, subresources ...string) (*{{.prefix}}{{.schema.CodeName}}, error) {
|
|
|
|
obj, err := s.objectClient.Patch(o.Name, o, data, subresources...)
|
|
|
|
return obj.(*{{.prefix}}{{.schema.CodeName}}), err
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:44:02 +00:00
|
|
|
func (s *{{.schema.ID}}Client) DeleteCollection(deleteOpts *metav1.DeleteOptions, listOpts metav1.ListOptions) error {
|
|
|
|
return s.objectClient.DeleteCollection(deleteOpts, listOpts)
|
|
|
|
}
|
2017-12-16 08:22:59 +00:00
|
|
|
|
2018-10-30 16:54:49 +00:00
|
|
|
func (s *{{.schema.ID}}Client) AddHandler(ctx context.Context, name string, sync {{.schema.CodeName}}HandlerFunc) {
|
|
|
|
s.Controller().AddHandler(ctx, name, sync)
|
2017-12-16 08:22:59 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 16:54:49 +00:00
|
|
|
func (s *{{.schema.ID}}Client) AddLifecycle(ctx context.Context, name string, lifecycle {{.schema.CodeName}}Lifecycle) {
|
2018-01-15 20:38:10 +00:00
|
|
|
sync := New{{.schema.CodeName}}LifecycleAdapter(name, false, s, lifecycle)
|
2018-10-30 16:54:49 +00:00
|
|
|
s.Controller().AddHandler(ctx, name, sync)
|
2017-12-16 08:22:59 +00:00
|
|
|
}
|
2018-01-15 20:38:10 +00:00
|
|
|
|
2018-10-30 16:54:49 +00:00
|
|
|
func (s *{{.schema.ID}}Client) AddClusterScopedHandler(ctx context.Context, name, clusterName string, sync {{.schema.CodeName}}HandlerFunc) {
|
|
|
|
s.Controller().AddClusterScopedHandler(ctx, name, clusterName, sync)
|
2018-01-15 20:38:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 16:54:49 +00:00
|
|
|
func (s *{{.schema.ID}}Client) AddClusterScopedLifecycle(ctx context.Context, name, clusterName string, lifecycle {{.schema.CodeName}}Lifecycle) {
|
2018-01-15 20:38:10 +00:00
|
|
|
sync := New{{.schema.CodeName}}LifecycleAdapter(name+"_"+clusterName, true, s, lifecycle)
|
2018-10-30 16:54:49 +00:00
|
|
|
s.Controller().AddClusterScopedHandler(ctx, name, clusterName, sync)
|
2018-01-15 20:38:10 +00:00
|
|
|
}
|
2018-11-19 23:30:00 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
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) 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()
|
2018-11-26 17:46:11 +00:00
|
|
|
n.iface.AddLifecycle(ctx, name+"-create", &{{.schema.ID}}LifecycleDelegate{create: sync})
|
2018-11-19 23:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *{{.schema.ID}}Client2) OnChange(ctx context.Context, name string, sync {{.schema.CodeName}}ChangeHandlerFunc) {
|
|
|
|
n.loadController()
|
2018-11-26 17:46:11 +00:00
|
|
|
n.iface.AddLifecycle(ctx, name+"-change", &{{.schema.ID}}LifecycleDelegate{update: sync})
|
2018-11-19 23:30:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2017-11-11 04:44:02 +00:00
|
|
|
`
|