Skip to content

Commit

Permalink
dynamic host volumes: require node ID on register (#24795)
Browse files Browse the repository at this point in the history
When registering a host volume created out-of-band, the volume will have been
created on a specific node. Require the node ID field to be set.

Ref: #24789 (comment)
  • Loading branch information
tgross authored Jan 7, 2025
1 parent 08a6f87 commit 024c504
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions command/volume_register_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (c *VolumeRegisterCommand) hostVolumeRegister(client *api.Client, ast *ast.
c.Ui.Error(fmt.Sprintf("Error decoding the volume definition: %s", err))
return 1
}
if vol.NodeID == "" {
c.Ui.Error("Node ID is required for registering")
return 1
}

req := &api.HostVolumeRegisterRequest{
Volume: vol,
Expand Down
5 changes: 5 additions & 0 deletions nomad/host_volume_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nomad

import (
"errors"
"fmt"
"net/http"
"regexp"
Expand Down Expand Up @@ -316,6 +317,10 @@ func (v *HostVolume) Register(args *structs.HostVolumeRegisterRequest, reply *st
return err
}

if vol.NodeID == "" {
return errors.New("cannot register volume: node ID is required")
}

now := time.Now()
vol, err = v.validateVolumeUpdate(vol, snap, now)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion nomad/host_volume_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func TestHostVolumeEndpoint_CreateRegisterGetDelete(t *testing.T) {
must.EqError(t, err, fmt.Sprintf(
`validating volume "example" against state failed: node %q does not exist`,
invalidNode.ID))

req.Volume.NodeID = ""
err = msgpackrpc.CallWithCodec(codec, "HostVolume.Register", req, &resp)
must.EqError(t, err, "cannot register volume: node ID is required")
})

var expectIndex uint64
Expand Down Expand Up @@ -211,7 +215,7 @@ func TestHostVolumeEndpoint_CreateRegisterGetDelete(t *testing.T) {
t.Run("invalid updates", func(t *testing.T) {

invalidVol1 := vol1.Copy()
invalidVol2 := &structs.HostVolume{}
invalidVol2 := &structs.HostVolume{NodeID: uuid.Generate()}

createReq := &structs.HostVolumeCreateRequest{
Volume: invalidVol2,
Expand Down

0 comments on commit 024c504

Please sign in to comment.