Skip to content

Commit

Permalink
csi: allow namespace field to be passed in volume spec
Browse files Browse the repository at this point in the history
Use the volume spec's `namespace` field to override the value of the
`-namespace` and `NOMAD_NAMESPACE` field, just as we do with job spec.
  • Loading branch information
tgross committed Mar 29, 2022
1 parent 98e122c commit 5136d06
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
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
3 changes: 3 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 @@ -102,6 +103,7 @@ topology_request {
Name: "test",
PluginID: "myplugin",
SnapshotID: "snap-12345",
Namespace: "prod",
RequestedCapacityMin: 10485760,
RequestedCapacityMax: 1000000000,
RequestedCapabilities: []*api.CSIVolumeCapability{
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 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

0 comments on commit 5136d06

Please sign in to comment.