Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mkfs options parameter to volume context #512

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ build-linux:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o $(BINARY_NAME) -v ./cmd/

docker-build:
docker build $(BUILDARGS) -t ${IMG} -f Dockerfile .
docker build $(BUILDARGS) -t ${IMG} -f Dockerfile . --load

docker-push:
docker push ${IMG}
Expand Down
4 changes: 4 additions & 0 deletions pkg/driver/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const (
ParameterNodeID = "node_id"
// ParameterDeviceName is the device name parameter
ParameterDeviceName = "device_name"
// ParameterReadOnly is the name of the parameter used to specify whether the volume should be mounted as read-only
ParameterReadOnly = "readOnly"
// ParameterMkfsOptions is the name of the parameter used to specify the options for the mkfs command
ParameterMkfsOptions = "mkfs_options"

CSIDriverName = "csi.ironcore.dev"
topologyKey = "topology." + CSIDriverName + "/zone"
Expand Down
8 changes: 7 additions & 1 deletion pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func (d *driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
fstype = FSTypeExt4
}

mkfsOptions, ok := params[ParameterMkfsOptions]
if !ok {
mkfsOptions = ""
}

volumeClass, ok := params[ParameterType]
if !ok {
return nil, status.Errorf(codes.Internal, "Required parameter %s is missing", ParameterType)
Expand Down Expand Up @@ -136,6 +141,7 @@ func (d *driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
ParameterVolumePool: volumePoolName,
ParameterCreationTime: time.Unix(volume.CreationTimestamp.Unix(), 0).String(),
ParameterFSType: fstype,
ParameterMkfsOptions: mkfsOptions,
},
ContentSource: req.GetVolumeContentSource(),
AccessibleTopology: accessibleTopology,
Expand Down Expand Up @@ -461,7 +467,7 @@ func validateDeviceName(volume *storagev1alpha1.Volume, machine *computev1alpha1
}
}
}
return "", fmt.Errorf("failed to get device name of volume %s name from machine %s", client.ObjectKeyFromObject(volume), client.ObjectKeyFromObject(machine))
return "", fmt.Errorf("failed to get device name of volume %s from machine %s", client.ObjectKeyFromObject(volume), client.ObjectKeyFromObject(machine))
}

func isValidVolumeCapabilities(volCaps []*csi.VolumeCapability) bool {
Expand Down
29 changes: 18 additions & 11 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io"
"os"
"path/filepath"
"strings"

"github.com/container-storage-interface/spec/lib/go/csi"
"golang.org/x/sys/unix"
Expand All @@ -33,7 +34,8 @@ var (

func (d *driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
klog.InfoS("Staging volume on node ", "Volume", req.GetVolumeId(), "StagingTargetPath", req.GetStagingTargetPath())
fstype := req.GetVolumeContext()[ParameterFSType]
volumeContext := req.GetVolumeContext()
fstype := volumeContext[ParameterFSType]
devicePath := req.PublishContext[ParameterDeviceName]

klog.InfoS("Check if the device path exists")
Expand All @@ -42,16 +44,10 @@ func (d *driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
// We will requeue here since the device path is not ready yet.
return nil, status.Errorf(codes.Unavailable, "Device path %s does not exist: %v", devicePath, err)
} else {
return nil, status.Errorf(codes.Internal, "Failed to determine wether the device path %s exists: %v", devicePath, err)
return nil, status.Errorf(codes.Internal, "Failed to determine whether the device path %s exists: %v", devicePath, err)
}
}

readOnly := false
if req.GetVolumeContext()["readOnly"] == "true" {
readOnly = true
}
mountOptions := req.GetVolumeCapability().GetMount().GetMountFlags()

targetPath := req.GetStagingTargetPath()
klog.InfoS("Validate mount point", "MountPoint", targetPath)
notMnt, err := d.mounter.IsLikelyNotMountPoint(targetPath)
Expand All @@ -68,16 +64,27 @@ func (d *driver) NodeStageVolume(_ context.Context, req *csi.NodeStageVolumeRequ
return nil, status.Errorf(codes.Internal, "Failed to create target directory %s for volume %s: %v", targetPath, req.GetVolumeId(), err)
}

var options []string
mountOptions := req.GetVolumeCapability().GetMount().GetMountFlags()

if readOnly {
readOnly, ok := volumeContext[ParameterReadOnly]
readOnlyFlag := ok && readOnly == "true"

var options []string
if readOnlyFlag {
options = append(options, "ro")
} else {
options = append(options, "rw")
}
options = append(options, mountOptions...)

var formatOptions []string
mkfsOptions, ok := volumeContext[ParameterMkfsOptions]
if ok && mkfsOptions != "" {
formatOptions = append(formatOptions, strings.Split(mkfsOptions, " ")...)
}

klog.InfoS("Format and mount the volume")
if err = d.mounter.FormatAndMount(devicePath, targetPath, fstype, options); err != nil {
if err = d.mounter.FormatAndMountSensitiveWithFormatOptions(devicePath, targetPath, fstype, options, nil, formatOptions); err != nil {
return nil, status.Errorf(codes.Internal, "Failed to mount volume %s [%s] to %s: %v", devicePath, fstype, targetPath, err)
}
klog.InfoS("Staged volume on node", "Volume", req.GetVolumeId())
Expand Down
4 changes: 2 additions & 2 deletions pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var _ = Describe("Node", func() {
It("should fail if the mount operation fails", func(ctx SpecContext) {
mockMounter.EXPECT().IsLikelyNotMountPoint(targetPath).Return(true, nil)
mockOS.EXPECT().MkdirAll(targetPath, os.FileMode(0750)).Return(nil)
mockMounter.EXPECT().FormatAndMount(devicePath, targetPath, fstype, mountOptions).Return(errors.New("failed to mount volume"))
mockMounter.EXPECT().FormatAndMountSensitiveWithFormatOptions(devicePath, targetPath, fstype, mountOptions, nil, nil).Return(errors.New("failed to mount volume"))
_, err := drv.NodeStageVolume(ctx, req)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("Failed to mount volume"))
Expand All @@ -126,7 +126,7 @@ var _ = Describe("Node", func() {
It("should stage the volume", func(ctx SpecContext) {
mockMounter.EXPECT().IsLikelyNotMountPoint(targetPath).Return(true, nil)
mockOS.EXPECT().MkdirAll(targetPath, os.FileMode(0750)).Return(nil)
mockMounter.EXPECT().FormatAndMount(devicePath, targetPath, fstype, mountOptions).Return(nil)
mockMounter.EXPECT().FormatAndMountSensitiveWithFormatOptions(devicePath, targetPath, fstype, mountOptions, nil, nil).Return(nil)
_, err := drv.NodeStageVolume(ctx, req)
Expect(err).NotTo(HaveOccurred())
})
Expand Down
12 changes: 6 additions & 6 deletions pkg/utils/mount/mock_mountutils_unix.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/utils/mount/mountutils_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// SafeFormatAndMount). Defined it explicitly so that it can be mocked.
type MountWrapper interface {
k8smountutils.Interface
FormatAndMount(source string, target string, fstype string, options []string) error
FormatAndMountSensitiveWithFormatOptions(source string, target string, fstype string, options []string, sensitiveOptions []string, formatOptions []string) error
NewResizeFs() (Resizefs, error)
}

Expand Down
Loading