Update generated

This commit is contained in:
Erik Wilson 2019-12-15 23:20:08 -07:00
parent 76281bf731
commit 5679a8bd2f
2 changed files with 128 additions and 26 deletions

View File

@ -84,6 +84,10 @@ func NewFactory(clientset clientset.Interface, informerFactory informers.SharedI
}
}
func (c *Factory) Controllers() map[schema.GroupVersionKind]*generic.Controller {
return c.controllerManager.Controllers()
}
func (c *Factory) SetThreadiness(gvk schema.GroupVersionKind, threadiness int) {
c.threadiness[gvk] = threadiness
}

View File

@ -20,13 +20,17 @@ package v1
import (
"context"
"time"
v1 "github.com/rancher/k3s/pkg/apis/k3s.cattle.io/v1"
clientset "github.com/rancher/k3s/pkg/generated/clientset/versioned/typed/k3s.cattle.io/v1"
informers "github.com/rancher/k3s/pkg/generated/informers/externalversions/k3s.cattle.io/v1"
listers "github.com/rancher/k3s/pkg/generated/listers/k3s.cattle.io/v1"
"github.com/rancher/wrangler/pkg/apply"
"github.com/rancher/wrangler/pkg/condition"
"github.com/rancher/wrangler/pkg/generic"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
@ -40,20 +44,15 @@ import (
type AddonHandler func(string, *v1.Addon) (*v1.Addon, error)
type AddonController interface {
generic.ControllerMeta
AddonClient
OnChange(ctx context.Context, name string, sync AddonHandler)
OnRemove(ctx context.Context, name string, sync AddonHandler)
Enqueue(namespace, name string)
EnqueueAfter(namespace, name string, duration time.Duration)
Cache() AddonCache
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 AddonClient interface {
@ -118,26 +117,21 @@ func (c *addonController) Updater() generic.Updater {
}
}
func UpdateAddonOnChange(updater generic.Updater, handler AddonHandler) AddonHandler {
return func(key string, obj *v1.Addon) (*v1.Addon, 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.Addon)
}
}
return copyObj, err
func UpdateAddonDeepCopyOnChange(client AddonClient, obj *v1.Addon, handler func(obj *v1.Addon) (*v1.Addon, error)) (*v1.Addon, error) {
if obj == nil {
return obj, nil
}
copyObj := obj.DeepCopy()
newObj, err := handler(copyObj)
if newObj != nil {
copyObj = newObj
}
if obj.ResourceVersion == copyObj.ResourceVersion && !equality.Semantic.DeepEqual(obj, copyObj) {
return client.Update(copyObj)
}
return copyObj, err
}
func (c *addonController) AddGenericHandler(ctx context.Context, name string, handler generic.Handler) {
@ -162,6 +156,10 @@ func (c *addonController) Enqueue(namespace, name string) {
c.controllerManager.Enqueue(c.gvk, c.informer.Informer(), namespace, name)
}
func (c *addonController) EnqueueAfter(namespace, name string, duration time.Duration) {
c.controllerManager.EnqueueAfter(c.gvk, c.informer.Informer(), namespace, name, duration)
}
func (c *addonController) Informer() cache.SharedIndexInformer {
return c.informer.Informer()
}
@ -240,3 +238,103 @@ func (c *addonCache) GetByIndex(indexName, key string) (result []*v1.Addon, err
}
return result, nil
}
type AddonStatusHandler func(obj *v1.Addon, status v1.AddonStatus) (v1.AddonStatus, error)
type AddonGeneratingHandler func(obj *v1.Addon, status v1.AddonStatus) ([]runtime.Object, v1.AddonStatus, error)
func RegisterAddonStatusHandler(ctx context.Context, controller AddonController, condition condition.Cond, name string, handler AddonStatusHandler) {
statusHandler := &addonStatusHandler{
client: controller,
condition: condition,
handler: handler,
}
controller.AddGenericHandler(ctx, name, FromAddonHandlerToHandler(statusHandler.sync))
}
func RegisterAddonGeneratingHandler(ctx context.Context, controller AddonController, apply apply.Apply,
condition condition.Cond, name string, handler AddonGeneratingHandler, opts *generic.GeneratingHandlerOptions) {
statusHandler := &addonGeneratingHandler{
AddonGeneratingHandler: handler,
apply: apply,
name: name,
gvk: controller.GroupVersionKind(),
}
if opts != nil {
statusHandler.opts = *opts
}
RegisterAddonStatusHandler(ctx, controller, condition, name, statusHandler.Handle)
}
type addonStatusHandler struct {
client AddonClient
condition condition.Cond
handler AddonStatusHandler
}
func (a *addonStatusHandler) sync(key string, obj *v1.Addon) (*v1.Addon, error) {
if obj == nil {
return obj, nil
}
origStatus := obj.Status
obj = obj.DeepCopy()
newStatus, err := a.handler(obj, obj.Status)
if err != nil {
// Revert to old status on error
newStatus = *origStatus.DeepCopy()
}
obj.Status = newStatus
if a.condition != "" {
if errors.IsConflict(err) {
a.condition.SetError(obj, "", nil)
} else {
a.condition.SetError(obj, "", err)
}
}
if !equality.Semantic.DeepEqual(origStatus, obj.Status) {
var newErr error
obj, newErr = a.client.UpdateStatus(obj)
if err == nil {
err = newErr
}
}
return obj, err
}
type addonGeneratingHandler struct {
AddonGeneratingHandler
apply apply.Apply
opts generic.GeneratingHandlerOptions
gvk schema.GroupVersionKind
name string
}
func (a *addonGeneratingHandler) Handle(obj *v1.Addon, status v1.AddonStatus) (v1.AddonStatus, error) {
objs, newStatus, err := a.AddonGeneratingHandler(obj, status)
if err != nil {
return newStatus, err
}
apply := a.apply
if !a.opts.DynamicLookup {
apply = apply.WithStrictCaching()
}
if !a.opts.AllowCrossNamespace && !a.opts.AllowClusterScoped {
apply = apply.WithSetOwnerReference(true, false).
WithDefaultNamespace(obj.GetNamespace()).
WithListerNamespace(obj.GetNamespace())
}
if !a.opts.AllowClusterScoped {
apply = apply.WithRestrictClusterScoped()
}
return newStatus, apply.
WithOwner(obj).
WithSetID(a.name).
ApplyObjects(objs...)
}