diff --git a/pkg/api/types.go b/pkg/api/types.go index 5972cd0bc24..b63abde4bf6 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -209,7 +209,7 @@ type PodStatus string // These are the valid statuses of pods. const ( // PodWaiting means that we're waiting for the pod to begin running. - PodWaiting = "Waiting" + PodWaiting PodStatus = "Waiting" // PodRunning means that the pod is up and running. PodRunning PodStatus = "Running" // PodTerminated means that the pod has stopped. diff --git a/pkg/api/v1beta1/types.go b/pkg/api/v1beta1/types.go index 25a5d05558c..54e5b489dbe 100644 --- a/pkg/api/v1beta1/types.go +++ b/pkg/api/v1beta1/types.go @@ -212,7 +212,7 @@ type PodStatus string // These are the valid statuses of pods. const ( // PodWaiting means that we're waiting for the pod to begin running. - PodWaiting = "Waiting" + PodWaiting PodStatus = "Waiting" // PodRunning means that the pod is up and running. PodRunning PodStatus = "Running" // PodTerminated means that the pod has stopped. diff --git a/pkg/registry/etcdregistry.go b/pkg/registry/etcdregistry.go index de24edb13fb..bafbd68076c 100644 --- a/pkg/registry/etcdregistry.go +++ b/pkg/registry/etcdregistry.go @@ -86,18 +86,11 @@ func makeContainerKey(machine string) string { // CreatePod creates a pod based on a specification, schedule it onto a specific machine. func (registry *EtcdRegistry) CreatePod(machine string, pod api.Pod) error { - // TODO: When our client supports it, switch to atomic creates. - var pod2 api.Pod - err := registry.helper.ExtractObj(makePodKey(pod.ID), &pod2, false) - if err == nil { - return fmt.Errorf("a pod named %s already exists (%#v)", pod.ID, pod2) - } - // Set status to "Waiting". pod.CurrentState.Status = api.PodWaiting pod.CurrentState.Host = "" - err = registry.helper.SetObj(makePodKey(pod.ID), &pod) + err := registry.helper.CreateObj(makePodKey(pod.ID), &pod) if err != nil { return err } diff --git a/pkg/registry/etcdregistry_test.go b/pkg/registry/etcdregistry_test.go index 93c6e323f14..3e3fece4c59 100644 --- a/pkg/registry/etcdregistry_test.go +++ b/pkg/registry/etcdregistry_test.go @@ -141,6 +141,7 @@ func TestEtcdCreatePodAlreadyExisting(t *testing.T) { func TestEtcdCreatePodWithContainersError(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) + fakeClient.TestIndex = true fakeClient.Data["/registry/pods/foo"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, @@ -160,7 +161,7 @@ func TestEtcdCreatePodWithContainersError(t *testing.T) { }, }) if err == nil { - t.Error("Unexpected non-error") + t.Fatalf("Unexpected non-error") } _, err = fakeClient.Get("/registry/pods/foo", false, false) if err == nil { @@ -173,6 +174,7 @@ func TestEtcdCreatePodWithContainersError(t *testing.T) { func TestEtcdCreatePodWithContainersNotFound(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) + fakeClient.TestIndex = true fakeClient.Data["/registry/pods/foo"] = tools.EtcdResponseWithError{ R: &etcd.Response{ Node: nil, @@ -202,7 +204,7 @@ func TestEtcdCreatePodWithContainersNotFound(t *testing.T) { }, }) if err != nil { - t.Errorf("unexpected error: %v", err) + t.Fatalf("unexpected error: %v", err) } resp, err := fakeClient.Get("/registry/pods/foo", false, false) @@ -588,7 +590,7 @@ func TestEtcdCreateController(t *testing.T) { func TestEtcdCreateControllerAlreadyExisting(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) - fakeClient.Set("/registry/controllers/foo", util.MakeJSONString(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0) + fakeClient.Set("/registry/controllers/foo", api.EncodeOrDie(api.ReplicationController{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.CreateController(api.ReplicationController{ @@ -680,7 +682,7 @@ func TestEtcdCreateService(t *testing.T) { func TestEtcdCreateServiceAlreadyExisting(t *testing.T) { fakeClient := tools.MakeFakeEtcdClient(t) - fakeClient.Set("/registry/services/specs/foo", util.MakeJSONString(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0) + fakeClient.Set("/registry/services/specs/foo", api.EncodeOrDie(api.Service{JSONBase: api.JSONBase{ID: "foo"}}), 0) registry := MakeTestEtcdRegistry(fakeClient, []string{"machine"}) err := registry.CreateService(api.Service{ JSONBase: api.JSONBase{ID: "foo"}, @@ -782,7 +784,7 @@ func TestEtcdUpdateEndpoints(t *testing.T) { Endpoints: []string{"baz", "bar"}, } - fakeClient.Set("/registry/services/endpoints/foo", util.MakeJSONString(api.Endpoints{}), 0) + fakeClient.Set("/registry/services/endpoints/foo", api.EncodeOrDie(api.Endpoints{}), 0) err := registry.UpdateEndpoints(endpoints) if err != nil { diff --git a/pkg/tools/fake_etcd_client.go b/pkg/tools/fake_etcd_client.go index 39a0c320783..cb4c914ecbd 100644 --- a/pkg/tools/fake_etcd_client.go +++ b/pkg/tools/fake_etcd_client.go @@ -87,6 +87,7 @@ func (f *FakeEtcdClient) generateIndex() uint64 { } f.ChangeIndex++ + f.t.Logf("generating index %v", f.ChangeIndex) return f.ChangeIndex } @@ -115,7 +116,7 @@ func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response, func (f *FakeEtcdClient) nodeExists(key string) bool { result, ok := f.Data[key] - return ok && result.R != nil && result.R.Node != nil + return ok && result.R != nil && result.R.Node != nil && result.E == nil } func (f *FakeEtcdClient) setLocked(key, value string, ttl uint64) (*etcd.Response, error) { @@ -128,6 +129,7 @@ func (f *FakeEtcdClient) setLocked(key, value string, ttl uint64) (*etcd.Respons if f.nodeExists(key) { prevResult := f.Data[key] createdIndex := prevResult.R.Node.CreatedIndex + f.t.Logf("updating %v, index %v -> %v", key, createdIndex, i) result := EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ @@ -141,6 +143,7 @@ func (f *FakeEtcdClient) setLocked(key, value string, ttl uint64) (*etcd.Respons return result.R, nil } + f.t.Logf("creating %v, index %v", key, i) result := EtcdResponseWithError{ R: &etcd.Response{ Node: &etcd.Node{ @@ -163,6 +166,7 @@ func (f *FakeEtcdClient) Set(key, value string, ttl uint64) (*etcd.Response, err func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue string, prevIndex uint64) (*etcd.Response, error) { if f.Err != nil { + f.t.Logf("c&s: returning err %v", f.Err) return nil, f.Err } @@ -179,16 +183,19 @@ func (f *FakeEtcdClient) CompareAndSwap(key, value string, ttl uint64, prevValue defer f.Mutex.Unlock() if !f.nodeExists(key) { + f.t.Logf("c&s: node doesn't exist") return nil, EtcdErrorNotFound } prevNode := f.Data[key].R.Node if prevValue != "" && prevValue != prevNode.Value { + f.t.Logf("body didn't match") return nil, EtcdErrorTestFailed } if prevIndex != 0 && prevIndex != prevNode.ModifiedIndex { + f.t.Logf("got index %v but needed %v", prevIndex, prevNode.ModifiedIndex) return nil, EtcdErrorTestFailed }