Skip to content

Commit

Permalink
CSI: correctly handly empty capacity fields during registration
Browse files Browse the repository at this point in the history
When a volume is registered via `nomad volume register` and not via the
creation workflow, it may not have any of the capacity fields set (as they're
not used for registration). Handle this case without error, and let the
downstream RPCs handle the error case for `nomad volume create`.
  • Loading branch information
tgross committed Apr 2, 2021
1 parent d72a9b6 commit b1458b0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 6 additions & 2 deletions command/volume_register_csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,16 @@ func parseCapacityBytes(cap *ast.ObjectList) (int64, error) {
if !ok {
break
}
b, err := humanize.ParseBytes(strings.Trim(lit.Token.Text, "\""))
literal := strings.Trim(lit.Token.Text, "\"")
if literal == "" {
return 0, nil
}
b, err := humanize.ParseBytes(literal)
if err != nil {
return 0, fmt.Errorf("could not parse value as bytes: %v", err)
}
return int64(b), err
}
}
return 0, fmt.Errorf("could not parse value as bytes")
return 0, nil
}
30 changes: 29 additions & 1 deletion command/volume_register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestCSIVolumeDecode(t *testing.T) {
expected *api.CSIVolume
err string
}{{
name: "typical volume",
name: "volume creation",
hcl: `
id = "testvolume"
name = "test"
Expand Down Expand Up @@ -110,6 +110,34 @@ capability {
Secrets: map[string]string{"password": "xyzzy"},
},
err: "",
}, {
name: "volume registration",
hcl: `
id = "testvolume"
external_id = "vol-12345"
name = "test"
type = "csi"
plugin_id = "myplugin"
capacity_min = "" # meaningless for registration
capability {
access_mode = "single-node-writer"
attachment_mode = "file-system"
}
`,
expected: &api.CSIVolume{
ID: "testvolume",
ExternalID: "vol-12345",
Name: "test",
PluginID: "myplugin",
RequestedCapabilities: []*api.CSIVolumeCapability{
{
AccessMode: api.CSIVolumeAccessModeSingleNodeWriter,
AttachmentMode: api.CSIVolumeAttachmentModeFilesystem,
},
},
},
err: "",
},
}

Expand Down

0 comments on commit b1458b0

Please sign in to comment.