| 121 | } |
| 122 | |
| 123 | func (d *devPod) startWithRetry(ctx devspacecontext.Context, devPodConfig *latest.DevPod, options Options) error { |
| 124 | t := &tomb.Tomb{} |
| 125 | |
| 126 | go func(ctx devspacecontext.Context) { |
| 127 | // wait for parent context cancel |
| 128 | // or that the DevPod is done |
| 129 | select { |
| 130 | case <-ctx.Context().Done(): |
| 131 | case <-t.Dead(): |
| 132 | } |
| 133 | |
| 134 | if ctx.IsDone() { |
| 135 | <-t.Dead() |
| 136 | ctx.Log().Debugf("Stopped dev %s", devPodConfig.Name) |
| 137 | close(d.done) |
| 138 | return |
| 139 | } |
| 140 | |
| 141 | // check if pod was terminated |
| 142 | d.m.Lock() |
| 143 | selectedPod := d.selectedPod |
| 144 | d.selectedPod = nil |
| 145 | d.m.Unlock() |
| 146 | |
| 147 | // check if we need to restart |
| 148 | if selectedPod != nil { |
| 149 | shouldRestart := false |
| 150 | err := wait.PollUntilContextCancel(ctx.Context(), time.Second, true, func(context.Context) (bool, error) { |
| 151 | pod, err := ctx.KubeClient().KubeClient().CoreV1().Pods(selectedPod.Pod.Namespace).Get(ctx.Context(), selectedPod.Pod.Name, metav1.GetOptions{}) |
| 152 | if err != nil { |
| 153 | if kerrors.IsNotFound(err) { |
| 154 | ctx.Log().Debugf("Restart dev %s because pod isn't found anymore", devPodConfig.Name) |
| 155 | shouldRestart = true |
| 156 | return true, nil |
| 157 | } |
| 158 | |
| 159 | // this case means there might be problems with internet |
| 160 | ctx.Log().Debugf("error trying to retrieve pod: %v", err) |
| 161 | return false, nil |
| 162 | } else if pod.DeletionTimestamp != nil { |
| 163 | ctx.Log().Debugf("Restart dev %s because pod is terminating", devPodConfig.Name) |
| 164 | shouldRestart = true |
| 165 | return true, nil |
| 166 | } |
| 167 | |
| 168 | return true, nil |
| 169 | }) |
| 170 | if err != nil { |
| 171 | if wait.Interrupted(err) { |
| 172 | ctx.Log().Errorf("error restarting dev: %v", err) |
| 173 | } |
| 174 | } else if shouldRestart { |
| 175 | d.restart(ctx, devPodConfig, options) |
| 176 | return |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | ctx.Log().Debugf("Stopped dev %s", devPodConfig.Name) |