Skip to content

Commit

Permalink
Add ttl parameter to CreateObj
Browse files Browse the repository at this point in the history
  • Loading branch information
lavalamp committed Sep 25, 2014
1 parent 3ea383d commit 4d4ebcd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
6 changes: 3 additions & 3 deletions pkg/registry/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (r *Registry) CreatePod(pod *api.Pod) error {
// DesiredState.Host == "" is a signal to the scheduler that this pod needs scheduling.
pod.DesiredState.Status = api.PodRunning
pod.DesiredState.Host = ""
err := r.CreateObj(makePodKey(pod.ID), pod)
err := r.CreateObj(makePodKey(pod.ID), pod, 0)
return etcderr.InterpretCreateError(err, "pod", pod.ID)
}

Expand Down Expand Up @@ -253,7 +253,7 @@ func (r *Registry) GetController(controllerID string) (*api.ReplicationControlle

// CreateController creates a new ReplicationController.
func (r *Registry) CreateController(controller *api.ReplicationController) error {
err := r.CreateObj(makeControllerKey(controller.ID), controller)
err := r.CreateObj(makeControllerKey(controller.ID), controller, 0)
return etcderr.InterpretCreateError(err, "replicationController", controller.ID)
}

Expand Down Expand Up @@ -283,7 +283,7 @@ func (r *Registry) ListServices() (*api.ServiceList, error) {

// CreateService creates a new Service.
func (r *Registry) CreateService(svc *api.Service) error {
err := r.CreateObj(makeServiceKey(svc.ID), svc)
err := r.CreateObj(makeServiceKey(svc.ID), svc, 0)
return etcderr.InterpretCreateError(err, "service", svc.ID)
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/tools/etcd_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ func (h *EtcdHelper) bodyAndExtractObj(key string, objPtr runtime.Object, ignore
return body, response.Node.ModifiedIndex, err
}

// CreateObj adds a new object at a key unless it already exists.
func (h *EtcdHelper) CreateObj(key string, obj runtime.Object) error {
// CreateObj adds a new object at a key unless it already exists. 'ttl' is time-to-live in seconds,
// and 0 means forever.
func (h *EtcdHelper) CreateObj(key string, obj runtime.Object, ttl uint64) error {
data, err := h.Codec.Encode(obj)
if err != nil {
return err
Expand All @@ -219,7 +220,7 @@ func (h *EtcdHelper) CreateObj(key string, obj runtime.Object) error {
}
}

_, err = h.Client.Create(key, string(data), 0)
_, err = h.Client.Create(key, string(data), ttl)
return err
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/tools/etcd_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,27 @@ func TestExtractObjNotFoundErr(t *testing.T) {
try("/some/key3")
}

func TestCreateObj(t *testing.T) {
obj := &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
fakeClient := NewFakeEtcdClient(t)
helper := EtcdHelper{fakeClient, latest.Codec, versioner}
err := helper.CreateObj("/some/key", obj, 5)
if err != nil {
t.Errorf("Unexpected error %#v", err)
}
data, err := latest.Codec.Encode(obj)
if err != nil {
t.Errorf("Unexpected error %#v", err)
}
node := fakeClient.Data["/some/key"].R.Node
if e, a := string(data), node.Value; e != a {
t.Errorf("Wanted %v, got %v", e, a)
}
if e, a := uint64(5), fakeClient.LastSetTTL; e != a {
t.Errorf("Wanted %v, got %v", e, a)
}
}

func TestSetObj(t *testing.T) {
obj := &api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
fakeClient := NewFakeEtcdClient(t)
Expand Down
2 changes: 2 additions & 0 deletions pkg/tools/fake_etcd_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type FakeEtcdClient struct {
Ix int
TestIndex bool
ChangeIndex uint64
LastSetTTL uint64

// Will become valid after Watch is called; tester may write to it. Tester may
// also read from it to verify that it's closed after injecting an error.
Expand Down Expand Up @@ -135,6 +136,7 @@ func (f *FakeEtcdClient) nodeExists(key string) bool {
}

func (f *FakeEtcdClient) setLocked(key, value string, ttl uint64) (*etcd.Response, error) {
f.LastSetTTL = ttl
if f.Err != nil {
return nil, f.Err
}
Expand Down

0 comments on commit 4d4ebcd

Please sign in to comment.