mirror of
https://github.com/ceph/ceph-csi.git
synced 2024-11-10 00:10:20 +00:00
cephfs: add set/Get/List/Remove metadata utility functions
Add utility functions to set/Get/List/Remove PV/PVC/PVCNamespace metadata on subvolume. Signed-off-by: Prasanna Kumar Kalever <prasanna.kalever@redhat.com>
This commit is contained in:
parent
8c5563a9bc
commit
ecf03eb6ae
69
internal/cephfs/core/metadata.go
Normal file
69
internal/cephfs/core/metadata.go
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2022 The Ceph-CSI Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// setMetadata sets custom metadata on the subvolume in a volume as a
|
||||||
|
// key-value pair.
|
||||||
|
func (s *subVolumeClient) setMetadata(key, value string) error {
|
||||||
|
fsa, err := s.conn.GetFSAdmin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return fsa.SetMetadata(s.FsName, s.SubvolumeGroup, s.VolID, key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeMetadata removes custom metadata set on the subvolume in a volume
|
||||||
|
// using the metadata key.
|
||||||
|
func (s *subVolumeClient) removeMetadata(key string) error {
|
||||||
|
fsa, err := s.conn.GetFSAdmin()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return fsa.RemoveMetadata(s.FsName, s.SubvolumeGroup, s.VolID, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetAllMetadata set all the metadata from arg parameters on Ssubvolume.
|
||||||
|
func (s *subVolumeClient) SetAllMetadata(parameters map[string]string) error {
|
||||||
|
for k, v := range parameters {
|
||||||
|
err := s.setMetadata(k, v)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to set metadata key %q, value %q on subvolume %v: %w", k, v, s, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnsetAllMetadata unset all the metadata from arg keys on subvolume.
|
||||||
|
func (s *subVolumeClient) UnsetAllMetadata(keys []string) error {
|
||||||
|
for _, key := range keys {
|
||||||
|
err := s.removeMetadata(key)
|
||||||
|
// TODO: replace string comparison with errno.
|
||||||
|
if err != nil && !strings.Contains(err.Error(), "No such file or directory") {
|
||||||
|
return fmt.Errorf("failed to unset metadata key %q on subvolume %v: %w", key, s, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -72,6 +72,11 @@ type SubVolumeClient interface {
|
|||||||
CreateCloneFromSnapshot(ctx context.Context, snap Snapshot) error
|
CreateCloneFromSnapshot(ctx context.Context, snap Snapshot) error
|
||||||
// CleanupSnapshotFromSubvolume removes the snapshot from the subvolume.
|
// CleanupSnapshotFromSubvolume removes the snapshot from the subvolume.
|
||||||
CleanupSnapshotFromSubvolume(ctx context.Context, parentVol *SubVolume) error
|
CleanupSnapshotFromSubvolume(ctx context.Context, parentVol *SubVolume) error
|
||||||
|
|
||||||
|
// SetAllMetadata set all the metadata from arg parameters on Ssubvolume.
|
||||||
|
SetAllMetadata(parameters map[string]string) error
|
||||||
|
// UnsetAllMetadata unset all the metadata from arg keys on subvolume.
|
||||||
|
UnsetAllMetadata(keys []string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// subVolumeClient implements SubVolumeClient interface.
|
// subVolumeClient implements SubVolumeClient interface.
|
||||||
|
Loading…
Reference in New Issue
Block a user