From 4ba47628273a2e1820923ecde1fa58b68d56f89a Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Tue, 9 Sep 2014 15:23:34 -0700 Subject: [PATCH 1/2] Insert the current state of a replica controller. --- pkg/api/types.go | 1 + pkg/api/v1beta1/types.go | 3 ++- pkg/registry/controller/rest.go | 10 ++++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/api/types.go b/pkg/api/types.go index 88827128df4..149c30cfbc2 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -324,6 +324,7 @@ func (*ReplicationControllerList) IsAnAPIObject() {} type ReplicationController struct { JSONBase `json:",inline" yaml:",inline"` DesiredState ReplicationControllerState `json:"desiredState,omitempty" yaml:"desiredState,omitempty"` + CurrentState ReplicationControllerState `json:"currentState,omitempty" yaml:"currentState,omitempty"` Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` } diff --git a/pkg/api/v1beta1/types.go b/pkg/api/v1beta1/types.go index bc2a9c34fe7..3f58d1e12ef 100644 --- a/pkg/api/v1beta1/types.go +++ b/pkg/api/v1beta1/types.go @@ -17,10 +17,10 @@ limitations under the License. package v1beta1 import ( - "github.com/fsouza/go-dockerclient" "github.com/GoogleCloudPlatform/kubernetes/pkg/runtime" "github.com/GoogleCloudPlatform/kubernetes/pkg/util" "github.com/GoogleCloudPlatform/kubernetes/pkg/watch" + "github.com/fsouza/go-dockerclient" ) // Common string formats @@ -337,6 +337,7 @@ func (*ReplicationControllerList) IsAnAPIObject() {} type ReplicationController struct { JSONBase `json:",inline" yaml:",inline"` DesiredState ReplicationControllerState `json:"desiredState,omitempty" yaml:"desiredState,omitempty"` + CurrentState ReplicationControllerState `json:"currentState,omitempty" yaml:"currentState,omitempty"` Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"` } diff --git a/pkg/registry/controller/rest.go b/pkg/registry/controller/rest.go index d7e6f19f3db..703d8f0a99e 100644 --- a/pkg/registry/controller/rest.go +++ b/pkg/registry/controller/rest.go @@ -92,6 +92,7 @@ func (rs *REST) Get(id string) (runtime.Object, error) { if err != nil { return nil, err } + rs.fillCurrentState(&controller) return controller, err } @@ -104,6 +105,7 @@ func (rs *REST) List(selector labels.Selector) (runtime.Object, error) { filtered := []api.ReplicationController{} for _, controller := range controllers.Items { if selector.Matches(labels.Set(controller.Labels)) { + rs.fillCurrentState(&controller) filtered = append(filtered, controller) } } @@ -164,3 +166,11 @@ func (rs *REST) waitForController(ctrl *api.ReplicationController) (runtime.Obje } return ctrl, nil } + +func (rs *REST) fillCurrentState(ctrl *api.ReplicationController) error { + list, err := rs.podLister.ListPods(ctrl.DesiredState.ReplicaSelector) + if err != nil { + return err + } + ctrl.CurrentState.Replicas = len(list.Items) +} From e7625c8d91c7d932edcaf0d93d7a6e9e2fadd58f Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Tue, 9 Sep 2014 15:45:31 -0700 Subject: [PATCH 2/2] Add current state to replica controller. --- pkg/registry/controller/rest.go | 14 ++++++++-- pkg/registry/controller/rest_test.go | 41 ++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/pkg/registry/controller/rest.go b/pkg/registry/controller/rest.go index 703d8f0a99e..d494c7791af 100644 --- a/pkg/registry/controller/rest.go +++ b/pkg/registry/controller/rest.go @@ -92,7 +92,7 @@ func (rs *REST) Get(id string) (runtime.Object, error) { if err != nil { return nil, err } - rs.fillCurrentState(&controller) + rs.fillCurrentState(controller) return controller, err } @@ -149,7 +149,11 @@ func (rs *REST) Watch(label, field labels.Selector, resourceVersion uint64) (wat } return watch.Filter(incoming, func(e watch.Event) (watch.Event, bool) { repController := e.Object.(*api.ReplicationController) - return e, label.Matches(labels.Set(repController.Labels)) + match := label.Matches(labels.Set(repController.Labels)) + if match { + rs.fillCurrentState(repController) + } + return e, match }), nil } @@ -168,9 +172,13 @@ func (rs *REST) waitForController(ctrl *api.ReplicationController) (runtime.Obje } func (rs *REST) fillCurrentState(ctrl *api.ReplicationController) error { - list, err := rs.podLister.ListPods(ctrl.DesiredState.ReplicaSelector) + if rs.podLister == nil { + return nil + } + list, err := rs.podLister.ListPods(labels.Set(ctrl.DesiredState.ReplicaSelector).AsSelector()) if err != nil { return err } ctrl.CurrentState.Replicas = len(list.Items) + return nil } diff --git a/pkg/registry/controller/rest_test.go b/pkg/registry/controller/rest_test.go index 2332a1eecaa..d2f66810aa6 100644 --- a/pkg/registry/controller/rest_test.go +++ b/pkg/registry/controller/rest_test.go @@ -316,3 +316,44 @@ func TestControllerStorageValidatesUpdate(t *testing.T) { } } } + +type fakePodLister struct { + e error + l api.PodList + s labels.Selector +} + +func (f *fakePodLister) ListPods(s labels.Selector) (*api.PodList, error) { + f.s = s + return &f.l, f.e +} + +func TestFillCurrentState(t *testing.T) { + fakeLister := fakePodLister{ + l: api.PodList{ + Items: []api.Pod{ + {JSONBase: api.JSONBase{ID: "foo"}}, + {JSONBase: api.JSONBase{ID: "bar"}}, + }, + }, + } + mockRegistry := registrytest.ControllerRegistry{} + storage := REST{ + registry: &mockRegistry, + podLister: &fakeLister, + } + controller := api.ReplicationController{ + DesiredState: api.ReplicationControllerState{ + ReplicaSelector: map[string]string{ + "foo": "bar", + }, + }, + } + storage.fillCurrentState(&controller) + if controller.CurrentState.Replicas != 2 { + t.Errorf("expected 2, got: %d", controller.CurrentState.Replicas) + } + if !reflect.DeepEqual(fakeLister.s, labels.Set(controller.DesiredState.ReplicaSelector).AsSelector()) { + t.Errorf("unexpected output: %#v %#v", labels.Set(controller.DesiredState.ReplicaSelector).AsSelector(), fakeLister.s) + } +}