Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make replica controllers return current state. #1248

Merged
merged 2 commits into from
Sep 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`
}

Expand Down
20 changes: 19 additions & 1 deletion pkg/registry/controller/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O(N * M) scaling.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, do we care? This is all pretty local.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we care at the moment, but stuff like this will keep us from scaling out of the 100's of pods and rep. controllers.

filtered = append(filtered, controller)
}
}
Expand Down Expand Up @@ -147,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
}

Expand All @@ -164,3 +170,15 @@ func (rs *REST) waitForController(ctrl *api.ReplicationController) (runtime.Obje
}
return ctrl, nil
}

func (rs *REST) fillCurrentState(ctrl *api.ReplicationController) error {
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this pattern where apiserver fills in the current state intended to be permanent? If so, I think we should not store the CurrentState field in etcd. Otherwise, we should have some component that updates the thing that is stored in etcd and apiserver shouldn't do this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like, as implemented here you couldn't watch for this. I think it's totally reasonable to want to watch for this data instead of polling for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't want to write transient state into etcd. If we want to have a background poll which updates this in memory, and then we watch that, I'd be ok with that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't want to write transient state into etcd.

Why not?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it leads to lots of unnecessary write load on the data store, and also, it makes compareAndSwap a lot trickier, because suddenly there are writes you care about, and writes you don't care about, or else you have a bunch of unnecessary conflicts.

I think its better hygiene to keep etcd/persistent store the domain of desired state, and current state is always dynamically obtained from the source (possibly with a caching layer for performance)

return nil
}
41 changes: 41 additions & 0 deletions pkg/registry/controller/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}