func waitForQuota(t *testing.T, quota *api.ResourceQuota, clientset *clientset.Clientset) { w, err := clientset.Core().ResourceQuotas(quota.Namespace).Watch(api.SingleObject(api.ObjectMeta{Name: quota.Name})) if err != nil { t.Fatalf("unexpected error: %v", err) } if _, err := clientset.Core().ResourceQuotas(quota.Namespace).Create(quota); err != nil { t.Fatalf("unexpected error: %v", err) } _, err = watch.Until(1*time.Minute, w, func(event watch.Event) (bool, error) { switch event.Type { case watch.Modified: default: return false, nil } switch cast := event.Object.(type) { case *api.ResourceQuota: if len(cast.Status.Hard) > 0 { return true, nil } } return false, nil }) if err != nil { t.Fatalf("unexpected error: %v", err) } }
func scale(t *testing.T, namespace string, clientset *clientset.Clientset) { target := 100 rc := &api.ReplicationController{ ObjectMeta: api.ObjectMeta{ Name: "foo", Namespace: namespace, }, Spec: api.ReplicationControllerSpec{ Replicas: int32(target), Selector: map[string]string{"foo": "bar"}, Template: &api.PodTemplateSpec{ ObjectMeta: api.ObjectMeta{ Labels: map[string]string{ "foo": "bar", }, }, Spec: api.PodSpec{ Containers: []api.Container{ { Name: "container", Image: "busybox", }, }, }, }, }, } w, err := clientset.Core().ReplicationControllers(namespace).Watch(api.SingleObject(api.ObjectMeta{Name: rc.Name})) if err != nil { t.Fatalf("unexpected error: %v", err) } if _, err := clientset.Core().ReplicationControllers(namespace).Create(rc); err != nil { t.Fatalf("unexpected error: %v", err) } _, err = watch.Until(3*time.Minute, w, func(event watch.Event) (bool, error) { switch event.Type { case watch.Modified: default: return false, nil } switch cast := event.Object.(type) { case *api.ReplicationController: fmt.Printf("Found %v of %v replicas\n", int(cast.Status.Replicas), target) if int(cast.Status.Replicas) == target { return true, nil } } return false, nil }) if err != nil { pods, _ := clientset.Core().Pods(namespace).List(api.ListOptions{LabelSelector: labels.Everything(), FieldSelector: fields.Everything()}) t.Fatalf("unexpected error: %v, ended with %v pods", err, len(pods.Items)) } }
// waitForPod watches the given pod until the exitCondition is true. Each two seconds // the tick function is called e.g. for progress output. func waitForPod(podClient coreclient.PodsGetter, ns, name string, exitCondition watch.ConditionFunc, tick func(*api.Pod)) (*api.Pod, error) { w, err := podClient.Pods(ns).Watch(api.SingleObject(api.ObjectMeta{Name: name})) if err != nil { return nil, err } pods := make(chan *api.Pod) // observed pods passed to the exitCondition defer close(pods) // wait for the first event, then start the 2 sec ticker and loop go func() { pod := <-pods if pod == nil { return } tick(pod) t := time.NewTicker(2 * time.Second) defer t.Stop() for { select { case pod = <-pods: if pod == nil { return } case _, ok := <-t.C: if !ok { return } tick(pod) } } }() intr := interrupt.New(nil, w.Stop) var result *api.Pod err = intr.Run(func() error { ev, err := watch.Until(0, w, func(ev watch.Event) (bool, error) { c, err := exitCondition(ev) if c == false && err == nil { pods <- ev.Object.(*api.Pod) // send to ticker } return c, err }) result = ev.Object.(*api.Pod) return err }) return result, err }
// waitForPod watches the given pod until the exitCondition is true func waitForPod(podClient coreclient.PodsGetter, ns, name string, exitCondition watch.ConditionFunc) (*api.Pod, error) { w, err := podClient.Pods(ns).Watch(api.SingleObject(api.ObjectMeta{Name: name})) if err != nil { return nil, err } intr := interrupt.New(nil, w.Stop) var result *api.Pod err = intr.Run(func() error { ev, err := watch.Until(0, w, func(ev watch.Event) (bool, error) { return exitCondition(ev) }) result = ev.Object.(*api.Pod) return err }) return result, err }
// waitForPod watches the given pod until the exitCondition is true. Each two seconds // the tick function is called e.g. for progress output. func waitForPod(podClient coreclient.PodsGetter, ns, name string, exitCondition func(*api.Pod) bool, tick func(*api.Pod)) (*api.Pod, error) { pod, err := podClient.Pods(ns).Get(name) if err != nil { return nil, err } if exitCondition(pod) { return pod, nil } tick(pod) w, err := podClient.Pods(ns).Watch(api.SingleObject(api.ObjectMeta{Name: pod.Name, ResourceVersion: pod.ResourceVersion})) if err != nil { return nil, err } t := time.NewTicker(2 * time.Second) defer t.Stop() go func() { for range t.C { tick(pod) } }() err = nil result := pod kubectl.WatchLoop(w, func(ev watch.Event) error { switch ev.Type { case watch.Added, watch.Modified: pod = ev.Object.(*api.Pod) if exitCondition(pod) { result = pod w.Stop() } case watch.Deleted: w.Stop() case watch.Error: result = nil err = fmt.Errorf("failed to watch pod %s/%s", ns, name) w.Stop() } return nil }) return result, err }
func validateDNSResults(f *e2e.Framework, pod *api.Pod, fileNames sets.String, expect int) { By("submitting the pod to kubernetes") podClient := f.Client.Pods(f.Namespace.Name) defer func() { By("deleting the pod") defer GinkgoRecover() podClient.Delete(pod.Name, api.NewDeleteOptions(0)) }() updated, err := podClient.Create(pod) if err != nil { e2e.Failf("Failed to create %s pod: %v", pod.Name, err) } w, err := f.Client.Pods(f.Namespace.Name).Watch(api.SingleObject(api.ObjectMeta{Name: pod.Name, ResourceVersion: updated.ResourceVersion})) if err != nil { e2e.Failf("Failed: %v", err) } if _, err = watch.Until(e2e.PodStartTimeout, w, PodSucceeded); err != nil { e2e.Failf("Failed: %v", err) } By("retrieving the pod logs") r, err := podClient.GetLogs(pod.Name, &api.PodLogOptions{Container: "querier"}).Stream() if err != nil { e2e.Failf("Failed to get pod logs %s: %v", pod.Name, err) } out, err := ioutil.ReadAll(r) if err != nil { e2e.Failf("Failed to read pod logs %s: %v", pod.Name, err) } // Try to find results for each expected name. By("looking for the results for each expected name from probiers") if err := assertLinesExist(fileNames, expect, bytes.NewBuffer(out)); err != nil { e2e.Logf("Got results from pod:\n%s", out) e2e.Failf("Unexpected results: %v", err) } e2e.Logf("DNS probes using %s succeeded\n", pod.Name) }
By("creating statefulset with conflicting port in namespace " + f.Namespace.Name) ps := newStatefulSet(statefulSetName, f.Namespace.Name, headlessSvcName, 1, nil, nil, labels) petContainer := &ps.Spec.Template.Spec.Containers[0] petContainer.Ports = append(petContainer.Ports, conflictingPort) ps.Spec.Template.Spec.NodeName = node.Name _, err = f.ClientSet.Apps().StatefulSets(f.Namespace.Name).Create(ps) framework.ExpectNoError(err) By("waiting until pod " + podName + " will start running in namespace " + f.Namespace.Name) if err := f.WaitForPodRunning(podName); err != nil { framework.Failf("Pod %v did not start running: %v", podName, err) } var initialPetPodUID types.UID By("waiting until pet pod " + petPodName + " will be recreated and deleted at least once in namespace " + f.Namespace.Name) w, err := f.ClientSet.Core().Pods(f.Namespace.Name).Watch(api.SingleObject(api.ObjectMeta{Name: petPodName})) framework.ExpectNoError(err) // we need to get UID from pod in any state and wait until pet set controller will remove pod atleast once _, err = watch.Until(petPodTimeout, w, func(event watch.Event) (bool, error) { pod := event.Object.(*api.Pod) switch event.Type { case watch.Deleted: framework.Logf("Observed delete event for pet pod %v in namespace %v", pod.Name, pod.Namespace) if initialPetPodUID == "" { return false, nil } return true, nil } framework.Logf("Observed pet pod in namespace: %v, name: %v, uid: %v, status phase: %v. Waiting for statefulset controller to delete.", pod.Namespace, pod.Name, pod.UID, pod.Status.Phase) initialPetPodUID = pod.UID
By("Before scale up finished setting 2nd pod to be not ready by breaking readiness probe") pst.breakProbe(ps, testProbe) pst.waitForRunningAndNotReady(2, ps) By("Continue scale operation after the 2nd pod, and scaling down to 1 replica") pst.setHealthy(ps) pst.updateReplicas(ps, 1) By("Verifying that the 2nd pod wont be removed if it is not running and ready") pst.confirmPetCount(2, ps, 10*time.Second) expectedPodName := ps.Name + "-1" expectedPod, err := f.ClientSet.Core().Pods(ns).Get(expectedPodName) Expect(err).NotTo(HaveOccurred()) watcher, err := f.ClientSet.Core().Pods(ns).Watch(api.SingleObject( api.ObjectMeta{ Name: expectedPod.Name, ResourceVersion: expectedPod.ResourceVersion, }, )) Expect(err).NotTo(HaveOccurred()) By("Verifying the 2nd pod is removed only when it becomes running and ready") pst.restoreProbe(ps, testProbe) _, err = watch.Until(statefulsetTimeout, watcher, func(event watch.Event) (bool, error) { pod := event.Object.(*api.Pod) if event.Type == watch.Deleted && pod.Name == expectedPodName { return false, fmt.Errorf("Pod %v was deleted before enter running", pod.Name) } framework.Logf("Observed event %v for pod %v. Phase %v, Pod is ready %v", event.Type, pod.Name, pod.Status.Phase, api.IsPodReady(pod)) if pod.Name != expectedPodName { return false, nil
}, Containers: []api.Container{ { Name: "run1", Image: "gcr.io/google_containers/busybox:1.24", Command: []string{"/bin/true"}, }, }, }, } defer podClient.Delete(pod.Name, nil) startedPod, err := podClient.Create(pod) if err != nil { framework.Failf("Error creating a pod: %v", err) } w, err := podClient.Watch(api.SingleObject(startedPod.ObjectMeta)) if err != nil { framework.Failf("Error watching a pod: %v", err) } wr := watch.NewRecorder(w) event, err := watch.Until(framework.PodStartTimeout, wr, client.PodCompleted) Expect(err).To(BeNil()) framework.CheckInvariants(wr.Events(), framework.ContainerInitInvariant) endPod := event.Object.(*api.Pod) Expect(endPod.Status.Phase).To(Equal(api.PodSucceeded)) _, init := api.GetPodCondition(&endPod.Status, api.PodInitialized) Expect(init).NotTo(BeNil()) Expect(init.Status).To(Equal(api.ConditionTrue)) Expect(len(endPod.Status.InitContainerStatuses)).To(Equal(2))
func runAppArmorTest(f *framework.Framework, shouldRun bool, profile string) api.PodStatus { pod := createPodWithAppArmor(f, profile) if shouldRun { // The pod needs to start before it stops, so wait for the longer start timeout. framework.ExpectNoError(framework.WaitTimeoutForPodNoLongerRunningInNamespace( f.ClientSet, pod.Name, f.Namespace.Name, "", framework.PodStartTimeout)) } else { // Pod should remain in the pending state. Wait for the Reason to be set to "AppArmor". w, err := f.PodClient().Watch(api.SingleObject(api.ObjectMeta{Name: pod.Name})) framework.ExpectNoError(err) _, err = watch.Until(framework.PodStartTimeout, w, func(e watch.Event) (bool, error) { switch e.Type { case watch.Deleted: return false, errors.NewNotFound(unversioned.GroupResource{Resource: "pods"}, pod.Name) } switch t := e.Object.(type) { case *api.Pod: if t.Status.Reason == "AppArmor" { return true, nil } } return false, nil }) framework.ExpectNoError(err) } p, err := f.PodClient().Get(pod.Name) framework.ExpectNoError(err) return p.Status }