Skip to content

Commit

Permalink
feat(vd): override virtualdisk's pvc parameters via StorageClass anno…
Browse files Browse the repository at this point in the history
…tations (#351)

Signed-off-by: yaroslavborbat <[email protected]>
  • Loading branch information
yaroslavborbat authored Sep 10, 2024
1 parent 8df6c59 commit fa37881
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
6 changes: 6 additions & 0 deletions images/virtualization-artifact/pkg/controller/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ const (
// AnnImmediateBinding provides a const to indicate whether immediate binding should be performed on the PV (overrides global config)
AnnImmediateBinding = AnnAPIGroup + "/storage.bind.immediate.requested"

AnnAPIGroupV = "virtualization.deckhouse.io"
AnnVirtualDisk = "virtualdisk." + AnnAPIGroupV
AnnVirtualDiskVolumeMode = AnnVirtualDisk + "/volume-mode"
AnnVirtualDiskAccessMode = AnnVirtualDisk + "/access-mode"
AnnVirtualDiskBindingMode = AnnVirtualDisk + "/binding-mode"

// AnnSelectedNode annotation is added to a PVC that has been triggered by scheduler to
// be dynamically provisioned. Its value is the name of the selected node.
AnnSelectedNode = "volume.kubernetes.io/selected-node"
Expand Down
4 changes: 4 additions & 0 deletions images/virtualization-artifact/pkg/controller/kvbuilder/dv.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (b *DV) SetPVC(storageClassName *string,
)
}

func (b *DV) SetImmediate() {
b.AddAnnotation("cdi.kubevirt.io/storage.bind.immediate.requested", "true")
}

func (b *DV) SetDataSource(source *cdiv1.DataVolumeSource) {
b.Resource.Spec.Source = source
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,36 @@ func (s DiskService) Start(
dvBuilder.SetDataSource(source)
dvBuilder.SetOwnerRef(obj, obj.GroupVersionKind())

sc, err := s.GetStorageClass(ctx, storageClass)
if err != nil {
return err
}

var (
accessMode corev1.PersistentVolumeAccessMode
volumeMode corev1.PersistentVolumeMode
)

sprofile, err := s.GetStorageProfile(ctx, storageClass)
if err != nil {
return err
}
storageCaps := s.parseVolumeMode(sprofile.Status)
storageCaps := s.parseStorageCapabilities(sprofile.Status)
accessMode = storageCaps.AccessMode
volumeMode = storageCaps.VolumeMode

if m, override := s.parseVolumeMode(sc); override {
volumeMode = m
}
if m, override := s.parseAccessMode(sc); override {
accessMode = m
}

dvBuilder.SetPVC(ptr.To(sprofile.GetName()), pvcSize, accessMode, volumeMode)

dvBuilder.SetPVC(ptr.To(sprofile.GetName()), pvcSize, storageCaps.AccessMode, storageCaps.VolumeMode)
if s.parseImmediateBindingMode(sc) {
dvBuilder.SetImmediate()
}

err = s.client.Create(ctx, dvBuilder.GetResource())
if err != nil && !k8serrors.IsAlreadyExists(err) {
Expand Down Expand Up @@ -296,7 +319,42 @@ func getAccessModeMax(modes []corev1.PersistentVolumeAccessMode) corev1.Persiste
return m
}

func (s DiskService) parseVolumeMode(status cdiv1.StorageProfileStatus) StorageCapabilities {
func (s DiskService) parseVolumeMode(sc *storev1.StorageClass) (corev1.PersistentVolumeMode, bool) {
if sc == nil {
return "", false
}
switch sc.GetAnnotations()[common.AnnVirtualDiskVolumeMode] {
case string(corev1.PersistentVolumeBlock):
return corev1.PersistentVolumeBlock, true
case string(corev1.PersistentVolumeFilesystem):
return corev1.PersistentVolumeFilesystem, true
default:
return "", false
}
}

func (s DiskService) parseAccessMode(sc *storev1.StorageClass) (corev1.PersistentVolumeAccessMode, bool) {
if sc == nil {
return "", false
}
switch sc.GetAnnotations()[common.AnnVirtualDiskAccessMode] {
case string(corev1.ReadWriteOnce):
return corev1.ReadWriteOnce, true
case string(corev1.ReadWriteMany):
return corev1.ReadWriteMany, true
default:
return "", false
}
}

func (s DiskService) parseImmediateBindingMode(sc *storev1.StorageClass) bool {
if sc == nil {
return false
}
return sc.GetAnnotations()[common.AnnVirtualDiskBindingMode] == string(storev1.VolumeBindingImmediate)
}

func (s DiskService) parseStorageCapabilities(status cdiv1.StorageProfileStatus) StorageCapabilities {
var storageCapabilities []StorageCapabilities
for _, cp := range status.ClaimPropertySets {
var mode corev1.PersistentVolumeMode
Expand Down

0 comments on commit fa37881

Please sign in to comment.