Skip to content

Commit

Permalink
Merge pull request #199 from ibuildthecloud/master
Browse files Browse the repository at this point in the history
Random changes
  • Loading branch information
ibuildthecloud authored Sep 24, 2018
2 parents 5f7e9df + f06b994 commit e5c60cd
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions generator/controller_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
71 changes: 71 additions & 0 deletions pkg/changeset/changeset.go
Original file line number Diff line number Diff line change
@@ -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
})
}
24 changes: 24 additions & 0 deletions pkg/kv/split.go
Original file line number Diff line number Diff line change
@@ -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]
}

0 comments on commit e5c60cd

Please sign in to comment.