From 15a6cc21931d9061c9a645e2f6f3daf7ccabc393 Mon Sep 17 00:00:00 2001 From: andyzhangx Date: Wed, 1 Jan 2025 09:35:49 +0000 Subject: [PATCH] feat: add accessMode in storageClassMap fix --- .../example/default_example_provisioner_generated.yaml | 1 + docs/provisioner.md | 2 ++ helm/examples/baremetal-default-storage.yaml | 1 + helm/provisioner/values.yaml | 2 ++ pkg/common/common.go | 10 +++++++++- pkg/discovery/discovery.go | 9 +++++++-- 6 files changed, 22 insertions(+), 3 deletions(-) diff --git a/deployment/kubernetes/example/default_example_provisioner_generated.yaml b/deployment/kubernetes/example/default_example_provisioner_generated.yaml index b0e4fd24d..854b8d985 100644 --- a/deployment/kubernetes/example/default_example_provisioner_generated.yaml +++ b/deployment/kubernetes/example/default_example_provisioner_generated.yaml @@ -15,6 +15,7 @@ data: - "/scripts/shred.sh" - "2" volumeMode: Filesystem + accessMode: ReadWriteOnce fsType: ext4 namePattern: "*" --- diff --git a/docs/provisioner.md b/docs/provisioner.md index 190c90c2a..fe7d7ecca 100644 --- a/docs/provisioner.md +++ b/docs/provisioner.md @@ -128,6 +128,8 @@ data: # # intended to use as a formatted filesystem volume or to remain in block # # state. Value of Filesystem is implied when omitted. # volumeMode: Filesystem + # # Access mode of the volume. default to ReadWriteOnce if not specified. + # accessMode: ReadWriteOnce # # The filesystem to format before mounting on the node. This applies # # only when the volume source is a device and mode is Filesystem. # # The default value is to auto-select a filesystem in Kubernetes if unspecified. diff --git a/helm/examples/baremetal-default-storage.yaml b/helm/examples/baremetal-default-storage.yaml index d200b8490..1a2ca4a6c 100644 --- a/helm/examples/baremetal-default-storage.yaml +++ b/helm/examples/baremetal-default-storage.yaml @@ -2,6 +2,7 @@ classes: - name: local-storage hostDir: /mnt/disks volumeMode: Filesystem + accessMode: ReadWriteOnce storageClass: # create and set storage class as default isDefaultClass: true diff --git a/helm/provisioner/values.yaml b/helm/provisioner/values.yaml index 8541a98dc..1b5e827a3 100644 --- a/helm/provisioner/values.yaml +++ b/helm/provisioner/values.yaml @@ -62,6 +62,8 @@ classes: # The volume mode of created PersistentVolume object. Default to Filesystem # if not specified. volumeMode: Filesystem + # Access mode of the volume. default to ReadWriteOnce if not specified. + accessMode: ReadWriteOnce # Filesystem type to mount. # It applies only when the source path is a block device, # and desire volume mode is Filesystem. diff --git a/pkg/common/common.go b/pkg/common/common.go index 023882c68..87c323104 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -132,6 +132,9 @@ type MountConfig struct { // The volume mode of created PersistentVolume object, // default to Filesystem if not specified. VolumeMode string `json:"volumeMode" yaml:"volumeMode"` + // The access mode of created PersistentVolume object + // default to ReadWriteOnce if not specified. + AccessMode string `json:"accessMode" yaml:"accessMode"` // Filesystem type to mount. // It applies only when the source path is a block device, // and desire volume mode is Filesystem. @@ -180,6 +183,7 @@ type LocalPVConfig struct { AffinityAnn string NodeAffinity *v1.VolumeNodeAffinity VolumeMode v1.PersistentVolumeMode + AccessMode v1.PersistentVolumeAccessMode MountOptions []string FsType *string Labels map[string]string @@ -248,7 +252,7 @@ func CreateLocalPVSpec(config *LocalPVConfig) *v1.PersistentVolume { }, }, AccessModes: []v1.PersistentVolumeAccessMode{ - v1.ReadWriteOnce, + config.AccessMode, }, StorageClassName: config.StorageClass, VolumeMode: &config.VolumeMode, @@ -256,6 +260,10 @@ func CreateLocalPVSpec(config *LocalPVConfig) *v1.PersistentVolume { }, } + if config.AccessMode == "" { + pv.Spec.AccessModes = []v1.PersistentVolumeAccessMode{v1.ReadWriteOnce} + } + if config.UseAlphaAPI { pv.ObjectMeta.Annotations[AlphaStorageNodeAffinityAnnotation] = config.AffinityAnn } else { diff --git a/pkg/discovery/discovery.go b/pkg/discovery/discovery.go index ba76f13ff..1ef19bec2 100644 --- a/pkg/discovery/discovery.go +++ b/pkg/discovery/discovery.go @@ -305,6 +305,10 @@ func (d *Discoverer) discoverVolumesAtPath(class string, config common.MountConf } var capacityByte int64 + desiredAccessMode := v1.ReadWriteOnce + if config.AccessMode != "" { + desiredAccessMode = v1.PersistentVolumeAccessMode(config.AccessMode) + } desireVolumeMode := v1.PersistentVolumeMode(config.VolumeMode) switch volMode { case v1.PersistentVolumeBlock: @@ -345,7 +349,7 @@ func (d *Discoverer) discoverVolumesAtPath(class string, config common.MountConf continue } - err = d.createPV(file, class, reclaimPolicy, mountOptions, config, capacityByte, desireVolumeMode, startTime) + err = d.createPV(file, class, reclaimPolicy, mountOptions, config, capacityByte, desireVolumeMode, desiredAccessMode, startTime) if err != nil { discoErrors = append(discoErrors, err) } @@ -367,7 +371,7 @@ func generatePVName(file, node, class string) string { return fmt.Sprintf("local-pv-%x", h.Sum32()) } -func (d *Discoverer) createPV(file, class string, reclaimPolicy v1.PersistentVolumeReclaimPolicy, mountOptions []string, config common.MountConfig, capacityByte int64, volMode v1.PersistentVolumeMode, startTime time.Time) error { +func (d *Discoverer) createPV(file, class string, reclaimPolicy v1.PersistentVolumeReclaimPolicy, mountOptions []string, config common.MountConfig, capacityByte int64, volMode v1.PersistentVolumeMode, accessMode v1.PersistentVolumeAccessMode, startTime time.Time) error { pvName := generatePVName(file, d.Node.Name, class) outsidePath := filepath.Join(config.HostDir, file) @@ -382,6 +386,7 @@ func (d *Discoverer) createPV(file, class string, reclaimPolicy v1.PersistentVol ReclaimPolicy: reclaimPolicy, ProvisionerName: d.Name, VolumeMode: volMode, + AccessMode: accessMode, Labels: d.Labels, MountOptions: mountOptions, SetPVOwnerRef: d.SetPVOwnerRef,