Skip to content

Commit

Permalink
chore: add PV subpath label to provisioned PV (#1031)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuhui zhang <[email protected]>
  • Loading branch information
zxh326 authored Jul 12, 2024
1 parent b560adb commit 3f1e0fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ const (
DefaultMountPodMemLimit = "5Gi"
DefaultMountPodCpuRequest = "1000m"
DefaultMountPodMemRequest = "1Gi"

// pv labels
PVSubpathKey = "juicefs/sub-path"
)

var PodLocks [1024]sync.Mutex
Expand Down
3 changes: 3 additions & 0 deletions pkg/driver/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ func (j *provisionerService) Provision(ctx context.Context, options provisioncon
pv := &corev1.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: options.PVName,
Labels: map[string]string{
config.PVSubpathKey: subPath,
},
},
Spec: corev1.PersistentVolumeSpec{
Capacity: corev1.ResourceList{
Expand Down
19 changes: 18 additions & 1 deletion pkg/util/resource/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"

"github.com/juicedata/juicefs-csi-driver/pkg/config"
Expand Down Expand Up @@ -106,8 +107,24 @@ func CheckForSubPath(ctx context.Context, client *k8s.K8sClient, volume *v1.Pers
return false, nil
}

// list by labels
labelsSelector := metav1.LabelSelector{
MatchLabels: map[string]string{
config.PVSubpathKey: nowSubPath,
},
}
pvs, err := client.ListPersistentVolumes(ctx, &labelsSelector, nil)
if err != nil {
return false, err
}
if len(pvs) > 1 {
klog.V(5).Infof("Found more than one PV with the same subPath %s, skip delete volume %s data", nowSubPath, volume.Name)
return false, nil
}

// FIXME: we still need to list all PVs to check because the labels were not set in the old version.
// get all pvs
pvs, err := client.ListPersistentVolumes(ctx, nil, nil)
pvs, err = client.ListPersistentVolumes(ctx, nil, nil)
if err != nil {
return false, err
}
Expand Down

0 comments on commit 3f1e0fe

Please sign in to comment.