2016-01-20 01:03:20 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2016 The Kubernetes Authors.
|
2016-01-20 01:03:20 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-07-20 10:22:11 +00:00
|
|
|
package apps
|
2016-01-20 01:03:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2017-06-22 18:24:23 +00:00
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
extensions "k8s.io/api/extensions/v1beta1"
|
2017-02-06 14:40:20 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
2017-01-25 13:13:07 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
2017-01-11 14:09:48 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2017-01-24 14:35:22 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/uuid"
|
2017-01-11 14:09:48 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
2016-10-17 15:19:26 +00:00
|
|
|
"k8s.io/kubernetes/pkg/controller/replicaset"
|
2016-04-07 17:21:31 +00:00
|
|
|
"k8s.io/kubernetes/test/e2e/framework"
|
2016-01-20 01:03:20 +00:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2017-08-29 08:32:08 +00:00
|
|
|
imageutils "k8s.io/kubernetes/test/utils/image"
|
2016-01-20 01:03:20 +00:00
|
|
|
)
|
|
|
|
|
2016-10-17 15:19:26 +00:00
|
|
|
func newRS(rsName string, replicas int32, rsPodLabels map[string]string, imageName string, image string) *extensions.ReplicaSet {
|
|
|
|
zero := int64(0)
|
|
|
|
return &extensions.ReplicaSet{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-10-17 15:19:26 +00:00
|
|
|
Name: rsName,
|
|
|
|
},
|
|
|
|
Spec: extensions.ReplicaSetSpec{
|
2017-03-11 15:50:36 +00:00
|
|
|
Replicas: &replicas,
|
2016-11-18 20:55:17 +00:00
|
|
|
Template: v1.PodTemplateSpec{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-10-17 15:19:26 +00:00
|
|
|
Labels: rsPodLabels,
|
|
|
|
},
|
2016-11-18 20:55:17 +00:00
|
|
|
Spec: v1.PodSpec{
|
2016-10-17 15:19:26 +00:00
|
|
|
TerminationGracePeriodSeconds: &zero,
|
2016-11-18 20:55:17 +00:00
|
|
|
Containers: []v1.Container{
|
2016-10-17 15:19:26 +00:00
|
|
|
{
|
|
|
|
Name: imageName,
|
|
|
|
Image: image,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 20:55:17 +00:00
|
|
|
func newPodQuota(name, number string) *v1.ResourceQuota {
|
|
|
|
return &v1.ResourceQuota{
|
2017-01-17 03:38:19 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
2016-10-17 15:19:26 +00:00
|
|
|
Name: name,
|
|
|
|
},
|
2016-11-18 20:55:17 +00:00
|
|
|
Spec: v1.ResourceQuotaSpec{
|
|
|
|
Hard: v1.ResourceList{
|
|
|
|
v1.ResourcePods: resource.MustParse(number),
|
2016-10-17 15:19:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-12 18:35:57 +00:00
|
|
|
var _ = SIGDescribe("ReplicaSet", func() {
|
2016-04-07 17:21:31 +00:00
|
|
|
f := framework.NewDefaultFramework("replicaset")
|
2016-01-20 01:03:20 +00:00
|
|
|
|
2017-10-26 17:46:09 +00:00
|
|
|
framework.ConformanceIt("should serve a basic image on each replica with a public image ", func() {
|
2017-04-24 07:08:39 +00:00
|
|
|
testReplicaSetServeImageOrFail(f, "basic", framework.ServeHostnameImage)
|
2016-01-20 01:03:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should serve a basic image on each replica with a private image", func() {
|
|
|
|
// requires private images
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.SkipUnlessProviderIs("gce", "gke")
|
2017-08-29 08:32:08 +00:00
|
|
|
privateimage := imageutils.ServeHostname
|
|
|
|
privateimage.SetRegistry(imageutils.PrivateRegistry)
|
|
|
|
testReplicaSetServeImageOrFail(f, "private", imageutils.GetE2EImage(privateimage))
|
2016-01-20 01:03:20 +00:00
|
|
|
})
|
2016-10-17 15:19:26 +00:00
|
|
|
|
|
|
|
It("should surface a failure condition on a common issue like exceeded quota", func() {
|
2017-03-11 15:50:36 +00:00
|
|
|
testReplicaSetConditionCheck(f)
|
2016-10-17 15:19:26 +00:00
|
|
|
})
|
2017-02-06 14:40:20 +00:00
|
|
|
|
2017-10-07 01:28:45 +00:00
|
|
|
It("should adopt matching pods on creation and release no longer matching pods", func() {
|
|
|
|
testRSAdoptMatchingAndReleaseNotMatching(f)
|
2017-02-06 14:40:20 +00:00
|
|
|
})
|
2016-01-20 01:03:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// A basic test to check the deployment of an image using a ReplicaSet. The
|
|
|
|
// image serves its hostname which is checked for each replica.
|
2017-03-11 15:50:36 +00:00
|
|
|
func testReplicaSetServeImageOrFail(f *framework.Framework, test string, image string) {
|
2016-07-26 15:13:18 +00:00
|
|
|
name := "my-hostname-" + test + "-" + string(uuid.NewUUID())
|
2017-03-11 15:50:36 +00:00
|
|
|
replicas := int32(1)
|
2016-01-20 01:03:20 +00:00
|
|
|
|
|
|
|
// Create a ReplicaSet for a service that serves its hostname.
|
|
|
|
// The source for the Docker containter kubernetes/serve_hostname is
|
|
|
|
// in contrib/for-demos/serve_hostname
|
2017-03-11 15:50:36 +00:00
|
|
|
framework.Logf("Creating ReplicaSet %s", name)
|
|
|
|
newRS := newRS(name, replicas, map[string]string{"name": name}, name, image)
|
|
|
|
newRS.Spec.Template.Spec.Containers[0].Ports = []v1.ContainerPort{{ContainerPort: 9376}}
|
2017-07-28 07:54:13 +00:00
|
|
|
_, err := f.ClientSet.ExtensionsV1beta1().ReplicaSets(f.Namespace.Name).Create(newRS)
|
2016-01-20 01:03:20 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2017-03-11 15:50:36 +00:00
|
|
|
// Check that pods for the new RS were created.
|
|
|
|
// TODO: Maybe switch PodsCreated to just check owner references.
|
2016-10-18 13:00:38 +00:00
|
|
|
pods, err := framework.PodsCreated(f.ClientSet, f.Namespace.Name, name, replicas)
|
2016-02-27 01:45:50 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2016-01-20 01:03:20 +00:00
|
|
|
|
|
|
|
// Wait for the pods to enter the running state. Waiting loops until the pods
|
|
|
|
// are running so non-running pods cause a timeout for this test.
|
2017-03-11 15:50:36 +00:00
|
|
|
framework.Logf("Ensuring a pod for ReplicaSet %q is running", name)
|
|
|
|
running := int32(0)
|
2016-01-20 01:03:20 +00:00
|
|
|
for _, pod := range pods.Items {
|
|
|
|
if pod.DeletionTimestamp != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = f.WaitForPodRunning(pod.Name)
|
2017-03-11 15:50:36 +00:00
|
|
|
if err != nil {
|
2017-10-25 15:54:32 +00:00
|
|
|
updatePod, getErr := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(pod.Name, metav1.GetOptions{})
|
2017-03-11 15:50:36 +00:00
|
|
|
if getErr == nil {
|
|
|
|
err = fmt.Errorf("Pod %q never run (phase: %s, conditions: %+v): %v", updatePod.Name, updatePod.Status.Phase, updatePod.Status.Conditions, err)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Pod %q never run: %v", pod.Name, err)
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 01:03:20 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2017-03-11 15:50:36 +00:00
|
|
|
framework.Logf("Pod %q is running (conditions: %+v)", pod.Name, pod.Status.Conditions)
|
|
|
|
running++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
if running != replicas {
|
|
|
|
Expect(fmt.Errorf("unexpected number of running pods: %+v", pods.Items)).NotTo(HaveOccurred())
|
2016-01-20 01:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that something is listening.
|
2017-03-11 15:50:36 +00:00
|
|
|
framework.Logf("Trying to dial the pod")
|
2016-01-20 01:03:20 +00:00
|
|
|
retryTimeout := 2 * time.Minute
|
|
|
|
retryInterval := 5 * time.Second
|
2017-03-11 15:50:36 +00:00
|
|
|
label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name}))
|
2016-10-18 13:00:38 +00:00
|
|
|
err = wait.Poll(retryInterval, retryTimeout, framework.PodProxyResponseChecker(f.ClientSet, f.Namespace.Name, label, name, true, pods).CheckAllResponses)
|
2016-01-20 01:03:20 +00:00
|
|
|
if err != nil {
|
2016-04-07 17:21:31 +00:00
|
|
|
framework.Failf("Did not get expected responses within the timeout period of %.2f seconds.", retryTimeout.Seconds())
|
2016-01-20 01:03:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-17 15:19:26 +00:00
|
|
|
|
|
|
|
// 1. Create a quota restricting pods in the current namespace to 2.
|
|
|
|
// 2. Create a replica set that wants to run 3 pods.
|
|
|
|
// 3. Check replica set conditions for a ReplicaFailure condition.
|
|
|
|
// 4. Scale down the replica set and observe the condition is gone.
|
2017-03-11 15:50:36 +00:00
|
|
|
func testReplicaSetConditionCheck(f *framework.Framework) {
|
2016-10-17 15:19:26 +00:00
|
|
|
c := f.ClientSet
|
|
|
|
namespace := f.Namespace.Name
|
|
|
|
name := "condition-test"
|
|
|
|
|
|
|
|
By(fmt.Sprintf("Creating quota %q that allows only two pods to run in the current namespace", name))
|
|
|
|
quota := newPodQuota(name, "2")
|
2017-10-25 15:54:32 +00:00
|
|
|
_, err := c.CoreV1().ResourceQuotas(namespace).Create(quota)
|
2016-10-17 15:19:26 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
|
2017-10-25 15:54:32 +00:00
|
|
|
quota, err = c.CoreV1().ResourceQuotas(namespace).Get(name, metav1.GetOptions{})
|
2016-10-17 15:19:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
quantity := resource.MustParse("2")
|
2016-11-18 20:55:17 +00:00
|
|
|
podQuota := quota.Status.Hard[v1.ResourcePods]
|
2016-10-17 15:19:26 +00:00
|
|
|
return (&podQuota).Cmp(quantity) == 0, nil
|
|
|
|
})
|
|
|
|
if err == wait.ErrWaitTimeout {
|
|
|
|
err = fmt.Errorf("resource quota %q never synced", name)
|
|
|
|
}
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
By(fmt.Sprintf("Creating replica set %q that asks for more than the allowed pod quota", name))
|
2017-05-25 09:18:42 +00:00
|
|
|
rs := newRS(name, 3, map[string]string{"name": name}, NginxImageName, NginxImage)
|
2017-07-28 07:54:13 +00:00
|
|
|
rs, err = c.ExtensionsV1beta1().ReplicaSets(namespace).Create(rs)
|
2016-10-17 15:19:26 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
By(fmt.Sprintf("Checking replica set %q has the desired failure condition set", name))
|
|
|
|
generation := rs.Generation
|
|
|
|
conditions := rs.Status.Conditions
|
|
|
|
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
|
2017-07-28 07:54:13 +00:00
|
|
|
rs, err = c.ExtensionsV1beta1().ReplicaSets(namespace).Get(name, metav1.GetOptions{})
|
2016-10-17 15:19:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if generation > rs.Status.ObservedGeneration {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
conditions = rs.Status.Conditions
|
|
|
|
|
|
|
|
cond := replicaset.GetCondition(rs.Status, extensions.ReplicaSetReplicaFailure)
|
|
|
|
return cond != nil, nil
|
|
|
|
|
|
|
|
})
|
|
|
|
if err == wait.ErrWaitTimeout {
|
|
|
|
err = fmt.Errorf("rs controller never added the failure condition for replica set %q: %#v", name, conditions)
|
|
|
|
}
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
By(fmt.Sprintf("Scaling down replica set %q to satisfy pod quota", name))
|
|
|
|
rs, err = framework.UpdateReplicaSetWithRetries(c, namespace, name, func(update *extensions.ReplicaSet) {
|
2016-11-18 20:55:17 +00:00
|
|
|
x := int32(2)
|
|
|
|
update.Spec.Replicas = &x
|
2016-10-17 15:19:26 +00:00
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
By(fmt.Sprintf("Checking replica set %q has no failure condition set", name))
|
|
|
|
generation = rs.Generation
|
|
|
|
conditions = rs.Status.Conditions
|
|
|
|
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
|
2017-07-28 07:54:13 +00:00
|
|
|
rs, err = c.ExtensionsV1beta1().ReplicaSets(namespace).Get(name, metav1.GetOptions{})
|
2016-10-17 15:19:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if generation > rs.Status.ObservedGeneration {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
conditions = rs.Status.Conditions
|
|
|
|
|
|
|
|
cond := replicaset.GetCondition(rs.Status, extensions.ReplicaSetReplicaFailure)
|
|
|
|
return cond == nil, nil
|
|
|
|
})
|
|
|
|
if err == wait.ErrWaitTimeout {
|
|
|
|
err = fmt.Errorf("rs controller never removed the failure condition for rs %q: %#v", name, conditions)
|
|
|
|
}
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|
2017-02-06 14:40:20 +00:00
|
|
|
|
2017-10-07 01:28:45 +00:00
|
|
|
func testRSAdoptMatchingAndReleaseNotMatching(f *framework.Framework) {
|
|
|
|
name := "pod-adoption-release"
|
2017-02-06 14:40:20 +00:00
|
|
|
By(fmt.Sprintf("Given a Pod with a 'name' label %s is created", name))
|
|
|
|
p := f.PodClient().CreateSync(&v1.Pod{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": name,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: name,
|
2017-05-25 09:18:42 +00:00
|
|
|
Image: NginxImageName,
|
2017-02-06 14:40:20 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
By("When a replicaset with a matching selector is created")
|
|
|
|
replicas := int32(1)
|
2017-05-25 09:18:42 +00:00
|
|
|
rsSt := newRS(name, replicas, map[string]string{"name": name}, name, NginxImageName)
|
2017-02-06 14:40:20 +00:00
|
|
|
rsSt.Spec.Selector = &metav1.LabelSelector{MatchLabels: map[string]string{"name": name}}
|
2017-07-28 07:54:13 +00:00
|
|
|
rs, err := f.ClientSet.ExtensionsV1beta1().ReplicaSets(f.Namespace.Name).Create(rsSt)
|
2017-02-06 14:40:20 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
By("Then the orphan pod is adopted")
|
|
|
|
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
|
2017-10-25 15:54:32 +00:00
|
|
|
p2, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(p.Name, metav1.GetOptions{})
|
2017-02-06 14:40:20 +00:00
|
|
|
// The Pod p should either be adopted or deleted by the ReplicaSet
|
|
|
|
if errors.IsNotFound(err) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, owner := range p2.OwnerReferences {
|
|
|
|
if *owner.Controller && owner.UID == rs.UID {
|
|
|
|
// pod adopted
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// pod still not adopted
|
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
By("When the matched label of one of its pods change")
|
|
|
|
pods, err := framework.PodsCreated(f.ClientSet, f.Namespace.Name, rs.Name, replicas)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2017-10-07 01:28:45 +00:00
|
|
|
p = &pods.Items[0]
|
2017-03-31 08:17:38 +00:00
|
|
|
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
|
2017-10-25 15:54:32 +00:00
|
|
|
pod, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(p.Name, metav1.GetOptions{})
|
2017-03-31 08:17:38 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
pod.Labels = map[string]string{"name": "not-matching-name"}
|
2017-10-25 15:54:32 +00:00
|
|
|
_, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Update(pod)
|
2017-03-31 08:17:38 +00:00
|
|
|
if err != nil && errors.IsConflict(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
})
|
2017-02-06 14:40:20 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
By("Then the pod is released")
|
|
|
|
err = wait.PollImmediate(1*time.Second, 1*time.Minute, func() (bool, error) {
|
2017-10-25 15:54:32 +00:00
|
|
|
p2, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).Get(p.Name, metav1.GetOptions{})
|
2017-02-06 14:40:20 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
for _, owner := range p2.OwnerReferences {
|
|
|
|
if *owner.Controller && owner.UID == rs.UID {
|
|
|
|
// pod still belonging to the replicaset
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// pod already released
|
|
|
|
return true, nil
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}
|