-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from ibuildthecloud/master
Random changes
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |