Rename LabelSet labels.Set

This commit is contained in:
Daniel Smith 2014-06-16 19:22:46 -07:00
parent 5c3e4fab58
commit c534d070e5
9 changed files with 40 additions and 40 deletions

View File

@ -26,11 +26,11 @@ type Labels interface {
} }
// A map of label:value. Implements Labels. // A map of label:value. Implements Labels.
type LabelSet map[string]string type Set map[string]string
// All labels listed as a human readable string. Conveiently, exactly the format // All labels listed as a human readable string. Conveiently, exactly the format
// that ParseQuery takes. // that ParseQuery takes.
func (ls LabelSet) String() string { func (ls Set) String() string {
query := make([]string, 0, len(ls)) query := make([]string, 0, len(ls))
for key, value := range ls { for key, value := range ls {
query = append(query, key+"="+value) query = append(query, key+"="+value)
@ -39,6 +39,6 @@ func (ls LabelSet) String() string {
} }
// Implement Labels interface. // Implement Labels interface.
func (ls LabelSet) Get(label string) string { func (ls Set) Get(label string) string {
return ls[label] return ls[label]
} }

View File

@ -20,24 +20,24 @@ import (
"testing" "testing"
) )
func matches(t *testing.T, ls LabelSet, want string) { func matches(t *testing.T, ls Set, want string) {
if ls.String() != want { if ls.String() != want {
t.Errorf("Expected '%s', but got '%s'", want, ls.String()) t.Errorf("Expected '%s', but got '%s'", want, ls.String())
} }
} }
func TestLabelSetString(t *testing.T) { func TestSetString(t *testing.T) {
matches(t, LabelSet{"x": "y"}, "x=y") matches(t, Set{"x": "y"}, "x=y")
matches(t, LabelSet{"foo": "bar"}, "foo=bar") matches(t, Set{"foo": "bar"}, "foo=bar")
matches(t, LabelSet{"foo": "bar", "baz": "qup"}, "foo=bar,baz=qup") matches(t, Set{"foo": "bar", "baz": "qup"}, "foo=bar,baz=qup")
// TODO: Make our label representation robust enough to handel labels // TODO: Make our label representation robust enough to handel labels
// with ",=!" characters in their names. // with ",=!" characters in their names.
} }
func TestLabelGet(t *testing.T) { func TestLabelGet(t *testing.T) {
ls := LabelSet{"x": "y"} ls := Set{"x": "y"}
if ls.Get("x") != "y" { if ls.Get("x") != "y" {
t.Errorf("LabelSet.Get is broken") t.Errorf("Set.Get is broken")
} }
} }

View File

@ -42,7 +42,7 @@ type queryTerm struct {
// Exactly one of the below three items should be used. // Exactly one of the below three items should be used.
// If non-nil, we match LabelSet l iff l[*label] == *value. // If non-nil, we match Set l iff l[*label] == *value.
label, value *string label, value *string
// A list of terms which must all match for this query term to return true. // A list of terms which must all match for this query term to return true.
@ -89,8 +89,8 @@ func try(queryPiece, op string) (lhs, rhs string, ok bool) {
return "", "", false return "", "", false
} }
// Given a LabelSet, return a Query which will match exactly that LabelSet. // Given a Set, return a Query which will match exactly that Set.
func QueryFromSet(ls LabelSet) Query { func QueryFromSet(ls Set) Query {
var query queryTerm var query queryTerm
for l, v := range ls { for l, v := range ls {
// Make a copy, because we're taking the address below // Make a copy, because we're taking the address below

View File

@ -47,7 +47,7 @@ func TestQueryParse(t *testing.T) {
} }
} }
func expectMatch(t *testing.T, query string, ls LabelSet) { func expectMatch(t *testing.T, query string, ls Set) {
lq, err := ParseQuery(query) lq, err := ParseQuery(query)
if err != nil { if err != nil {
t.Errorf("Unable to parse %v as a query\n", query) t.Errorf("Unable to parse %v as a query\n", query)
@ -58,7 +58,7 @@ func expectMatch(t *testing.T, query string, ls LabelSet) {
} }
} }
func expectNoMatch(t *testing.T, query string, ls LabelSet) { func expectNoMatch(t *testing.T, query string, ls Set) {
lq, err := ParseQuery(query) lq, err := ParseQuery(query)
if err != nil { if err != nil {
t.Errorf("Unable to parse %v as a query\n", query) t.Errorf("Unable to parse %v as a query\n", query)
@ -70,21 +70,21 @@ func expectNoMatch(t *testing.T, query string, ls LabelSet) {
} }
func TestEverything(t *testing.T) { func TestEverything(t *testing.T) {
if !Everything().Matches(LabelSet{"x": "y"}) { if !Everything().Matches(Set{"x": "y"}) {
t.Errorf("Nil query didn't match") t.Errorf("Nil query didn't match")
} }
} }
func TestLabelQueryMatches(t *testing.T) { func TestLabelQueryMatches(t *testing.T) {
expectMatch(t, "", LabelSet{"x": "y"}) expectMatch(t, "", Set{"x": "y"})
expectMatch(t, "x=y", LabelSet{"x": "y"}) expectMatch(t, "x=y", Set{"x": "y"})
expectMatch(t, "x=y,z=w", LabelSet{"x": "y", "z": "w"}) expectMatch(t, "x=y,z=w", Set{"x": "y", "z": "w"})
expectMatch(t, "x!=y,z!=w", LabelSet{"x": "z", "z": "a"}) expectMatch(t, "x!=y,z!=w", Set{"x": "z", "z": "a"})
expectNoMatch(t, "x=y", LabelSet{"x": "z"}) expectNoMatch(t, "x=y", Set{"x": "z"})
expectNoMatch(t, "x=y,z=w", LabelSet{"x": "w", "z": "w"}) expectNoMatch(t, "x=y,z=w", Set{"x": "w", "z": "w"})
expectNoMatch(t, "x!=y,z!=w", LabelSet{"x": "z", "z": "w"}) expectNoMatch(t, "x!=y,z!=w", Set{"x": "z", "z": "w"})
labelset := LabelSet{ labelset := Set{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
} }
@ -96,28 +96,28 @@ func TestLabelQueryMatches(t *testing.T) {
expectNoMatch(t, "foo=bar,foobar=bar,baz=blah", labelset) expectNoMatch(t, "foo=bar,foobar=bar,baz=blah", labelset)
} }
func expectMatchDirect(t *testing.T, query, ls LabelSet) { func expectMatchDirect(t *testing.T, query, ls Set) {
if !QueryFromSet(query).Matches(ls) { if !QueryFromSet(query).Matches(ls) {
t.Errorf("Wanted %s to match '%s', but it did not.\n", query, ls) t.Errorf("Wanted %s to match '%s', but it did not.\n", query, ls)
} }
} }
func expectNoMatchDirect(t *testing.T, query, ls LabelSet) { func expectNoMatchDirect(t *testing.T, query, ls Set) {
if QueryFromSet(query).Matches(ls) { if QueryFromSet(query).Matches(ls) {
t.Errorf("Wanted '%s' to not match '%s', but it did.", query, ls) t.Errorf("Wanted '%s' to not match '%s', but it did.", query, ls)
} }
} }
func TestLabelSetMatches(t *testing.T) { func TestSetMatches(t *testing.T) {
labelset := LabelSet{ labelset := Set{
"foo": "bar", "foo": "bar",
"baz": "blah", "baz": "blah",
} }
expectMatchDirect(t, LabelSet{}, labelset) expectMatchDirect(t, Set{}, labelset)
expectMatchDirect(t, LabelSet{"foo": "bar"}, labelset) expectMatchDirect(t, Set{"foo": "bar"}, labelset)
expectMatchDirect(t, LabelSet{"baz": "blah"}, labelset) expectMatchDirect(t, Set{"baz": "blah"}, labelset)
expectMatchDirect(t, LabelSet{"foo": "bar", "baz": "blah"}, labelset) expectMatchDirect(t, Set{"foo": "bar", "baz": "blah"}, labelset)
expectNoMatchDirect(t, LabelSet{"foo": "=blah"}, labelset) expectNoMatchDirect(t, Set{"foo": "=blah"}, labelset)
expectNoMatchDirect(t, LabelSet{"baz": "=bar"}, labelset) expectNoMatchDirect(t, Set{"baz": "=bar"}, labelset)
expectNoMatchDirect(t, LabelSet{"foo": "=bar", "foobar": "bar", "baz": "blah"}, labelset) expectNoMatchDirect(t, Set{"foo": "=bar", "foobar": "bar", "baz": "blah"}, labelset)
} }

View File

@ -40,7 +40,7 @@ func (storage *ControllerRegistryStorage) List(query labels.Query) (interface{},
controllers, err := storage.registry.ListControllers() controllers, err := storage.registry.ListControllers()
if err == nil { if err == nil {
for _, controller := range controllers { for _, controller := range controllers {
if query.Matches(labels.LabelSet(controller.Labels)) { if query.Matches(labels.Set(controller.Labels)) {
result.Items = append(result.Items, controller) result.Items = append(result.Items, controller)
} }
} }

View File

@ -42,7 +42,7 @@ func (e *EndpointController) SyncServiceEndpoints() error {
} }
var resultErr error var resultErr error
for _, service := range services.Items { for _, service := range services.Items {
pods, err := e.podRegistry.ListPods(labels.QueryFromSet(labels.LabelSet(service.Labels))) pods, err := e.podRegistry.ListPods(labels.QueryFromSet(labels.Set(service.Labels)))
if err != nil { if err != nil {
log.Printf("Error syncing service: %#v, skipping.", service) log.Printf("Error syncing service: %#v, skipping.", service)
resultErr = err resultErr = err

View File

@ -75,7 +75,7 @@ func (registry *EtcdRegistry) ListPods(query labels.Query) ([]api.Pod, error) {
return pods, err return pods, err
} }
for _, pod := range machinePods { for _, pod := range machinePods {
if query.Matches(labels.LabelSet(pod.Labels)) { if query.Matches(labels.Set(pod.Labels)) {
pods = append(pods, pod) pods = append(pods, pod)
} }
} }

View File

@ -39,7 +39,7 @@ func MakeMemoryRegistry() *MemoryRegistry {
func (registry *MemoryRegistry) ListPods(query labels.Query) ([]api.Pod, error) { func (registry *MemoryRegistry) ListPods(query labels.Query) ([]api.Pod, error) {
result := []api.Pod{} result := []api.Pod{}
for _, value := range registry.podData { for _, value := range registry.podData {
if query.Matches(labels.LabelSet(value.Labels)) { if query.Matches(labels.Set(value.Labels)) {
result = append(result, value) result = append(result, value)
} }
} }

View File

@ -58,7 +58,7 @@ func (sr *ServiceRegistryStorage) List(query labels.Query) (interface{}, error)
list.Kind = "cluster#serviceList" list.Kind = "cluster#serviceList"
var filtered []api.Service var filtered []api.Service
for _, service := range list.Items { for _, service := range list.Items {
if query.Matches(labels.LabelSet(service.Labels)) { if query.Matches(labels.Set(service.Labels)) {
filtered = append(filtered, service) filtered = append(filtered, service)
} }
} }