// StartPods check for numPods in TestNS. If they exist, it no-ops, otherwise it starts up // a temp rc, scales it to match numPods, then deletes the rc leaving behind the pods. func StartPods(numPods int, host string, restClient *client.Client) error { start := time.Now() defer func() { glog.Infof("StartPods took %v with numPods %d", time.Since(start), numPods) }() hostField := fields.OneTermEqualSelector(client.PodHost, host) pods, err := restClient.Pods(TestNS).List(labels.Everything(), hostField) if err != nil || len(pods.Items) == numPods { return err } glog.Infof("Found %d pods that match host %v, require %d", len(pods.Items), hostField, numPods) // For the sake of simplicity, assume all pods in TestNS have selectors matching TestRCManifest. controller := RCFromManifest(TestRCManifest) // Make the rc unique to the given host. controller.Spec.Replicas = numPods controller.Spec.Template.Spec.NodeName = host controller.Name = controller.Name + host controller.Spec.Selector["host"] = host controller.Spec.Template.Labels["host"] = host if rc, err := StartRC(controller, restClient); err != nil { return err } else { // Delete the rc, otherwise when we restart master components for the next benchmark // the rc controller will race with the pods controller in the rc manager. return restClient.ReplicationControllers(TestNS).Delete(rc.Name) } }
// Describer returns the default describe functions for each of the standard // Kubernetes types. func DescriberFor(kind string, c *client.Client) (Describer, bool) { switch kind { case "Pod": return &PodDescriber{ PodClient: func(namespace string) (client.PodInterface, error) { return c.Pods(namespace), nil }, ReplicationControllerClient: func(namespace string) (client.ReplicationControllerInterface, error) { return c.ReplicationControllers(namespace), nil }, }, true case "ReplicationController": return &ReplicationControllerDescriber{ PodClient: func(namespace string) (client.PodInterface, error) { return c.Pods(namespace), nil }, ReplicationControllerClient: func(namespace string) (client.ReplicationControllerInterface, error) { return c.ReplicationControllers(namespace), nil }, }, true case "Service": return &ServiceDescriber{ ServiceClient: func(namespace string) (client.ServiceInterface, error) { return c.Services(namespace), nil }, }, true } return nil, false }
func runStaticPodTest(c *client.Client, configFilePath string) { manifest := `version: v1beta2 id: static-pod containers: - name: static-container image: kubernetes/pause` manifestFile, err := ioutil.TempFile(configFilePath, "") defer os.Remove(manifestFile.Name()) ioutil.WriteFile(manifestFile.Name(), []byte(manifest), 0600) // Wait for the mirror pod to be created. hostname, _ := os.Hostname() podName := fmt.Sprintf("static-pod-%s", hostname) namespace := kubelet.NamespaceDefault if err := wait.Poll(time.Second, time.Second*30, podRunning(c, namespace, podName)); err != nil { glog.Fatalf("FAILED: mirror pod has not been created or is not running: %v", err) } // Delete the mirror pod, and wait for it to be recreated. c.Pods(namespace).Delete(podName) if err = wait.Poll(time.Second, time.Second*30, podRunning(c, namespace, podName)); err != nil { glog.Fatalf("FAILED: mirror pod has not been re-created or is not running: %v", err) } // Remove the manifest file, and wait for the mirror pod to be deleted. os.Remove(manifestFile.Name()) if err = wait.Poll(time.Second, time.Second*30, podNotFound(c, namespace, podName)); err != nil { glog.Fatalf("FAILED: mirror pod has not been deleted: %v", err) } }
// waitForPodSuccess returns true if the pod reached state success, or false if it reached failure or ran too long. func waitForPodSuccess(c *client.Client, podName string, contName string) bool { for i := 0; i < 10; i++ { if i > 0 { time.Sleep(5 * time.Second) } pod, err := c.Pods(api.NamespaceDefault).Get(podName) if err != nil { glog.Warningf("Get pod failed: %v", err) continue } // Cannot use pod.Status.Phase == api.PodSucceeded/api.PodFailed due to #2632 ci, ok := pod.Status.Info[contName] if !ok { glog.Infof("No Status.Info for container %s in pod %s yet", contName, podName) } else { if ci.State.Termination != nil { if ci.State.Termination.ExitCode == 0 { glog.Infof("Saw pod success") return true } else { glog.Infof("Saw pod failure: %+v", ci.State.Termination) } glog.Infof("Waiting for pod %q status to be success or failure", podName) } else { glog.Infof("Nil State.Termination for container %s in pod %s so far", contName, podName) } } } glog.Warningf("Gave up waiting for pod %q status to be success or failure", podName) return false }
// testHostIP tests that a pod gets a host IP func testHostIP(c *client.Client, pod *api.Pod) { ns := "e2e-test-" + string(util.NewUUID()) podClient := c.Pods(ns) By("creating pod") defer podClient.Delete(pod.Name) _, err := podClient.Create(pod) if err != nil { Fail(fmt.Sprintf("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 = waitForPodRunningInNamespace(c, pod.Name, ns) Expect(err).NotTo(HaveOccurred()) // Try to make sure we get a hostIP for each pod. hostIPTimeout := 2 * time.Minute t := time.Now() for { p, err := podClient.Get(pod.Name) Expect(err).NotTo(HaveOccurred()) 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()) } Logf("Retrying to get the hostIP of pod %s", p.Name) time.Sleep(5 * time.Second) } }
func runReplicationControllerTest(c *client.Client) { data, err := ioutil.ReadFile("api/examples/controller.json") if err != nil { glog.Fatalf("Unexpected error: %v", err) } var controller api.ReplicationController if err := api.Scheme.DecodeInto(data, &controller); err != nil { glog.Fatalf("Unexpected error: %v", err) } glog.Infof("Creating replication controllers") if _, err := c.ReplicationControllers(api.NamespaceDefault).Create(&controller); err != nil { glog.Fatalf("Unexpected error: %v", err) } glog.Infof("Done creating replication controllers") // Give the controllers some time to actually create the pods if err := wait.Poll(time.Second, time.Second*30, client.ControllerHasDesiredReplicas(c, &controller)); err != nil { glog.Fatalf("FAILED: pods never created %v", err) } // wait for minions to indicate they have info about the desired pods pods, err := c.Pods(api.NamespaceDefault).List(labels.Set(controller.Spec.Selector).AsSelector()) if err != nil { glog.Fatalf("FAILED: unable to get pods to list: %v", err) } if err := wait.Poll(time.Second, time.Second*30, podsOnMinions(c, *pods)); err != nil { glog.Fatalf("FAILED: pods never started running %v", err) } glog.Infof("Pods created") }
func verifyExpectedRcsExistAndGetExpectedPods(c *client.Client) ([]string, error) { expectedPods := []string{} // Iterate over the labels that identify the replication controllers that we // want to check. The rcLabels contains the value values for the k8s-app key // that identify the replication controllers that we want to check. Using a label // rather than an explicit name is preferred because the names will typically have // a version suffix e.g. heapster-monitoring-v1 and this will change after a rolling // update e.g. to heapster-monitoring-v2. By using a label query we can check for the // situaiton when a heapster-monitoring-v1 and heapster-monitoring-v2 replication controller // is running (which would be an error except during a rolling update). for _, rcLabel := range rcLabels { rcList, err := c.ReplicationControllers(api.NamespaceDefault).List(labels.Set{"k8s-app": rcLabel}.AsSelector()) if err != nil { return nil, err } if len(rcList.Items) != 1 { return nil, fmt.Errorf("expected to find one replica for RC with label %s but got %d", rcLabel, len(rcList.Items)) } for _, rc := range rcList.Items { podList, err := c.Pods(api.NamespaceDefault).List(labels.Set(rc.Spec.Selector).AsSelector(), fields.Everything()) if err != nil { return nil, err } for _, pod := range podList.Items { expectedPods = append(expectedPods, string(pod.UID)) } } } return expectedPods, nil }
func verifyExpectedRcsExistAndGetExpectedPods(c *client.Client) ([]string, error) { rcList, err := c.ReplicationControllers(api.NamespaceDefault).List(labels.Everything()) if err != nil { return nil, err } expectedPods := []string{} for _, rc := range rcList.Items { if _, ok := expectedRcs[rc.Name]; ok { if rc.Status.Replicas != 1 { return nil, fmt.Errorf("expected to find only one replica for rc %q, found %d", rc.Name, rc.Status.Replicas) } expectedRcs[rc.Name] = true podList, err := c.Pods(api.NamespaceDefault).List(labels.Set(rc.Spec.Selector).AsSelector(), fields.Everything()) if err != nil { return nil, err } for _, pod := range podList.Items { expectedPods = append(expectedPods, string(pod.UID)) } } } for rc, found := range expectedRcs { if !found { return nil, fmt.Errorf("Replication Controller %q not found.", rc) } } return expectedPods, nil }
func newPodOnNode(c *client.Client, namespace, podName, nodeName string) error { pod, err := c.Pods(namespace).Create(podOnNode(podName, nodeName, serveHostnameImage)) if err == nil { Logf("Created pod %s on node %s", pod.ObjectMeta.Name, nodeName) } else { Logf("Failed to create pod %s on node %s: %v", podName, nodeName, err) } return err }
func runSchedulerNoPhantomPodsTest(client *client.Client) { pod := &api.Pod{ Spec: api.PodSpec{ Containers: []api.Container{ { Name: "c1", Image: "kubernetes/pause", Ports: []api.ContainerPort{ {ContainerPort: 1234, HostPort: 9999}, }, ImagePullPolicy: api.PullIfNotPresent, }, }, }, } // Assuming we only have two kublets, the third pod here won't schedule // if the scheduler doesn't correctly handle the delete for the second // pod. pod.ObjectMeta.Name = "phantom.foo" foo, err := client.Pods(api.NamespaceDefault).Create(pod) if err != nil { glog.Fatalf("Failed to create pod: %v, %v", pod, err) } if err := wait.Poll(time.Second, time.Second*30, podRunning(client, foo.Namespace, foo.Name)); err != nil { glog.Fatalf("FAILED: pod never started running %v", err) } pod.ObjectMeta.Name = "phantom.bar" bar, err := client.Pods(api.NamespaceDefault).Create(pod) if err != nil { glog.Fatalf("Failed to create pod: %v, %v", pod, err) } if err := wait.Poll(time.Second, time.Second*30, podRunning(client, bar.Namespace, bar.Name)); err != nil { glog.Fatalf("FAILED: pod never started running %v", err) } // Delete a pod to free up room. glog.Infof("Deleting pod %v", bar.Name) err = client.Pods(api.NamespaceDefault).Delete(bar.Name, nil) if err != nil { glog.Fatalf("FAILED: couldn't delete pod %q: %v", bar.Name, err) } pod.ObjectMeta.Name = "phantom.baz" baz, err := client.Pods(api.NamespaceDefault).Create(pod) if err != nil { glog.Fatalf("Failed to create pod: %v, %v", pod, err) } if err := wait.Poll(time.Second, time.Second*60, podRunning(client, baz.Namespace, baz.Name)); err != nil { glog.Fatalf("FAILED: (Scheduler probably didn't process deletion of 'phantom.bar') Pod never started running: %v", err) } glog.Info("Scheduler doesn't make phantom pods: test passed.") }
// Convenient wrapper around listing pods supporting retries. func listPods(c *client.Client, namespace string, label labels.Selector, field fields.Selector) (*api.PodList, error) { maxRetries := 4 pods, err := c.Pods(namespace).List(label, field) for i := 0; i < maxRetries; i++ { if err == nil { return pods, nil } pods, err = c.Pods(namespace).List(label, field) } return pods, err }
func podRunning(c *client.Client, podNamespace string, podID string) wait.ConditionFunc { return func() (bool, error) { pod, err := c.Pods(podNamespace).Get(podID) if err != nil { return false, err } if pod.Status.Phase != api.PodRunning { return false, errors.New(fmt.Sprintf("Pod status is %q", pod.Status.Phase)) } return true, nil } }
func validateIPsOrFail(c *client.Client, ns string, expectedPods []string, ips util.StringSet) { for _, name := range expectedPods { pod, err := c.Pods(ns).Get(name) if err != nil { Failf("failed to get pod %s, that's pretty weird. validation failed: %s", name, err) } if !ips.Has(pod.Status.PodIP) { Failf("ip validation failed, expected: %v, saw: %v", ips, pod.Status.PodIP) } By(fmt.Sprintf("")) } By(fmt.Sprintf("successfully validated IPs %v against expected endpoints %v on namespace %s", ips, expectedPods, ns)) }
func runBuildRunningPodDeleteTest(t *testing.T, clusterAdminClient *client.Client, clusterAdminKubeClient *kclient.Client) { buildWatch, err := clusterAdminClient.Builds(testutil.Namespace()).Watch(labels.Everything(), fields.Everything(), "0") if err != nil { t.Fatalf("Couldn't subscribe to Builds %v", err) } defer buildWatch.Stop() created, err := clusterAdminClient.Builds(testutil.Namespace()).Create(mockBuild()) if err != nil { t.Fatalf("Couldn't create Build: %v", err) } podWatch, err := clusterAdminKubeClient.Pods(testutil.Namespace()).Watch(labels.Everything(), fields.Everything(), created.ResourceVersion) if err != nil { t.Fatalf("Couldn't subscribe to Pods %v", err) } defer podWatch.Stop() // wait for initial build event from the creation of the imagerepo with tag latest event := waitForWatch(t, "initial build added", buildWatch) if e, a := watchapi.Added, event.Type; e != a { t.Fatalf("expected watch event type %s, got %s", e, a) } newBuild := event.Object.(*buildapi.Build) // initial pod creation for build event = waitForWatch(t, "build pod created", podWatch) if e, a := watchapi.Added, event.Type; e != a { t.Fatalf("expected watch event type %s, got %s", e, a) } event = waitForWatch(t, "build updated to pending", buildWatch) if e, a := watchapi.Modified, event.Type; e != a { t.Fatalf("expected watch event type %s, got %s", e, a) } newBuild = event.Object.(*buildapi.Build) if newBuild.Status.Phase != buildapi.BuildPhasePending { t.Fatalf("expected build status to be marked pending, but was marked %s", newBuild.Status.Phase) } clusterAdminKubeClient.Pods(testutil.Namespace()).Delete(buildutil.GetBuildPodName(newBuild), kapi.NewDeleteOptions(0)) event = waitForWatch(t, "build updated to error", buildWatch) if e, a := watchapi.Modified, event.Type; e != a { t.Fatalf("expected watch event type %s, got %s", e, a) } newBuild = event.Object.(*buildapi.Build) if newBuild.Status.Phase != buildapi.BuildPhaseError { t.Fatalf("expected build status to be marked error, but was marked %s", newBuild.Status.Phase) } }
func translatePodNameToIpOrFail(c *client.Client, ns string, expectedEndpoints map[string][]int) map[string][]int { portsByIp := make(map[string][]int) for name, portList := range expectedEndpoints { pod, err := c.Pods(ns).Get(name) if err != nil { Failf("failed to get pod %s, that's pretty weird. validation failed: %s", name, err) } portsByIp[pod.Status.PodIP] = portList By(fmt.Sprintf("")) } By(fmt.Sprintf("successfully translated pod names to ips: %v -> %v on namespace %s", expectedEndpoints, portsByIp, ns)) return portsByIp }
func waitForPodRunning(c *client.Client, id string) { for { time.Sleep(5 * time.Second) pod, err := c.Pods(api.NamespaceDefault).Get(id) if err != nil { glog.Warningf("Get pod failed: %v", err) continue } if pod.Status.Phase == api.PodRunning { break } glog.Infof("Waiting for pod status to be %q (found %q)", api.PodRunning, pod.Status.Phase) } }
func doServiceAccountAPIRequests(t *testing.T, c *client.Client, ns string, authenticated bool, canRead bool, canWrite bool) { testSecret := &api.Secret{ ObjectMeta: api.ObjectMeta{Name: "testSecret"}, Data: map[string][]byte{"test": []byte("data")}, } readOps := []testOperation{ func() error { _, err := c.Secrets(ns).List(labels.Everything(), fields.Everything()); return err }, func() error { _, err := c.Pods(ns).List(labels.Everything(), fields.Everything()); return err }, } writeOps := []testOperation{ func() error { _, err := c.Secrets(ns).Create(testSecret); return err }, func() error { return c.Secrets(ns).Delete(testSecret.Name) }, } for _, op := range readOps { err := op() unauthorizedError := errors.IsUnauthorized(err) forbiddenError := errors.IsForbidden(err) switch { case !authenticated && !unauthorizedError: t.Fatalf("expected unauthorized error, got %v", err) case authenticated && unauthorizedError: t.Fatalf("unexpected unauthorized error: %v", err) case authenticated && canRead && forbiddenError: t.Fatalf("unexpected forbidden error: %v", err) case authenticated && !canRead && !forbiddenError: t.Fatalf("expected forbidden error, got: %v", err) } } for _, op := range writeOps { err := op() unauthorizedError := errors.IsUnauthorized(err) forbiddenError := errors.IsForbidden(err) switch { case !authenticated && !unauthorizedError: t.Fatalf("expected unauthorized error, got %v", err) case authenticated && unauthorizedError: t.Fatalf("unexpected unauthorized error: %v", err) case authenticated && canWrite && forbiddenError: t.Fatalf("unexpected forbidden error: %v", err) case authenticated && !canWrite && !forbiddenError: t.Fatalf("expected forbidden error, got: %v", err) } } }
func podScheduled(c *client.Client, podNamespace, podName string) wait.ConditionFunc { return func() (bool, error) { pod, err := c.Pods(podNamespace).Get(podName) if errors.IsNotFound(err) { return false, nil } if err != nil { // This could be a connection error so we want to retry. return false, nil } if pod.Spec.NodeName == "" { return false, nil } return true, nil } }
func podsCreated(c *client.Client, ns, name string, replicas int) (*api.PodList, error) { // List the pods, making sure we observe all the replicas. label := labels.SelectorFromSet(labels.Set(map[string]string{"name": name})) for start := time.Now(); time.Since(start) < time.Minute; time.Sleep(5 * time.Second) { pods, err := c.Pods(ns).List(label, fields.Everything()) if err != nil { return nil, err } Logf("Pod name %s: Found %d pods out of %d", name, len(pods.Items), replicas) if len(pods.Items) == replicas { return pods, nil } } return nil, fmt.Errorf("Pod name %s: Gave up waiting for %d pods to come up", name, replicas) }
func waitForPodCondition(c *client.Client, ns, podName, desc string, condition podCondition) error { By(fmt.Sprintf("waiting up to %v for pod %s status to be %s", podStartTimeout, podName, desc)) for start := time.Now(); time.Since(start) < podStartTimeout; time.Sleep(5 * time.Second) { pod, err := c.Pods(ns).Get(podName) if err != nil { Logf("Get pod failed, ignoring for 5s: %v", err) continue } done, err := condition(pod) if done { return err } Logf("Waiting for pod %s in namespace %s status to be %q (found %q) (%v)", podName, ns, desc, pod.Status.Phase, time.Since(start)) } return fmt.Errorf("gave up waiting for pod %s to be %s after %.2f seconds", podName, desc, podStartTimeout.Seconds()) }
func DoTestUnschedulableNodes(t *testing.T, client *client.Client) { node := &api.Node{ ObjectMeta: api.ObjectMeta{Name: "node"}, Spec: api.NodeSpec{Unschedulable: true}, } if _, err := client.Nodes().Create(node); err != nil { t.Fatalf("Failed to create node: %v", err) } pod := &api.Pod{ ObjectMeta: api.ObjectMeta{Name: "my-pod"}, Spec: api.PodSpec{ Containers: []api.Container{{Name: "container", Image: "kubernetes/pause:go"}}, }, } myPod, err := client.Pods(api.NamespaceDefault).Create(pod) if err != nil { t.Fatalf("Failed to create pod: %v", err) } // There are no schedulable nodes - the pod shouldn't be scheduled. err = wait.Poll(time.Second, time.Second*10, podScheduled(client, myPod.Namespace, myPod.Name)) if err == nil { t.Errorf("Pod scheduled successfully on unschedulable nodes") } if err != wait.ErrWaitTimeout { t.Errorf("Failed while waiting for scheduled pod: %v", err) } // Make the node schedulable and wait until the pod is scheduled. newNode, err := client.Nodes().Get(node.Name) if err != nil { t.Fatalf("Failed to get node: %v", err) } newNode.Spec.Unschedulable = false if _, err = client.Nodes().Update(newNode); err != nil { t.Fatalf("Failed to update node: %v", err) } err = wait.Poll(time.Second, time.Second*10, podScheduled(client, myPod.Namespace, myPod.Name)) if err != nil { t.Errorf("Failed to schedule a pod: %v", err) } err = client.Pods(api.NamespaceDefault).Delete(myPod.Name) if err != nil { t.Errorf("Failed to delete pod: %v", err) } }
func podRunning(c *client.Client, podNamespace string, podName string) wait.ConditionFunc { return func() (bool, error) { pod, err := c.Pods(podNamespace).Get(podName) if apierrors.IsNotFound(err) { return false, nil } if err != nil { // This could be a connection error so we want to retry, but log the error. glog.Errorf("Error when reading pod %q: %v", podName, err) return false, nil } if pod.Status.Phase != api.PodRunning { return false, nil } return true, nil } }
// Starts a container specified by config.serverImage and exports all // config.serverPorts from it. The returned pod should be used to get the server // IP address and create appropriate VolumeSource. func startVolumeServer(client *client.Client, config VolumeTestConfig) *api.Pod { podClient := client.Pods(config.namespace) portCount := len(config.serverPorts) serverPodPorts := make([]api.ContainerPort, portCount) for i := 0; i < portCount; i++ { portName := fmt.Sprintf("%s-%d", config.prefix, i) serverPodPorts[i] = api.ContainerPort{ Name: portName, ContainerPort: config.serverPorts[i], Protocol: api.ProtocolTCP, } } By(fmt.Sprint("creating ", config.prefix, " server pod")) privileged := new(bool) *privileged = true serverPod := &api.Pod{ TypeMeta: api.TypeMeta{ Kind: "Pod", APIVersion: "v1", }, ObjectMeta: api.ObjectMeta{ Name: config.prefix + "-server", Labels: map[string]string{ "role": config.prefix + "-server", }, }, Spec: api.PodSpec{ Containers: []api.Container{ { Name: config.prefix + "-server", Image: config.serverImage, SecurityContext: &api.SecurityContext{ Privileged: privileged, }, Ports: serverPodPorts, }, }, }, } _, err := podClient.Create(serverPod) expectNoError(err, "Failed to create %s pod: %v", serverPod.Name, err) expectNoError(waitForPodRunningInNamespace(client, serverPod.Name, config.namespace)) By("locating the server pod") pod, err := podClient.Get(serverPod.Name) expectNoError(err, "Cannot locate the server pod %v: %v", serverPod.Name, err) By("sleeping a bit to give the server time to start") time.Sleep(20 * time.Second) return pod }
func runBuildDeleteTest(t *testing.T, clusterAdminClient *client.Client, clusterAdminKubeClient *kclient.Client) { buildWatch, err := clusterAdminClient.Builds(testutil.Namespace()).Watch(labels.Everything(), fields.Everything(), "0") if err != nil { t.Fatalf("Couldn't subscribe to Builds %v", err) } defer buildWatch.Stop() created, err := clusterAdminClient.Builds(testutil.Namespace()).Create(mockBuild()) if err != nil { t.Fatalf("Couldn't create Build: %v", err) } podWatch, err := clusterAdminKubeClient.Pods(testutil.Namespace()).Watch(labels.Everything(), fields.Everything(), created.ResourceVersion) if err != nil { t.Fatalf("Couldn't subscribe to Pods %v", err) } defer podWatch.Stop() // wait for initial build event from the creation of the imagerepo with tag latest event := waitForWatch(t, "initial build added", buildWatch) if e, a := watchapi.Added, event.Type; e != a { t.Fatalf("expected watch event type %s, got %s", e, a) } newBuild := event.Object.(*buildapi.Build) // initial pod creation for build event = waitForWatch(t, "build pod created", podWatch) if e, a := watchapi.Added, event.Type; e != a { t.Fatalf("expected watch event type %s, got %s", e, a) } clusterAdminClient.Builds(testutil.Namespace()).Delete(newBuild.Name) event = waitForWatch(t, "pod deleted due to build deleted", podWatch) if e, a := watchapi.Deleted, event.Type; e != a { t.Fatalf("expected watch event type %s, got %s", e, a) } pod := event.Object.(*kapi.Pod) if expected := buildutil.GetBuildPodName(newBuild); pod.Name != expected { t.Fatalf("Expected pod %s to be deleted, but pod %s was deleted", expected, pod.Name) } }
func TestPodUpdate(c *client.Client) bool { podClient := c.Pods(api.NamespaceDefault) pod := loadPodOrDie(assetPath("api", "examples", "pod.json")) value := strconv.Itoa(time.Now().Nanosecond()) pod.Labels["time"] = value _, err := podClient.Create(pod) if err != nil { glog.Errorf("Failed to create pod: %v", err) return false } defer podClient.Delete(pod.Name) waitForPodRunning(c, pod.Name) pods, err := podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))) if len(pods.Items) != 1 { glog.Errorf("Failed to find the correct pod") return false } podOut, err := podClient.Get(pod.Name) if err != nil { glog.Errorf("Failed to get pod: %v", err) return false } value = "time" + value pod.Labels["time"] = value pod.ResourceVersion = podOut.ResourceVersion pod.UID = podOut.UID pod, err = podClient.Update(pod) if err != nil { glog.Errorf("Failed to update pod: %v", err) return false } waitForPodRunning(c, pod.Name) pods, err = podClient.List(labels.SelectorFromSet(labels.Set(map[string]string{"time": value}))) if len(pods.Items) != 1 { glog.Errorf("Failed to find the correct pod after update.") return false } glog.Infof("pod update OK") return true }
func forEachPod(c *client.Client, ns, selectorKey, selectorValue string, fn func(api.Pod)) { var pods *api.PodList var err error for t := time.Now(); time.Since(t) < podListTimeout; time.Sleep(poll) { pods, err = c.Pods(ns).List(labels.SelectorFromSet(labels.Set(map[string]string{selectorKey: selectorValue})), fields.Everything()) Expect(err).NotTo(HaveOccurred()) if len(pods.Items) > 0 { break } } if pods == nil || len(pods.Items) == 0 { Failf("No pods found") } for _, pod := range pods.Items { err = waitForPodRunningInNamespace(c, pod.Name, ns) Expect(err).NotTo(HaveOccurred()) fn(pod) } }
func addEndpointPodOrFail(c *client.Client, ns, name string, labels map[string]string, containerPorts []api.ContainerPort) { By(fmt.Sprintf("Adding pod %v in namespace %v", name, ns)) pod := &api.Pod{ ObjectMeta: api.ObjectMeta{ Name: name, Labels: labels, }, Spec: api.PodSpec{ Containers: []api.Container{ { Name: "test", Image: "gcr.io/google_containers/pause", Ports: containerPorts, }, }, }, } _, err := c.Pods(ns).Create(pod) Expect(err).NotTo(HaveOccurred()) }
// Clean both server and client pods. func volumeTestCleanup(client *client.Client, config VolumeTestConfig) { By(fmt.Sprint("cleaning the environment after ", config.prefix)) defer GinkgoRecover() podClient := client.Pods(config.namespace) // ignore all errors, the pods may not be even created podClient.Delete(config.prefix+"-client", nil) podClient.Delete(config.prefix+"-server", nil) }
func LaunchNetTestPodPerNode(nodes *api.NodeList, name string, c *client.Client, ns string) []string { podNames := []string{} totalPods := len(nodes.Items) Expect(totalPods).NotTo(Equal(0)) for i, node := range nodes.Items { podName := fmt.Sprintf("%s-%d", name, i) podNames = append(podNames, podName) Logf("Creating pod %s on node %s", podName, node.Name) _, err := c.Pods(ns).Create(&api.Pod{ ObjectMeta: api.ObjectMeta{ Name: podName, Labels: map[string]string{ "name": name, }, }, Spec: api.PodSpec{ Containers: []api.Container{ { Name: "webserver", Image: "gcr.io/google_containers/nettest:1.1", Args: []string{ "-service=" + name, //peers >= totalPods should be asserted by the container. //the nettest container finds peers by looking up list of svc endpoints. fmt.Sprintf("-peers=%d", totalPods), "-namespace=" + ns}, Ports: []api.ContainerPort{{ContainerPort: 8080}}, }, }, Host: node.Name, RestartPolicy: api.RestartPolicyNever, }, }) Expect(err).NotTo(HaveOccurred()) } return podNames }
// Simplified version of RunRC, that does not create RC, but creates plain Pods and // requires passing whole Pod definition, which is needed to test various Scheduler predicates. func startPods(c *client.Client, replicas int, ns string, podNamePrefix string, pod api.Pod) { pods, err := c.Pods(api.NamespaceAll).List(labels.Everything(), fields.Everything()) expectNoError(err) podsRunningBefore := len(pods.Items) for i := 0; i < replicas; i++ { podName := fmt.Sprintf("%v-%v", podNamePrefix, i) pod.ObjectMeta.Name = podName pod.ObjectMeta.Labels["name"] = podName pod.Spec.Containers[0].Name = podName _, err = c.Pods(ns).Create(&pod) expectNoError(err) } // Wait for pods to start running. timeout := 2 * time.Minute startTime := time.Now() currentlyRunningPods := 0 for podsRunningBefore+replicas != currentlyRunningPods { allPods, err := c.Pods(api.NamespaceAll).List(labels.Everything(), fields.Everything()) expectNoError(err) runningPods := 0 for _, pod := range allPods.Items { if pod.Status.Phase == api.PodRunning { runningPods += 1 } } currentlyRunningPods = runningPods if startTime.Add(timeout).Before(time.Now()) { break } time.Sleep(5 * time.Second) } Expect(currentlyRunningPods).To(Equal(podsRunningBefore + replicas)) }