Skip to content

Commit

Permalink
rename unique alloc id to volume claim id
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveenrajmani committed Sep 12, 2023
1 parent 5d35d88 commit 54cd3c0
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
10 changes: 5 additions & 5 deletions docs/volume-scheduling.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ DirectPV CSI controller selects suitable drive for `CreateVolume` request like b
3. As no `DirectPVDrive` CRD object has the requested volume, each drive is selected by
a. By requested capacity
b. By access-tier if requested
c. If unique id doesn't match
c. If volume claim id doesn't match
d. By topology constraints if requested
4. In the process of step (3), if more than one drive is selected, the maximum free capacity drive is picked.
5. If step (4) picks up more than one drive, a drive is randomly selected.
Expand Down Expand Up @@ -47,7 +47,7 @@ DirectPV CSI controller selects suitable drive for `CreateVolume` request like b
| │ Filter drives │ | |
| │ by maximum │ | ╭╌╌╌╌╌╌╌V╌╌╌╌╌╌╌╌╌╮
| │ free capacity │ | Yes | Match by |
| └───────────────┘ |<----| unique-alloc-id |
| └───────────────┘ |<----| volume-claim-id |
| | | | if requested? |
| ╭╌╌╌╌╌╌╌V╌╌╌╌╌╌╌╮ | ╰╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╯
| No │ Is more than │ | | No
Expand Down Expand Up @@ -100,12 +100,12 @@ EOF

By default, DirectPV allocates drives for volumes based on the free capacity present on the drives. So, the drive with most free capacity gets selected for a volume provisioning request. For setups that require unique drive allocation for its volumes where one drive cannot share more than one volume, use the below steps to enable unique allocation of drives.

* Create new storage class with `directpv.min.io/unique-alloc-id: <any-uid>` using [create-storage-class.sh script](../tools/create-storage-class.sh). Below is an example:
* Create new storage class with `directpv.min.io/volume-claim-id: <any-uid>` using [create-storage-class.sh script](../tools/create-storage-class.sh). Below is an example:
```sh
# NOTE: The allocation id must be 47 characters or less and should be valid label value.
#
# Create new storage class 'directpv-optimal' with the label 'directpv.min.io/unique-alloc-id: 1234'
$ create-storage-class.sh directpv-optimal 'directpv.min.io/unique-alloc-id: 1234'
# Create new storage class 'directpv-optimal' with the label 'directpv.min.io/volume-claim-id: 1234'
$ create-storage-class.sh directpv-optimal 'directpv.min.io/volume-claim-id: 1234'
```

* Use newly created storage class in [volume provisioning](./volume-provisioning.md). Below is an example:
Expand Down
11 changes: 7 additions & 4 deletions pkg/apis/directpv.min.io/types/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@ const (
// SuspendLabelKey denotes if the volume is suspended.
SuspendLabelKey LabelKey = consts.GroupName + "/suspend"

// UniqueAllocIDLabelKey label key to denote the unique allocation of drives for volumes
UniqueAllocIDLabelKey LabelKey = consts.GroupName + "/unique-alloc-id"
// VolumeClaimIDLabelKey label key to denote the unique allocation of drives for volumes
VolumeClaimIDLabelKey LabelKey = consts.GroupName + "/volume-claim-id"

// UniqueAllocIDLabelKeyPrefix label key prefix for unique allocation id to be set on the drive
UniqueAllocIDLabelKeyPrefix = consts.GroupName + "/unique-alloc-id-"
// VolumeClaimIDLabelKeyPrefix label key prefix for volume claim id to be set on the drive
VolumeClaimIDLabelKeyPrefix = consts.GroupName + "/volume-claim-id-"

// ClaimIDLabelKey label key to denote the claim id of the volumes
ClaimIDLabelKey LabelKey = consts.GroupName + "/claim-id"
)

// LabelValue is a type definition for label value
Expand Down
18 changes: 9 additions & 9 deletions pkg/apis/directpv.min.io/v1beta1/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,22 @@ func (drive DirectPVDrive) GetNodeID() types.NodeID {
return types.NodeID(drive.getLabel(types.NodeLabelKey))
}

// HasUniqueAllocID checks if the provided allocation id is set on the drive.
func (drive *DirectPVDrive) HasUniqueAllocID(allocID string) bool {
if v, ok := drive.GetLabels()[types.UniqueAllocIDLabelKeyPrefix+allocID]; ok && v == strconv.FormatBool(true) {
// HasVolumeClaimID checks if the provided volume claim id is set on the drive.
func (drive *DirectPVDrive) HasVolumeClaimID(claimID string) bool {
if v, ok := drive.GetLabels()[types.VolumeClaimIDLabelKeyPrefix+claimID]; ok && v == strconv.FormatBool(true) {
return true
}
return false
}

// SetUniqueAllocID sets the provided allocation id on the drive.
func (drive *DirectPVDrive) SetUniqueAllocID(allocID string) {
drive.SetLabel(types.LabelKey(types.UniqueAllocIDLabelKeyPrefix+allocID), types.LabelValue(strconv.FormatBool(true)))
// SetVolumeClaimID sets the provided claim id on the drive.
func (drive *DirectPVDrive) SetVolumeClaimID(claimID string) {
drive.SetLabel(types.LabelKey(types.VolumeClaimIDLabelKeyPrefix+claimID), types.LabelValue(strconv.FormatBool(true)))
}

// RemoveUniqueAllocID removes the unique alloc ID label.
func (drive *DirectPVDrive) RemoveUniqueAllocID(allocID string) {
drive.RemoveLabel(types.LabelKey(types.UniqueAllocIDLabelKeyPrefix + allocID))
// RemoveVolumeClaimID removes the volume claim id label.
func (drive *DirectPVDrive) RemoveVolumeClaimID(claimID string) {
drive.RemoveLabel(types.LabelKey(types.VolumeClaimIDLabelKeyPrefix + claimID))
}

// SetLabel sets label to this drive.
Expand Down
12 changes: 6 additions & 6 deletions pkg/apis/directpv.min.io/v1beta1/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ func (volume DirectPVVolume) IsSuspended() bool {
return string(volume.getLabel(types.SuspendLabelKey)) == strconv.FormatBool(true)
}

// SetUniqueAllocID sets the provided allocation id on the volume.
func (volume *DirectPVVolume) SetUniqueAllocID(allocID string) {
volume.SetLabel(types.UniqueAllocIDLabelKey, types.LabelValue(allocID))
// SetClaimID sets the provided claim id on the volume.
func (volume *DirectPVVolume) SetClaimID(claimID string) {
volume.SetLabel(types.ClaimIDLabelKey, types.LabelValue(claimID))
}

// GetUniqueAllocID gets the provided allocation id on the volume.
func (volume *DirectPVVolume) GetUniqueAllocID() string {
return string(volume.getLabel(types.UniqueAllocIDLabelKey))
// GetClaimID gets the claim id set on the volume.
func (volume *DirectPVVolume) GetClaimID() string {
return string(volume.getLabel(types.ClaimIDLabelKey))
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
18 changes: 9 additions & 9 deletions pkg/csi/controller/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,18 @@ func (c *Server) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
return nil, status.Errorf(codes.InvalidArgument, "unsupported filesystem type %v for volume %v", req.GetVolumeCapabilities()[0].GetMount().GetFsType(), name)
}

var uniqueAllocID string
var volumeClaimID string
for key, value := range req.GetParameters() {
switch key {
case string(directpvtypes.AccessTierLabelKey):
if _, err := directpvtypes.StringsToAccessTiers(value); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "unknown access-tier %v for volume %v; %v", value, name, err)
}
case string(directpvtypes.UniqueAllocIDLabelKey):
if err := validAllocID(value); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid %v value; %v", directpvtypes.UniqueAllocIDLabelKey, err)
case string(directpvtypes.VolumeClaimIDLabelKey):
if err := validVolumeClaimID(value); err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid %v value; %v", directpvtypes.VolumeClaimIDLabelKey, err)
}
uniqueAllocID = value
volumeClaimID = value
}
}

Expand Down Expand Up @@ -184,8 +184,8 @@ func (c *Server) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
drive.GetDriveName(),
size,
)
if uniqueAllocID != "" {
newVolume.SetUniqueAllocID(uniqueAllocID)
if volumeClaimID != "" {
newVolume.SetClaimID(volumeClaimID)
}

if _, err := client.VolumeClient().Create(ctx, newVolume, metav1.CreateOptions{}); err != nil {
Expand Down Expand Up @@ -216,8 +216,8 @@ func (c *Server) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
}

if drive.AddVolumeFinalizer(req.GetName()) {
if uniqueAllocID != "" {
drive.SetUniqueAllocID(uniqueAllocID)
if volumeClaimID != "" {
drive.SetVolumeClaimID(volumeClaimID)
}
drive.Status.FreeCapacity -= size
drive.Status.AllocatedCapacity += size
Expand Down
12 changes: 6 additions & 6 deletions pkg/csi/controller/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func matchDrive(drive *types.Drive, req *csi.CreateVolumeRequest) bool {
if len(accessTiers) > 0 && drive.GetAccessTier() != accessTiers[0] {
return false
}
case string(directpvtypes.UniqueAllocIDLabelKey):
if value != "" && drive.HasUniqueAllocID(value) {
// Do not allocate another volume with this unique alloc id
case string(directpvtypes.VolumeClaimIDLabelKey):
if value != "" && drive.HasVolumeClaimID(value) {
// Do not allocate another volume with this claim id
return false
}
default:
Expand Down Expand Up @@ -165,10 +165,10 @@ func selectDrive(ctx context.Context, req *csi.CreateVolumeRequest) (*types.Driv
return &maxFreeCapacityDrives[n.Int64()], nil
}

func validAllocID(allocID string) error {
_, err := directpvtypes.NewLabelKey(directpvtypes.UniqueAllocIDLabelKeyPrefix + allocID)
func validVolumeClaimID(claimID string) error {
_, err := directpvtypes.NewLabelKey(directpvtypes.VolumeClaimIDLabelKeyPrefix + claimID)
if err == nil {
_, err = directpvtypes.NewLabelValue(allocID)
_, err = directpvtypes.NewLabelValue(claimID)
}
return err
}
4 changes: 2 additions & 2 deletions pkg/csi/controller/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,13 @@ func TestGetFilteredDrives(t *testing.T) {
"node-1",
directpvtypes.DriveName("sdd"),
map[directpvtypes.LabelKey]directpvtypes.LabelValue{
consts.GroupName + "/unique-alloc-id-xxx": "true",
consts.GroupName + "/volume-claim-id-xxx": "true",
},
),
}
case15Request := &csi.CreateVolumeRequest{
Name: "volume-1",
Parameters: map[string]string{consts.GroupName + "/unique-alloc-id": "xxx"},
Parameters: map[string]string{consts.GroupName + "/volume-claim-id": "xxx"},
}
case15Result := []types.Drive{
*types.NewDrive(
Expand Down
4 changes: 2 additions & 2 deletions pkg/volume/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func (handler *volumeEventHandler) releaseVolume(ctx context.Context, volume *ty

drive.Status.FreeCapacity += volume.Status.TotalCapacity
drive.Status.AllocatedCapacity = drive.Status.TotalCapacity - drive.Status.FreeCapacity
if uniqueAllocID := volume.GetUniqueAllocID(); uniqueAllocID != "" {
drive.RemoveUniqueAllocID(uniqueAllocID)
if claimID := volume.GetClaimID(); claimID != "" {
drive.RemoveVolumeClaimID(claimID)
}
_, err = client.DriveClient().Update(
ctx, drive, metav1.UpdateOptions{TypeMeta: types.NewDriveTypeMeta()},
Expand Down

0 comments on commit 54cd3c0

Please sign in to comment.