2015-01-13 02:11:27 +00:00
|
|
|
/*
|
2015-05-01 16:19:44 +00:00
|
|
|
Copyright 2014 The Kubernetes Authors All rights reserved.
|
2015-01-13 02:11:27 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
2015-01-30 20:59:13 +00:00
|
|
|
"fmt"
|
2015-01-13 02:11:27 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/errors"
|
|
|
|
"k8s.io/kubernetes/pkg/api/resource"
|
|
|
|
"k8s.io/kubernetes/pkg/client"
|
|
|
|
"k8s.io/kubernetes/pkg/fields"
|
|
|
|
"k8s.io/kubernetes/pkg/labels"
|
|
|
|
"k8s.io/kubernetes/pkg/util"
|
|
|
|
"k8s.io/kubernetes/pkg/util/wait"
|
|
|
|
"k8s.io/kubernetes/pkg/watch"
|
2015-01-28 00:38:48 +00:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
2015-01-13 02:11:27 +00:00
|
|
|
)
|
|
|
|
|
2015-07-09 11:56:19 +00:00
|
|
|
func runLivenessTest(c *client.Client, ns string, podDescr *api.Pod, expectRestart bool) {
|
2015-02-10 14:03:21 +00:00
|
|
|
By(fmt.Sprintf("Creating pod %s in namespace %s", podDescr.Name, ns))
|
2015-07-09 11:56:19 +00:00
|
|
|
_, err := c.Pods(ns).Create(podDescr)
|
2015-02-12 18:37:31 +00:00
|
|
|
expectNoError(err, fmt.Sprintf("creating pod %s", podDescr.Name))
|
2015-02-10 14:03:21 +00:00
|
|
|
|
|
|
|
// At the end of the test, clean up by removing the pod.
|
|
|
|
defer func() {
|
|
|
|
By("deleting the pod")
|
2015-06-02 21:40:05 +00:00
|
|
|
c.Pods(ns).Delete(podDescr.Name, nil)
|
2015-02-10 14:03:21 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// 'Terminated' which can cause indefinite blocking.)
|
2015-03-05 20:04:00 +00:00
|
|
|
expectNoError(waitForPodNotPending(c, ns, podDescr.Name),
|
2015-02-12 18:37:31 +00:00
|
|
|
fmt.Sprintf("starting pod %s in namespace %s", podDescr.Name, ns))
|
2015-02-10 14:03:21 +00:00
|
|
|
By(fmt.Sprintf("Started pod %s in namespace %s", podDescr.Name, ns))
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
pod, err := c.Pods(ns).Get(podDescr.Name)
|
2015-02-12 18:37:31 +00:00
|
|
|
expectNoError(err, fmt.Sprintf("getting pod %s in namespace %s", podDescr.Name, ns))
|
2015-03-25 11:09:35 +00:00
|
|
|
initialRestartCount := api.GetExistingContainerStatus(pod.Status.ContainerStatuses, "liveness").RestartCount
|
2015-02-10 14:03:21 +00:00
|
|
|
By(fmt.Sprintf("Initial restart count of pod %s is %d", podDescr.Name, initialRestartCount))
|
|
|
|
|
2015-07-18 01:17:06 +00:00
|
|
|
// Wait for the restart state to be as desired.
|
|
|
|
restarts, deadline := false, time.Now().Add(2*time.Minute)
|
|
|
|
for start := time.Now(); time.Now().Before(deadline); time.Sleep(2 * time.Second) {
|
2015-02-10 14:03:21 +00:00
|
|
|
pod, err = c.Pods(ns).Get(podDescr.Name)
|
2015-02-12 18:37:31 +00:00
|
|
|
expectNoError(err, fmt.Sprintf("getting pod %s", podDescr.Name))
|
2015-03-25 11:09:35 +00:00
|
|
|
restartCount := api.GetExistingContainerStatus(pod.Status.ContainerStatuses, "liveness").RestartCount
|
2015-07-18 01:17:06 +00:00
|
|
|
By(fmt.Sprintf("Restart count of pod %s/%s is now %d (%v elapsed)",
|
|
|
|
ns, podDescr.Name, restartCount, time.Since(start)))
|
2015-02-10 14:03:21 +00:00
|
|
|
if restartCount > initialRestartCount {
|
2015-07-18 01:17:06 +00:00
|
|
|
By(fmt.Sprintf("Restart count of pod %s/%s changed from %d to %d",
|
|
|
|
ns, podDescr.Name, initialRestartCount, restartCount))
|
2015-05-04 23:19:36 +00:00
|
|
|
restarts = true
|
2015-02-10 14:03:21 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 23:19:36 +00:00
|
|
|
if restarts != expectRestart {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("pod %s/%s - expected restarts: %t, found restarts: %t",
|
|
|
|
ns, podDescr.Name, expectRestart, restarts)
|
2015-02-10 14:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 20:00:07 +00:00
|
|
|
// testHostIP tests that a pod gets a host IP
|
2015-07-09 11:56:19 +00:00
|
|
|
func testHostIP(c *client.Client, ns string, pod *api.Pod) {
|
2015-04-10 20:00:07 +00:00
|
|
|
podClient := c.Pods(ns)
|
|
|
|
By("creating pod")
|
2015-06-02 21:40:05 +00:00
|
|
|
defer podClient.Delete(pod.Name, nil)
|
2015-07-09 11:56:19 +00:00
|
|
|
_, err := podClient.Create(pod)
|
2015-04-10 20:00:07 +00:00
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to create pod: %v", err)
|
2015-04-10 20:00:07 +00:00
|
|
|
}
|
|
|
|
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 = waitForPodRunningInNamespace(c, pod.Name, ns)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
// Try to make sure we get a hostIP for each pod.
|
2015-05-19 18:17:32 +00:00
|
|
|
hostIPTimeout := 2 * time.Minute
|
|
|
|
t := time.Now()
|
|
|
|
for {
|
|
|
|
p, err := podClient.Get(pod.Name)
|
2015-04-10 20:00:07 +00:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-05-19 18:17:32 +00:00
|
|
|
if p.Status.HostIP != "" {
|
|
|
|
Logf("Pod %s has hostIP: %s", p.Name, p.Status.HostIP)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if time.Since(t) >= hostIPTimeout {
|
|
|
|
Failf("Gave up waiting for hostIP of pod %s after %v seconds",
|
|
|
|
p.Name, time.Since(t).Seconds())
|
2015-04-10 20:00:07 +00:00
|
|
|
}
|
2015-05-19 18:17:32 +00:00
|
|
|
Logf("Retrying to get the hostIP of pod %s", p.Name)
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
2015-04-10 20:00:07 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 20:59:13 +00:00
|
|
|
var _ = Describe("Pods", func() {
|
2015-07-09 11:56:19 +00:00
|
|
|
framework := NewFramework("pods")
|
2015-01-30 20:59:13 +00:00
|
|
|
|
2015-04-14 23:58:08 +00:00
|
|
|
PIt("should get a host IP", func() {
|
2015-04-10 20:00:07 +00:00
|
|
|
name := "pod-hostip-" + string(util.NewUUID())
|
2015-07-09 11:56:19 +00:00
|
|
|
testHostIP(framework.Client, framework.Namespace.Name, &api.Pod{
|
2015-04-10 20:00:07 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Image: "gcr.io/google_containers/pause",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2015-07-09 11:56:19 +00:00
|
|
|
|
2015-06-09 21:19:21 +00:00
|
|
|
It("should be schedule with cpu and memory limits", func() {
|
2015-07-09 11:56:19 +00:00
|
|
|
podClient := framework.Client.Pods(framework.Namespace.Name)
|
2015-06-09 21:19:21 +00:00
|
|
|
|
|
|
|
By("creating the pod")
|
|
|
|
name := "pod-update-" + string(util.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: "gcr.io/google_containers/pause",
|
|
|
|
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 {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Error creating a pod: %v", err)
|
2015-06-09 21:19:21 +00:00
|
|
|
}
|
2015-07-09 11:56:19 +00:00
|
|
|
expectNoError(framework.WaitForPodRunning(pod.Name))
|
2015-06-09 21:19:21 +00:00
|
|
|
})
|
2015-07-09 11:56:19 +00:00
|
|
|
|
2015-01-30 21:00:03 +00:00
|
|
|
It("should be submitted and removed", func() {
|
2015-07-09 11:56:19 +00:00
|
|
|
podClient := framework.Client.Pods(framework.Namespace.Name)
|
2015-01-30 21:00:03 +00:00
|
|
|
|
2015-02-03 20:29:23 +00:00
|
|
|
By("creating the pod")
|
|
|
|
name := "pod-update-" + string(util.NewUUID())
|
2015-01-30 21:00:03 +00:00
|
|
|
value := strconv.Itoa(time.Now().Nanosecond())
|
2015-02-03 20:29:23 +00:00
|
|
|
pod := &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "foo",
|
|
|
|
"time": value,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "nginx",
|
2015-04-02 17:15:20 +00:00
|
|
|
Image: "gcr.io/google_containers/nginx:1.7.9",
|
2015-02-23 22:25:56 +00:00
|
|
|
Ports: []api.ContainerPort{{ContainerPort: 80}},
|
2015-02-03 20:29:23 +00:00
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
HTTPGet: &api.HTTPGetAction{
|
|
|
|
Path: "/index.html",
|
|
|
|
Port: util.NewIntOrStringFromInt(8080),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
InitialDelaySeconds: 30,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-01-30 21:00:03 +00:00
|
|
|
|
2015-03-10 08:41:42 +00:00
|
|
|
By("setting up watch")
|
2015-04-20 18:53:06 +00:00
|
|
|
pods, err := podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value})), fields.Everything())
|
2015-03-10 08:41:42 +00:00
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to query for pods: %v", err)
|
2015-03-10 08:41:42 +00:00
|
|
|
}
|
|
|
|
Expect(len(pods.Items)).To(Equal(0))
|
|
|
|
w, err := podClient.Watch(
|
2015-03-15 21:51:41 +00:00
|
|
|
labels.SelectorFromSet(labels.Set(map[string]string{"time": value})), fields.Everything(), pods.ListMeta.ResourceVersion)
|
2015-03-10 08:41:42 +00:00
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to set up watch: %v", err)
|
2015-03-10 08:41:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 21:00:03 +00:00
|
|
|
By("submitting the pod to kubernetes")
|
2015-02-19 23:24:38 +00:00
|
|
|
// We call defer here in case there is a problem with
|
|
|
|
// the test so we can ensure that we clean up after
|
|
|
|
// ourselves
|
2015-06-02 21:40:05 +00:00
|
|
|
defer podClient.Delete(pod.Name, nil)
|
2015-03-10 08:41:42 +00:00
|
|
|
_, err = podClient.Create(pod)
|
2015-01-30 21:00:03 +00:00
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to create pod: %v", err)
|
2015-01-30 21:00:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
By("verifying the pod is in kubernetes")
|
2015-04-20 18:53:06 +00:00
|
|
|
pods, err = podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value})), fields.Everything())
|
2015-01-30 21:00:03 +00:00
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to query for pods: %v", err)
|
2015-01-30 21:00:03 +00:00
|
|
|
}
|
|
|
|
Expect(len(pods.Items)).To(Equal(1))
|
|
|
|
|
2015-06-02 21:40:05 +00:00
|
|
|
By("veryfying pod creation was observed")
|
2015-03-10 08:41:42 +00:00
|
|
|
select {
|
|
|
|
case event, _ := <-w.ResultChan():
|
|
|
|
if event.Type != watch.Added {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to observe pod creation: %v", event)
|
2015-03-10 08:41:42 +00:00
|
|
|
}
|
|
|
|
case <-time.After(podStartTimeout):
|
|
|
|
Fail("Timeout while waiting for pod creation")
|
|
|
|
}
|
|
|
|
|
2015-06-02 21:40:05 +00:00
|
|
|
By("deleting the pod")
|
2015-08-07 16:40:59 +00:00
|
|
|
if err := podClient.Delete(pod.Name, nil); err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to delete pod: %v", err)
|
2015-03-10 08:41:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 16:40:59 +00:00
|
|
|
By("verifying pod deletion was observed")
|
2015-03-10 08:41:42 +00:00
|
|
|
deleted := false
|
|
|
|
timeout := false
|
|
|
|
timer := time.After(podStartTimeout)
|
|
|
|
for !deleted && !timeout {
|
|
|
|
select {
|
|
|
|
case event, _ := <-w.ResultChan():
|
|
|
|
if event.Type == watch.Deleted {
|
|
|
|
deleted = true
|
|
|
|
}
|
|
|
|
case <-timer:
|
|
|
|
timeout = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !deleted {
|
|
|
|
Fail("Failed to observe pod deletion")
|
|
|
|
}
|
2015-08-07 16:40:59 +00:00
|
|
|
|
|
|
|
pods, err = podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value})), fields.Everything())
|
|
|
|
if err != nil {
|
|
|
|
Fail(fmt.Sprintf("Failed to list pods to verify deletion: %v", err))
|
|
|
|
}
|
|
|
|
Expect(len(pods.Items)).To(Equal(0))
|
2015-01-30 21:00:03 +00:00
|
|
|
})
|
|
|
|
|
2015-01-30 20:59:13 +00:00
|
|
|
It("should be updated", func() {
|
2015-07-09 11:56:19 +00:00
|
|
|
podClient := framework.Client.Pods(framework.Namespace.Name)
|
2015-01-30 20:59:13 +00:00
|
|
|
|
|
|
|
By("creating the pod")
|
|
|
|
name := "pod-update-" + string(util.NewUUID())
|
|
|
|
value := strconv.Itoa(time.Now().Nanosecond())
|
|
|
|
pod := &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: name,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": "foo",
|
|
|
|
"time": value,
|
|
|
|
},
|
2015-01-30 23:25:56 +00:00
|
|
|
},
|
2015-01-30 20:59:13 +00:00
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "nginx",
|
2015-04-02 17:15:20 +00:00
|
|
|
Image: "gcr.io/google_containers/nginx:1.7.9",
|
2015-02-23 22:25:56 +00:00
|
|
|
Ports: []api.ContainerPort{{ContainerPort: 80}},
|
2015-01-30 20:59:13 +00:00
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
HTTPGet: &api.HTTPGetAction{
|
|
|
|
Path: "/index.html",
|
|
|
|
Port: util.NewIntOrStringFromInt(8080),
|
|
|
|
},
|
2015-01-30 23:25:56 +00:00
|
|
|
},
|
2015-01-30 20:59:13 +00:00
|
|
|
InitialDelaySeconds: 30,
|
2015-01-30 23:25:56 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-01-30 20:59:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
By("submitting the pod to kubernetes")
|
|
|
|
defer func() {
|
|
|
|
By("deleting the pod")
|
2015-06-02 21:40:05 +00:00
|
|
|
podClient.Delete(pod.Name, nil)
|
2015-01-30 20:59:13 +00:00
|
|
|
}()
|
2015-04-21 00:42:17 +00:00
|
|
|
pod, err := podClient.Create(pod)
|
2015-02-19 23:24:38 +00:00
|
|
|
if err != nil {
|
2015-04-21 00:42:17 +00:00
|
|
|
Failf("Failed to create pod: %v", err)
|
2015-02-19 23:24:38 +00:00
|
|
|
}
|
2015-01-30 20:59:13 +00:00
|
|
|
|
2015-07-09 11:56:19 +00:00
|
|
|
expectNoError(framework.WaitForPodRunning(pod.Name))
|
2015-01-30 20:59:13 +00:00
|
|
|
|
|
|
|
By("verifying the pod is in kubernetes")
|
2015-04-20 18:53:06 +00:00
|
|
|
pods, err := podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value})), fields.Everything())
|
2015-01-30 20:59:13 +00:00
|
|
|
Expect(len(pods.Items)).To(Equal(1))
|
|
|
|
|
2015-04-21 00:42:17 +00:00
|
|
|
// Standard get, update retry loop
|
|
|
|
expectNoError(wait.Poll(time.Millisecond*500, time.Second*30, func() (bool, error) {
|
|
|
|
By("updating the pod")
|
|
|
|
value = strconv.Itoa(time.Now().Nanosecond())
|
|
|
|
if pod == nil { // on retries we need to re-get
|
|
|
|
pod, err = podClient.Get(name)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed to get pod: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pod.Labels["time"] = value
|
|
|
|
pod, err = podClient.Update(pod)
|
|
|
|
if err == nil {
|
|
|
|
Logf("Successfully updated pod")
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if errors.IsConflict(err) {
|
|
|
|
Logf("Conflicting update to pod, re-get and re-update: %v", err)
|
|
|
|
pod = nil // re-get it when we retry
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("failed to update pod: %v", err)
|
|
|
|
}))
|
2015-01-30 20:59:13 +00:00
|
|
|
|
2015-07-09 11:56:19 +00:00
|
|
|
expectNoError(framework.WaitForPodRunning(pod.Name))
|
2015-01-30 20:59:13 +00:00
|
|
|
|
|
|
|
By("verifying the updated pod is in kubernetes")
|
2015-04-20 18:53:06 +00:00
|
|
|
pods, err = podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value})), fields.Everything())
|
2015-01-30 20:59:13 +00:00
|
|
|
Expect(len(pods.Items)).To(Equal(1))
|
2015-04-21 00:42:17 +00:00
|
|
|
Logf("Pod update OK")
|
2015-01-28 00:38:48 +00:00
|
|
|
})
|
2015-02-02 14:11:06 +00:00
|
|
|
|
|
|
|
It("should contain environment variables for services", func() {
|
|
|
|
// Make a pod that will be a service.
|
|
|
|
// This pod serves its hostname via HTTP.
|
2015-02-03 18:43:14 +00:00
|
|
|
serverName := "server-envvars-" + string(util.NewUUID())
|
|
|
|
serverPod := &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: serverName,
|
|
|
|
Labels: map[string]string{"name": serverName},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "srv",
|
2015-04-01 00:03:44 +00:00
|
|
|
Image: "gcr.io/google_containers/serve_hostname:1.1",
|
2015-02-23 22:25:56 +00:00
|
|
|
Ports: []api.ContainerPort{{ContainerPort: 9376}},
|
2015-02-03 18:43:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-07-09 11:56:19 +00:00
|
|
|
defer framework.Client.Pods(framework.Namespace.Name).Delete(serverPod.Name, nil)
|
|
|
|
_, err := framework.Client.Pods(framework.Namespace.Name).Create(serverPod)
|
2015-02-02 14:11:06 +00:00
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to create serverPod: %v", err)
|
2015-02-02 14:11:06 +00:00
|
|
|
}
|
2015-07-09 11:56:19 +00:00
|
|
|
expectNoError(framework.WaitForPodRunning(serverPod.Name))
|
2015-02-02 14:11:06 +00:00
|
|
|
|
2015-02-03 18:43:14 +00:00
|
|
|
// 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:
|
|
|
|
// svcName := "svc-envvars-" + randomSuffix()
|
|
|
|
// However, that affects the name of the environment variables which are the capitalized
|
|
|
|
// service name, so that breaks this test. One possibility is to tweak the variable names
|
|
|
|
// to match the service. Another is to rethink environment variable names and possibly
|
|
|
|
// allow overriding the prefix in the service manifest.
|
|
|
|
svcName := "fooservice"
|
|
|
|
svc := &api.Service{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: svcName,
|
|
|
|
Labels: map[string]string{
|
|
|
|
"name": svcName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Spec: api.ServiceSpec{
|
2015-03-13 15:16:41 +00:00
|
|
|
Ports: []api.ServicePort{{
|
|
|
|
Port: 8765,
|
|
|
|
TargetPort: util.NewIntOrStringFromInt(8080),
|
|
|
|
}},
|
2015-02-03 18:43:14 +00:00
|
|
|
Selector: map[string]string{
|
|
|
|
"name": serverName,
|
|
|
|
},
|
|
|
|
},
|
2015-02-02 14:11:06 +00:00
|
|
|
}
|
2015-07-09 11:56:19 +00:00
|
|
|
defer framework.Client.Services(framework.Namespace.Name).Delete(svc.Name)
|
|
|
|
_, err = framework.Client.Services(framework.Namespace.Name).Create(svc)
|
2015-02-02 14:11:06 +00:00
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to create service: %v", err)
|
2015-02-02 14:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make a client pod that verifies that it has the service environment variables.
|
2015-03-31 17:25:14 +00:00
|
|
|
podName := "client-envvars-" + string(util.NewUUID())
|
|
|
|
pod := &api.Pod{
|
2015-02-03 18:43:14 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
2015-03-31 17:25:14 +00:00
|
|
|
Name: podName,
|
|
|
|
Labels: map[string]string{"name": podName},
|
2015-02-03 18:43:14 +00:00
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "env3cont",
|
2015-03-31 17:25:20 +00:00
|
|
|
Image: "gcr.io/google_containers/busybox",
|
2015-03-31 17:25:14 +00:00
|
|
|
Command: []string{"sh", "-c", "env"},
|
2015-02-03 18:43:14 +00:00
|
|
|
},
|
|
|
|
},
|
2015-03-14 01:38:07 +00:00
|
|
|
RestartPolicy: api.RestartPolicyNever,
|
2015-02-03 18:43:14 +00:00
|
|
|
},
|
|
|
|
}
|
2015-02-02 14:11:06 +00:00
|
|
|
|
2015-07-09 11:56:19 +00:00
|
|
|
framework.TestContainerOutput("service env", pod, 0, []string{
|
2015-02-02 14:11:06 +00:00
|
|
|
"FOOSERVICE_SERVICE_HOST=",
|
|
|
|
"FOOSERVICE_SERVICE_PORT=",
|
|
|
|
"FOOSERVICE_PORT=",
|
|
|
|
"FOOSERVICE_PORT_8765_TCP_PORT=",
|
|
|
|
"FOOSERVICE_PORT_8765_TCP_PROTO=",
|
|
|
|
"FOOSERVICE_PORT_8765_TCP=",
|
|
|
|
"FOOSERVICE_PORT_8765_TCP_ADDR=",
|
2015-03-31 17:25:14 +00:00
|
|
|
})
|
2015-02-02 14:11:06 +00:00
|
|
|
})
|
2015-02-10 14:03:21 +00:00
|
|
|
|
|
|
|
It("should be restarted with a docker exec \"cat /tmp/health\" liveness probe", func() {
|
2015-07-09 11:56:19 +00:00
|
|
|
runLivenessTest(framework.Client, framework.Namespace.Name, &api.Pod{
|
2015-02-10 14:03:21 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "liveness-exec",
|
|
|
|
Labels: map[string]string{"test": "liveness"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "liveness",
|
2015-03-31 17:25:20 +00:00
|
|
|
Image: "gcr.io/google_containers/busybox",
|
2015-05-08 16:48:31 +00:00
|
|
|
Command: []string{"/bin/sh", "-c", "echo ok >/tmp/health; sleep 10; rm -rf /tmp/health; sleep 600"},
|
2015-02-10 14:03:21 +00:00
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{
|
|
|
|
Command: []string{"cat", "/tmp/health"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
InitialDelaySeconds: 15,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-05-04 23:19:36 +00:00
|
|
|
}, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should *not* be restarted with a docker exec \"cat /tmp/health\" liveness probe", func() {
|
2015-07-09 11:56:19 +00:00
|
|
|
runLivenessTest(framework.Client, framework.Namespace.Name, &api.Pod{
|
2015-05-04 23:19:36 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "liveness-exec",
|
|
|
|
Labels: map[string]string{"test": "liveness"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "liveness",
|
|
|
|
Image: "gcr.io/google_containers/busybox",
|
|
|
|
Command: []string{"/bin/sh", "-c", "echo ok >/tmp/health; sleep 600"},
|
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
Exec: &api.ExecAction{
|
|
|
|
Command: []string{"cat", "/tmp/health"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
InitialDelaySeconds: 15,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, false)
|
2015-02-10 14:03:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should be restarted with a /healthz http liveness probe", func() {
|
2015-07-09 11:56:19 +00:00
|
|
|
runLivenessTest(framework.Client, framework.Namespace.Name, &api.Pod{
|
2015-02-10 14:03:21 +00:00
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "liveness-http",
|
|
|
|
Labels: map[string]string{"test": "liveness"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "liveness",
|
2015-03-31 17:25:20 +00:00
|
|
|
Image: "gcr.io/google_containers/liveness",
|
2015-02-10 14:03:21 +00:00
|
|
|
Command: []string{"/server"},
|
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
HTTPGet: &api.HTTPGetAction{
|
|
|
|
Path: "/healthz",
|
|
|
|
Port: util.NewIntOrStringFromInt(8080),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
InitialDelaySeconds: 15,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-05-04 23:19:36 +00:00
|
|
|
}, true)
|
2015-02-10 14:03:21 +00:00
|
|
|
})
|
2015-01-08 20:41:38 +00:00
|
|
|
|
2015-08-07 03:59:37 +00:00
|
|
|
It("should *not* be restarted with a /healthz http liveness probe", func() {
|
|
|
|
runLivenessTest(framework.Client, framework.Namespace.Name, &api.Pod{
|
|
|
|
ObjectMeta: api.ObjectMeta{
|
|
|
|
Name: "liveness-http",
|
|
|
|
Labels: map[string]string{"test": "liveness"},
|
|
|
|
},
|
|
|
|
Spec: api.PodSpec{
|
|
|
|
Containers: []api.Container{
|
|
|
|
{
|
|
|
|
Name: "liveness",
|
|
|
|
Image: "gcr.io/google_containers/nettest:1.6",
|
|
|
|
// These args are garbage but the image will exit if they're not there
|
|
|
|
// we just care about /read serving a 200, which it always does.
|
|
|
|
Args: []string{
|
|
|
|
"-service=liveness-http",
|
|
|
|
"-peers=1",
|
|
|
|
"-namespace=" + framework.Namespace.Name},
|
|
|
|
Ports: []api.ContainerPort{{ContainerPort: 8080}},
|
|
|
|
LivenessProbe: &api.Probe{
|
|
|
|
Handler: api.Handler{
|
|
|
|
HTTPGet: &api.HTTPGetAction{
|
|
|
|
Path: "/read",
|
|
|
|
Port: util.NewIntOrStringFromInt(8080),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
InitialDelaySeconds: 15,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, false)
|
|
|
|
})
|
|
|
|
|
2015-01-08 20:41:38 +00:00
|
|
|
// The following tests for remote command execution and port forwarding are
|
|
|
|
// commented out because the GCE environment does not currently have nsenter
|
|
|
|
// in the kubelet's PATH, nor does it have socat installed. Once we figure
|
|
|
|
// out the best way to have nsenter and socat available in GCE (and hopefully
|
|
|
|
// all providers), we can enable these tests.
|
|
|
|
/*
|
|
|
|
It("should support remote command execution", func() {
|
|
|
|
clientConfig, err := loadConfig()
|
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to create client config: %v", err)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 11:56:19 +00:00
|
|
|
podClient := framework.Client.Pods(framework.Namespace.Name)
|
2015-01-08 20:41:38 +00:00
|
|
|
|
|
|
|
By("creating the pod")
|
|
|
|
name := "pod-exec-" + string(util.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",
|
2015-06-16 18:49:36 +00:00
|
|
|
Image: "gcr.io/google_containers/nginx:1.7.9",
|
2015-01-08 20:41:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
By("submitting the pod to kubernetes")
|
|
|
|
_, err = podClient.Create(pod)
|
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to create pod: %v", err)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
// We call defer here in case there is a problem with
|
|
|
|
// the test so we can ensure that we clean up after
|
|
|
|
// ourselves
|
2015-06-02 21:40:05 +00:00
|
|
|
podClient.Delete(pod.Name)
|
2015-01-08 20:41:38 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
By("waiting for the pod to start running")
|
2015-07-09 11:56:19 +00:00
|
|
|
expectNoError(framework.WaitForPodRunning(pod.Name))
|
2015-01-08 20:41:38 +00:00
|
|
|
|
|
|
|
By("verifying the pod is in kubernetes")
|
|
|
|
pods, err := podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value})))
|
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to query for pods: %v", err)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
Expect(len(pods.Items)).To(Equal(1))
|
|
|
|
|
|
|
|
pod = &pods.Items[0]
|
|
|
|
By(fmt.Sprintf("executing command on host %s pod %s in container %s",
|
|
|
|
pod.Status.Host, pod.Name, pod.Spec.Containers[0].Name))
|
2015-07-09 11:56:19 +00:00
|
|
|
req := framework.Client.Get().
|
2015-01-08 20:41:38 +00:00
|
|
|
Prefix("proxy").
|
|
|
|
Resource("minions").
|
|
|
|
Name(pod.Status.Host).
|
2015-07-09 11:56:19 +00:00
|
|
|
Suffix("exec", framework.Namespace.Name, pod.Name, pod.Spec.Containers[0].Name)
|
2015-01-08 20:41:38 +00:00
|
|
|
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
e := remotecommand.New(req, clientConfig, []string{"whoami"}, nil, out, nil, false)
|
|
|
|
err = e.Execute()
|
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to execute command on host %s pod %s in container %s: %v",
|
|
|
|
pod.Status.Host, pod.Name, pod.Spec.Containers[0].Name, err)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
if e, a := "root\n", out.String(); e != a {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("exec: whoami: expected '%s', got '%s'", e, a)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should support port forwarding", func() {
|
|
|
|
clientConfig, err := loadConfig()
|
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to create client config: %v", err)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 11:56:19 +00:00
|
|
|
podClient := framework.Client.Pods(framework.Namespace.Name)
|
2015-01-08 20:41:38 +00:00
|
|
|
|
|
|
|
By("creating the pod")
|
|
|
|
name := "pod-portforward-" + string(util.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",
|
2015-06-16 18:49:36 +00:00
|
|
|
Image: "gcr.io/google_containers/nginx:1.7.9",
|
2015-01-08 20:41:38 +00:00
|
|
|
Ports: []api.Port{{ContainerPort: 80}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
By("submitting the pod to kubernetes")
|
|
|
|
_, err = podClient.Create(pod)
|
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to create pod: %v", err)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
// We call defer here in case there is a problem with
|
|
|
|
// the test so we can ensure that we clean up after
|
|
|
|
// ourselves
|
2015-06-02 21:40:05 +00:00
|
|
|
podClient.Delete(pod.Name)
|
2015-01-08 20:41:38 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
By("waiting for the pod to start running")
|
2015-07-09 11:56:19 +00:00
|
|
|
expectNoError(framework.WaitForPodRunning(pod.Name))
|
2015-01-08 20:41:38 +00:00
|
|
|
|
|
|
|
By("verifying the pod is in kubernetes")
|
|
|
|
pods, err := podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value})))
|
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Failed to query for pods: %v", err)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
Expect(len(pods.Items)).To(Equal(1))
|
|
|
|
|
|
|
|
pod = &pods.Items[0]
|
|
|
|
By(fmt.Sprintf("initiating port forwarding to host %s pod %s in container %s",
|
|
|
|
pod.Status.Host, pod.Name, pod.Spec.Containers[0].Name))
|
|
|
|
|
2015-07-09 11:56:19 +00:00
|
|
|
req := framework.Client.Get().
|
2015-01-08 20:41:38 +00:00
|
|
|
Prefix("proxy").
|
|
|
|
Resource("minions").
|
|
|
|
Name(pod.Status.Host).
|
2015-07-09 11:56:19 +00:00
|
|
|
Suffix("portForward", framework.Namespace.Name, pod.Name)
|
2015-01-08 20:41:38 +00:00
|
|
|
|
|
|
|
stopChan := make(chan struct{})
|
|
|
|
pf, err := portforward.New(req, clientConfig, []string{"5678:80"}, stopChan)
|
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Error creating port forwarder: %s", err)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
errorChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errorChan <- pf.ForwardPorts()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait for listeners to start
|
|
|
|
<-pf.Ready
|
|
|
|
|
|
|
|
resp, err := http.Get("http://localhost:5678/")
|
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Error with http get to localhost:5678: %s", err)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("Error reading response body: %s", err)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
titleRegex := regexp.MustCompile("<title>(.+)</title>")
|
|
|
|
matches := titleRegex.FindStringSubmatch(string(body))
|
|
|
|
if len(matches) != 2 {
|
|
|
|
Fail("Unable to locate page title in response HTML")
|
|
|
|
}
|
|
|
|
if e, a := "Welcome to nginx on Debian!", matches[1]; e != a {
|
2015-07-23 23:03:34 +00:00
|
|
|
Failf("<title>: expected '%s', got '%s'", e, a)
|
2015-01-08 20:41:38 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
*/
|
2015-01-28 00:38:48 +00:00
|
|
|
})
|