Introducing ReplicaSet, a new incarnation of Replication Controller.

This commit adds the API types, conversion, validation and defaulting code.
This commit is contained in:
Madhusudan.C.S
2015-11-11 17:15:06 -08:00
parent 2374a3225a
commit 572abe1c50
15 changed files with 5859 additions and 1927 deletions

View File

@@ -22,6 +22,7 @@ import (
"k8s.io/kubernetes/pkg/api"
_ "k8s.io/kubernetes/pkg/api/install"
"k8s.io/kubernetes/pkg/api/resource"
"k8s.io/kubernetes/pkg/api/v1"
_ "k8s.io/kubernetes/pkg/apis/extensions/install"
. "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
@@ -373,6 +374,219 @@ func TestSetDefaultJob(t *testing.T) {
}
}
func TestSetDefaultReplicaSet(t *testing.T) {
tests := []struct {
rs *ReplicaSet
expectLabels bool
expectSelector bool
}{
{
rs: &ReplicaSet{
Spec: ReplicaSetSpec{
Template: &v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
},
},
},
},
},
expectLabels: true,
expectSelector: true,
},
{
rs: &ReplicaSet{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"bar": "foo",
},
},
Spec: ReplicaSetSpec{
Template: &v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
},
},
},
},
},
expectLabels: false,
expectSelector: true,
},
{
rs: &ReplicaSet{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"bar": "foo",
},
},
Spec: ReplicaSetSpec{
Selector: &LabelSelector{
MatchLabels: map[string]string{
"some": "other",
},
},
Template: &v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
},
},
},
},
},
expectLabels: false,
expectSelector: false,
},
{
rs: &ReplicaSet{
Spec: ReplicaSetSpec{
Selector: &LabelSelector{
MatchLabels: map[string]string{
"some": "other",
},
},
Template: &v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
},
},
},
},
},
expectLabels: true,
expectSelector: false,
},
}
for _, test := range tests {
rs := test.rs
obj2 := roundTrip(t, runtime.Object(rs))
rs2, ok := obj2.(*ReplicaSet)
if !ok {
t.Errorf("unexpected object: %v", rs2)
t.FailNow()
}
if test.expectSelector != reflect.DeepEqual(rs2.Spec.Selector.MatchLabels, rs2.Spec.Template.Labels) {
if test.expectSelector {
t.Errorf("expected: %v, got: %v", rs2.Spec.Template.Labels, rs2.Spec.Selector)
} else {
t.Errorf("unexpected equality: %v", rs.Spec.Selector)
}
}
if test.expectLabels != reflect.DeepEqual(rs2.Labels, rs2.Spec.Template.Labels) {
if test.expectLabels {
t.Errorf("expected: %v, got: %v", rs2.Spec.Template.Labels, rs2.Labels)
} else {
t.Errorf("unexpected equality: %v", rs.Labels)
}
}
}
}
func TestSetDefaultReplicaSetReplicas(t *testing.T) {
tests := []struct {
rs ReplicaSet
expectReplicas int32
}{
{
rs: ReplicaSet{
Spec: ReplicaSetSpec{
Template: &v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
},
},
},
},
},
expectReplicas: 1,
},
{
rs: ReplicaSet{
Spec: ReplicaSetSpec{
Replicas: newInt32(0),
Template: &v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
},
},
},
},
},
expectReplicas: 0,
},
{
rs: ReplicaSet{
Spec: ReplicaSetSpec{
Replicas: newInt32(3),
Template: &v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
},
},
},
},
},
expectReplicas: 3,
},
}
for _, test := range tests {
rs := &test.rs
obj2 := roundTrip(t, runtime.Object(rs))
rs2, ok := obj2.(*ReplicaSet)
if !ok {
t.Errorf("unexpected object: %v", rs2)
t.FailNow()
}
if rs2.Spec.Replicas == nil {
t.Errorf("unexpected nil Replicas")
} else if test.expectReplicas != *rs2.Spec.Replicas {
t.Errorf("expected: %d replicas, got: %d", test.expectReplicas, *rs2.Spec.Replicas)
}
}
}
func TestDefaultRequestIsNotSetForReplicaSet(t *testing.T) {
s := v1.PodSpec{}
s.Containers = []v1.Container{
{
Resources: v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceCPU: resource.MustParse("100m"),
},
},
},
}
rs := &ReplicaSet{
Spec: ReplicaSetSpec{
Replicas: newInt32(3),
Template: &v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
},
},
Spec: s,
},
},
}
output := roundTrip(t, runtime.Object(rs))
rs2 := output.(*ReplicaSet)
defaultRequest := rs2.Spec.Template.Spec.Containers[0].Resources.Requests
requestValue := defaultRequest[v1.ResourceCPU]
if requestValue.String() != "0" {
t.Errorf("Expected 0 request value, got: %s", requestValue.String())
}
}
func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
data, err := runtime.Encode(Codec, obj)
if err != nil {