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

chore: add support for ReadWriteOncePod access mode #3236

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shikanime
Copy link

Signed-off-by: William Phetsinorath [email protected]

@derekbit
Copy link
Member

@shikanime Awesome! Thanks for your contribution.
@james-munson Please help review the PR if you have time. Thank you.

Copy link

coderabbitai bot commented Oct 24, 2024

Walkthrough

The changes in this pull request enhance the volume management system by introducing new access modes for volumes and refining error handling across various methods. Key updates include the addition of three new access modes in the ControllerServer, enhanced logic for handling exclusive access in volume creation and publishing, and the introduction of a new function to determine exclusive access requirements. Additionally, the access mode validation logic has been updated to accommodate the new modes, ensuring robust handling of volume options.

Changes

File Change Summary
csi/controller_server.go Updated NewControllerServer, CreateVolume, and ControllerPublishVolume methods to incorporate new access modes and exclusive access logic.
csi/util.go Added exclusive parameter to getVolumeOptions and introduced requireExclusiveAccess function for exclusive access checks. Enhanced error handling for volume options.
datastore/kubernetes.go Modified NewPVCManifestForVolume to utilize a switch statement for handling multiple access modes, including AccessModeReadWriteOncePod.
k8s/pkg/apis/longhorn/v1beta1/volume.go Added new access mode constant AccessModeReadWriteOncePod.
k8s/pkg/apis/longhorn/v1beta2/volume.go Added new access mode constant AccessModeReadWriteOncePod and updated validation annotation for access modes.
types/types.go Updated ValidateAccessMode function to include AccessModeReadWriteOncePod as a valid access mode.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ControllerServer
    participant VolumeManager
    participant PVCManager

    User->>ControllerServer: Create Volume Request
    ControllerServer->>VolumeManager: Validate Access Modes
    VolumeManager->>ControllerServer: Access Modes Validated
    ControllerServer->>VolumeManager: Create Volume
    VolumeManager->>ControllerServer: Volume Created
    ControllerServer->>User: Volume Creation Response

    User->>ControllerServer: Publish Volume Request
    ControllerServer->>VolumeManager: Check Exclusive Access
    VolumeManager->>ControllerServer: Exclusive Access Validated
    ControllerServer->>PVCManager: Update PVC Manifest
    PVCManager->>ControllerServer: PVC Manifest Updated
    ControllerServer->>User: Volume Published Response
Loading

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b76fbc8 and 7b00b88.

📒 Files selected for processing (6)
  • csi/controller_server.go (3 hunks)
  • csi/util.go (2 hunks)
  • datastore/kubernetes.go (1 hunks)
  • k8s/pkg/apis/longhorn/v1beta1/volume.go (1 hunks)
  • k8s/pkg/apis/longhorn/v1beta2/volume.go (1 hunks)
  • types/types.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • k8s/pkg/apis/longhorn/v1beta1/volume.go
  • k8s/pkg/apis/longhorn/v1beta2/volume.go
  • types/types.go
🧰 Additional context used
🪛 GitHub Check: CodeFactor
csi/util.go

[notice] 471-471: csi/util.go#L471
Redefinition of the built-in function cap. (redefines-builtin-id)

🔇 Additional comments (6)
csi/util.go (2)

121-137: LGTM! Well-structured implementation of exclusive access mode support.

The implementation correctly:

  • Validates the exclusive parameter
  • Prevents conflicting access modes (share + exclusive)
  • Sets appropriate default access mode

471-483: LGTM! Clean implementation of exclusive access check.

The function correctly determines exclusive access requirement by checking both:

  • Volume's ReadWriteOncePod access mode
  • CSI capability's SINGLE_NODE_SINGLE_WRITER mode
🧰 Tools
🪛 GitHub Check: CodeFactor

[notice] 471-471: csi/util.go#L471
Redefinition of the built-in function cap. (redefines-builtin-id)

datastore/kubernetes.go (1)

1018-1022: LGTM! Consider adding a version compatibility comment.

The switch statement is a clean way to handle multiple access modes and makes the code more maintainable. The implementation correctly maps Longhorn's access modes to Kubernetes PVC access modes.

Consider adding a comment about the Kubernetes version requirement since ReadWriteOncePod requires Kubernetes v1.27+.

Let's verify the usage of ReadWriteOncePod access mode:

✅ Verification successful

Let me gather more information about the version requirements and documentation.


Based on the gathered information, I can now provide the final response:

Add version compatibility notice for ReadWriteOncePod access mode

The implementation is correct, but since ReadWriteOncePod requires Kubernetes v1.27+, we should add a comment to document this requirement. The codebase is already using Kubernetes v1.31.1 (from go.mod), which fully supports this feature.

  • Add a comment above the switch statement in datastore/kubernetes.go:
    // Note: AccessModeReadWriteOncePod requires Kubernetes v1.27+
    switch v.Spec.AccessMode {
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for ReadWriteOncePod usage and version requirements
# Test 1: Search for ReadWriteOncePod usage in code
echo "=== Searching for ReadWriteOncePod usage ==="
rg -l "ReadWriteOncePod"

# Test 2: Check if version requirement is documented
echo -e "\n=== Checking version requirement documentation ==="
rg -i "kubernetes.*1\.27|ReadWriteOncePod.*requirement"

Length of output: 1146


Script:

#!/bin/bash
# Check documentation files and version requirements
echo "=== Checking README and documentation ==="
fd -e md | xargs rg -i "kubernetes.*version|ReadWriteOncePod"

echo -e "\n=== Checking version information in go.mod ==="
cat go.mod | grep -i "k8s.io"

echo -e "\n=== Checking for any version-related constants or variables ==="
rg -i "MinimumKubernetesVersion|minimum.*version|supported.*version" --type go

Length of output: 31893

csi/controller_server.go (3)

72-73: Approved inclusion of new access modes

The addition of csi.VolumeCapability_AccessMode_SINGLE_NODE_SINGLE_WRITER and csi.VolumeCapability_AccessMode_SINGLE_NODE_MULTI_WRITER appropriately expands the supported volume access modes.


225-228: Approved logic for setting exclusive volume parameter

The code correctly sets the exclusive parameter when requireExclusiveAccess returns true in the CreateVolume method, ensuring that volumes requiring exclusive access are properly handled.


463-469: Approved update of volume access mode for exclusive access

The update to change the volume's access mode to ReadWriteOncePod when exclusive access is required in the ControllerPublishVolume method is correct and ensures exclusive access is enforced.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
k8s/pkg/apis/longhorn/v1beta1/volume.go (1)

62-64: LGTM! The new access mode aligns with Kubernetes standards.

The addition of AccessModeReadWriteOncePod follows the Kubernetes storage specification correctly. This mode provides stronger guarantees than ReadWriteOnce by ensuring exclusive access at the pod level rather than the node level.

Note that this access mode requires Kubernetes 1.22+ and provides better isolation for workloads requiring strict exclusive access, such as databases or stateful applications with strict consistency requirements.

types/types.go (1)

847-847: LGTM! Consider improving readability.

The addition of AccessModeReadWriteOncePod is correct and aligns with the PR objective.

Consider improving readability by formatting the condition across multiple lines:

-	if mode != longhorn.AccessModeReadWriteMany && mode != longhorn.AccessModeReadWriteOnce  && mode != longhorn.AccessModeReadWriteOncePod {
+	if mode != longhorn.AccessModeReadWriteMany &&
+		mode != longhorn.AccessModeReadWriteOnce &&
+		mode != longhorn.AccessModeReadWriteOncePod {
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e7db6e7 and b76fbc8.

📒 Files selected for processing (6)
  • csi/controller_server.go (3 hunks)
  • csi/util.go (2 hunks)
  • datastore/kubernetes.go (1 hunks)
  • k8s/pkg/apis/longhorn/v1beta1/volume.go (1 hunks)
  • k8s/pkg/apis/longhorn/v1beta2/volume.go (1 hunks)
  • types/types.go (1 hunks)
🧰 Additional context used
🪛 GitHub Check: CodeFactor
csi/util.go

[notice] 471-471: csi/util.go#L471
Redefinition of the built-in function cap. (redefines-builtin-id)

🔇 Additional comments (9)
k8s/pkg/apis/longhorn/v1beta2/volume.go (2)

60-62: LGTM! Constants follow Kubernetes conventions

The new AccessModeReadWriteOncePod constant follows proper naming conventions and uses the standard "rwop" abbreviation that aligns with Kubernetes CSI specifications.


56-62: Verify integration with Kubernetes CSI and existing volumes

While the implementation looks correct, please ensure:

  1. Existing volumes with other access modes continue to work
  2. CSI driver properly handles the new access mode
  3. Documentation is updated to reflect this new capability

Let's check for any existing volumes that might be affected:

datastore/kubernetes.go (1)

1018-1022: LGTM! Clean implementation of ReadWriteOncePod access mode support.

The switch statement implementation correctly maps the Longhorn access mode to the corresponding Kubernetes PVC access mode.

Let's verify the access mode constant is properly defined:

✅ Verification successful

AccessModeReadWriteOncePod constant is properly defined and consistent across API versions

The constant is correctly defined in both v1beta1 and v1beta2 API versions with the same value "rwop", ensuring backward compatibility and proper type safety. The implementation in the switch statement correctly uses this constant.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify AccessModeReadWriteOncePod is defined in the types
rg "AccessModeReadWriteOncePod.*=.*" -A 2

Length of output: 484

csi/controller_server.go (4)

72-73: LGTM: Access mode additions follow CSI spec

The new access modes SINGLE_NODE_SINGLE_WRITER and SINGLE_NODE_MULTI_WRITER enhance the granularity of volume access control and align with the CSI specification.


225-228: LGTM: Proper exclusive access handling in volume creation

The exclusive access check is properly implemented and follows the same pattern as the shared access check. The volumeParameters are correctly updated when exclusive access is required.


463-469: LGTM: Proper exclusive access handling in volume publishing

The exclusive access handling in ControllerPublishVolume is well-implemented:

  • Follows the same pattern as shared access handling
  • Properly updates volume access mode to ReadWriteOncePod when required
  • Maintains consistent error handling

463-469: Verify the requireExclusiveAccess implementation

Let's verify the implementation of the requireExclusiveAccess function to ensure proper handling of exclusive access modes.

✅ Verification successful

Based on the shell script results, I can now generate the final response as we have sufficient information about both the requireExclusiveAccess implementation and the access mode update mechanism.

Implementation verified - Access mode handling is properly implemented

The requireExclusiveAccess function in csi/util.go correctly handles exclusive access by:

  • Checking if the volume's current access mode is ReadWriteOncePod
  • Verifying if the requested capability is SINGLE_NODE_SINGLE_WRITER
  • Properly combining both conditions with logical OR

The access mode update flow is also well-implemented:

  • Changes are handled through a proper API client interface
  • Updates are processed via the volume manager with appropriate error handling
  • The operation is atomic at the API level
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for the requireExclusiveAccess function implementation
ast-grep --pattern 'func requireExclusiveAccess($_, $_) bool {
  $$$
}'

# Search for potential race conditions in access mode changes
rg -A 5 'UpdateAccessMode|ActionUpdateAccessMode'

Length of output: 12110

csi/util.go (2)

121-132: The 'exclusive' parameter is correctly handled and mutually exclusive with 'share'.

The logic implemented ensures that if 'exclusive' is set to true, it cannot be combined with 'share' set to true, preventing conflicting access modes. The assignment of ReadWriteOncePod to vol.AccessMode when 'exclusive' is true is appropriate.


134-136: Default access mode set to 'ReadWriteOnce' when unspecified.

The code correctly assigns ReadWriteOnce as the default vol.AccessMode when no access mode is specified, ensuring consistent behavior.

k8s/pkg/apis/longhorn/v1beta2/volume.go Outdated Show resolved Hide resolved
csi/util.go Show resolved Hide resolved
@shikanime shikanime closed this Oct 24, 2024
@shikanime shikanime reopened this Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants