mirror of
https://github.com/rancher/norman.git
synced 2025-09-17 15:49:53 +00:00
@@ -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,
|
||||
|
71
pkg/changeset/changeset.go
Normal file
71
pkg/changeset/changeset.go
Normal file
@@ -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
pkg/kv/split.go
Normal file
24
pkg/kv/split.go
Normal file
@@ -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]
|
||||
}
|
Reference in New Issue
Block a user