Skip to content

Commit

Permalink
Merge pull request #4921 from mikedanese/too-many-gets
Browse files Browse the repository at this point in the history
Interactions with etcd should optionally take a value that is filled with the result
  • Loading branch information
smarterclayton committed Mar 4, 2015
2 parents b457379 + 4f6e09d commit eea1e88
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 91 deletions.
4 changes: 2 additions & 2 deletions pkg/registry/controller/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Registry interface {
ListControllers(ctx api.Context) (*api.ReplicationControllerList, error)
WatchControllers(ctx api.Context, label, field labels.Selector, resourceVersion string) (watch.Interface, error)
GetController(ctx api.Context, controllerID string) (*api.ReplicationController, error)
CreateController(ctx api.Context, controller *api.ReplicationController) error
UpdateController(ctx api.Context, controller *api.ReplicationController) error
CreateController(ctx api.Context, controller *api.ReplicationController) (*api.ReplicationController, error)
UpdateController(ctx api.Context, controller *api.ReplicationController) (*api.ReplicationController, error)
DeleteController(ctx api.Context, controllerID string) error
}
13 changes: 4 additions & 9 deletions pkg/registry/controller/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/rest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/validation"
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
rc "github.com/GoogleCloudPlatform/kubernetes/pkg/controller"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
Expand Down Expand Up @@ -60,11 +59,11 @@ func (rs *REST) Create(ctx api.Context, obj runtime.Object) (runtime.Object, err
return nil, err
}

if err := rs.registry.CreateController(ctx, controller); err != nil {
out, err := rs.registry.CreateController(ctx, controller)
if err != nil {
err = rest.CheckGeneratedNameError(rest.ReplicationControllers, err, controller)
return apiserver.RESTResult{}, err
}
return rs.registry.GetController(ctx, controller.Name)
return out, err
}

// Delete asynchronously deletes the ReplicationController specified by its id.
Expand Down Expand Up @@ -124,11 +123,7 @@ func (rs *REST) Update(ctx api.Context, obj runtime.Object) (runtime.Object, boo
if errs := validation.ValidateReplicationController(controller); len(errs) > 0 {
return nil, false, errors.NewInvalid("replicationController", controller.Name, errs)
}
err := rs.registry.UpdateController(ctx, controller)
if err != nil {
return nil, false, err
}
out, err := rs.registry.GetController(ctx, controller.Name)
out, err := rs.registry.UpdateController(ctx, controller)
return out, false, err
}

Expand Down
40 changes: 22 additions & 18 deletions pkg/registry/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,25 @@ func (r *Registry) GetController(ctx api.Context, controllerID string) (*api.Rep
}

// CreateController creates a new ReplicationController.
func (r *Registry) CreateController(ctx api.Context, controller *api.ReplicationController) error {
func (r *Registry) CreateController(ctx api.Context, controller *api.ReplicationController) (*api.ReplicationController, error) {
key, err := makeControllerKey(ctx, controller.Name)
if err != nil {
return err
return nil, err
}
err = r.CreateObj(key, controller, 0)
return etcderr.InterpretCreateError(err, "replicationController", controller.Name)
out := &api.ReplicationController{}
err = r.CreateObj(key, controller, out, 0)
return out, etcderr.InterpretCreateError(err, "replicationController", controller.Name)
}

// UpdateController replaces an existing ReplicationController.
func (r *Registry) UpdateController(ctx api.Context, controller *api.ReplicationController) error {
func (r *Registry) UpdateController(ctx api.Context, controller *api.ReplicationController) (*api.ReplicationController, error) {
key, err := makeControllerKey(ctx, controller.Name)
if err != nil {
return err
return nil, err
}
err = r.SetObj(key, controller, 0 /* ttl */)
return etcderr.InterpretUpdateError(err, "replicationController", controller.Name)
out := &api.ReplicationController{}
err = r.SetObj(key, controller, out, 0)
return out, etcderr.InterpretUpdateError(err, "replicationController", controller.Name)
}

// DeleteController deletes a ReplicationController specified by its ID.
Expand Down Expand Up @@ -199,13 +201,14 @@ func (r *Registry) ListServices(ctx api.Context) (*api.ServiceList, error) {
}

// CreateService creates a new Service.
func (r *Registry) CreateService(ctx api.Context, svc *api.Service) error {
func (r *Registry) CreateService(ctx api.Context, svc *api.Service) (*api.Service, error) {
key, err := makeServiceKey(ctx, svc.Name)
if err != nil {
return err
return nil, err
}
err = r.CreateObj(key, svc, 0)
return etcderr.InterpretCreateError(err, "service", svc.Name)
out := &api.Service{}
err = r.CreateObj(key, svc, out, 0)
return out, etcderr.InterpretCreateError(err, "service", svc.Name)
}

// GetService obtains a Service specified by its name.
Expand Down Expand Up @@ -270,13 +273,14 @@ func (r *Registry) DeleteService(ctx api.Context, name string) error {
}

// UpdateService replaces an existing Service.
func (r *Registry) UpdateService(ctx api.Context, svc *api.Service) error {
func (r *Registry) UpdateService(ctx api.Context, svc *api.Service) (*api.Service, error) {
key, err := makeServiceKey(ctx, svc.Name)
if err != nil {
return err
return nil, err
}
err = r.SetObj(key, svc, 0 /* ttl */)
return etcderr.InterpretUpdateError(err, "service", svc.Name)
out := &api.Service{}
err = r.SetObj(key, svc, out, 0)
return out, etcderr.InterpretUpdateError(err, "service", svc.Name)
}

// WatchServices begins watching for new, changed, or deleted service configurations.
Expand Down Expand Up @@ -362,13 +366,13 @@ func (r *Registry) ListMinions(ctx api.Context) (*api.NodeList, error) {

func (r *Registry) CreateMinion(ctx api.Context, minion *api.Node) error {
// TODO: Add some validations.
err := r.CreateObj(makeNodeKey(minion.Name), minion, 0)
err := r.CreateObj(makeNodeKey(minion.Name), minion, nil, 0)
return etcderr.InterpretCreateError(err, "minion", minion.Name)
}

func (r *Registry) UpdateMinion(ctx api.Context, minion *api.Node) error {
// TODO: Add some validations.
err := r.SetObj(makeNodeKey(minion.Name), minion, 0 /* ttl */)
err := r.SetObj(makeNodeKey(minion.Name), minion, nil, 0)
return etcderr.InterpretUpdateError(err, "minion", minion.Name)
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/registry/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func TestEtcdCreateController(t *testing.T) {
fakeClient := tools.NewFakeEtcdClient(t)
registry := NewTestEtcdRegistry(fakeClient)
key, _ := makeControllerKey(ctx, "foo")
err := registry.CreateController(ctx, &api.ReplicationController{
_, err := registry.CreateController(ctx, &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestEtcdCreateControllerAlreadyExisting(t *testing.T) {
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)

registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreateController(ctx, &api.ReplicationController{
_, err := registry.CreateController(ctx, &api.ReplicationController{
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
Expand All @@ -259,7 +259,7 @@ func TestEtcdUpdateController(t *testing.T) {
key, _ := makeControllerKey(ctx, "foo")
resp, _ := fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.ReplicationController{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
registry := NewTestEtcdRegistry(fakeClient)
err := registry.UpdateController(ctx, &api.ReplicationController{
_, err := registry.UpdateController(ctx, &api.ReplicationController{
ObjectMeta: api.ObjectMeta{Name: "foo", ResourceVersion: strconv.FormatUint(resp.Node.ModifiedIndex, 10)},
Spec: api.ReplicationControllerSpec{
Replicas: 2,
Expand Down Expand Up @@ -417,7 +417,7 @@ func TestEtcdCreateService(t *testing.T) {
ctx := api.NewDefaultContext()
fakeClient := tools.NewFakeEtcdClient(t)
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreateService(ctx, &api.Service{
_, err := registry.CreateService(ctx, &api.Service{
ObjectMeta: api.ObjectMeta{Name: "foo"},
})
if err != nil {
Expand Down Expand Up @@ -447,7 +447,7 @@ func TestEtcdCreateServiceAlreadyExisting(t *testing.T) {
key, _ := makeServiceKey(ctx, "foo")
fakeClient.Set(key, runtime.EncodeOrDie(latest.Codec, &api.Service{ObjectMeta: api.ObjectMeta{Name: "foo"}}), 0)
registry := NewTestEtcdRegistry(fakeClient)
err := registry.CreateService(ctx, &api.Service{
_, err := registry.CreateService(ctx, &api.Service{
ObjectMeta: api.ObjectMeta{Name: "foo"},
})
if !errors.IsAlreadyExists(err) {
Expand Down Expand Up @@ -572,7 +572,7 @@ func TestEtcdUpdateService(t *testing.T) {
SessionAffinity: "None",
},
}
err := registry.UpdateService(ctx, &testService)
_, err := registry.UpdateService(ctx, &testService)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/generic/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (e *Etcd) CreateWithName(ctx api.Context, name string, obj runtime.Object)
return err
}
}
err = e.Helper.CreateObj(key, obj, ttl)
err = e.Helper.CreateObj(key, obj, nil, ttl)
err = etcderr.InterpretCreateError(err, e.EndpointName, name)
if err == nil && e.Decorator != nil {
err = e.Decorator(obj)
Expand Down Expand Up @@ -177,7 +177,7 @@ func (e *Etcd) Create(ctx api.Context, obj runtime.Object) (runtime.Object, erro
}
}
out := e.NewFunc()
if err := e.Helper.Create(key, obj, out, ttl); err != nil {
if err := e.Helper.CreateObj(key, obj, out, ttl); err != nil {
err = etcderr.InterpretCreateError(err, e.EndpointName, name)
err = rest.CheckGeneratedNameError(e.CreateStrategy, err, obj)
return nil, err
Expand Down Expand Up @@ -209,7 +209,7 @@ func (e *Etcd) UpdateWithName(ctx api.Context, name string, obj runtime.Object)
return err
}
}
err = e.Helper.SetObj(key, obj, ttl)
err = e.Helper.SetObj(key, obj, nil, ttl)
err = etcderr.InterpretUpdateError(err, e.EndpointName, name)
if err == nil && e.Decorator != nil {
err = e.Decorator(obj)
Expand Down
8 changes: 4 additions & 4 deletions pkg/registry/registrytest/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ func (r *ControllerRegistry) GetController(ctx api.Context, ID string) (*api.Rep
return &api.ReplicationController{}, r.Err
}

func (r *ControllerRegistry) CreateController(ctx api.Context, controller *api.ReplicationController) error {
func (r *ControllerRegistry) CreateController(ctx api.Context, controller *api.ReplicationController) (*api.ReplicationController, error) {
r.Lock()
defer r.Unlock()
return r.Err
return controller, r.Err
}

func (r *ControllerRegistry) UpdateController(ctx api.Context, controller *api.ReplicationController) error {
func (r *ControllerRegistry) UpdateController(ctx api.Context, controller *api.ReplicationController) (*api.ReplicationController, error) {
r.Lock()
defer r.Unlock()
return r.Err
return controller, r.Err
}

func (r *ControllerRegistry) DeleteController(ctx api.Context, ID string) error {
Expand Down
8 changes: 4 additions & 4 deletions pkg/registry/registrytest/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func (r *ServiceRegistry) ListServices(ctx api.Context) (*api.ServiceList, error
return res, r.Err
}

func (r *ServiceRegistry) CreateService(ctx api.Context, svc *api.Service) error {
func (r *ServiceRegistry) CreateService(ctx api.Context, svc *api.Service) (*api.Service, error) {
r.mu.Lock()
defer r.mu.Unlock()

r.Service = new(api.Service)
*r.Service = *svc
r.List.Items = append(r.List.Items, *svc)
return r.Err
return svc, r.Err
}

func (r *ServiceRegistry) GetService(ctx api.Context, id string) (*api.Service, error) {
Expand All @@ -98,13 +98,13 @@ func (r *ServiceRegistry) DeleteService(ctx api.Context, id string) error {
return r.Err
}

func (r *ServiceRegistry) UpdateService(ctx api.Context, svc *api.Service) error {
func (r *ServiceRegistry) UpdateService(ctx api.Context, svc *api.Service) (*api.Service, error) {
r.mu.Lock()
defer r.mu.Unlock()

r.UpdatedID = svc.Name
*r.Service = *svc
return r.Err
return svc, r.Err
}

func (r *ServiceRegistry) WatchServices(ctx api.Context, label labels.Selector, field labels.Selector, resourceVersion string) (watch.Interface, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/service/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (
// Registry is an interface for things that know how to store services.
type Registry interface {
ListServices(ctx api.Context) (*api.ServiceList, error)
CreateService(ctx api.Context, svc *api.Service) error
CreateService(ctx api.Context, svc *api.Service) (*api.Service, error)
GetService(ctx api.Context, name string) (*api.Service, error)
DeleteService(ctx api.Context, name string) error
UpdateService(ctx api.Context, svc *api.Service) error
UpdateService(ctx api.Context, svc *api.Service) (*api.Service, error)
WatchServices(ctx api.Context, labels, fields labels.Selector, resourceVersion string) (watch.Interface, error)

// TODO: endpoints and their implementation should be separated, setting endpoints should be
Expand Down
12 changes: 4 additions & 8 deletions pkg/registry/service/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ func (rs *REST) Create(ctx api.Context, obj runtime.Object) (runtime.Object, err
}
}

if err := rs.registry.CreateService(ctx, service); err != nil {
out, err := rs.registry.CreateService(ctx, service)
if err != nil {
rs.portalMgr.Release(net.ParseIP(service.Spec.PortalIP))
err = rest.CheckGeneratedNameError(rest.Services, err, service)
return nil, err
}
return rs.registry.GetService(ctx, service.Name)
return out, err
}

func hostsFromMinionList(list *api.NodeList) []string {
Expand Down Expand Up @@ -213,11 +213,7 @@ func (rs *REST) Update(ctx api.Context, obj runtime.Object) (runtime.Object, boo
}
}
}
err = rs.registry.UpdateService(ctx, service)
if err != nil {
return nil, false, err
}
out, err := rs.registry.GetService(ctx, service.Name)
out, err := rs.registry.UpdateService(ctx, service)
return out, false, err
}

Expand Down
Loading

0 comments on commit eea1e88

Please sign in to comment.