func recreatePods(client *internalclientset.Clientset, namespace string, selector map[string]string) error { pods, err := client.Pods(namespace).List(api.ListOptions{ FieldSelector: fields.Everything(), LabelSelector: labels.Set(selector).AsSelector(), }) if err != nil { return err } // Restart pods for _, pod := range pods.Items { log.Printf("Restarting pod: %v/%v", pod.Namespace, pod.Name) // Delete each pod for get them restarted with changed spec. err := client.Pods(pod.Namespace).Delete(pod.Name, &api.DeleteOptions{ Preconditions: &api.Preconditions{ UID: &pod.UID, }, }) if err != nil { return err } } return nil }
// GetPodLogs returns logs for particular pod and container. When container // is null, logs for the first one are returned. func GetPodLogs(client *client.Clientset, namespace, podID string, container string, logSelector *logs.LogViewSelector) (*logs.Logs, error) { pod, err := client.Pods(namespace).Get(podID) if err != nil { return nil, err } if len(container) == 0 { container = pod.Spec.Containers[0].Name } logOptions := &api.PodLogOptions{ Container: container, Follow: false, Previous: false, Timestamps: true, } rawLogs, err := getRawPodLogs(client, namespace, podID, logOptions) if err != nil { return nil, err } return ConstructLogs(podID, rawLogs, container, logSelector), nil }
// GetPodContainers returns containers that a pod has. func GetPodContainers(client *client.Clientset, namespace, podID string) (*PodContainerList, error) { pod, err := client.Pods(namespace).Get(podID) if err != nil { return nil, err } containers := &PodContainerList{Containers: make([]string, 0)} for _, container := range pod.Spec.Containers { containers.Containers = append(containers.Containers, container.Name) } return containers, nil }
func RunBuildPodControllerTest(t testingT, osClient *client.Client, kClient *kclientset.Clientset) { ns := testutil.Namespace() waitTime := BuildPodControllerTestWait tests := []buildControllerPodTest{ { Name: "running state test", States: []buildControllerPodState{ { PodPhase: kapi.PodRunning, BuildPhase: buildapi.BuildPhaseRunning, }, }, }, { Name: "build succeeded", States: []buildControllerPodState{ { PodPhase: kapi.PodRunning, BuildPhase: buildapi.BuildPhaseRunning, }, { PodPhase: kapi.PodSucceeded, BuildPhase: buildapi.BuildPhaseComplete, }, }, }, { Name: "build failed", States: []buildControllerPodState{ { PodPhase: kapi.PodRunning, BuildPhase: buildapi.BuildPhaseRunning, }, { PodPhase: kapi.PodFailed, BuildPhase: buildapi.BuildPhaseFailed, }, }, }, } for _, test := range tests { // Setup communications channels podReadyChan := make(chan *kapi.Pod) // Will receive a value when a build pod is ready errChan := make(chan error) // Will receive a value when an error occurs stateReached := int32(0) // Create a build b, err := osClient.Builds(ns).Create(mockBuild()) if err != nil { t.Fatal(err) } // Watch build pod for transition to pending podWatch, err := kClient.Pods(ns).Watch(kapi.ListOptions{FieldSelector: fields.OneTermEqualSelector("metadata.name", buildapi.GetBuildPodName(b))}) if err != nil { t.Fatal(err) } go func() { for e := range podWatch.ResultChan() { pod, ok := e.Object.(*kapi.Pod) if !ok { t.Fatalf("%s: unexpected object received: %#v\n", test.Name, e.Object) } if pod.Status.Phase == kapi.PodPending { podReadyChan <- pod break } } }() var pod *kapi.Pod select { case pod = <-podReadyChan: if pod.Status.Phase != kapi.PodPending { t.Errorf("Got wrong pod phase: %s", pod.Status.Phase) podWatch.Stop() continue } case <-time.After(BuildControllersWatchTimeout): t.Errorf("Timed out waiting for build pod to be ready") podWatch.Stop() continue } podWatch.Stop() for _, state := range test.States { if err := retry.RetryOnConflict(retry.DefaultRetry, func() error { // Update pod state and verify that corresponding build state happens accordingly pod, err := kClient.Pods(ns).Get(pod.Name) if err != nil { return err } if pod.Status.Phase == state.PodPhase { return fmt.Errorf("another client altered the pod phase to %s: %#v", state.PodPhase, pod) } pod.Status.Phase = state.PodPhase _, err = kClient.Pods(ns).UpdateStatus(pod) return err }); err != nil { t.Fatal(err) } buildWatch, err := osClient.Builds(ns).Watch(kapi.ListOptions{FieldSelector: fields.OneTermEqualSelector("metadata.name", b.Name), ResourceVersion: b.ResourceVersion}) if err != nil { t.Fatal(err) } defer buildWatch.Stop() go func() { done := false for e := range buildWatch.ResultChan() { var ok bool b, ok = e.Object.(*buildapi.Build) if !ok { errChan <- fmt.Errorf("%s: unexpected object received: %#v", test.Name, e.Object) } if e.Type != watchapi.Modified { errChan <- fmt.Errorf("%s: unexpected event received: %s, object: %#v", test.Name, e.Type, e.Object) } if done { errChan <- fmt.Errorf("%s: unexpected build state: %#v", test.Name, e.Object) } else if b.Status.Phase == state.BuildPhase { done = true atomic.StoreInt32(&stateReached, 1) } } }() select { case err := <-errChan: buildWatch.Stop() t.Errorf("%s: Error: %v\n", test.Name, err) break case <-time.After(waitTime): buildWatch.Stop() if atomic.LoadInt32(&stateReached) != 1 { t.Errorf("%s: Did not reach desired build state: %s", test.Name, state.BuildPhase) break } } } } }