@@ -2,15 +2,18 @@ package podreplace
22
33import (
44 "fmt"
5+ "strconv"
6+ "time"
7+
58 "github.com/loft-sh/devspace/pkg/devspace/config/remotecache"
69 devspacecontext "github.com/loft-sh/devspace/pkg/devspace/context"
710 "github.com/loft-sh/devspace/pkg/devspace/context/values"
811 "github.com/loft-sh/devspace/pkg/devspace/deploy"
12+ "github.com/loft-sh/devspace/pkg/devspace/kubectl/selector"
913 patch2 "github.com/loft-sh/devspace/pkg/util/patch"
1014 "github.com/loft-sh/devspace/pkg/util/stringutil"
1115 "k8s.io/apimachinery/pkg/api/resource"
12- "strconv"
13- "time"
16+ "k8s.io/apimachinery/pkg/util/wait"
1417
1518 "github.com/loft-sh/devspace/pkg/devspace/config/loader"
1619 "github.com/loft-sh/devspace/pkg/devspace/config/versions/latest"
@@ -21,7 +24,6 @@ import (
2124 kerrors "k8s.io/apimachinery/pkg/api/errors"
2225 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2326 "k8s.io/apimachinery/pkg/runtime"
24- "k8s.io/apimachinery/pkg/util/wait"
2527)
2628
2729const (
@@ -231,8 +233,6 @@ func (p *replacer) replace(ctx devspacecontext.Context, deploymentName string, t
231233}
232234
233235func updatePVC (ctx devspacecontext.Context , deployment * appsv1.Deployment , devPod * latest.DevPod ) error {
234- var err error
235-
236236 // create a pvc if needed
237237 hasPersistPath := false
238238 loader .EachDevContainer (devPod , func (devContainer * latest.DevContainer ) bool {
@@ -242,7 +242,31 @@ func updatePVC(ctx devspacecontext.Context, deployment *appsv1.Deployment, devPo
242242 }
243243 return true
244244 })
245+
245246 if hasPersistPath {
247+ name := getClaimName (deployment , devPod )
248+ existingPVC , err := ctx .KubeClient ().KubeClient ().CoreV1 ().PersistentVolumeClaims (deployment .Namespace ).Get (ctx .Context (), name , metav1.GetOptions {})
249+ if existingPVC != nil {
250+ replicaSets , err := ctx .KubeClient ().KubeClient ().AppsV1 ().ReplicaSets (deployment .Namespace ).List (ctx .Context (), metav1.ListOptions {LabelSelector : selector .ReplacedLabel })
251+ if err != nil {
252+ return errors .Wrap (err , "list replica sets" )
253+ }
254+
255+ for _ , rs := range replicaSets .Items {
256+ for _ , v := range rs .Spec .Template .Spec .Volumes {
257+ if v .PersistentVolumeClaim != nil && v .PersistentVolumeClaim .ClaimName == name {
258+ ctx .Log ().Debugf ("Scaled down ReplicaSet %s to release persistent volume claim" , rs .Name )
259+ err = scaleDownTarget (ctx , & rs )
260+ if err != nil {
261+ return errors .Wrap (err , "scale down persistent volume claim replica sets" )
262+ }
263+ }
264+ }
265+ }
266+ } else if err != nil && ! kerrors .IsNotFound (err ) {
267+ return errors .Wrap (err , "get existing persistent volume claim" )
268+ }
269+
246270 err = createPVC (ctx , deployment , devPod )
247271 if err != nil {
248272 if kerrors .IsAlreadyExists (err ) {
@@ -294,10 +318,7 @@ func createPVC(ctx devspacecontext.Context, deployment *appsv1.Deployment, devPo
294318 }
295319 }
296320
297- name := deployment .Name
298- if devPod .PersistenceOptions != nil && devPod .PersistenceOptions .Name != "" {
299- name = devPod .PersistenceOptions .Name
300- }
321+ name := getClaimName (deployment , devPod )
301322
302323 _ , err = ctx .KubeClient ().KubeClient ().CoreV1 ().PersistentVolumeClaims (deployment .Namespace ).Create (ctx .Context (), & corev1.PersistentVolumeClaim {
303324 ObjectMeta : metav1.ObjectMeta {
@@ -429,3 +450,12 @@ func scaleDownTarget(ctx devspacecontext.Context, obj runtime.Object) error {
429450
430451 return fmt .Errorf ("unrecognized object" )
431452}
453+
454+ func getClaimName (deployment * appsv1.Deployment , devPod * latest.DevPod ) string {
455+ name := deployment .Name
456+ if devPod .PersistenceOptions != nil && devPod .PersistenceOptions .Name != "" {
457+ name = devPod .PersistenceOptions .Name
458+ }
459+
460+ return name
461+ }
0 commit comments