From 58903b2c3d80df3a12a64bfea9a8fb17843a4541 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Fri, 6 Dec 2024 08:43:55 -0500 Subject: [PATCH] dynamic host volumes: refactor HTTP routes for volumes list dispatch (#24612) The List Volumes API was originally written for CSI but assumed we'd have future volume types, dispatched on a query parameter. Dynamic host volumes uses this, but the resulting code has host volumes concerns comingled in the CSI volumes endpoint. Refactor this so that we have a top-level `GET /v1/volumes` route that's shared between CSI and DHV, and have it dispatch to the appropriate handler in the type-specific endpoints. Ref: https://github.com/hashicorp/nomad/pull/24479 --- command/agent/csi_endpoint.go | 18 +----------------- command/agent/http.go | 1 + command/agent/volumes_endpoint.go | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 command/agent/volumes_endpoint.go diff --git a/command/agent/csi_endpoint.go b/command/agent/csi_endpoint.go index 9f4b87aa0fc..325ce5bb6d0 100644 --- a/command/agent/csi_endpoint.go +++ b/command/agent/csi_endpoint.go @@ -20,28 +20,12 @@ func (s *HTTPServer) CSIVolumesRequest(resp http.ResponseWriter, req *http.Reque return nil, CodedError(405, ErrInvalidMethod) } - // Type filters volume lists to a specific type. When support for non-CSI volumes is - // introduced, we'll need to dispatch here - query := req.URL.Query() - qtype, ok := query["type"] - if !ok { - return []*structs.CSIVolListStub{}, nil - } - // TODO(1.10.0): move handling of GET /v1/volumes/ out so that we're not - // co-mingling the call for listing host volume here - switch qtype[0] { - case "host": - return s.HostVolumesListRequest(resp, req) - case "csi": - default: - return nil, nil - } - args := structs.CSIVolumeListRequest{} if s.parse(resp, req, &args.Region, &args.QueryOptions) { return nil, nil } + query := req.URL.Query() args.Prefix = query.Get("prefix") args.PluginID = query.Get("plugin_id") args.NodeID = query.Get("node_id") diff --git a/command/agent/http.go b/command/agent/http.go index cb1b9359a5e..6d47e4e78dd 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -404,6 +404,7 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) { s.mux.HandleFunc("/v1/deployments", s.wrap(s.DeploymentsRequest)) s.mux.HandleFunc("/v1/deployment/", s.wrap(s.DeploymentSpecificRequest)) + s.mux.HandleFunc("GET /v1/volumes", s.wrap(s.ListVolumesRequest)) s.mux.HandleFunc("/v1/volumes", s.wrap(s.CSIVolumesRequest)) s.mux.HandleFunc("/v1/volumes/external", s.wrap(s.CSIExternalVolumesRequest)) s.mux.HandleFunc("/v1/volumes/snapshot", s.wrap(s.CSISnapshotsRequest)) diff --git a/command/agent/volumes_endpoint.go b/command/agent/volumes_endpoint.go new file mode 100644 index 00000000000..3ee84eceb7f --- /dev/null +++ b/command/agent/volumes_endpoint.go @@ -0,0 +1,27 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package agent + +import ( + "net/http" + + "github.com/hashicorp/nomad/nomad/structs" +) + +// ListVolumesRequest dispatches requests for listing volumes to a specific type. +func (s *HTTPServer) ListVolumesRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { + query := req.URL.Query() + qtype, ok := query["type"] + if !ok { + return []*structs.CSIVolListStub{}, nil + } + switch qtype[0] { + case "host": + return s.HostVolumesListRequest(resp, req) + case "csi": + return s.CSIVolumesRequest(resp, req) + default: + return nil, CodedError(404, resourceNotFoundErr) + } +}