Skip to content

Commit

Permalink
Generate non-namespaced client for plugins (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mir Shahriar authored and tamalsaha committed Apr 20, 2018
1 parent 2228fac commit 6aaed16
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 83 deletions.
1 change: 1 addition & 0 deletions apis/monitoring/v1alpha1/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
)

// +genclient
// +genclient:nonNamespaced
// +genclient:skipVerbs=updateStatus
// +k8s:openapi-gen=true
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (c *FakeMonitoringV1alpha1) PodAlerts(namespace string) v1alpha1.PodAlertIn
return &FakePodAlerts{c, namespace}
}

func (c *FakeMonitoringV1alpha1) SearchlightPlugins(namespace string) v1alpha1.SearchlightPluginInterface {
return &FakeSearchlightPlugins{c, namespace}
func (c *FakeMonitoringV1alpha1) SearchlightPlugins() v1alpha1.SearchlightPluginInterface {
return &FakeSearchlightPlugins{c}
}

// RESTClient returns a RESTClient that is used to communicate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
// FakeSearchlightPlugins implements SearchlightPluginInterface
type FakeSearchlightPlugins struct {
Fake *FakeMonitoringV1alpha1
ns string
}

var searchlightpluginsResource = schema.GroupVersionResource{Group: "monitoring.appscode.com", Version: "v1alpha1", Resource: "searchlightplugins"}
Expand All @@ -39,8 +38,7 @@ var searchlightpluginsKind = schema.GroupVersionKind{Group: "monitoring.appscode
// Get takes name of the searchlightPlugin, and returns the corresponding searchlightPlugin object, and an error if there is any.
func (c *FakeSearchlightPlugins) Get(name string, options v1.GetOptions) (result *v1alpha1.SearchlightPlugin, err error) {
obj, err := c.Fake.
Invokes(testing.NewGetAction(searchlightpluginsResource, c.ns, name), &v1alpha1.SearchlightPlugin{})

Invokes(testing.NewRootGetAction(searchlightpluginsResource, name), &v1alpha1.SearchlightPlugin{})
if obj == nil {
return nil, err
}
Expand All @@ -50,8 +48,7 @@ func (c *FakeSearchlightPlugins) Get(name string, options v1.GetOptions) (result
// List takes label and field selectors, and returns the list of SearchlightPlugins that match those selectors.
func (c *FakeSearchlightPlugins) List(opts v1.ListOptions) (result *v1alpha1.SearchlightPluginList, err error) {
obj, err := c.Fake.
Invokes(testing.NewListAction(searchlightpluginsResource, searchlightpluginsKind, c.ns, opts), &v1alpha1.SearchlightPluginList{})

Invokes(testing.NewRootListAction(searchlightpluginsResource, searchlightpluginsKind, opts), &v1alpha1.SearchlightPluginList{})
if obj == nil {
return nil, err
}
Expand All @@ -72,15 +69,13 @@ func (c *FakeSearchlightPlugins) List(opts v1.ListOptions) (result *v1alpha1.Sea
// Watch returns a watch.Interface that watches the requested searchlightPlugins.
func (c *FakeSearchlightPlugins) Watch(opts v1.ListOptions) (watch.Interface, error) {
return c.Fake.
InvokesWatch(testing.NewWatchAction(searchlightpluginsResource, c.ns, opts))

InvokesWatch(testing.NewRootWatchAction(searchlightpluginsResource, opts))
}

// Create takes the representation of a searchlightPlugin and creates it. Returns the server's representation of the searchlightPlugin, and an error, if there is any.
func (c *FakeSearchlightPlugins) Create(searchlightPlugin *v1alpha1.SearchlightPlugin) (result *v1alpha1.SearchlightPlugin, err error) {
obj, err := c.Fake.
Invokes(testing.NewCreateAction(searchlightpluginsResource, c.ns, searchlightPlugin), &v1alpha1.SearchlightPlugin{})

Invokes(testing.NewRootCreateAction(searchlightpluginsResource, searchlightPlugin), &v1alpha1.SearchlightPlugin{})
if obj == nil {
return nil, err
}
Expand All @@ -90,8 +85,7 @@ func (c *FakeSearchlightPlugins) Create(searchlightPlugin *v1alpha1.SearchlightP
// Update takes the representation of a searchlightPlugin and updates it. Returns the server's representation of the searchlightPlugin, and an error, if there is any.
func (c *FakeSearchlightPlugins) Update(searchlightPlugin *v1alpha1.SearchlightPlugin) (result *v1alpha1.SearchlightPlugin, err error) {
obj, err := c.Fake.
Invokes(testing.NewUpdateAction(searchlightpluginsResource, c.ns, searchlightPlugin), &v1alpha1.SearchlightPlugin{})

Invokes(testing.NewRootUpdateAction(searchlightpluginsResource, searchlightPlugin), &v1alpha1.SearchlightPlugin{})
if obj == nil {
return nil, err
}
Expand All @@ -101,14 +95,13 @@ func (c *FakeSearchlightPlugins) Update(searchlightPlugin *v1alpha1.SearchlightP
// Delete takes name of the searchlightPlugin and deletes it. Returns an error if one occurs.
func (c *FakeSearchlightPlugins) Delete(name string, options *v1.DeleteOptions) error {
_, err := c.Fake.
Invokes(testing.NewDeleteAction(searchlightpluginsResource, c.ns, name), &v1alpha1.SearchlightPlugin{})

Invokes(testing.NewRootDeleteAction(searchlightpluginsResource, name), &v1alpha1.SearchlightPlugin{})
return err
}

// DeleteCollection deletes a collection of objects.
func (c *FakeSearchlightPlugins) DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error {
action := testing.NewDeleteCollectionAction(searchlightpluginsResource, c.ns, listOptions)
action := testing.NewRootDeleteCollectionAction(searchlightpluginsResource, listOptions)

_, err := c.Fake.Invokes(action, &v1alpha1.SearchlightPluginList{})
return err
Expand All @@ -117,8 +110,7 @@ func (c *FakeSearchlightPlugins) DeleteCollection(options *v1.DeleteOptions, lis
// Patch applies the patch and returns the patched searchlightPlugin.
func (c *FakeSearchlightPlugins) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1alpha1.SearchlightPlugin, err error) {
obj, err := c.Fake.
Invokes(testing.NewPatchSubresourceAction(searchlightpluginsResource, c.ns, name, data, subresources...), &v1alpha1.SearchlightPlugin{})

Invokes(testing.NewRootPatchSubresourceAction(searchlightpluginsResource, name, data, subresources...), &v1alpha1.SearchlightPlugin{})
if obj == nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (c *MonitoringV1alpha1Client) PodAlerts(namespace string) PodAlertInterface
return newPodAlerts(c, namespace)
}

func (c *MonitoringV1alpha1Client) SearchlightPlugins(namespace string) SearchlightPluginInterface {
return newSearchlightPlugins(c, namespace)
func (c *MonitoringV1alpha1Client) SearchlightPlugins() SearchlightPluginInterface {
return newSearchlightPlugins(c)
}

// NewForConfig creates a new MonitoringV1alpha1Client for the given config.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
// SearchlightPluginsGetter has a method to return a SearchlightPluginInterface.
// A group's client should implement this interface.
type SearchlightPluginsGetter interface {
SearchlightPlugins(namespace string) SearchlightPluginInterface
SearchlightPlugins() SearchlightPluginInterface
}

// SearchlightPluginInterface has methods to work with SearchlightPlugin resources.
Expand All @@ -47,22 +47,19 @@ type SearchlightPluginInterface interface {
// searchlightPlugins implements SearchlightPluginInterface
type searchlightPlugins struct {
client rest.Interface
ns string
}

// newSearchlightPlugins returns a SearchlightPlugins
func newSearchlightPlugins(c *MonitoringV1alpha1Client, namespace string) *searchlightPlugins {
func newSearchlightPlugins(c *MonitoringV1alpha1Client) *searchlightPlugins {
return &searchlightPlugins{
client: c.RESTClient(),
ns: namespace,
}
}

// Get takes name of the searchlightPlugin, and returns the corresponding searchlightPlugin object, and an error if there is any.
func (c *searchlightPlugins) Get(name string, options v1.GetOptions) (result *v1alpha1.SearchlightPlugin, err error) {
result = &v1alpha1.SearchlightPlugin{}
err = c.client.Get().
Namespace(c.ns).
Resource("searchlightplugins").
Name(name).
VersionedParams(&options, scheme.ParameterCodec).
Expand All @@ -75,7 +72,6 @@ func (c *searchlightPlugins) Get(name string, options v1.GetOptions) (result *v1
func (c *searchlightPlugins) List(opts v1.ListOptions) (result *v1alpha1.SearchlightPluginList, err error) {
result = &v1alpha1.SearchlightPluginList{}
err = c.client.Get().
Namespace(c.ns).
Resource("searchlightplugins").
VersionedParams(&opts, scheme.ParameterCodec).
Do().
Expand All @@ -87,7 +83,6 @@ func (c *searchlightPlugins) List(opts v1.ListOptions) (result *v1alpha1.Searchl
func (c *searchlightPlugins) Watch(opts v1.ListOptions) (watch.Interface, error) {
opts.Watch = true
return c.client.Get().
Namespace(c.ns).
Resource("searchlightplugins").
VersionedParams(&opts, scheme.ParameterCodec).
Watch()
Expand All @@ -97,7 +92,6 @@ func (c *searchlightPlugins) Watch(opts v1.ListOptions) (watch.Interface, error)
func (c *searchlightPlugins) Create(searchlightPlugin *v1alpha1.SearchlightPlugin) (result *v1alpha1.SearchlightPlugin, err error) {
result = &v1alpha1.SearchlightPlugin{}
err = c.client.Post().
Namespace(c.ns).
Resource("searchlightplugins").
Body(searchlightPlugin).
Do().
Expand All @@ -109,7 +103,6 @@ func (c *searchlightPlugins) Create(searchlightPlugin *v1alpha1.SearchlightPlugi
func (c *searchlightPlugins) Update(searchlightPlugin *v1alpha1.SearchlightPlugin) (result *v1alpha1.SearchlightPlugin, err error) {
result = &v1alpha1.SearchlightPlugin{}
err = c.client.Put().
Namespace(c.ns).
Resource("searchlightplugins").
Name(searchlightPlugin.Name).
Body(searchlightPlugin).
Expand All @@ -121,7 +114,6 @@ func (c *searchlightPlugins) Update(searchlightPlugin *v1alpha1.SearchlightPlugi
// Delete takes name of the searchlightPlugin and deletes it. Returns an error if one occurs.
func (c *searchlightPlugins) Delete(name string, options *v1.DeleteOptions) error {
return c.client.Delete().
Namespace(c.ns).
Resource("searchlightplugins").
Name(name).
Body(options).
Expand All @@ -132,7 +124,6 @@ func (c *searchlightPlugins) Delete(name string, options *v1.DeleteOptions) erro
// DeleteCollection deletes a collection of objects.
func (c *searchlightPlugins) DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error {
return c.client.Delete().
Namespace(c.ns).
Resource("searchlightplugins").
VersionedParams(&listOptions, scheme.ParameterCodec).
Body(options).
Expand All @@ -144,7 +135,6 @@ func (c *searchlightPlugins) DeleteCollection(options *v1.DeleteOptions, listOpt
func (c *searchlightPlugins) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1alpha1.SearchlightPlugin, err error) {
result = &v1alpha1.SearchlightPlugin{}
err = c.client.Patch(pt).
Namespace(c.ns).
Resource("searchlightplugins").
SubResource(subresources...).
Name(name).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
)

func CreateOrPatchSearchlightPlugin(c cs.MonitoringV1alpha1Interface, meta metav1.ObjectMeta, transform func(alert *api.SearchlightPlugin) *api.SearchlightPlugin) (*api.SearchlightPlugin, kutil.VerbType, error) {
cur, err := c.SearchlightPlugins(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
cur, err := c.SearchlightPlugins().Get(meta.Name, metav1.GetOptions{})
if kerr.IsNotFound(err) {
glog.V(3).Infof("Creating SearchlightPlugin %s/%s.", meta.Namespace, meta.Name)
out, err := c.SearchlightPlugins(meta.Namespace).Create(transform(&api.SearchlightPlugin{
out, err := c.SearchlightPlugins().Create(transform(&api.SearchlightPlugin{
TypeMeta: metav1.TypeMeta{
Kind: "SearchlightPlugin",
APIVersion: api.SchemeGroupVersion.String(),
Expand Down Expand Up @@ -56,19 +56,19 @@ func PatchSearchlightPluginObject(c cs.MonitoringV1alpha1Interface, cur, mod *ap
return cur, kutil.VerbUnchanged, nil
}
glog.V(3).Infof("Patching SearchlightPlugin %s/%s with %s.", cur.Namespace, cur.Name, string(patch))
out, err := c.SearchlightPlugins(cur.Namespace).Patch(cur.Name, types.MergePatchType, patch)
out, err := c.SearchlightPlugins().Patch(cur.Name, types.MergePatchType, patch)
return out, kutil.VerbPatched, err
}

func TryUpdateSearchlightPlugin(c cs.MonitoringV1alpha1Interface, meta metav1.ObjectMeta, transform func(*api.SearchlightPlugin) *api.SearchlightPlugin) (result *api.SearchlightPlugin, err error) {
attempt := 0
err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) {
attempt++
cur, e2 := c.SearchlightPlugins(meta.Namespace).Get(meta.Name, metav1.GetOptions{})
cur, e2 := c.SearchlightPlugins().Get(meta.Name, metav1.GetOptions{})
if kerr.IsNotFound(e2) {
return false, e2
} else if e2 == nil {
result, e2 = c.SearchlightPlugins(cur.Namespace).Update(transform(cur.DeepCopy()))
result, e2 = c.SearchlightPlugins().Update(transform(cur.DeepCopy()))
return e2 == nil, nil
}
glog.Errorf("Attempt %d failed to update SearchlightPlugin %s/%s due to %v.", attempt, cur.Namespace, cur.Name, e2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ func (v *version) PodAlerts() PodAlertInformer {

// SearchlightPlugins returns a SearchlightPluginInformer.
func (v *version) SearchlightPlugins() SearchlightPluginInformer {
return &searchlightPluginInformer{factory: v.factory, namespace: v.namespace, tweakListOptions: v.tweakListOptions}
return &searchlightPluginInformer{factory: v.factory, tweakListOptions: v.tweakListOptions}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,32 @@ type SearchlightPluginInformer interface {
type searchlightPluginInformer struct {
factory internalinterfaces.SharedInformerFactory
tweakListOptions internalinterfaces.TweakListOptionsFunc
namespace string
}

// NewSearchlightPluginInformer constructs a new informer for SearchlightPlugin type.
// Always prefer using an informer factory to get a shared informer instead of getting an independent
// one. This reduces memory footprint and number of connections to the server.
func NewSearchlightPluginInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer {
return NewFilteredSearchlightPluginInformer(client, namespace, resyncPeriod, indexers, nil)
func NewSearchlightPluginInformer(client versioned.Interface, resyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer {
return NewFilteredSearchlightPluginInformer(client, resyncPeriod, indexers, nil)
}

// NewFilteredSearchlightPluginInformer constructs a new informer for SearchlightPlugin type.
// Always prefer using an informer factory to get a shared informer instead of getting an independent
// one. This reduces memory footprint and number of connections to the server.
func NewFilteredSearchlightPluginInformer(client versioned.Interface, namespace string, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer {
func NewFilteredSearchlightPluginInformer(client versioned.Interface, resyncPeriod time.Duration, indexers cache.Indexers, tweakListOptions internalinterfaces.TweakListOptionsFunc) cache.SharedIndexInformer {
return cache.NewSharedIndexInformer(
&cache.ListWatch{
ListFunc: func(options v1.ListOptions) (runtime.Object, error) {
if tweakListOptions != nil {
tweakListOptions(&options)
}
return client.MonitoringV1alpha1().SearchlightPlugins(namespace).List(options)
return client.MonitoringV1alpha1().SearchlightPlugins().List(options)
},
WatchFunc: func(options v1.ListOptions) (watch.Interface, error) {
if tweakListOptions != nil {
tweakListOptions(&options)
}
return client.MonitoringV1alpha1().SearchlightPlugins(namespace).Watch(options)
return client.MonitoringV1alpha1().SearchlightPlugins().Watch(options)
},
},
&monitoring_v1alpha1.SearchlightPlugin{},
Expand All @@ -77,7 +76,7 @@ func NewFilteredSearchlightPluginInformer(client versioned.Interface, namespace
}

func (f *searchlightPluginInformer) defaultInformer(client versioned.Interface, resyncPeriod time.Duration) cache.SharedIndexInformer {
return NewFilteredSearchlightPluginInformer(client, f.namespace, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
return NewFilteredSearchlightPluginInformer(client, resyncPeriod, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, f.tweakListOptions)
}

func (f *searchlightPluginInformer) Informer() cache.SharedIndexInformer {
Expand Down
4 changes: 0 additions & 4 deletions client/listers/monitoring/v1alpha1/expansion_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,3 @@ type PodAlertNamespaceListerExpansion interface{}
// SearchlightPluginListerExpansion allows custom methods to be added to
// SearchlightPluginLister.
type SearchlightPluginListerExpansion interface{}

// SearchlightPluginNamespaceListerExpansion allows custom methods to be added to
// SearchlightPluginNamespaceLister.
type SearchlightPluginNamespaceListerExpansion interface{}
39 changes: 5 additions & 34 deletions client/listers/monitoring/v1alpha1/searchlightplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
type SearchlightPluginLister interface {
// List lists all SearchlightPlugins in the indexer.
List(selector labels.Selector) (ret []*v1alpha1.SearchlightPlugin, err error)
// SearchlightPlugins returns an object that can list and get SearchlightPlugins.
SearchlightPlugins(namespace string) SearchlightPluginNamespaceLister
// Get retrieves the SearchlightPlugin from the index for a given name.
Get(name string) (*v1alpha1.SearchlightPlugin, error)
SearchlightPluginListerExpansion
}

Expand All @@ -52,38 +52,9 @@ func (s *searchlightPluginLister) List(selector labels.Selector) (ret []*v1alpha
return ret, err
}

// SearchlightPlugins returns an object that can list and get SearchlightPlugins.
func (s *searchlightPluginLister) SearchlightPlugins(namespace string) SearchlightPluginNamespaceLister {
return searchlightPluginNamespaceLister{indexer: s.indexer, namespace: namespace}
}

// SearchlightPluginNamespaceLister helps list and get SearchlightPlugins.
type SearchlightPluginNamespaceLister interface {
// List lists all SearchlightPlugins in the indexer for a given namespace.
List(selector labels.Selector) (ret []*v1alpha1.SearchlightPlugin, err error)
// Get retrieves the SearchlightPlugin from the indexer for a given namespace and name.
Get(name string) (*v1alpha1.SearchlightPlugin, error)
SearchlightPluginNamespaceListerExpansion
}

// searchlightPluginNamespaceLister implements the SearchlightPluginNamespaceLister
// interface.
type searchlightPluginNamespaceLister struct {
indexer cache.Indexer
namespace string
}

// List lists all SearchlightPlugins in the indexer for a given namespace.
func (s searchlightPluginNamespaceLister) List(selector labels.Selector) (ret []*v1alpha1.SearchlightPlugin, err error) {
err = cache.ListAllByNamespace(s.indexer, s.namespace, selector, func(m interface{}) {
ret = append(ret, m.(*v1alpha1.SearchlightPlugin))
})
return ret, err
}

// Get retrieves the SearchlightPlugin from the indexer for a given namespace and name.
func (s searchlightPluginNamespaceLister) Get(name string) (*v1alpha1.SearchlightPlugin, error) {
obj, exists, err := s.indexer.GetByKey(s.namespace + "/" + name)
// Get retrieves the SearchlightPlugin from the index for a given name.
func (s *searchlightPluginLister) Get(name string) (*v1alpha1.SearchlightPlugin, error) {
obj, exists, err := s.indexer.GetByKey(name)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 6aaed16

Please sign in to comment.