From a5698a8e866f2c20e968a2dd2be9b693305e6b7e Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Mon, 24 Sep 2018 08:58:30 -0700 Subject: [PATCH 1/3] Add method to client to get GenericController --- generator/controller_template.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/generator/controller_template.go b/generator/controller_template.go index 2f0d6a622..eed516056 100644 --- a/generator/controller_template.go +++ b/generator/controller_template.go @@ -49,6 +49,7 @@ type {{.schema.CodeName}}Lister interface { } type {{.schema.CodeName}}Controller interface { + Generic() controller.GenericController Informer() cache.SharedIndexInformer Lister() {{.schema.CodeName}}Lister AddHandler(name string, handler {{.schema.CodeName}}HandlerFunc) @@ -111,6 +112,10 @@ type {{.schema.ID}}Controller struct { controller.GenericController } +func (c *{{.schema.ID}}Controller) Generic() controller.GenericController { + return c.GenericController +} + func (c *{{.schema.ID}}Controller) Lister() {{.schema.CodeName}}Lister { return &{{.schema.ID}}Lister{ controller: c, From e93f1d7870c00ad13bd23d276b4b47540c463a98 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Mon, 24 Sep 2018 08:59:16 -0700 Subject: [PATCH 2/3] Add changeset package changeset is an easy way to track parent/child relationships and trigger parent updates if child changes --- pkg/changeset/changeset.go | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 pkg/changeset/changeset.go diff --git a/pkg/changeset/changeset.go b/pkg/changeset/changeset.go new file mode 100644 index 000000000..a8789a0a6 --- /dev/null +++ b/pkg/changeset/changeset.go @@ -0,0 +1,71 @@ +package changeset + +import ( + "strings" + + "github.com/rancher/norman/controller" + "k8s.io/apimachinery/pkg/runtime" +) + +type Key struct { + Namespace string + Name string +} + +type ControllerProvider interface { + Generic() controller.GenericController +} + +type Enqueuer func(namespace, name string) + +type Resolver func(namespace, name string, obj runtime.Object) ([]Key, error) + +func Watch(name string, resolve Resolver, enq Enqueuer, controllers ...ControllerProvider) { + for _, c := range controllers { + watch(name, enq, resolve, c.Generic()) + } +} + +func watch(name string, enq Enqueuer, resolve Resolver, genericController controller.GenericController) { + genericController.AddHandler(name, func(key string) error { + obj, exists, err := genericController.Informer().GetStore().GetByKey(key) + if err != nil { + return err + } + + if !exists { + obj = nil + } + + var ( + ns string + name string + ) + + parts := strings.SplitN(key, "/", 2) + if len(parts) == 2 { + ns = parts[0] + name = parts[1] + } else { + name = parts[0] + } + + ro, ok := obj.(runtime.Object) + if !ok { + return nil + } + + keys, err := resolve(ns, name, ro) + if err != nil { + return err + } + + for _, key := range keys { + if key.Name != "" { + enq(key.Namespace, key.Name) + } + } + + return nil + }) +} From f06b9944e7446626a215a3aa357de3d1e35499db Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Mon, 24 Sep 2018 08:59:45 -0700 Subject: [PATCH 3/3] Some util methods for kv style string --- pkg/kv/split.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 pkg/kv/split.go diff --git a/pkg/kv/split.go b/pkg/kv/split.go new file mode 100644 index 000000000..19e791444 --- /dev/null +++ b/pkg/kv/split.go @@ -0,0 +1,24 @@ +package kv + +import "strings" + +func Split(s, sep string) (string, string) { + parts := strings.SplitN(s, sep, 2) + return strings.TrimSpace(parts[0]), strings.TrimSpace(safeIndex(parts, 1)) +} + +func SplitMap(s, sep string) map[string]string { + result := map[string]string{} + for _, part := range strings.Split(s, sep) { + k, v := Split(part, "=") + result[k] = v + } + return result +} + +func safeIndex(parts []string, idx int) string { + if len(parts) <= idx { + return "" + } + return parts[idx] +}