Remaining refactor for PodTemplateSpec and fixing test cases

This commit is contained in:
Clayton Coleman
2014-11-06 21:09:46 -05:00
parent 72bf12c86d
commit 94c873e7a4
18 changed files with 285 additions and 201 deletions

View File

@@ -64,8 +64,6 @@ func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan apiserver.RE
if len(controller.Name) == 0 {
controller.Name = util.NewUUID().String()
}
// Pod Manifest ID should be assigned by the pod API
controller.DesiredState.PodTemplate.DesiredState.Manifest.ID = ""
if errs := validation.ValidateReplicationController(controller); len(errs) > 0 {
return nil, errors.NewInvalid("replicationController", controller.Name, errs)
}
@@ -158,41 +156,41 @@ func (rs *REST) Watch(ctx api.Context, label, field labels.Selector, resourceVer
// TODO(lavalamp): remove watch.Filter, which is broken. Implement consistent way of filtering.
// TODO(lavalamp): this watch method needs a test.
return watch.Filter(incoming, func(e watch.Event) (watch.Event, bool) {
repController, ok := e.Object.(*api.ReplicationController)
controller, ok := e.Object.(*api.ReplicationController)
if !ok {
// must be an error event-- pass it on
return e, true
}
match := label.Matches(labels.Set(repController.Labels))
match := label.Matches(labels.Set(controller.Labels))
if match {
rs.fillCurrentState(ctx, repController)
rs.fillCurrentState(ctx, controller)
}
return e, match
}), nil
}
func (rs *REST) waitForController(ctx api.Context, ctrl *api.ReplicationController) (runtime.Object, error) {
func (rs *REST) waitForController(ctx api.Context, controller *api.ReplicationController) (runtime.Object, error) {
for {
pods, err := rs.podLister.ListPods(ctx, labels.Set(ctrl.DesiredState.ReplicaSelector).AsSelector())
pods, err := rs.podLister.ListPods(ctx, labels.Set(controller.Spec.Selector).AsSelector())
if err != nil {
return ctrl, err
return controller, err
}
if len(pods.Items) == ctrl.DesiredState.Replicas {
if len(pods.Items) == controller.Spec.Replicas {
break
}
time.Sleep(rs.pollPeriod)
}
return ctrl, nil
return controller, nil
}
func (rs *REST) fillCurrentState(ctx api.Context, ctrl *api.ReplicationController) error {
func (rs *REST) fillCurrentState(ctx api.Context, controller *api.ReplicationController) error {
if rs.podLister == nil {
return nil
}
list, err := rs.podLister.ListPods(ctx, labels.Set(ctrl.DesiredState.ReplicaSelector).AsSelector())
list, err := rs.podLister.ListPods(ctx, labels.Set(controller.Spec.Selector).AsSelector())
if err != nil {
return err
}
ctrl.CurrentState.Replicas = len(list.Items)
controller.Status.Replicas = len(list.Items)
return nil
}

View File

@@ -116,6 +116,15 @@ func TestControllerDecode(t *testing.T) {
ObjectMeta: api.ObjectMeta{
Name: "foo",
},
Spec: api.ReplicationControllerSpec{
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"name": "nginx",
},
},
},
},
}
body, err := latest.Codec.Encode(controller)
if err != nil {
@@ -140,30 +149,30 @@ func TestControllerParsing(t *testing.T) {
"name": "nginx",
},
},
DesiredState: api.ReplicationControllerState{
Spec: api.ReplicationControllerSpec{
Replicas: 2,
ReplicaSelector: map[string]string{
Selector: map[string]string{
"name": "nginx",
},
PodTemplate: api.PodTemplate{
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
Containers: []api.Container{
{
Image: "dockerfile/nginx",
Ports: []api.Port{
{
ContainerPort: 80,
HostPort: 8080,
},
Template: &api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"name": "nginx",
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Image: "dockerfile/nginx",
Ports: []api.Port{
{
ContainerPort: 80,
HostPort: 8080,
},
},
},
},
},
Labels: map[string]string{
"name": "nginx",
},
},
},
}
@@ -205,9 +214,11 @@ func TestControllerParsing(t *testing.T) {
}
var validPodTemplate = api.PodTemplate{
DesiredState: api.PodState{
Manifest: api.ContainerManifest{
Version: "v1beta1",
Spec: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{"a": "b"},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "test",
@@ -216,7 +227,6 @@ var validPodTemplate = api.PodTemplate{
},
},
},
Labels: map[string]string{"a": "b"},
}
func TestCreateController(t *testing.T) {
@@ -240,10 +250,10 @@ func TestCreateController(t *testing.T) {
}
controller := &api.ReplicationController{
ObjectMeta: api.ObjectMeta{Name: "test"},
DesiredState: api.ReplicationControllerState{
Replicas: 2,
ReplicaSelector: map[string]string{"a": "b"},
PodTemplate: validPodTemplate,
Spec: api.ReplicationControllerSpec{
Replicas: 2,
Selector: map[string]string{"a": "b"},
Template: &validPodTemplate.Spec,
},
}
ctx := api.NewDefaultContext()
@@ -273,13 +283,13 @@ func TestControllerStorageValidatesCreate(t *testing.T) {
failureCases := map[string]api.ReplicationController{
"empty ID": {
ObjectMeta: api.ObjectMeta{Name: ""},
DesiredState: api.ReplicationControllerState{
ReplicaSelector: map[string]string{"bar": "baz"},
Spec: api.ReplicationControllerSpec{
Selector: map[string]string{"bar": "baz"},
},
},
"empty selector": {
ObjectMeta: api.ObjectMeta{Name: "abc"},
DesiredState: api.ReplicationControllerState{},
ObjectMeta: api.ObjectMeta{Name: "abc"},
Spec: api.ReplicationControllerSpec{},
},
}
ctx := api.NewDefaultContext()
@@ -304,13 +314,13 @@ func TestControllerStorageValidatesUpdate(t *testing.T) {
failureCases := map[string]api.ReplicationController{
"empty ID": {
ObjectMeta: api.ObjectMeta{Name: ""},
DesiredState: api.ReplicationControllerState{
ReplicaSelector: map[string]string{"bar": "baz"},
Spec: api.ReplicationControllerSpec{
Selector: map[string]string{"bar": "baz"},
},
},
"empty selector": {
ObjectMeta: api.ObjectMeta{Name: "abc"},
DesiredState: api.ReplicationControllerState{},
ObjectMeta: api.ObjectMeta{Name: "abc"},
Spec: api.ReplicationControllerSpec{},
},
}
ctx := api.NewDefaultContext()
@@ -351,19 +361,19 @@ func TestFillCurrentState(t *testing.T) {
podLister: &fakeLister,
}
controller := api.ReplicationController{
DesiredState: api.ReplicationControllerState{
ReplicaSelector: map[string]string{
Spec: api.ReplicationControllerSpec{
Selector: map[string]string{
"foo": "bar",
},
},
}
ctx := api.NewContext()
storage.fillCurrentState(ctx, &controller)
if controller.CurrentState.Replicas != 2 {
t.Errorf("expected 2, got: %d", controller.CurrentState.Replicas)
if controller.Status.Replicas != 2 {
t.Errorf("expected 2, got: %d", controller.Status.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)
if !reflect.DeepEqual(fakeLister.s, labels.Set(controller.Spec.Selector).AsSelector()) {
t.Errorf("unexpected output: %#v %#v", labels.Set(controller.Spec.Selector).AsSelector(), fakeLister.s)
}
}