cmd/kube-controller-manager

This commit is contained in:
Chao Xu
2016-11-18 12:50:17 -08:00
parent 48536eaef9
commit 7eeb71f698
109 changed files with 4380 additions and 4153 deletions

View File

@@ -26,8 +26,9 @@ import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
"k8s.io/kubernetes/pkg/api/v1"
extensions "k8s.io/kubernetes/pkg/apis/extensions/v1beta1"
"k8s.io/kubernetes/pkg/client/clientset_generated/release_1_5/fake"
"k8s.io/kubernetes/pkg/client/testing/core"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/intstr"
@@ -74,22 +75,22 @@ func addUpdateRSReactor(fakeClient *fake.Clientset) *fake.Clientset {
func addUpdatePodsReactor(fakeClient *fake.Clientset) *fake.Clientset {
fakeClient.AddReactor("update", "pods", func(action core.Action) (handled bool, ret runtime.Object, err error) {
obj := action.(core.UpdateAction).GetObject().(*api.Pod)
obj := action.(core.UpdateAction).GetObject().(*v1.Pod)
return true, obj, nil
})
return fakeClient
}
func newPod(now time.Time, ready bool, beforeSec int) api.Pod {
conditionStatus := api.ConditionFalse
func newPod(now time.Time, ready bool, beforeSec int) v1.Pod {
conditionStatus := v1.ConditionFalse
if ready {
conditionStatus = api.ConditionTrue
conditionStatus = v1.ConditionTrue
}
return api.Pod{
Status: api.PodStatus{
Conditions: []api.PodCondition{
return v1.Pod{
Status: v1.PodStatus{
Conditions: []v1.PodCondition{
{
Type: api.PodReady,
Type: v1.PodReady,
LastTransitionTime: unversioned.NewTime(now.Add(-1 * time.Duration(beforeSec) * time.Second)),
Status: conditionStatus,
},
@@ -99,27 +100,27 @@ func newPod(now time.Time, ready bool, beforeSec int) api.Pod {
}
// generatePodFromRS creates a pod, with the input ReplicaSet's selector and its template
func generatePodFromRS(rs extensions.ReplicaSet) api.Pod {
return api.Pod{
ObjectMeta: api.ObjectMeta{
func generatePodFromRS(rs extensions.ReplicaSet) v1.Pod {
return v1.Pod{
ObjectMeta: v1.ObjectMeta{
Labels: rs.Labels,
},
Spec: rs.Spec.Template.Spec,
}
}
func generatePod(labels map[string]string, image string) api.Pod {
return api.Pod{
ObjectMeta: api.ObjectMeta{
func generatePod(labels map[string]string, image string) v1.Pod {
return v1.Pod{
ObjectMeta: v1.ObjectMeta{
Labels: labels,
},
Spec: api.PodSpec{
Containers: []api.Container{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: image,
Image: image,
ImagePullPolicy: api.PullAlways,
TerminationMessagePath: api.TerminationMessagePathDefault,
ImagePullPolicy: v1.PullAlways,
TerminationMessagePath: v1.TerminationMessagePathDefault,
},
},
},
@@ -128,24 +129,24 @@ func generatePod(labels map[string]string, image string) api.Pod {
func generateRSWithLabel(labels map[string]string, image string) extensions.ReplicaSet {
return extensions.ReplicaSet{
ObjectMeta: api.ObjectMeta{
Name: api.SimpleNameGenerator.GenerateName("replicaset"),
ObjectMeta: v1.ObjectMeta{
Name: v1.SimpleNameGenerator.GenerateName("replicaset"),
Labels: labels,
},
Spec: extensions.ReplicaSetSpec{
Replicas: 1,
Replicas: func(i int32) *int32 { return &i }(1),
Selector: &unversioned.LabelSelector{MatchLabels: labels},
Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Template: v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: labels,
},
Spec: api.PodSpec{
Containers: []api.Container{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: image,
Image: image,
ImagePullPolicy: api.PullAlways,
TerminationMessagePath: api.TerminationMessagePathDefault,
ImagePullPolicy: v1.PullAlways,
TerminationMessagePath: v1.TerminationMessagePathDefault,
},
},
},
@@ -158,11 +159,12 @@ func generateRSWithLabel(labels map[string]string, image string) extensions.Repl
func generateRS(deployment extensions.Deployment) extensions.ReplicaSet {
template := GetNewReplicaSetTemplate(&deployment)
return extensions.ReplicaSet{
ObjectMeta: api.ObjectMeta{
Name: api.SimpleNameGenerator.GenerateName("replicaset"),
ObjectMeta: v1.ObjectMeta{
Name: v1.SimpleNameGenerator.GenerateName("replicaset"),
Labels: template.Labels,
},
Spec: extensions.ReplicaSetSpec{
Replicas: func() *int32 { i := int32(0); return &i }(),
Template: template,
Selector: &unversioned.LabelSelector{MatchLabels: template.Labels},
},
@@ -174,29 +176,29 @@ func generateDeployment(image string) extensions.Deployment {
podLabels := map[string]string{"name": image}
terminationSec := int64(30)
return extensions.Deployment{
ObjectMeta: api.ObjectMeta{
ObjectMeta: v1.ObjectMeta{
Name: image,
},
Spec: extensions.DeploymentSpec{
Replicas: 1,
Replicas: func(i int32) *int32 { return &i }(1),
Selector: &unversioned.LabelSelector{MatchLabels: podLabels},
Template: api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
Template: v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Labels: podLabels,
},
Spec: api.PodSpec{
Containers: []api.Container{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: image,
Image: image,
ImagePullPolicy: api.PullAlways,
TerminationMessagePath: api.TerminationMessagePathDefault,
ImagePullPolicy: v1.PullAlways,
TerminationMessagePath: v1.TerminationMessagePathDefault,
},
},
DNSPolicy: api.DNSClusterFirst,
DNSPolicy: v1.DNSClusterFirst,
TerminationGracePeriodSeconds: &terminationSec,
RestartPolicy: api.RestartPolicyAlways,
SecurityContext: &api.PodSecurityContext{},
RestartPolicy: v1.RestartPolicyAlways,
SecurityContext: &v1.PodSecurityContext{},
},
},
},
@@ -215,7 +217,7 @@ func TestGetNewRC(t *testing.T) {
{
"No new ReplicaSet",
[]runtime.Object{
&api.PodList{},
&v1.PodList{},
&extensions.ReplicaSetList{
Items: []extensions.ReplicaSet{
generateRS(generateDeployment("foo")),
@@ -228,7 +230,7 @@ func TestGetNewRC(t *testing.T) {
{
"Has new ReplicaSet",
[]runtime.Object{
&api.PodList{},
&v1.PodList{},
&extensions.ReplicaSetList{
Items: []extensions.ReplicaSet{
generateRS(generateDeployment("foo")),
@@ -253,7 +255,7 @@ func TestGetNewRC(t *testing.T) {
if err != nil {
t.Errorf("In test case %s, got unexpected error %v", test.test, err)
}
if !api.Semantic.DeepEqual(rs, test.expected) {
if !v1.Semantic.DeepEqual(rs, test.expected) {
t.Errorf("In test case %s, expected %#v, got %#v", test.test, test.expected, rs)
}
}
@@ -262,25 +264,25 @@ func TestGetNewRC(t *testing.T) {
func TestGetOldRCs(t *testing.T) {
newDeployment := generateDeployment("nginx")
newRS := generateRS(newDeployment)
newRS.Status.FullyLabeledReplicas = newRS.Spec.Replicas
newRS.Status.FullyLabeledReplicas = *(newRS.Spec.Replicas)
newPod := generatePodFromRS(newRS)
// create 2 old deployments and related replica sets/pods, with the same labels but different template
oldDeployment := generateDeployment("nginx")
oldDeployment.Spec.Template.Spec.Containers[0].Name = "nginx-old-1"
oldRS := generateRS(oldDeployment)
oldRS.Status.FullyLabeledReplicas = oldRS.Spec.Replicas
oldRS.Status.FullyLabeledReplicas = *(oldRS.Spec.Replicas)
oldPod := generatePodFromRS(oldRS)
oldDeployment2 := generateDeployment("nginx")
oldDeployment2.Spec.Template.Spec.Containers[0].Name = "nginx-old-2"
oldRS2 := generateRS(oldDeployment2)
oldRS2.Status.FullyLabeledReplicas = oldRS2.Spec.Replicas
oldRS2.Status.FullyLabeledReplicas = *(oldRS2.Spec.Replicas)
oldPod2 := generatePodFromRS(oldRS2)
// create 1 ReplicaSet that existed before the deployment, with the same labels as the deployment
existedPod := generatePod(newDeployment.Spec.Template.Labels, "foo")
existedRS := generateRSWithLabel(newDeployment.Spec.Template.Labels, "foo")
existedRS.Status.FullyLabeledReplicas = existedRS.Spec.Replicas
existedRS.Status.FullyLabeledReplicas = *(existedRS.Spec.Replicas)
tests := []struct {
test string
@@ -290,8 +292,8 @@ func TestGetOldRCs(t *testing.T) {
{
"No old ReplicaSets",
[]runtime.Object{
&api.PodList{
Items: []api.Pod{
&v1.PodList{
Items: []v1.Pod{
generatePod(newDeployment.Spec.Template.Labels, "foo"),
generatePod(newDeployment.Spec.Template.Labels, "bar"),
newPod,
@@ -310,8 +312,8 @@ func TestGetOldRCs(t *testing.T) {
{
"Has old ReplicaSet",
[]runtime.Object{
&api.PodList{
Items: []api.Pod{
&v1.PodList{
Items: []v1.Pod{
oldPod,
oldPod2,
generatePod(map[string]string{"name": "bar"}, "bar"),
@@ -359,14 +361,14 @@ func TestGetOldRCs(t *testing.T) {
}
}
func generatePodTemplateSpec(name, nodeName string, annotations, labels map[string]string) api.PodTemplateSpec {
return api.PodTemplateSpec{
ObjectMeta: api.ObjectMeta{
func generatePodTemplateSpec(name, nodeName string, annotations, labels map[string]string) v1.PodTemplateSpec {
return v1.PodTemplateSpec{
ObjectMeta: v1.ObjectMeta{
Name: name,
Annotations: annotations,
Labels: labels,
},
Spec: api.PodSpec{
Spec: v1.PodSpec{
NodeName: nodeName,
},
}
@@ -375,7 +377,7 @@ func generatePodTemplateSpec(name, nodeName string, annotations, labels map[stri
func TestEqualIgnoreHash(t *testing.T) {
tests := []struct {
test string
former, latter api.PodTemplateSpec
former, latter v1.PodTemplateSpec
expected bool
}{
{
@@ -429,7 +431,7 @@ func TestEqualIgnoreHash(t *testing.T) {
}
for _, test := range tests {
runTest := func(t1, t2 api.PodTemplateSpec, reversed bool) {
runTest := func(t1, t2 v1.PodTemplateSpec, reversed bool) {
// Set up
t1Copy, err := api.Scheme.DeepCopy(t1)
if err != nil {
@@ -472,7 +474,7 @@ func TestFindNewReplicaSet(t *testing.T) {
oldDeployment := generateDeployment("nginx")
oldDeployment.Spec.Template.Spec.Containers[0].Name = "nginx-old-1"
oldRS := generateRS(oldDeployment)
oldRS.Status.FullyLabeledReplicas = oldRS.Spec.Replicas
oldRS.Status.FullyLabeledReplicas = *(oldRS.Spec.Replicas)
tests := []struct {
test string
@@ -508,7 +510,7 @@ func TestFindOldReplicaSets(t *testing.T) {
oldDeployment := generateDeployment("nginx")
oldDeployment.Spec.Template.Spec.Containers[0].Name = "nginx-old-1"
oldRS := generateRS(oldDeployment)
oldRS.Status.FullyLabeledReplicas = oldRS.Spec.Replicas
oldRS.Status.FullyLabeledReplicas = *(oldRS.Spec.Replicas)
newPod := generatePodFromRS(newRS)
oldPod := generatePodFromRS(oldRS)
@@ -516,15 +518,15 @@ func TestFindOldReplicaSets(t *testing.T) {
test string
deployment extensions.Deployment
rsList []*extensions.ReplicaSet
podList *api.PodList
podList *v1.PodList
expected []*extensions.ReplicaSet
}{
{
test: "Get old ReplicaSets",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&newRS, &oldRS},
podList: &api.PodList{
Items: []api.Pod{
podList: &v1.PodList{
Items: []v1.Pod{
newPod,
oldPod,
},
@@ -535,8 +537,8 @@ func TestFindOldReplicaSets(t *testing.T) {
test: "Get old ReplicaSets with no new ReplicaSet",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&oldRS},
podList: &api.PodList{
Items: []api.Pod{
podList: &v1.PodList{
Items: []v1.Pod{
oldPod,
},
},
@@ -546,8 +548,8 @@ func TestFindOldReplicaSets(t *testing.T) {
test: "Get empty old ReplicaSets",
deployment: deployment,
rsList: []*extensions.ReplicaSet{&newRS},
podList: &api.PodList{
Items: []api.Pod{
podList: &v1.PodList{
Items: []v1.Pod{
newPod,
},
},
@@ -584,10 +586,10 @@ func equal(rss1, rss2 []*extensions.ReplicaSet) bool {
func TestGetReplicaCountForReplicaSets(t *testing.T) {
rs1 := generateRS(generateDeployment("foo"))
rs1.Spec.Replicas = 1
*(rs1.Spec.Replicas) = 1
rs1.Status.Replicas = 2
rs2 := generateRS(generateDeployment("bar"))
rs2.Spec.Replicas = 2
*(rs2.Spec.Replicas) = 2
rs2.Status.Replicas = 3
tests := []struct {
@@ -715,16 +717,16 @@ func TestNewRSNewReplicas(t *testing.T) {
newDeployment := generateDeployment("nginx")
newRC := generateRS(newDeployment)
rs5 := generateRS(newDeployment)
rs5.Spec.Replicas = 5
*(rs5.Spec.Replicas) = 5
for _, test := range tests {
newDeployment.Spec.Replicas = test.depReplicas
*(newDeployment.Spec.Replicas) = test.depReplicas
newDeployment.Spec.Strategy = extensions.DeploymentStrategy{Type: test.strategyType}
newDeployment.Spec.Strategy.RollingUpdate = &extensions.RollingUpdateDeployment{
MaxUnavailable: intstr.FromInt(1),
MaxSurge: intstr.FromInt(test.maxSurge),
MaxUnavailable: func(i int) *intstr.IntOrString { x := intstr.FromInt(i); return &x }(1),
MaxSurge: func(i int) *intstr.IntOrString { x := intstr.FromInt(i); return &x }(test.maxSurge),
}
newRC.Spec.Replicas = test.newRSReplicas
*(newRC.Spec.Replicas) = test.newRSReplicas
rs, err := NewRSNewReplicas(&newDeployment, []*extensions.ReplicaSet{&rs5}, &newRC)
if err != nil {
t.Errorf("In test case %s, got unexpected error %v", test.test, err)
@@ -739,7 +741,7 @@ var (
condProgressing = func() extensions.DeploymentCondition {
return extensions.DeploymentCondition{
Type: extensions.DeploymentProgressing,
Status: api.ConditionFalse,
Status: v1.ConditionFalse,
Reason: "ForSomeReason",
}
}
@@ -747,7 +749,7 @@ var (
condProgressing2 = func() extensions.DeploymentCondition {
return extensions.DeploymentCondition{
Type: extensions.DeploymentProgressing,
Status: api.ConditionTrue,
Status: v1.ConditionTrue,
Reason: "BecauseItIs",
}
}
@@ -755,7 +757,7 @@ var (
condAvailable = func() extensions.DeploymentCondition {
return extensions.DeploymentCondition{
Type: extensions.DeploymentAvailable,
Status: api.ConditionTrue,
Status: v1.ConditionTrue,
Reason: "AwesomeController",
}
}
@@ -775,7 +777,7 @@ func TestGetCondition(t *testing.T) {
status extensions.DeploymentStatus
condType extensions.DeploymentConditionType
condStatus api.ConditionStatus
condStatus v1.ConditionStatus
condReason string
expected bool
@@ -897,10 +899,11 @@ func TestDeploymentComplete(t *testing.T) {
deployment := func(desired, current, updated, available, maxUnavailable int32) *extensions.Deployment {
return &extensions.Deployment{
Spec: extensions.DeploymentSpec{
Replicas: desired,
Replicas: &desired,
Strategy: extensions.DeploymentStrategy{
RollingUpdate: &extensions.RollingUpdateDeployment{
MaxUnavailable: intstr.FromInt(int(maxUnavailable)),
MaxUnavailable: func(i int) *intstr.IntOrString { x := intstr.FromInt(i); return &x }(int(maxUnavailable)),
MaxSurge: func() *intstr.IntOrString { x := intstr.FromInt(0); return &x }(),
},
Type: extensions.RollingUpdateDeploymentStrategyType,
},
@@ -1047,7 +1050,7 @@ func TestDeploymentTimedOut(t *testing.T) {
timeFn := func(min, sec int) time.Time {
return time.Date(2016, 1, 1, 0, min, sec, 0, time.UTC)
}
deployment := func(condType extensions.DeploymentConditionType, status api.ConditionStatus, pds *int32, from time.Time) extensions.Deployment {
deployment := func(condType extensions.DeploymentConditionType, status v1.ConditionStatus, pds *int32, from time.Time) extensions.Deployment {
return extensions.Deployment{
Spec: extensions.DeploymentSpec{
ProgressDeadlineSeconds: pds,
@@ -1075,21 +1078,21 @@ func TestDeploymentTimedOut(t *testing.T) {
{
name: "no progressDeadlineSeconds specified - no timeout",
d: deployment(extensions.DeploymentProgressing, api.ConditionTrue, null, timeFn(1, 9)),
d: deployment(extensions.DeploymentProgressing, v1.ConditionTrue, null, timeFn(1, 9)),
nowFn: func() time.Time { return timeFn(1, 20) },
expected: false,
},
{
name: "progressDeadlineSeconds: 10s, now - started => 00:01:20 - 00:01:09 => 11s",
d: deployment(extensions.DeploymentProgressing, api.ConditionTrue, &ten, timeFn(1, 9)),
d: deployment(extensions.DeploymentProgressing, v1.ConditionTrue, &ten, timeFn(1, 9)),
nowFn: func() time.Time { return timeFn(1, 20) },
expected: true,
},
{
name: "progressDeadlineSeconds: 10s, now - started => 00:01:20 - 00:01:11 => 9s",
d: deployment(extensions.DeploymentProgressing, api.ConditionTrue, &ten, timeFn(1, 11)),
d: deployment(extensions.DeploymentProgressing, v1.ConditionTrue, &ten, timeFn(1, 11)),
nowFn: func() time.Time { return timeFn(1, 20) },
expected: false,
},