2019-05-09 22:03:45 +00:00
|
|
|
package relatedresource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-12-12 01:27:03 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
|
2019-05-09 22:03:45 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/generic"
|
|
|
|
"github.com/rancher/wrangler/pkg/kv"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Key struct {
|
|
|
|
Namespace string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewKey(namespace, name string) Key {
|
|
|
|
return Key{
|
|
|
|
Namespace: namespace,
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ControllerWrapper interface {
|
|
|
|
Informer() cache.SharedIndexInformer
|
|
|
|
AddGenericHandler(ctx context.Context, name string, handler generic.Handler)
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
type ClusterScopedEnqueuer interface {
|
|
|
|
Enqueue(name string)
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:03:45 +00:00
|
|
|
type Enqueuer interface {
|
|
|
|
Enqueue(namespace, name string)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Resolver func(namespace, name string, obj runtime.Object) ([]Key, error)
|
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
func WatchClusterScoped(ctx context.Context, name string, resolve Resolver, enq ClusterScopedEnqueuer, watching ...ControllerWrapper) {
|
|
|
|
Watch(ctx, name, resolve, &wrapper{ClusterScopedEnqueuer: enq}, watching...)
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:03:45 +00:00
|
|
|
func Watch(ctx context.Context, name string, resolve Resolver, enq Enqueuer, watching ...ControllerWrapper) {
|
|
|
|
for _, c := range watching {
|
|
|
|
watch(ctx, name, enq, resolve, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func watch(ctx context.Context, name string, enq Enqueuer, resolve Resolver, controller ControllerWrapper) {
|
2019-12-12 01:27:03 +00:00
|
|
|
runResolve := func(ns, name string, obj runtime.Object) error {
|
|
|
|
keys, err := resolve(ns, name, obj)
|
2019-05-09 22:03:45 +00:00
|
|
|
if err != nil {
|
2019-12-12 01:27:03 +00:00
|
|
|
return err
|
2019-05-09 22:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
if key.Name != "" {
|
|
|
|
enq.Enqueue(key.Namespace, key.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 01:27:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
controller.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
|
|
|
DeleteFunc: func(obj interface{}) {
|
|
|
|
ro, ok := obj.(runtime.Object)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err := meta.Accessor(ro)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
runResolve(meta.GetNamespace(), meta.GetName(), ro)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
controller.AddGenericHandler(ctx, name, func(key string, obj runtime.Object) (runtime.Object, error) {
|
|
|
|
ns, name := kv.Split(key, "/")
|
|
|
|
return obj, runResolve(ns, name, obj)
|
2019-05-09 22:03:45 +00:00
|
|
|
})
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
|
|
|
|
type wrapper struct {
|
|
|
|
ClusterScopedEnqueuer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Enqueue(namespace, name string) {
|
|
|
|
w.ClusterScopedEnqueuer.Enqueue(name)
|
|
|
|
}
|