(ctx devspacecontext.Context, deployment *appsv1.Deployment, devPod *latest.DevPod)
| 234 | } |
| 235 | |
| 236 | func updatePVC(ctx devspacecontext.Context, deployment *appsv1.Deployment, devPod *latest.DevPod) error { |
| 237 | // create a pvc if needed |
| 238 | hasPersistPath := false |
| 239 | loader.EachDevContainer(devPod, func(devContainer *latest.DevContainer) bool { |
| 240 | if len(devContainer.PersistPaths) > 0 { |
| 241 | hasPersistPath = true |
| 242 | return false |
| 243 | } |
| 244 | return true |
| 245 | }) |
| 246 | |
| 247 | if hasPersistPath { |
| 248 | name := getClaimName(deployment, devPod) |
| 249 | existingPVC, err := ctx.KubeClient().KubeClient().CoreV1().PersistentVolumeClaims(deployment.Namespace).Get(ctx.Context(), name, metav1.GetOptions{}) |
| 250 | if existingPVC != nil { |
| 251 | replicaSets, err := ctx.KubeClient().KubeClient().AppsV1().ReplicaSets(deployment.Namespace).List(ctx.Context(), metav1.ListOptions{LabelSelector: selector.ReplacedLabel}) |
| 252 | if err != nil { |
| 253 | return errors.Wrap(err, "list replica sets") |
| 254 | } |
| 255 | |
| 256 | for _, rs := range replicaSets.Items { |
| 257 | for _, v := range rs.Spec.Template.Spec.Volumes { |
| 258 | if v.PersistentVolumeClaim != nil && v.PersistentVolumeClaim.ClaimName == name { |
| 259 | ctx.Log().Debugf("Scaled down ReplicaSet %s to release persistent volume claim", rs.Name) |
| 260 | err = scaleDownTarget(ctx, &rs) |
| 261 | if err != nil { |
| 262 | return errors.Wrap(err, "scale down persistent volume claim replica sets") |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } else if err != nil && !kerrors.IsNotFound(err) { |
| 268 | return errors.Wrap(err, "get existing persistent volume claim") |
| 269 | } |
| 270 | |
| 271 | err = createPVC(ctx, deployment, devPod) |
| 272 | if err != nil { |
| 273 | if kerrors.IsAlreadyExists(err) { |
| 274 | // delete the old one and wait |
| 275 | _ = ctx.KubeClient().KubeClient().CoreV1().PersistentVolumeClaims(deployment.Namespace).Delete(ctx.Context(), deployment.Name, metav1.DeleteOptions{}) |
| 276 | ctx.Log().Infof("Waiting for old persistent volume claim to terminate") |
| 277 | err = wait.PollUntilContextTimeout(ctx.Context(), time.Second, time.Minute*2, false, func(ctxPullUntil context.Context) (done bool, err error) { |
| 278 | _, err = ctx.KubeClient().KubeClient().CoreV1().PersistentVolumeClaims(deployment.Namespace).Get(ctxPullUntil, deployment.Name, metav1.GetOptions{}) |
| 279 | return kerrors.IsNotFound(err), nil |
| 280 | }) |
| 281 | if err != nil { |
| 282 | return errors.Wrap(err, "waiting for pvc to terminate") |
| 283 | } |
| 284 | |
| 285 | // create the new one |
| 286 | err = createPVC(ctx, deployment, devPod) |
| 287 | if err != nil { |
| 288 | return errors.Wrap(err, "create persistent volume claim") |
| 289 | } |
| 290 | } else { |
| 291 | return errors.Wrap(err, "create persistent volume claim") |
| 292 | } |
| 293 | } |
no test coverage detected