Move pods.go into common directory.

This commit is contained in:
Random-Liu 2016-07-18 00:52:39 -07:00
parent 804109d349
commit 0362789283

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2014 The Kubernetes Authors. Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package e2e package common
import ( import (
"bytes" "bytes"
@ -42,7 +42,7 @@ import (
) )
const ( const (
defaultObservationTimeout = time.Minute * 2 defaultObservationTimeout = time.Minute * 1
) )
var ( var (
@ -51,45 +51,47 @@ var (
maxBackOffTolerance = time.Duration(1.3 * float64(kubelet.MaxContainerBackOff)) maxBackOffTolerance = time.Duration(1.3 * float64(kubelet.MaxContainerBackOff))
) )
func runLivenessTest(c *client.Client, ns string, podDescr *api.Pod, expectNumRestarts int, timeout time.Duration) { func runLivenessTest(f *framework.Framework, pod *api.Pod, expectNumRestarts int, timeout time.Duration) {
By(fmt.Sprintf("Creating pod %s in namespace %s", podDescr.Name, ns)) podClient := f.PodClient()
podDescr, err := c.Pods(ns).Create(podDescr) ns := f.Namespace.Name
framework.ExpectNoError(err, fmt.Sprintf("creating pod %s", podDescr.Name)) Expect(pod.Spec.Containers).NotTo(BeEmpty())
containerName := pod.Spec.Containers[0].Name
// At the end of the test, clean up by removing the pod. // At the end of the test, clean up by removing the pod.
defer func() { defer func() {
By("deleting the pod") By("deleting the pod")
c.Pods(ns).Delete(podDescr.Name, api.NewDeleteOptions(0)) podClient.Delete(pod.Name, api.NewDeleteOptions(0))
}() }()
By(fmt.Sprintf("Creating pod %s in namespace %s", pod.Name, ns))
podClient.Create(pod)
// Wait until the pod is not pending. (Here we need to check for something other than // Wait until the pod is not pending. (Here we need to check for something other than
// 'Pending' other than checking for 'Running', since when failures occur, we go to // 'Pending' other than checking for 'Running', since when failures occur, we go to
// 'Terminated' which can cause indefinite blocking.) // 'Terminated' which can cause indefinite blocking.)
framework.ExpectNoError(framework.WaitForPodNotPending(c, ns, podDescr.Name, podDescr.ResourceVersion), framework.ExpectNoError(framework.WaitForPodNotPending(f.Client, ns, pod.Name, pod.ResourceVersion),
fmt.Sprintf("starting pod %s in namespace %s", podDescr.Name, ns)) fmt.Sprintf("starting pod %s in namespace %s", pod.Name, ns))
framework.Logf("Started pod %s in namespace %s", podDescr.Name, ns) framework.Logf("Started pod %s in namespace %s", pod.Name, ns)
// Check the pod's current state and verify that restartCount is present. // Check the pod's current state and verify that restartCount is present.
By("checking the pod's current state and verifying that restartCount is present") By("checking the pod's current state and verifying that restartCount is present")
pod, err := c.Pods(ns).Get(podDescr.Name) pod, err := podClient.Get(pod.Name)
framework.ExpectNoError(err, fmt.Sprintf("getting pod %s in namespace %s", podDescr.Name, ns)) framework.ExpectNoError(err, fmt.Sprintf("getting pod %s in namespace %s", pod.Name, ns))
initialRestartCount := api.GetExistingContainerStatus(pod.Status.ContainerStatuses, "liveness").RestartCount initialRestartCount := api.GetExistingContainerStatus(pod.Status.ContainerStatuses, containerName).RestartCount
framework.Logf("Initial restart count of pod %s is %d", podDescr.Name, initialRestartCount) framework.Logf("Initial restart count of pod %s is %d", pod.Name, initialRestartCount)
// Wait for the restart state to be as desired. // Wait for the restart state to be as desired.
deadline := time.Now().Add(timeout) deadline := time.Now().Add(timeout)
lastRestartCount := initialRestartCount lastRestartCount := initialRestartCount
observedRestarts := int32(0) observedRestarts := int32(0)
for start := time.Now(); time.Now().Before(deadline); time.Sleep(2 * time.Second) { for start := time.Now(); time.Now().Before(deadline); time.Sleep(2 * time.Second) {
pod, err = c.Pods(ns).Get(podDescr.Name) pod, err = podClient.Get(pod.Name)
framework.ExpectNoError(err, fmt.Sprintf("getting pod %s", podDescr.Name)) framework.ExpectNoError(err, fmt.Sprintf("getting pod %s", pod.Name))
restartCount := api.GetExistingContainerStatus(pod.Status.ContainerStatuses, "liveness").RestartCount restartCount := api.GetExistingContainerStatus(pod.Status.ContainerStatuses, containerName).RestartCount
if restartCount != lastRestartCount { if restartCount != lastRestartCount {
framework.Logf("Restart count of pod %s/%s is now %d (%v elapsed)", framework.Logf("Restart count of pod %s/%s is now %d (%v elapsed)",
ns, podDescr.Name, restartCount, time.Since(start)) ns, pod.Name, restartCount, time.Since(start))
if restartCount < lastRestartCount { if restartCount < lastRestartCount {
framework.Failf("Restart count should increment monotonically: restart cont of pod %s/%s changed from %d to %d", framework.Failf("Restart count should increment monotonically: restart cont of pod %s/%s changed from %d to %d",
ns, podDescr.Name, lastRestartCount, restartCount) ns, pod.Name, lastRestartCount, restartCount)
} }
} }
observedRestarts = restartCount - initialRestartCount observedRestarts = restartCount - initialRestartCount
@ -104,25 +106,17 @@ func runLivenessTest(c *client.Client, ns string, podDescr *api.Pod, expectNumRe
// If we expected n restarts (n > 0), fail if we observed < n restarts. // If we expected n restarts (n > 0), fail if we observed < n restarts.
if (expectNumRestarts == 0 && observedRestarts > 0) || (expectNumRestarts > 0 && if (expectNumRestarts == 0 && observedRestarts > 0) || (expectNumRestarts > 0 &&
int(observedRestarts) < expectNumRestarts) { int(observedRestarts) < expectNumRestarts) {
framework.Failf("pod %s/%s - expected number of restarts: %t, found restarts: %t", framework.Failf("pod %s/%s - expected number of restarts: %d, found restarts: %d",
ns, podDescr.Name, expectNumRestarts, observedRestarts) ns, pod.Name, expectNumRestarts, observedRestarts)
} }
} }
// testHostIP tests that a pod gets a host IP // testHostIP tests that a pod gets a host IP
func testHostIP(c *client.Client, ns string, pod *api.Pod) { func testHostIP(podClient *framework.PodClient, pod *api.Pod) {
podClient := c.Pods(ns)
By("creating pod") By("creating pod")
defer podClient.Delete(pod.Name, api.NewDeleteOptions(0)) defer podClient.Delete(pod.Name, api.NewDeleteOptions(0))
pod, err := podClient.Create(pod) podClient.CreateSync(pod)
if err != nil {
framework.Failf("Failed to create pod: %v", err)
}
By("ensuring that pod is running and has a hostIP")
// 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.
err = framework.WaitForPodRunningInNamespace(c, pod)
Expect(err).NotTo(HaveOccurred())
// Try to make sure we get a hostIP for each pod. // Try to make sure we get a hostIP for each pod.
hostIPTimeout := 2 * time.Minute hostIPTimeout := 2 * time.Minute
t := time.Now() t := time.Now()
@ -142,54 +136,39 @@ func testHostIP(c *client.Client, ns string, pod *api.Pod) {
} }
} }
func runPodFromStruct(f *framework.Framework, pod *api.Pod) { func startPodAndGetBackOffs(podClient *framework.PodClient, pod *api.Pod, sleepAmount time.Duration) (time.Duration, time.Duration) {
By("submitting the pod to kubernetes") podClient.CreateSync(pod)
podClient := f.Client.Pods(f.Namespace.Name)
pod, err := podClient.Create(pod)
if err != nil {
framework.Failf("Failed to create pod: %v", err)
}
framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
By("verifying the pod is in kubernetes")
pod, err = podClient.Get(pod.Name)
if err != nil {
framework.Failf("failed to get pod: %v", err)
}
}
func startPodAndGetBackOffs(f *framework.Framework, pod *api.Pod, podName string, containerName string, sleepAmount time.Duration) (time.Duration, time.Duration) {
runPodFromStruct(f, pod)
time.Sleep(sleepAmount) time.Sleep(sleepAmount)
Expect(pod.Spec.Containers).NotTo(BeEmpty())
podName := pod.Name
containerName := pod.Spec.Containers[0].Name
By("getting restart delay-0") By("getting restart delay-0")
_, err := getRestartDelay(f.Client, pod, f.Namespace.Name, podName, containerName) _, err := getRestartDelay(podClient, podName, containerName)
if err != nil { if err != nil {
framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName) framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName)
} }
By("getting restart delay-1") By("getting restart delay-1")
delay1, err := getRestartDelay(f.Client, pod, f.Namespace.Name, podName, containerName) delay1, err := getRestartDelay(podClient, podName, containerName)
if err != nil { if err != nil {
framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName) framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName)
} }
By("getting restart delay-2") By("getting restart delay-2")
delay2, err := getRestartDelay(f.Client, pod, f.Namespace.Name, podName, containerName) delay2, err := getRestartDelay(podClient, podName, containerName)
if err != nil { if err != nil {
framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName) framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName)
} }
return delay1, delay2 return delay1, delay2
} }
func getRestartDelay(c *client.Client, pod *api.Pod, ns string, name string, containerName string) (time.Duration, error) { func getRestartDelay(podClient *framework.PodClient, podName string, containerName string) (time.Duration, error) {
beginTime := time.Now() beginTime := time.Now()
for time.Since(beginTime) < (2 * maxBackOffTolerance) { // may just miss the 1st MaxContainerBackOff delay for time.Since(beginTime) < (2 * maxBackOffTolerance) { // may just miss the 1st MaxContainerBackOff delay
time.Sleep(time.Second) time.Sleep(time.Second)
pod, err := c.Pods(ns).Get(name) pod, err := podClient.Get(podName)
framework.ExpectNoError(err, fmt.Sprintf("getting pod %s", name)) framework.ExpectNoError(err, fmt.Sprintf("getting pod %s", podName))
status, ok := api.GetContainerStatus(pod.Status.ContainerStatuses, containerName) status, ok := api.GetContainerStatus(pod.Status.ContainerStatuses, containerName)
if !ok { if !ok {
framework.Logf("getRestartDelay: status missing") framework.Logf("getRestartDelay: status missing")
@ -208,10 +187,14 @@ func getRestartDelay(c *client.Client, pod *api.Pod, ns string, name string, con
var _ = framework.KubeDescribe("Pods", func() { var _ = framework.KubeDescribe("Pods", func() {
f := framework.NewDefaultFramework("pods") f := framework.NewDefaultFramework("pods")
var podClient *framework.PodClient
BeforeEach(func() {
podClient = f.PodClient()
})
It("should get a host IP [Conformance]", func() { It("should get a host IP [Conformance]", func() {
name := "pod-hostip-" + string(uuid.NewUUID()) name := "pod-hostip-" + string(uuid.NewUUID())
testHostIP(f.Client, f.Namespace.Name, &api.Pod{ testHostIP(podClient, &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: name, Name: name,
}, },
@ -226,48 +209,9 @@ var _ = framework.KubeDescribe("Pods", func() {
}) })
}) })
It("should be schedule with cpu and memory limits [Conformance]", func() {
podClient := f.Client.Pods(f.Namespace.Name)
By("creating the pod")
name := "pod-update-" + string(uuid.NewUUID())
value := strconv.Itoa(time.Now().Nanosecond())
pod := &api.Pod{
ObjectMeta: api.ObjectMeta{
Name: name,
Labels: map[string]string{
"name": "foo",
"time": value,
},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: "nginx",
Image: framework.GetPauseImageName(f.Client),
Resources: api.ResourceRequirements{
Limits: api.ResourceList{
api.ResourceCPU: *resource.NewMilliQuantity(100, resource.DecimalSI),
api.ResourceMemory: *resource.NewQuantity(10*1024*1024, resource.DecimalSI),
},
},
},
},
},
}
defer podClient.Delete(pod.Name, nil)
_, err := podClient.Create(pod)
if err != nil {
framework.Failf("Error creating a pod: %v", err)
}
framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
})
It("should be submitted and removed [Conformance]", func() { It("should be submitted and removed [Conformance]", func() {
podClient := f.Client.Pods(f.Namespace.Name)
By("creating the pod") By("creating the pod")
name := "pod-update-" + string(uuid.NewUUID()) name := "pod-submit-remove-" + string(uuid.NewUUID())
value := strconv.Itoa(time.Now().Nanosecond()) value := strconv.Itoa(time.Now().Nanosecond())
pod := &api.Pod{ pod := &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
@ -282,16 +226,6 @@ var _ = framework.KubeDescribe("Pods", func() {
{ {
Name: "nginx", Name: "nginx",
Image: "gcr.io/google_containers/nginx-slim:0.7", Image: "gcr.io/google_containers/nginx-slim:0.7",
Ports: []api.ContainerPort{{ContainerPort: 80}},
LivenessProbe: &api.Probe{
Handler: api.Handler{
HTTPGet: &api.HTTPGetAction{
Path: "/index.html",
Port: intstr.FromInt(8080),
},
},
InitialDelaySeconds: 30,
},
}, },
}, },
}, },
@ -301,36 +235,27 @@ var _ = framework.KubeDescribe("Pods", func() {
selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})) selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
options := api.ListOptions{LabelSelector: selector} options := api.ListOptions{LabelSelector: selector}
pods, err := podClient.List(options) pods, err := podClient.List(options)
if err != nil { Expect(err).NotTo(HaveOccurred(), "failed to query for pods")
framework.Failf("Failed to query for pods: %v", err)
}
Expect(len(pods.Items)).To(Equal(0)) Expect(len(pods.Items)).To(Equal(0))
options = api.ListOptions{ options = api.ListOptions{
LabelSelector: selector, LabelSelector: selector,
ResourceVersion: pods.ListMeta.ResourceVersion, ResourceVersion: pods.ListMeta.ResourceVersion,
} }
w, err := podClient.Watch(options) w, err := podClient.Watch(options)
if err != nil { Expect(err).NotTo(HaveOccurred(), "failed to set up watch")
framework.Failf("Failed to set up watch: %v", err)
}
By("submitting the pod to kubernetes") By("submitting the pod to kubernetes")
// We call defer here in case there is a problem with // We call defer here in case there is a problem with
// the test so we can ensure that we clean up after // the test so we can ensure that we clean up after
// ourselves // ourselves
defer podClient.Delete(pod.Name, api.NewDeleteOptions(0)) defer podClient.Delete(pod.Name, api.NewDeleteOptions(0))
_, err = podClient.Create(pod) podClient.Create(pod)
if err != nil {
framework.Failf("Failed to create pod: %v", err)
}
By("verifying the pod is in kubernetes") By("verifying the pod is in kubernetes")
selector = labels.SelectorFromSet(labels.Set(map[string]string{"time": value})) selector = labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
options = api.ListOptions{LabelSelector: selector} options = api.ListOptions{LabelSelector: selector}
pods, err = podClient.List(options) pods, err = podClient.List(options)
if err != nil { Expect(err).NotTo(HaveOccurred(), "failed to query for pods")
framework.Failf("Failed to query for pods: %v", err)
}
Expect(len(pods.Items)).To(Equal(1)) Expect(len(pods.Items)).To(Equal(1))
By("verifying pod creation was observed") By("verifying pod creation was observed")
@ -343,19 +268,17 @@ var _ = framework.KubeDescribe("Pods", func() {
Fail("Timeout while waiting for pod creation") Fail("Timeout while waiting for pod creation")
} }
// We need to wait for the pod to be scheduled, otherwise the deletion // We need to wait for the pod to be running, otherwise the deletion
// will be carried out immediately rather than gracefully. // may be carried out immediately rather than gracefully.
framework.ExpectNoError(f.WaitForPodRunning(pod.Name)) framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
// save the scheduled pod // save the running pod
if pod, err = podClient.Get(pod.Name); err != nil { pod, err = podClient.Get(pod.Name)
Expect(err).NotTo(HaveOccurred(), "failed to GET scheduled pod") Expect(err).NotTo(HaveOccurred(), "failed to GET scheduled pod")
} framework.Logf("running pod: %#v", pod)
framework.Logf("scheduled pod: %#v", pod)
By("deleting the pod gracefully") By("deleting the pod gracefully")
if err := podClient.Delete(pod.Name, api.NewDeleteOptions(30)); err != nil { err = podClient.Delete(pod.Name, api.NewDeleteOptions(30))
framework.Failf("Failed to delete pod: %v", err) Expect(err).NotTo(HaveOccurred(), "failed to delete pod")
}
By("verifying the kubelet observed the termination notice") By("verifying the kubelet observed the termination notice")
Expect(wait.Poll(time.Second*5, time.Second*30, func() (bool, error) { Expect(wait.Poll(time.Second*5, time.Second*30, func() (bool, error) {
@ -404,15 +327,11 @@ var _ = framework.KubeDescribe("Pods", func() {
selector = labels.SelectorFromSet(labels.Set(map[string]string{"time": value})) selector = labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
options = api.ListOptions{LabelSelector: selector} options = api.ListOptions{LabelSelector: selector}
pods, err = podClient.List(options) pods, err = podClient.List(options)
if err != nil { Expect(err).NotTo(HaveOccurred(), "failed to query for pods")
Fail(fmt.Sprintf("Failed to list pods to verify deletion: %v", err))
}
Expect(len(pods.Items)).To(Equal(0)) Expect(len(pods.Items)).To(Equal(0))
}) })
It("should be updated [Conformance]", func() { It("should be updated [Conformance]", func() {
podClient := f.Client.Pods(f.Namespace.Name)
By("creating the pod") By("creating the pod")
name := "pod-update-" + string(uuid.NewUUID()) name := "pod-update-" + string(uuid.NewUUID())
value := strconv.Itoa(time.Now().Nanosecond()) value := strconv.Itoa(time.Now().Nanosecond())
@ -429,16 +348,6 @@ var _ = framework.KubeDescribe("Pods", func() {
{ {
Name: "nginx", Name: "nginx",
Image: "gcr.io/google_containers/nginx-slim:0.7", Image: "gcr.io/google_containers/nginx-slim:0.7",
Ports: []api.ContainerPort{{ContainerPort: 80}},
LivenessProbe: &api.Probe{
Handler: api.Handler{
HTTPGet: &api.HTTPGetAction{
Path: "/index.html",
Port: intstr.FromInt(8080),
},
},
InitialDelaySeconds: 30,
},
}, },
}, },
}, },
@ -449,21 +358,17 @@ var _ = framework.KubeDescribe("Pods", func() {
By("deleting the pod") By("deleting the pod")
podClient.Delete(pod.Name, api.NewDeleteOptions(0)) podClient.Delete(pod.Name, api.NewDeleteOptions(0))
}() }()
pod, err := podClient.Create(pod) pod = podClient.CreateSync(pod)
if err != nil {
framework.Failf("Failed to create pod: %v", err)
}
framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
By("verifying the pod is in kubernetes") By("verifying the pod is in kubernetes")
selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})) selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
options := api.ListOptions{LabelSelector: selector} options := api.ListOptions{LabelSelector: selector}
pods, err := podClient.List(options) pods, err := podClient.List(options)
Expect(err).NotTo(HaveOccurred(), "failed to query for pods")
Expect(len(pods.Items)).To(Equal(1)) Expect(len(pods.Items)).To(Equal(1))
By("updating the pod") By("updating the pod")
f.PodClient().Update(name, func(pod *api.Pod) { podClient.Update(name, func(pod *api.Pod) {
value = strconv.Itoa(time.Now().Nanosecond()) value = strconv.Itoa(time.Now().Nanosecond())
pod.Labels["time"] = value pod.Labels["time"] = value
}) })
@ -474,13 +379,12 @@ var _ = framework.KubeDescribe("Pods", func() {
selector = labels.SelectorFromSet(labels.Set(map[string]string{"time": value})) selector = labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
options = api.ListOptions{LabelSelector: selector} options = api.ListOptions{LabelSelector: selector}
pods, err = podClient.List(options) pods, err = podClient.List(options)
Expect(err).NotTo(HaveOccurred(), "failed to query for pods")
Expect(len(pods.Items)).To(Equal(1)) Expect(len(pods.Items)).To(Equal(1))
framework.Logf("Pod update OK") framework.Logf("Pod update OK")
}) })
It("should allow activeDeadlineSeconds to be updated [Conformance]", func() { It("should allow activeDeadlineSeconds to be updated [Conformance]", func() {
podClient := f.Client.Pods(f.Namespace.Name)
By("creating the pod") By("creating the pod")
name := "pod-update-activedeadlineseconds-" + string(uuid.NewUUID()) name := "pod-update-activedeadlineseconds-" + string(uuid.NewUUID())
value := strconv.Itoa(time.Now().Nanosecond()) value := strconv.Itoa(time.Now().Nanosecond())
@ -497,16 +401,6 @@ var _ = framework.KubeDescribe("Pods", func() {
{ {
Name: "nginx", Name: "nginx",
Image: "gcr.io/google_containers/nginx-slim:0.7", Image: "gcr.io/google_containers/nginx-slim:0.7",
Ports: []api.ContainerPort{{ContainerPort: 80}},
LivenessProbe: &api.Probe{
Handler: api.Handler{
HTTPGet: &api.HTTPGetAction{
Path: "/index.html",
Port: intstr.FromInt(8080),
},
},
InitialDelaySeconds: 30,
},
}, },
}, },
}, },
@ -517,21 +411,17 @@ var _ = framework.KubeDescribe("Pods", func() {
By("deleting the pod") By("deleting the pod")
podClient.Delete(pod.Name, api.NewDeleteOptions(0)) podClient.Delete(pod.Name, api.NewDeleteOptions(0))
}() }()
pod, err := podClient.Create(pod) podClient.CreateSync(pod)
if err != nil {
framework.Failf("Failed to create pod: %v", err)
}
framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
By("verifying the pod is in kubernetes") By("verifying the pod is in kubernetes")
selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})) selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))
options := api.ListOptions{LabelSelector: selector} options := api.ListOptions{LabelSelector: selector}
pods, err := podClient.List(options) pods, err := podClient.List(options)
Expect(err).NotTo(HaveOccurred(), "failed to query for pods")
Expect(len(pods.Items)).To(Equal(1)) Expect(len(pods.Items)).To(Equal(1))
By("updating the pod") By("updating the pod")
f.PodClient().Update(name, func(pod *api.Pod) { podClient.Update(name, func(pod *api.Pod) {
newDeadline := int64(5) newDeadline := int64(5)
pod.Spec.ActiveDeadlineSeconds = &newDeadline pod.Spec.ActiveDeadlineSeconds = &newDeadline
}) })
@ -558,12 +448,8 @@ var _ = framework.KubeDescribe("Pods", func() {
}, },
}, },
} }
defer f.Client.Pods(f.Namespace.Name).Delete(serverPod.Name, api.NewDeleteOptions(0)) defer podClient.Delete(serverPod.Name, api.NewDeleteOptions(0))
_, err := f.Client.Pods(f.Namespace.Name).Create(serverPod) podClient.CreateSync(serverPod)
if err != nil {
framework.Failf("Failed to create serverPod: %v", err)
}
framework.ExpectNoError(f.WaitForPodRunning(serverPod.Name))
// This service exposes port 8080 of the test pod as a service on port 8765 // This service exposes port 8080 of the test pod as a service on port 8765
// TODO(filbranden): We would like to use a unique service name such as: // TODO(filbranden): We would like to use a unique service name such as:
@ -591,10 +477,8 @@ var _ = framework.KubeDescribe("Pods", func() {
}, },
} }
defer f.Client.Services(f.Namespace.Name).Delete(svc.Name) defer f.Client.Services(f.Namespace.Name).Delete(svc.Name)
_, err = f.Client.Services(f.Namespace.Name).Create(svc) _, err := f.Client.Services(f.Namespace.Name).Create(svc)
if err != nil { Expect(err).NotTo(HaveOccurred(), "failed to create service")
framework.Failf("Failed to create service: %v", err)
}
// Make a client pod that verifies that it has the service environment variables. // Make a client pod that verifies that it has the service environment variables.
podName := "client-envvars-" + string(uuid.NewUUID()) podName := "client-envvars-" + string(uuid.NewUUID())
@ -628,8 +512,8 @@ var _ = framework.KubeDescribe("Pods", func() {
It("should invoke init containers on a RestartNever pod", func() { It("should invoke init containers on a RestartNever pod", func() {
framework.SkipIfContainerRuntimeIs("rkt") // #25988 framework.SkipIfContainerRuntimeIs("rkt") // #25988
podClient := f.Client.Pods(f.Namespace.Name)
podClient := f.PodClient()
By("creating the pod") By("creating the pod")
name := "pod-init-" + string(uuid.NewUUID()) name := "pod-init-" + string(uuid.NewUUID())
value := strconv.Itoa(time.Now().Nanosecond()) value := strconv.Itoa(time.Now().Nanosecond())
@ -665,14 +549,9 @@ var _ = framework.KubeDescribe("Pods", func() {
}, },
} }
defer podClient.Delete(pod.Name, nil) defer podClient.Delete(pod.Name, nil)
startedPod, err := podClient.Create(pod) startedPod := podClient.Create(pod)
if err != nil {
framework.Failf("Error creating a pod: %v", err)
}
w, err := podClient.Watch(api.SingleObject(startedPod.ObjectMeta)) w, err := podClient.Watch(api.SingleObject(startedPod.ObjectMeta))
if err != nil { Expect(err).NotTo(HaveOccurred(), "error watching a pod")
framework.Failf("Error watching a pod: %v", err)
}
wr := watch.NewRecorder(w) wr := watch.NewRecorder(w)
event, err := watch.Until(framework.PodStartTimeout, wr, client.PodCompleted) event, err := watch.Until(framework.PodStartTimeout, wr, client.PodCompleted)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
@ -694,8 +573,8 @@ var _ = framework.KubeDescribe("Pods", func() {
It("should invoke init containers on a RestartAlways pod", func() { It("should invoke init containers on a RestartAlways pod", func() {
framework.SkipIfContainerRuntimeIs("rkt") // #25988 framework.SkipIfContainerRuntimeIs("rkt") // #25988
podClient := f.Client.Pods(f.Namespace.Name)
podClient := f.PodClient()
By("creating the pod") By("creating the pod")
name := "pod-init-" + string(uuid.NewUUID()) name := "pod-init-" + string(uuid.NewUUID())
value := strconv.Itoa(time.Now().Nanosecond()) value := strconv.Itoa(time.Now().Nanosecond())
@ -735,14 +614,9 @@ var _ = framework.KubeDescribe("Pods", func() {
}, },
} }
defer podClient.Delete(pod.Name, nil) defer podClient.Delete(pod.Name, nil)
startedPod, err := podClient.Create(pod) startedPod := podClient.Create(pod)
if err != nil {
framework.Failf("Error creating a pod: %v", err)
}
w, err := podClient.Watch(api.SingleObject(startedPod.ObjectMeta)) w, err := podClient.Watch(api.SingleObject(startedPod.ObjectMeta))
if err != nil { Expect(err).NotTo(HaveOccurred(), "error watching a pod")
framework.Failf("Error watching a pod: %v", err)
}
wr := watch.NewRecorder(w) wr := watch.NewRecorder(w)
event, err := watch.Until(framework.PodStartTimeout, wr, client.PodRunning) event, err := watch.Until(framework.PodStartTimeout, wr, client.PodRunning)
Expect(err).To(BeNil()) Expect(err).To(BeNil())
@ -764,8 +638,8 @@ var _ = framework.KubeDescribe("Pods", func() {
It("should not start app containers if init containers fail on a RestartAlways pod", func() { It("should not start app containers if init containers fail on a RestartAlways pod", func() {
framework.SkipIfContainerRuntimeIs("rkt") // #25988 framework.SkipIfContainerRuntimeIs("rkt") // #25988
podClient := f.Client.Pods(f.Namespace.Name)
podClient := f.PodClient()
By("creating the pod") By("creating the pod")
name := "pod-init-" + string(uuid.NewUUID()) name := "pod-init-" + string(uuid.NewUUID())
value := strconv.Itoa(time.Now().Nanosecond()) value := strconv.Itoa(time.Now().Nanosecond())
@ -805,14 +679,9 @@ var _ = framework.KubeDescribe("Pods", func() {
}, },
} }
defer podClient.Delete(pod.Name, nil) defer podClient.Delete(pod.Name, nil)
startedPod, err := podClient.Create(pod) startedPod := podClient.Create(pod)
if err != nil {
framework.Failf("Error creating a pod: %v", err)
}
w, err := podClient.Watch(api.SingleObject(startedPod.ObjectMeta)) w, err := podClient.Watch(api.SingleObject(startedPod.ObjectMeta))
if err != nil { Expect(err).NotTo(HaveOccurred(), "error watching a pod")
framework.Failf("Error watching a pod: %v", err)
}
wr := watch.NewRecorder(w) wr := watch.NewRecorder(w)
event, err := watch.Until( event, err := watch.Until(
@ -880,8 +749,8 @@ var _ = framework.KubeDescribe("Pods", func() {
It("should not start app containers and fail the pod if init containers fail on a RestartNever pod", func() { It("should not start app containers and fail the pod if init containers fail on a RestartNever pod", func() {
framework.SkipIfContainerRuntimeIs("rkt") // #25988 framework.SkipIfContainerRuntimeIs("rkt") // #25988
podClient := f.Client.Pods(f.Namespace.Name)
podClient := f.PodClient()
By("creating the pod") By("creating the pod")
name := "pod-init-" + string(uuid.NewUUID()) name := "pod-init-" + string(uuid.NewUUID())
value := strconv.Itoa(time.Now().Nanosecond()) value := strconv.Itoa(time.Now().Nanosecond())
@ -923,14 +792,9 @@ var _ = framework.KubeDescribe("Pods", func() {
}, },
} }
defer podClient.Delete(pod.Name, nil) defer podClient.Delete(pod.Name, nil)
startedPod, err := podClient.Create(pod) startedPod := podClient.Create(pod)
if err != nil {
framework.Failf("Error creating a pod: %v", err)
}
w, err := podClient.Watch(api.SingleObject(startedPod.ObjectMeta)) w, err := podClient.Watch(api.SingleObject(startedPod.ObjectMeta))
if err != nil { Expect(err).NotTo(HaveOccurred(), "error watching a pod")
framework.Failf("Error watching a pod: %v", err)
}
wr := watch.NewRecorder(w) wr := watch.NewRecorder(w)
event, err := watch.Until( event, err := watch.Until(
@ -988,8 +852,8 @@ var _ = framework.KubeDescribe("Pods", func() {
Expect(endPod.Status.ContainerStatuses[0].State.Waiting).ToNot(BeNil()) Expect(endPod.Status.ContainerStatuses[0].State.Waiting).ToNot(BeNil())
}) })
It("should be restarted with a docker exec \"cat /tmp/health\" liveness probe [Conformance]", func() { It("should be restarted with a exec \"cat /tmp/health\" liveness probe [Conformance]", func() {
runLivenessTest(f.Client, f.Namespace.Name, &api.Pod{ runLivenessTest(f, &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "liveness-exec", Name: "liveness-exec",
Labels: map[string]string{"test": "liveness"}, Labels: map[string]string{"test": "liveness"},
@ -1015,8 +879,8 @@ var _ = framework.KubeDescribe("Pods", func() {
}, 1, defaultObservationTimeout) }, 1, defaultObservationTimeout)
}) })
It("should *not* be restarted with a docker exec \"cat /tmp/health\" liveness probe [Conformance]", func() { It("should *not* be restarted with a exec \"cat /tmp/health\" liveness probe [Conformance]", func() {
runLivenessTest(f.Client, f.Namespace.Name, &api.Pod{ runLivenessTest(f, &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "liveness-exec", Name: "liveness-exec",
Labels: map[string]string{"test": "liveness"}, Labels: map[string]string{"test": "liveness"},
@ -1043,7 +907,7 @@ var _ = framework.KubeDescribe("Pods", func() {
}) })
It("should be restarted with a /healthz http liveness probe [Conformance]", func() { It("should be restarted with a /healthz http liveness probe [Conformance]", func() {
runLivenessTest(f.Client, f.Namespace.Name, &api.Pod{ runLivenessTest(f, &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "liveness-http", Name: "liveness-http",
Labels: map[string]string{"test": "liveness"}, Labels: map[string]string{"test": "liveness"},
@ -1072,7 +936,7 @@ var _ = framework.KubeDescribe("Pods", func() {
// Slow by design (5 min) // Slow by design (5 min)
It("should have monotonically increasing restart count [Conformance] [Slow]", func() { It("should have monotonically increasing restart count [Conformance] [Slow]", func() {
runLivenessTest(f.Client, f.Namespace.Name, &api.Pod{ runLivenessTest(f, &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "liveness-http", Name: "liveness-http",
Labels: map[string]string{"test": "liveness"}, Labels: map[string]string{"test": "liveness"},
@ -1100,7 +964,7 @@ var _ = framework.KubeDescribe("Pods", func() {
}) })
It("should *not* be restarted with a /healthz http liveness probe [Conformance]", func() { It("should *not* be restarted with a /healthz http liveness probe [Conformance]", func() {
runLivenessTest(f.Client, f.Namespace.Name, &api.Pod{ runLivenessTest(f, &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "liveness-http", Name: "liveness-http",
Labels: map[string]string{"test": "liveness"}, Labels: map[string]string{"test": "liveness"},
@ -1109,23 +973,16 @@ var _ = framework.KubeDescribe("Pods", func() {
Containers: []api.Container{ Containers: []api.Container{
{ {
Name: "liveness", Name: "liveness",
Image: "gcr.io/google_containers/nettest:1.7", Image: "gcr.io/google_containers/nginx-slim:0.7",
// These args are garbage but the image will exit if they're not there Ports: []api.ContainerPort{{ContainerPort: 80}},
// we just care about /read serving a 200, which it always does.
Args: []string{
"-service=liveness-http",
"-peers=1",
"-namespace=" + f.Namespace.Name},
Ports: []api.ContainerPort{{ContainerPort: 8080}},
LivenessProbe: &api.Probe{ LivenessProbe: &api.Probe{
Handler: api.Handler{ Handler: api.Handler{
HTTPGet: &api.HTTPGetAction{ HTTPGet: &api.HTTPGetAction{
Path: "/read", Path: "/",
Port: intstr.FromInt(8080), Port: intstr.FromInt(80),
}, },
}, },
InitialDelaySeconds: 15, InitialDelaySeconds: 15,
TimeoutSeconds: 10,
FailureThreshold: 1, FailureThreshold: 1,
}, },
}, },
@ -1136,10 +993,7 @@ var _ = framework.KubeDescribe("Pods", func() {
It("should support remote command execution over websockets", func() { It("should support remote command execution over websockets", func() {
config, err := framework.LoadConfig() config, err := framework.LoadConfig()
if err != nil { Expect(err).NotTo(HaveOccurred(), "unable to get base config")
framework.Failf("Unable to get base config: %v", err)
}
podClient := f.Client.Pods(f.Namespace.Name)
By("creating the pod") By("creating the pod")
name := "pod-exec-websocket-" + string(uuid.NewUUID()) name := "pod-exec-websocket-" + string(uuid.NewUUID())
@ -1163,12 +1017,7 @@ var _ = framework.KubeDescribe("Pods", func() {
By("deleting the pod") By("deleting the pod")
podClient.Delete(pod.Name, api.NewDeleteOptions(0)) podClient.Delete(pod.Name, api.NewDeleteOptions(0))
}() }()
pod, err = podClient.Create(pod) pod = podClient.CreateSync(pod)
if err != nil {
framework.Failf("Failed to create pod: %v", err)
}
framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
req := f.Client.Get(). req := f.Client.Get().
Namespace(f.Namespace.Name). Namespace(f.Namespace.Name).
@ -1215,10 +1064,7 @@ var _ = framework.KubeDescribe("Pods", func() {
It("should support retrieving logs from the container over websockets", func() { It("should support retrieving logs from the container over websockets", func() {
config, err := framework.LoadConfig() config, err := framework.LoadConfig()
if err != nil { Expect(err).NotTo(HaveOccurred(), "unable to get base config")
framework.Failf("Unable to get base config: %v", err)
}
podClient := f.Client.Pods(f.Namespace.Name)
By("creating the pod") By("creating the pod")
name := "pod-logs-websocket-" + string(uuid.NewUUID()) name := "pod-logs-websocket-" + string(uuid.NewUUID())
@ -1242,12 +1088,7 @@ var _ = framework.KubeDescribe("Pods", func() {
By("deleting the pod") By("deleting the pod")
podClient.Delete(pod.Name, api.NewDeleteOptions(0)) podClient.Delete(pod.Name, api.NewDeleteOptions(0))
}() }()
pod, err = podClient.Create(pod) podClient.CreateSync(pod)
if err != nil {
framework.Failf("Failed to create pod: %v", err)
}
framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
req := f.Client.Get(). req := f.Client.Get().
Namespace(f.Namespace.Name). Namespace(f.Namespace.Name).
@ -1285,7 +1126,6 @@ var _ = framework.KubeDescribe("Pods", func() {
It("should have their auto-restart back-off timer reset on image update [Slow]", func() { It("should have their auto-restart back-off timer reset on image update [Slow]", func() {
podName := "pod-back-off-image" podName := "pod-back-off-image"
containerName := "back-off" containerName := "back-off"
podClient := f.Client.Pods(f.Namespace.Name)
pod := &api.Pod{ pod := &api.Pod{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: podName, Name: podName,
@ -1307,10 +1147,10 @@ var _ = framework.KubeDescribe("Pods", func() {
podClient.Delete(pod.Name, api.NewDeleteOptions(0)) podClient.Delete(pod.Name, api.NewDeleteOptions(0))
}() }()
delay1, delay2 := startPodAndGetBackOffs(f, pod, podName, containerName, buildBackOffDuration) delay1, delay2 := startPodAndGetBackOffs(podClient, pod, buildBackOffDuration)
By("updating the image") By("updating the image")
f.PodClient().Update(podName, func(pod *api.Pod) { podClient.Update(podName, func(pod *api.Pod) {
pod.Spec.Containers[0].Image = "gcr.io/google_containers/nginx-slim:0.7" pod.Spec.Containers[0].Image = "gcr.io/google_containers/nginx-slim:0.7"
}) })
@ -1318,7 +1158,7 @@ var _ = framework.KubeDescribe("Pods", func() {
framework.ExpectNoError(f.WaitForPodRunning(pod.Name)) framework.ExpectNoError(f.WaitForPodRunning(pod.Name))
By("get restart delay after image update") By("get restart delay after image update")
delayAfterUpdate, err := getRestartDelay(f.Client, pod, f.Namespace.Name, podName, containerName) delayAfterUpdate, err := getRestartDelay(podClient, podName, containerName)
if err != nil { if err != nil {
framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName) framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName)
} }
@ -1330,7 +1170,6 @@ var _ = framework.KubeDescribe("Pods", func() {
// Slow issue #19027 (20 mins) // Slow issue #19027 (20 mins)
It("should cap back-off at MaxContainerBackOff [Slow]", func() { It("should cap back-off at MaxContainerBackOff [Slow]", func() {
podClient := f.Client.Pods(f.Namespace.Name)
podName := "back-off-cap" podName := "back-off-cap"
containerName := "back-off-cap" containerName := "back-off-cap"
pod := &api.Pod{ pod := &api.Pod{
@ -1354,7 +1193,7 @@ var _ = framework.KubeDescribe("Pods", func() {
podClient.Delete(pod.Name, api.NewDeleteOptions(0)) podClient.Delete(pod.Name, api.NewDeleteOptions(0))
}() }()
runPodFromStruct(f, pod) podClient.CreateSync(pod)
time.Sleep(2 * kubelet.MaxContainerBackOff) // it takes slightly more than 2*x to get to a back-off of x time.Sleep(2 * kubelet.MaxContainerBackOff) // it takes slightly more than 2*x to get to a back-off of x
// wait for a delay == capped delay of MaxContainerBackOff // wait for a delay == capped delay of MaxContainerBackOff
@ -1364,7 +1203,7 @@ var _ = framework.KubeDescribe("Pods", func() {
err error err error
) )
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
delay1, err = getRestartDelay(f.Client, pod, f.Namespace.Name, podName, containerName) delay1, err = getRestartDelay(podClient, podName, containerName)
if err != nil { if err != nil {
framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName) framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName)
} }
@ -1379,7 +1218,7 @@ var _ = framework.KubeDescribe("Pods", func() {
} }
By("getting restart delay after a capped delay") By("getting restart delay after a capped delay")
delay2, err := getRestartDelay(f.Client, pod, f.Namespace.Name, podName, containerName) delay2, err := getRestartDelay(podClient, podName, containerName)
if err != nil { if err != nil {
framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName) framework.Failf("timed out waiting for container restart in pod=%s/%s", podName, containerName)
} }