From 9a6857cf2ccc33fc7e2c8e48c87ebc19b877edb4 Mon Sep 17 00:00:00 2001 From: Jerzy Szczepkowski Date: Tue, 10 Mar 2015 19:17:04 +0100 Subject: [PATCH] Fixed writing Host in Pod.Spec during binding. Fixed writing Host in Pod.Spec during binding. Related to #5207. --- pkg/registry/pod/etcd/etcd.go | 5 +++-- pkg/registry/pod/etcd/etcd_test.go | 21 +++++++++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pkg/registry/pod/etcd/etcd.go b/pkg/registry/pod/etcd/etcd.go index c60d9b1c42b..d4c0d06b680 100644 --- a/pkg/registry/pod/etcd/etcd.go +++ b/pkg/registry/pod/etcd/etcd.go @@ -173,9 +173,10 @@ func (r *BindingREST) setPodHostTo(ctx api.Context, podID, oldMachine, machine s if !ok { return nil, fmt.Errorf("unexpected object: %#v", obj) } - if pod.Status.Host != oldMachine { - return nil, fmt.Errorf("pod %v is already assigned to host %v", pod.Name, pod.Status.Host) + if pod.Spec.Host != oldMachine || pod.Status.Host != oldMachine { + return nil, fmt.Errorf("pod %v is already assigned to host %v or %v", pod.Name, pod.Spec.Host, pod.Status.Host) } + pod.Spec.Host = machine pod.Status.Host = machine finalPod = pod return pod, nil diff --git a/pkg/registry/pod/etcd/etcd_test.go b/pkg/registry/pod/etcd/etcd_test.go index c7853e75b42..87c75f3fc51 100644 --- a/pkg/registry/pod/etcd/etcd_test.go +++ b/pkg/registry/pod/etcd/etcd_test.go @@ -1090,28 +1090,28 @@ func TestEtcdCreateBinding(t *testing.T) { "badKind": { binding: api.Binding{ ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, - Target: api.ObjectReference{Name: "machine", Kind: "unknown"}, + Target: api.ObjectReference{Name: "machine1", Kind: "unknown"}, }, errOK: func(err error) bool { return errors.IsInvalid(err) }, }, "emptyKind": { binding: api.Binding{ ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, - Target: api.ObjectReference{Name: "machine"}, + Target: api.ObjectReference{Name: "machine2"}, }, errOK: func(err error) bool { return err == nil }, }, "kindNode": { binding: api.Binding{ ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, - Target: api.ObjectReference{Name: "machine", Kind: "Node"}, + Target: api.ObjectReference{Name: "machine3", Kind: "Node"}, }, errOK: func(err error) bool { return err == nil }, }, "kindMinion": { binding: api.Binding{ ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, - Target: api.ObjectReference{Name: "machine", Kind: "Minion"}, + Target: api.ObjectReference{Name: "machine4", Kind: "Minion"}, }, errOK: func(err error) bool { return err == nil }, }, @@ -1124,13 +1124,22 @@ func TestEtcdCreateBinding(t *testing.T) { }, E: tools.EtcdErrorNotFound, } - fakeClient.Set("/registry/nodes/machine/boundpods", runtime.EncodeOrDie(latest.Codec, &api.BoundPods{}), 0) + path := fmt.Sprintf("/registry/nodes/%v/boundpods", test.binding.Target.Name) + fakeClient.Set(path, runtime.EncodeOrDie(latest.Codec, &api.BoundPods{}), 0) if _, err := registry.Create(ctx, validNewPod()); err != nil { t.Fatalf("%s: unexpected error: %v", k, err) } - fakeClient.Set("/registry/nodes/machine/boundpods", runtime.EncodeOrDie(latest.Codec, &api.BoundPods{}), 0) + fakeClient.Set(path, runtime.EncodeOrDie(latest.Codec, &api.BoundPods{}), 0) if _, err := bindingRegistry.Create(ctx, &test.binding); !test.errOK(err) { t.Errorf("%s: unexpected error: %v", k, err) + } else if err == nil { + // If bind succeeded, verify Host field in pod's Spec. + pod, err := registry.Get(ctx, validNewPod().ObjectMeta.Name) + if err != nil { + t.Errorf("%s: unexpected error: %v", k, err) + } else if pod.(*api.Pod).Spec.Host != test.binding.Target.Name { + t.Errorf("%s: expected: %v, got: %v", k, pod.(*api.Pod).Spec.Host, test.binding.Target.Name) + } } } }