-
Notifications
You must be signed in to change notification settings - Fork 607
nvmeof: rearrangement create nvmeof resources #6237
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,13 @@ limitations under the License. | |
|
|
||
| package nvmeof | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "strconv" | ||
| ) | ||
|
|
||
| // NVMeoFVolumeData holds the data required for an NVMe-oF volume. | ||
| type NVMeoFVolumeData struct { | ||
| SubsystemNQN string | ||
|
|
@@ -44,3 +51,75 @@ func (v *NVMeoFVolumeData) SetListenersWithDefaults() { | |
| } | ||
| } | ||
| } | ||
|
|
||
| // SetupListeners parses the listeners JSON and validates the required fields. | ||
| // It returns a slice of ListenerDetails or an error if the JSON is invalid | ||
| // or if required fields are missing. | ||
| func SetupListeners(listenersJSON string) ([]ListenerDetails, error) { | ||
| if listenersJSON == "" { // No "listeners" entry was provided | ||
| return []ListenerDetails{}, nil | ||
| } | ||
| var listeners []ListenerDetails | ||
| if err := json.Unmarshal([]byte(listenersJSON), &listeners); err != nil { | ||
| return nil, fmt.Errorf("failed to parse listeners JSON: %w", err) | ||
| } | ||
|
|
||
| if len(listeners) == 0 { // At least one listener is required | ||
| return nil, errors.New("at least one listener must be specified") | ||
| } | ||
|
|
||
| // Validate each listener | ||
| // Listener address can be empty. It will be set later to the default 0.0.0.0. | ||
| // Port can be empty (it will be set later to the default 4420). | ||
| for i, listener := range listeners { | ||
| if listener.Hostname == "" { | ||
| return nil, fmt.Errorf("listener %d: missing required fields (hostname)", i) | ||
| } | ||
| } | ||
|
|
||
| return listeners, nil | ||
| } | ||
|
|
||
| // SetFromParameters populates the NVMeoFVolumeData fields based on the provided parameters map. | ||
| // It extracts the subsystem NQN, gateway management info, security config, and | ||
| // listener info from the parameters. | ||
| // It also applies default values to listeners if necessary. | ||
| func (v *NVMeoFVolumeData) SetFromParameters(parameters map[string]string) error { | ||
| // set subsystem NQN | ||
| v.SubsystemNQN = parameters["subsystemNQN"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered returning an error for required parameters?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about it, but at the beginning of |
||
|
|
||
| // set gw management info | ||
| if nvmeofGatewayPortStr := parameters["nvmeofGatewayPort"]; nvmeofGatewayPortStr != "" { | ||
| parsed, err := strconv.ParseUint(nvmeofGatewayPortStr, 10, 32) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid nvmeofGatewayPort %s: %w", nvmeofGatewayPortStr, err) | ||
| } | ||
| v.GatewayManagementInfo.Port = uint32(parsed) | ||
| } | ||
| v.GatewayManagementInfo.Address = parameters["nvmeofGatewayAddress"] | ||
|
|
||
| // set security config | ||
| v.Security.DhchapMode = parameters["dhchapMode"] | ||
| v.Security.AuthenticationKMSID = parameters["authenticationKMSID"] | ||
|
|
||
| // If dhchapMode was explicitly provided and is not "none", and authenticationKMSID is empty, | ||
| // use a default KMS ID - RBD metadata KMS. | ||
| // In production, users should always provide a KMS ID when using DH-CHAP. | ||
| if v.Security.DhchapMode != DHCHAPEmpty && | ||
| v.Security.DhchapMode != DHCHAPModeNone && | ||
| v.Security.AuthenticationKMSID == "" { | ||
| v.Security.AuthenticationKMSID = "metadata" | ||
| } | ||
|
|
||
| // set listeners | ||
| listeners, err := SetupListeners(parameters["listeners"]) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to set up listeners: %w", err) | ||
| } | ||
| v.ListenerInfo = listeners | ||
|
|
||
| // Apply default values to listeners if necessary | ||
| v.SetListenersWithDefaults() | ||
|
gadididi marked this conversation as resolved.
|
||
|
|
||
| return nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.