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

CSI: allow namespace field to be passed in volume spec #12400

Merged
merged 1 commit into from
Mar 29, 2022
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
3 changes: 3 additions & 0 deletions .changelog/12400.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
csi: allow namespace field to be passed in volume spec
```
2 changes: 2 additions & 0 deletions command/volume_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func (c *VolumeInitCommand) Run(args []string) int {

var defaultHclVolumeSpec = strings.TrimSpace(`
id = "ebs_prod_db1"
namespace = "default"
name = "database"
type = "csi"
plugin_id = "plugin_id"
Expand Down Expand Up @@ -183,6 +184,7 @@ context {
var defaultJsonVolumeSpec = strings.TrimSpace(`
{
"id": "ebs_prod_db1",
"namespace": "default",
"name": "database",
"type": "csi",
"plugin_id": "plugin_id",
Expand Down
4 changes: 4 additions & 0 deletions command/volume_register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestCSIVolumeDecode(t *testing.T) {
name: "volume creation",
hcl: `
id = "testvolume"
namespace = "prod"
name = "test"
type = "csi"
plugin_id = "myplugin"
Expand Down Expand Up @@ -99,6 +100,7 @@ topology_request {
`,
expected: &api.CSIVolume{
ID: "testvolume",
Namespace: "prod",
Name: "test",
PluginID: "myplugin",
SnapshotID: "snap-12345",
Expand Down Expand Up @@ -136,6 +138,7 @@ topology_request {
name: "volume registration",
hcl: `
id = "testvolume"
namespace = "prod"
external_id = "vol-12345"
name = "test"
type = "csi"
Expand All @@ -162,6 +165,7 @@ topology_request {
`,
expected: &api.CSIVolume{
ID: "testvolume",
Namespace: "prod",
ExternalID: "vol-12345",
Name: "test",
PluginID: "myplugin",
Expand Down
6 changes: 3 additions & 3 deletions nomad/csi_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,6 @@ func (v *CSIVolume) Register(args *structs.CSIVolumeRegisterRequest, reply *stru
if err != nil {
return err
}
// TODO: allow volume spec file to set namespace
// https://github.com/hashicorp/nomad/issues/11196
if vol.Namespace == "" {
vol.Namespace = args.RequestNamespace()
}
Expand Down Expand Up @@ -921,7 +919,9 @@ func (v *CSIVolume) Create(args *structs.CSIVolumeCreateRequest, reply *structs.
// We also validate that the plugin exists for each plugin, and validate the
// capabilities when the plugin has a controller.
for _, vol := range args.Volumes {
vol.Namespace = args.RequestNamespace()
if vol.Namespace == "" {
vol.Namespace = args.RequestNamespace()
}
if err = vol.Validate(); err != nil {
return err
}
Expand Down
20 changes: 12 additions & 8 deletions nomad/csi_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@ func TestCSIVolumeEndpoint_Register(t *testing.T) {
defer shutdown()
testutil.WaitForLeader(t, srv.RPC)

ns := structs.DefaultNamespace

state := srv.fsm.State()
store := srv.fsm.State()
codec := rpcClient(t, srv)

id0 := uuid.Generate()

// Create the register request
ns := mock.Namespace()
store.UpsertNamespaces(900, []*structs.Namespace{ns})

// Create the node and plugin
node := mock.Node()
node.CSINodePlugins = map[string]*structs.CSIInfo{
Expand All @@ -142,11 +144,12 @@ func TestCSIVolumeEndpoint_Register(t *testing.T) {
NodeInfo: &structs.CSINodeInfo{},
},
}
require.NoError(t, state.UpsertNode(structs.MsgTypeTestSetup, 1000, node))
require.NoError(t, store.UpsertNode(structs.MsgTypeTestSetup, 1000, node))

// Create the volume
vols := []*structs.CSIVolume{{
ID: id0,
Namespace: ns.Name,
PluginID: "minnie",
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader, // legacy field ignored
AttachmentMode: structs.CSIVolumeAttachmentModeBlockDevice, // legacy field ignored
Expand All @@ -166,7 +169,7 @@ func TestCSIVolumeEndpoint_Register(t *testing.T) {
Volumes: vols,
WriteRequest: structs.WriteRequest{
Region: "global",
Namespace: ns,
Namespace: "",
},
}
resp1 := &structs.CSIVolumeRegisterResponse{}
Expand All @@ -178,7 +181,8 @@ func TestCSIVolumeEndpoint_Register(t *testing.T) {
req2 := &structs.CSIVolumeGetRequest{
ID: id0,
QueryOptions: structs.QueryOptions{
Region: "global",
Region: "global",
Namespace: ns.Name,
},
}
resp2 := &structs.CSIVolumeGetResponse{}
Expand All @@ -201,7 +205,7 @@ func TestCSIVolumeEndpoint_Register(t *testing.T) {
VolumeIDs: []string{id0},
WriteRequest: structs.WriteRequest{
Region: "global",
Namespace: ns,
Namespace: ns.Name,
},
}
resp3 := &structs.CSIVolumeDeregisterResponse{}
Expand Down Expand Up @@ -1029,7 +1033,7 @@ func TestCSIVolumeEndpoint_Create(t *testing.T) {
vols := []*structs.CSIVolume{{
ID: volID,
Name: "vol",
Namespace: "notTheNamespace", // overriden by WriteRequest
Namespace: "", // overriden by WriteRequest
PluginID: "minnie",
AccessMode: structs.CSIVolumeAccessModeSingleNodeReader, // legacy field ignored
AttachmentMode: structs.CSIVolumeAttachmentModeBlockDevice, // legacy field ignored
Expand Down
5 changes: 5 additions & 0 deletions website/content/docs/commands/volume/create.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ The file may be provided as either HCL or JSON. An example HCL configuration:

```hcl
id = "ebs_prod_db1"
namespace = "default"
name = "database"
type = "csi"
plugin_id = "ebs-prod"
Expand Down Expand Up @@ -84,6 +85,10 @@ parameters {
[`volume.source`][csi_volume_source] field in a job specification will refer
to the volume.

- `namespace` `(string: <optional>)` - The namespace of the volume. This field
overrides the namespace provided by the `-namespace` flag or `NOMAD_NAMESPACE`
environment variable. Defaults to `"default"` if unset.

- `name` `(string: <required>)` - The display name of the volume. This field
may be used by the external storage provider to tag the volume.

Expand Down
4 changes: 4 additions & 0 deletions website/content/docs/commands/volume/register.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ context {
[`volume.source`][csi_volume_source] field in a job specification will refer
to the volume.

- `namespace` `(string: <optional>)` - The namespace of the volume. This field
overrides the namespace provided by the `-namespace` flag or `NOMAD_NAMESPACE`
environment variable. Defaults to `"default"` if unset.

- `name` `(string: <required>)` - The display name of the volume.

- `type` `(string: <required>)` - The type of volume. Currently only `"csi"`
Expand Down