// same as above func comments, except 'recyclerClient' is a narrower pod API // interface to ease testing func internalRecycleVolumeByWatchingPodUntilCompletion(pvName string, pod *api.Pod, recyclerClient recyclerClient) error { glog.V(5).Infof("creating recycler pod for volume %s\n", pod.Name) // Generate unique name for the recycler pod - we need to get "already // exists" error when a previous controller has already started recycling // the volume. Here we assume that pv.Name is already unique. pod.Name = "recycler-for-" + pvName pod.GenerateName = "" // Start the pod _, err := recyclerClient.CreatePod(pod) if err != nil { if errors.IsAlreadyExists(err) { glog.V(5).Infof("old recycler pod %q found for volume", pod.Name) } else { return fmt.Errorf("Unexpected error creating recycler pod: %+v\n", err) } } defer recyclerClient.DeletePod(pod.Name, pod.Namespace) // Now only the old pod or the new pod run. Watch it until it finishes. stopChannel := make(chan struct{}) defer close(stopChannel) nextPod := recyclerClient.WatchPod(pod.Name, pod.Namespace, stopChannel) for { watchedPod := nextPod() if watchedPod.Status.Phase == api.PodSucceeded { // volume.Recycle() returns nil on success, else error return nil } if watchedPod.Status.Phase == api.PodFailed { // volume.Recycle() returns nil on success, else error if watchedPod.Status.Message != "" { return fmt.Errorf(watchedPod.Status.Message) } else { return fmt.Errorf("pod failed, pod.Status.Message unknown.") } } } }
// same as above func comments, except 'recyclerClient' is a narrower pod API // interface to ease testing func internalRecycleVolumeByWatchingPodUntilCompletion(pvName string, pod *api.Pod, recyclerClient recyclerClient) error { glog.V(5).Infof("creating recycler pod for volume %s\n", pod.Name) // Generate unique name for the recycler pod - we need to get "already // exists" error when a previous controller has already started recycling // the volume. Here we assume that pv.Name is already unique. pod.Name = "recycler-for-" + pvName pod.GenerateName = "" stopChannel := make(chan struct{}) defer close(stopChannel) podCh, err := recyclerClient.WatchPod(pod.Name, pod.Namespace, stopChannel) if err != nil { glog.V(4).Infof("cannot start watcher for pod %s/%s: %v", pod.Namespace, pod.Name, err) return err } // Start the pod _, err = recyclerClient.CreatePod(pod) if err != nil { if errors.IsAlreadyExists(err) { glog.V(5).Infof("old recycler pod %q found for volume", pod.Name) } else { return fmt.Errorf("unexpected error creating recycler pod: %+v\n", err) } } defer func(pod *api.Pod) { glog.V(2).Infof("deleting recycler pod %s/%s", pod.Namespace, pod.Name) if err := recyclerClient.DeletePod(pod.Name, pod.Namespace); err != nil { glog.Errorf("failed to delete recycler pod %s/%s: %v", pod.Namespace, pod.Name, err) } }(pod) // Now only the old pod or the new pod run. Watch it until it finishes // and send all events on the pod to the PV for { event := <-podCh switch event.Object.(type) { case *api.Pod: // POD changed pod := event.Object.(*api.Pod) glog.V(4).Infof("recycler pod update received: %s %s/%s %s", event.Type, pod.Namespace, pod.Name, pod.Status.Phase) switch event.Type { case watch.Added, watch.Modified: if pod.Status.Phase == api.PodSucceeded { // Recycle succeeded. return nil } if pod.Status.Phase == api.PodFailed { if pod.Status.Message != "" { return fmt.Errorf(pod.Status.Message) } else { return fmt.Errorf("pod failed, pod.Status.Message unknown.") } } case watch.Deleted: return fmt.Errorf("recycler pod was deleted") case watch.Error: return fmt.Errorf("recycler pod watcher failed") } case *api.Event: // Event received podEvent := event.Object.(*api.Event) glog.V(4).Infof("recycler event received: %s %s/%s %s/%s %s", event.Type, podEvent.Namespace, podEvent.Name, podEvent.InvolvedObject.Namespace, podEvent.InvolvedObject.Name, podEvent.Message) if event.Type == watch.Added { recyclerClient.Event(podEvent.Type, podEvent.Message) } } } }