Skip to content

Commit

Permalink
Rename managed-by label on pvcs (fluid-cloudnative#2455)
Browse files Browse the repository at this point in the history
* Rename pvc label

Signed-off-by: dongyun.xzh <[email protected]>

* Refactor extracting manager dataset from labels

Signed-off-by: dongyun.xzh <[email protected]>

Signed-off-by: dongyun.xzh <[email protected]>
  • Loading branch information
TrafalgarZZZ authored Dec 20, 2022
1 parent e0c1ade commit cd76897
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
19 changes: 17 additions & 2 deletions pkg/common/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ const (
LabelAnnotationDataset = LabelAnnotationPrefix + "dataset"
// LabelAnnotationDatasetNum indicates the number of the dataset in specific node
LabelAnnotationDatasetNum = LabelAnnotationPrefix + "dataset-num"
// LabelAnnotationWrappedBy indicates the resource is wrapped by some dataset
LabelAnnotationWrappedBy = LabelAnnotationPrefix + "wrapped-by"

// LabelAnnotationManagedByDeprecated is a deprecated label key for LabelAnnotationManagedBy
LabelAnnotationManagedByDeprecated = LabelAnnotationPrefix + "wrapped-by"

// LabelAnnotationManagedBy indicates a pvc that is managed by Fluid
LabelAnnotationManagedBy = LabelAnnotationPrefix + "managed-by"

// fluid adminssion webhook inject flag
EnableFluidInjectionFlag = LabelAnnotationPrefix + "enable-injection"
Expand Down Expand Up @@ -124,3 +128,14 @@ func CheckExpectValue(m map[string]string, key string, targetValue string) bool
}
return false
}

func GetManagerDatasetFromLabels(labels map[string]string) (datasetName string, exists bool) {
datasetName, exists = labels[LabelAnnotationManagedBy]
if exists {
return
}

// fallback to check deprecated "fluid.io/wrapped-by" label
datasetName, exists = labels[LabelAnnotationManagedByDeprecated]
return
}
8 changes: 4 additions & 4 deletions pkg/ddc/thin/wrap_pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func (t *ThinEngine) wrapMountedPersistentVolumeClaim() (err error) {
return err
}

if _, exists := mountedPvc.Labels[common.LabelAnnotationWrappedBy]; !exists {
if _, exists := mountedPvc.Labels[common.LabelAnnotationManagedBy]; !exists {
labelsToModify := common.LabelsToModify{}
labelsToModify.Add(common.LabelAnnotationWrappedBy, t.name)
labelsToModify.Add(common.LabelAnnotationManagedBy, t.name)
_, err = utils.PatchLabels(t.Client, mountedPvc, labelsToModify)
if err != nil {
return err
Expand Down Expand Up @@ -130,9 +130,9 @@ func (t *ThinEngine) unwrapMountedPersistentVolumeClaims() (err error) {
return errors.Wrapf(err, "failed to get pvc when unwrapping pvc %s", pvcName)
}

if wrappedBy, exists := pvc.Labels[common.LabelAnnotationWrappedBy]; exists && wrappedBy == t.name {
if wrappedBy, exists := pvc.Labels[common.LabelAnnotationManagedBy]; exists && wrappedBy == t.name {
labelsToModify := common.LabelsToModify{}
labelsToModify.Delete(common.LabelAnnotationWrappedBy)
labelsToModify.Delete(common.LabelAnnotationManagedBy)
if _, err = utils.PatchLabels(t.Client, pvc, labelsToModify); err != nil {
return errors.Wrapf(err, "failed to remove label when unwrapping pvc %s", pvc.Name)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/ddc/thin/wrap_pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestThinEngine_wrapMountedPersistentVolumeClaim(t *testing.T) {
Name: "my-pvc-2",
Namespace: "default",
Labels: map[string]string{
common.LabelAnnotationWrappedBy: "dataset2",
common.LabelAnnotationManagedBy: "dataset2",
},
},
},
Expand Down Expand Up @@ -119,10 +119,10 @@ func TestThinEngine_wrapMountedPersistentVolumeClaim(t *testing.T) {
t.Errorf("Got error when checking pvc labels: %v", err)
}

if wrappedBy, exists := pvc.Labels[common.LabelAnnotationWrappedBy]; !exists {
t.Errorf("Expect get label \"%s=%s\" on pvc, but not exists", common.LabelAnnotationWrappedBy, engine.name)
if wrappedBy, exists := pvc.Labels[common.LabelAnnotationManagedBy]; !exists {
t.Errorf("Expect get label \"%s=%s\" on pvc, but not exists", common.LabelAnnotationManagedBy, engine.name)
} else if wrappedBy != engine.name {
t.Errorf("Expect get label \"%s=%s\" on pvc, but got %s", common.LabelAnnotationWrappedBy, engine.name, wrappedBy)
t.Errorf("Expect get label \"%s=%s\" on pvc, but got %s", common.LabelAnnotationManagedBy, engine.name, wrappedBy)
}
})
}
Expand All @@ -137,7 +137,7 @@ func TestThinEngine_unwrapMountedPersistentVolumeClaims(t *testing.T) {
Name: "my-pvc-1",
Namespace: "default",
Labels: map[string]string{
common.LabelAnnotationWrappedBy: "dataset1",
common.LabelAnnotationManagedBy: "dataset1",
},
},
},
Expand Down Expand Up @@ -236,8 +236,8 @@ func TestThinEngine_unwrapMountedPersistentVolumeClaims(t *testing.T) {
t.Errorf("Got error when checking pvc labels: %v", err)
}

if _, exists := pvc.Labels[common.LabelAnnotationWrappedBy]; exists {
t.Errorf("Expect no label \"%s\" on pvc, but it exists. pvc Labels: %v", common.LabelAnnotationWrappedBy, pvc.Labels)
if _, exists := pvc.Labels[common.LabelAnnotationManagedBy]; exists {
t.Errorf("Expect no label \"%s\" on pvc, but it exists. pvc Labels: %v", common.LabelAnnotationManagedBy, pvc.Labels)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/kubeclient/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func CheckIfPVCIsDataset(pvc *v1.PersistentVolumeClaim) (isDataset bool) {
}
_, isDataset = pvc.Labels[common.LabelAnnotationStorageCapacityPrefix+namespace+"-"+name]

if _, exists := pvc.Labels[common.LabelAnnotationWrappedBy]; exists {
if _, exists := common.GetManagerDatasetFromLabels(pvc.Labels); exists {
isDataset = true
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/webhook/scheduler/mutating/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func buildRuntimeInfoInternal(client client.Client,
namespace = corev1.NamespaceDefault
}
pvcName := pvc.GetName()
if wrapperName, exists := pvc.Labels[common.LabelAnnotationWrappedBy]; exists {
pvcName = wrapperName
if datasetName, exists := common.GetManagerDatasetFromLabels(pvc.Labels); exists {
pvcName = datasetName
}

dataset, err := utils.GetDataset(client, pvcName, namespace)
Expand Down

0 comments on commit cd76897

Please sign in to comment.