func()

in pkg/controller/controller.go [2045:2100]


func (c *FrameworkController) getOrCleanupPod(
	f *ci.Framework, cm *core.ConfigMap,
	taskRoleName string, taskIndex int32, confirm bool) (pod *core.Pod, err error) {
	logPfx := fmt.Sprintf("[%v][%v][%v]: getOrCleanupPod: ",
		f.Key(), taskRoleName, taskIndex)
	taskStatus := f.TaskStatus(taskRoleName, taskIndex)
	podName := taskStatus.PodName()

	if confirm {
		pod, err = c.kClient.CoreV1().Pods(f.Namespace).Get(podName,
			meta.GetOptions{})
	} else {
		pod, err = c.podLister.Pods(f.Namespace).Get(podName)
	}

	if err != nil {
		if apiErrors.IsNotFound(err) {
			return nil, nil
		} else {
			return nil, fmt.Errorf(logPfx+
				"Failed to get Pod %v: confirm: %v: %v",
				podName, confirm, err)
		}
	}

	if taskStatus.PodUID() == nil || *taskStatus.PodUID() != pod.UID {
		// pod is the unmanaged
		if meta.IsControlledBy(pod, cm) {
			// The managed Pod becomes unmanaged if and only if Framework.Status
			// is failed to persist due to FrameworkController restart or create fails
			// but succeeds on remote, so clean up the Pod to avoid unmanaged pod leak.
			klog.Warningf(logPfx+
				"Found unmanaged but controlled Pod, so explicitly delete it: %v, %v",
				pod.Name, pod.UID)
			if pod.DeletionTimestamp != nil {
				err = c.handlePodGracefulDeletion(f, taskRoleName, taskIndex, pod)
				if err != nil {
					return nil, err
				}
			}
			return nil, c.deletePod(f, taskRoleName, taskIndex, pod.UID, confirm, false)
		} else {
			// Do not own and manage the life cycle of not controlled object, so still
			// consider the get and controlled object clean up is success, and postpone
			// the potential naming conflict when creating the controlled object.
			klog.Warningf(logPfx+
				"Found unmanaged and uncontrolled Pod, and it may be naming conflict "+
				"with the controlled Pod to be created: %v, %v",
				pod.Name, pod.UID)
			return nil, nil
		}
	} else {
		// pod is the managed
		return pod, nil
	}
}