/*
Copyright The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Code generated by main. DO NOT EDIT.

package v1

import (
	"context"

	"github.com/rancher/wrangler/pkg/generic"
	v1 "k8s.io/api/rbac/v1"
	"k8s.io/apimachinery/pkg/api/equality"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/apimachinery/pkg/types"
	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apimachinery/pkg/watch"
	informers "k8s.io/client-go/informers/rbac/v1"
	clientset "k8s.io/client-go/kubernetes/typed/rbac/v1"
	listers "k8s.io/client-go/listers/rbac/v1"
	"k8s.io/client-go/tools/cache"
)

type ClusterRoleHandler func(string, *v1.ClusterRole) (*v1.ClusterRole, error)

type ClusterRoleController interface {
	ClusterRoleClient

	OnChange(ctx context.Context, name string, sync ClusterRoleHandler)
	OnRemove(ctx context.Context, name string, sync ClusterRoleHandler)
	Enqueue(name string)

	Cache() ClusterRoleCache

	Informer() cache.SharedIndexInformer
	GroupVersionKind() schema.GroupVersionKind

	AddGenericHandler(ctx context.Context, name string, handler generic.Handler)
	AddGenericRemoveHandler(ctx context.Context, name string, handler generic.Handler)
	Updater() generic.Updater
}

type ClusterRoleClient interface {
	Create(*v1.ClusterRole) (*v1.ClusterRole, error)
	Update(*v1.ClusterRole) (*v1.ClusterRole, error)

	Delete(name string, options *metav1.DeleteOptions) error
	Get(name string, options metav1.GetOptions) (*v1.ClusterRole, error)
	List(opts metav1.ListOptions) (*v1.ClusterRoleList, error)
	Watch(opts metav1.ListOptions) (watch.Interface, error)
	Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1.ClusterRole, err error)
}

type ClusterRoleCache interface {
	Get(name string) (*v1.ClusterRole, error)
	List(selector labels.Selector) ([]*v1.ClusterRole, error)

	AddIndexer(indexName string, indexer ClusterRoleIndexer)
	GetByIndex(indexName, key string) ([]*v1.ClusterRole, error)
}

type ClusterRoleIndexer func(obj *v1.ClusterRole) ([]string, error)

type clusterRoleController struct {
	controllerManager *generic.ControllerManager
	clientGetter      clientset.ClusterRolesGetter
	informer          informers.ClusterRoleInformer
	gvk               schema.GroupVersionKind
}

func NewClusterRoleController(gvk schema.GroupVersionKind, controllerManager *generic.ControllerManager, clientGetter clientset.ClusterRolesGetter, informer informers.ClusterRoleInformer) ClusterRoleController {
	return &clusterRoleController{
		controllerManager: controllerManager,
		clientGetter:      clientGetter,
		informer:          informer,
		gvk:               gvk,
	}
}

func FromClusterRoleHandlerToHandler(sync ClusterRoleHandler) generic.Handler {
	return func(key string, obj runtime.Object) (ret runtime.Object, err error) {
		var v *v1.ClusterRole
		if obj == nil {
			v, err = sync(key, nil)
		} else {
			v, err = sync(key, obj.(*v1.ClusterRole))
		}
		if v == nil {
			return nil, err
		}
		return v, err
	}
}

func (c *clusterRoleController) Updater() generic.Updater {
	return func(obj runtime.Object) (runtime.Object, error) {
		newObj, err := c.Update(obj.(*v1.ClusterRole))
		if newObj == nil {
			return nil, err
		}
		return newObj, err
	}
}

func UpdateClusterRoleOnChange(updater generic.Updater, handler ClusterRoleHandler) ClusterRoleHandler {
	return func(key string, obj *v1.ClusterRole) (*v1.ClusterRole, error) {
		if obj == nil {
			return handler(key, nil)
		}

		copyObj := obj.DeepCopy()
		newObj, err := handler(key, copyObj)
		if newObj != nil {
			copyObj = newObj
		}
		if obj.ResourceVersion == copyObj.ResourceVersion && !equality.Semantic.DeepEqual(obj, copyObj) {
			newObj, err := updater(copyObj)
			if newObj != nil && err == nil {
				copyObj = newObj.(*v1.ClusterRole)
			}
		}

		return copyObj, err
	}
}

func (c *clusterRoleController) AddGenericHandler(ctx context.Context, name string, handler generic.Handler) {
	c.controllerManager.AddHandler(ctx, c.gvk, c.informer.Informer(), name, handler)
}

func (c *clusterRoleController) AddGenericRemoveHandler(ctx context.Context, name string, handler generic.Handler) {
	removeHandler := generic.NewRemoveHandler(name, c.Updater(), handler)
	c.controllerManager.AddHandler(ctx, c.gvk, c.informer.Informer(), name, removeHandler)
}

func (c *clusterRoleController) OnChange(ctx context.Context, name string, sync ClusterRoleHandler) {
	c.AddGenericHandler(ctx, name, FromClusterRoleHandlerToHandler(sync))
}

func (c *clusterRoleController) OnRemove(ctx context.Context, name string, sync ClusterRoleHandler) {
	removeHandler := generic.NewRemoveHandler(name, c.Updater(), FromClusterRoleHandlerToHandler(sync))
	c.AddGenericHandler(ctx, name, removeHandler)
}

func (c *clusterRoleController) Enqueue(name string) {
	c.controllerManager.Enqueue(c.gvk, c.informer.Informer(), "", name)
}

func (c *clusterRoleController) Informer() cache.SharedIndexInformer {
	return c.informer.Informer()
}

func (c *clusterRoleController) GroupVersionKind() schema.GroupVersionKind {
	return c.gvk
}

func (c *clusterRoleController) Cache() ClusterRoleCache {
	return &clusterRoleCache{
		lister:  c.informer.Lister(),
		indexer: c.informer.Informer().GetIndexer(),
	}
}

func (c *clusterRoleController) Create(obj *v1.ClusterRole) (*v1.ClusterRole, error) {
	return c.clientGetter.ClusterRoles().Create(obj)
}

func (c *clusterRoleController) Update(obj *v1.ClusterRole) (*v1.ClusterRole, error) {
	return c.clientGetter.ClusterRoles().Update(obj)
}

func (c *clusterRoleController) Delete(name string, options *metav1.DeleteOptions) error {
	return c.clientGetter.ClusterRoles().Delete(name, options)
}

func (c *clusterRoleController) Get(name string, options metav1.GetOptions) (*v1.ClusterRole, error) {
	return c.clientGetter.ClusterRoles().Get(name, options)
}

func (c *clusterRoleController) List(opts metav1.ListOptions) (*v1.ClusterRoleList, error) {
	return c.clientGetter.ClusterRoles().List(opts)
}

func (c *clusterRoleController) Watch(opts metav1.ListOptions) (watch.Interface, error) {
	return c.clientGetter.ClusterRoles().Watch(opts)
}

func (c *clusterRoleController) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1.ClusterRole, err error) {
	return c.clientGetter.ClusterRoles().Patch(name, pt, data, subresources...)
}

type clusterRoleCache struct {
	lister  listers.ClusterRoleLister
	indexer cache.Indexer
}

func (c *clusterRoleCache) Get(name string) (*v1.ClusterRole, error) {
	return c.lister.Get(name)
}

func (c *clusterRoleCache) List(selector labels.Selector) ([]*v1.ClusterRole, error) {
	return c.lister.List(selector)
}

func (c *clusterRoleCache) AddIndexer(indexName string, indexer ClusterRoleIndexer) {
	utilruntime.Must(c.indexer.AddIndexers(map[string]cache.IndexFunc{
		indexName: func(obj interface{}) (strings []string, e error) {
			return indexer(obj.(*v1.ClusterRole))
		},
	}))
}

func (c *clusterRoleCache) GetByIndex(indexName, key string) (result []*v1.ClusterRole, err error) {
	objs, err := c.indexer.ByIndex(indexName, key)
	if err != nil {
		return nil, err
	}
	for _, obj := range objs {
		result = append(result, obj.(*v1.ClusterRole))
	}
	return result, nil
}
