Skip to content

Commit

Permalink
Implement new label system
Browse files Browse the repository at this point in the history
  • Loading branch information
lavalamp committed Jun 17, 2014
1 parent ad2ec27 commit 7d05ba4
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 209 deletions.
80 changes: 0 additions & 80 deletions pkg/labels/labelquery_test.go

This file was deleted.

123 changes: 123 additions & 0 deletions pkg/labels/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package labels

import (
"testing"
)

func TestQueryParse(t *testing.T) {
testGoodStrings := []string{
"x=a,y=b,z=c",
"",
"x!=a,y=b",
}
testBadStrings := []string{
"x=a||y=b",
"x==a==b",
}
for _, test := range testGoodStrings {
lq, err := ParseQuery(test)
if err != nil {
t.Errorf("%v: error %v (%#v)\n", test, err, err)
}
if test != lq.String() {
t.Errorf("%v restring gave: %v\n", test, lq.String())
}
}
for _, test := range testBadStrings {
_, err := ParseQuery(test)
if err == nil {
t.Errorf("%v: did not get expected error\n", test)
}
}
}

func expectMatch(t *testing.T, query string, ls LabelSet) {
lq, err := ParseQuery(query)
if err != nil {
t.Errorf("Unable to parse %v as a query\n", query)
return
}
if !lq.Matches(ls) {
t.Errorf("Wanted %s to match '%s', but it did not.\n", query, ls)
}
}

func expectNoMatch(t *testing.T, query string, ls LabelSet) {
lq, err := ParseQuery(query)
if err != nil {
t.Errorf("Unable to parse %v as a query\n", query)
return
}
if lq.Matches(ls) {
t.Errorf("Wanted '%s' to not match '%s', but it did.", query, ls)
}
}

func TestEverything(t *testing.T) {
if !Everything().Matches(LabelSet{"x": "y"}) {
t.Errorf("Nil query didn't match")
}
}

func TestLabelQueryMatches(t *testing.T) {
expectMatch(t, "", LabelSet{"x": "y"})
expectMatch(t, "x=y", LabelSet{"x": "y"})
expectMatch(t, "x=y,z=w", LabelSet{"x": "y", "z": "w"})
expectMatch(t, "x!=y,z!=w", LabelSet{"x": "z", "z": "a"})
expectNoMatch(t, "x=y", LabelSet{"x": "z"})
expectNoMatch(t, "x=y,z=w", LabelSet{"x": "w", "z": "w"})
expectNoMatch(t, "x!=y,z!=w", LabelSet{"x": "z", "z": "w"})

labelset := LabelSet{
"foo": "bar",
"baz": "blah",
}
expectMatch(t, "foo=bar", labelset)
expectMatch(t, "baz=blah", labelset)
expectMatch(t, "foo=bar,baz=blah", labelset)
expectNoMatch(t, "foo=blah", labelset)
expectNoMatch(t, "baz=bar", labelset)
expectNoMatch(t, "foo=bar,foobar=bar,baz=blah", labelset)
}

func expectMatchDirect(t *testing.T, query, ls LabelSet) {
if !QueryFromSet(query).Matches(ls) {
t.Errorf("Wanted %s to match '%s', but it did not.\n", query, ls)
}
}

func expectNoMatchDirect(t *testing.T, query, ls LabelSet) {
if QueryFromSet(query).Matches(ls) {
t.Errorf("Wanted '%s' to not match '%s', but it did.", query, ls)
}
}

func TestLabelSetMatches(t *testing.T) {
labelset := LabelSet{
"foo": "bar",
"baz": "blah",
}
expectMatchDirect(t, LabelSet{}, labelset)
expectMatchDirect(t, LabelSet{"foo": "bar"}, labelset)
expectMatchDirect(t, LabelSet{"baz": "blah"}, labelset)
expectMatchDirect(t, LabelSet{"foo": "bar", "baz": "blah"}, labelset)
expectNoMatchDirect(t, LabelSet{"foo": "=blah"}, labelset)
expectNoMatchDirect(t, LabelSet{"baz": "=bar"}, labelset)
expectNoMatchDirect(t, LabelSet{"foo": "=bar", "foobar": "bar", "baz": "blah"}, labelset)
}
5 changes: 3 additions & 2 deletions pkg/registry/controller_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)

type MockControllerRegistry struct {
Expand Down Expand Up @@ -71,7 +72,7 @@ func TestListEmptyControllerList(t *testing.T) {
storage := ControllerRegistryStorage{
registry: &mockRegistry,
}
controllers, err := storage.List(nil)
controllers, err := storage.List(labels.Everything())
expectNoError(t, err)
if len(controllers.(api.ReplicationControllerList).Items) != 0 {
t.Errorf("Unexpected non-zero ctrl list: %#v", controllers)
Expand All @@ -96,7 +97,7 @@ func TestListControllerList(t *testing.T) {
storage := ControllerRegistryStorage{
registry: &mockRegistry,
}
controllersObj, err := storage.List(nil)
controllersObj, err := storage.List(labels.Everything())
controllers := controllersObj.(api.ReplicationControllerList)
expectNoError(t, err)
if len(controllers.Items) != 2 {
Expand Down
3 changes: 2 additions & 1 deletion pkg/registry/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)

func MakeEndpointController(serviceRegistry ServiceRegistry, podRegistry PodRegistry) *EndpointController {
Expand All @@ -41,7 +42,7 @@ func (e *EndpointController) SyncServiceEndpoints() error {
}
var resultErr error
for _, service := range services.Items {
pods, err := e.podRegistry.ListPods(&service.Labels)
pods, err := e.podRegistry.ListPods(labels.QueryFromSet(labels.LabelSet(service.Labels)))
if err != nil {
log.Printf("Error syncing service: %#v, skipping.", service)
resultErr = err
Expand Down
5 changes: 3 additions & 2 deletions pkg/registry/etcd_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/coreos/go-etcd/etcd"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)

// TODO: Need to add a reconciler loop that makes sure that things in pods are reflected into
Expand Down Expand Up @@ -66,15 +67,15 @@ func makePodKey(machine, podID string) string {
return "/registry/hosts/" + machine + "/pods/" + podID
}

func (registry *EtcdRegistry) ListPods(query *map[string]string) ([]api.Pod, error) {
func (registry *EtcdRegistry) ListPods(query labels.Query) ([]api.Pod, error) {
pods := []api.Pod{}
for _, machine := range registry.machines {
machinePods, err := registry.listPodsForMachine(machine)
if err != nil {
return pods, err
}
for _, pod := range machinePods {
if LabelsMatch(pod, query) {
if query.Matches(labels.LabelSet(pod.Labels)) {
pods = append(pods, pod)
}
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/registry/etcd_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
"github.com/coreos/go-etcd/etcd"
)
Expand Down Expand Up @@ -307,7 +308,7 @@ func TestEtcdEmptyListPods(t *testing.T) {
E: nil,
}
registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"})
pods, err := registry.ListPods(nil)
pods, err := registry.ListPods(labels.Everything())
expectNoError(t, err)
if len(pods) != 0 {
t.Errorf("Unexpected pod list: %#v", pods)
Expand All @@ -322,7 +323,7 @@ func TestEtcdListPodsNotFound(t *testing.T) {
E: &etcd.EtcdError{ErrorCode: 100},
}
registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"})
pods, err := registry.ListPods(nil)
pods, err := registry.ListPods(labels.Everything())
expectNoError(t, err)
if len(pods) != 0 {
t.Errorf("Unexpected pod list: %#v", pods)
Expand All @@ -348,7 +349,7 @@ func TestEtcdListPods(t *testing.T) {
E: nil,
}
registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"})
pods, err := registry.ListPods(nil)
pods, err := registry.ListPods(labels.Everything())
expectNoError(t, err)
if len(pods) != 2 || pods[0].ID != "foo" || pods[1].ID != "bar" {
t.Errorf("Unexpected pod list: %#v", pods)
Expand Down
18 changes: 14 additions & 4 deletions pkg/registry/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package registry

import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)

// PodRegistry is an interface implemented by things that know how to store Pod objects
// PodRegistry is an interface implemented by things that know how to store Pod objects.
type PodRegistry interface {
// ListPods obtains a list of pods that match query.
// Query may be nil in which case all pods are returned.
ListPods(query *map[string]string) ([]api.Pod, error)
ListPods(query labels.Query) ([]api.Pod, error)
// Get a specific pod
GetPod(podID string) (*api.Pod, error)
// Create a pod based on a specification, schedule it onto a specific machine.
Expand All @@ -34,11 +34,21 @@ type PodRegistry interface {
DeletePod(podID string) error
}

// ControllerRegistry is an interface for things that know how to store Controllers
// ControllerRegistry is an interface for things that know how to store Controllers.
type ControllerRegistry interface {
ListControllers() ([]api.ReplicationController, error)
GetController(controllerId string) (*api.ReplicationController, error)
CreateController(controller api.ReplicationController) error
UpdateController(controller api.ReplicationController) error
DeleteController(controllerId string) error
}

// ServiceRegistry is an interface for things that know how to store services.
type ServiceRegistry interface {
ListServices() (api.ServiceList, error)
CreateService(svc api.Service) error
GetService(name string) (*api.Service, error)
DeleteService(name string) error
UpdateService(svc api.Service) error
UpdateEndpoints(e api.Endpoints) error
}
5 changes: 3 additions & 2 deletions pkg/registry/memory_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package registry

import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)

// An implementation of PodRegistry and ControllerRegistry that is backed by memory
Expand All @@ -35,10 +36,10 @@ func MakeMemoryRegistry() *MemoryRegistry {
}
}

func (registry *MemoryRegistry) ListPods(labelQuery *map[string]string) ([]api.Pod, error) {
func (registry *MemoryRegistry) ListPods(query labels.Query) ([]api.Pod, error) {
result := []api.Pod{}
for _, value := range registry.podData {
if LabelsMatch(value, labelQuery) {
if query.Matches(labels.LabelSet(value.Labels)) {
result = append(result, value)
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/registry/memory_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import (
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)

func TestListPodsEmpty(t *testing.T) {
registry := MakeMemoryRegistry()
pods, err := registry.ListPods(nil)
pods, err := registry.ListPods(labels.Everything())
expectNoError(t, err)
if len(pods) != 0 {
t.Errorf("Unexpected pod list: %#v", pods)
Expand All @@ -33,7 +34,7 @@ func TestListPodsEmpty(t *testing.T) {
func TestMemoryListPods(t *testing.T) {
registry := MakeMemoryRegistry()
registry.CreatePod("machine", api.Pod{JSONBase: api.JSONBase{ID: "foo"}})
pods, err := registry.ListPods(nil)
pods, err := registry.ListPods(labels.Everything())
expectNoError(t, err)
if len(pods) != 1 || pods[0].ID != "foo" {
t.Errorf("Unexpected pod list: %#v", pods)
Expand Down
Loading

0 comments on commit 7d05ba4

Please sign in to comment.