2019-12-18 22:20:30 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-07-01 16:17:51 +00:00
|
|
|
"strconv"
|
2019-12-18 22:20:30 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *HTTPServer) CSIVolumesRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2020-08-11 16:24:41 +00:00
|
|
|
switch req.Method {
|
|
|
|
case http.MethodPut, http.MethodPost:
|
2021-03-23 13:45:14 +00:00
|
|
|
return s.csiVolumeRegister(resp, req)
|
2020-08-11 16:24:41 +00:00
|
|
|
case http.MethodGet:
|
|
|
|
default:
|
2019-12-18 22:20:30 +00:00
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:20:40 +00:00
|
|
|
// 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 {
|
2020-03-24 18:28:28 +00:00
|
|
|
return []*structs.CSIVolListStub{}, nil
|
2020-03-12 18:20:40 +00:00
|
|
|
}
|
|
|
|
if qtype[0] != "csi" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:20:30 +00:00
|
|
|
args := structs.CSIVolumeListRequest{}
|
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-03-23 13:45:14 +00:00
|
|
|
|
|
|
|
args.Prefix = query.Get("prefix")
|
|
|
|
args.PluginID = query.Get("plugin_id")
|
|
|
|
args.NodeID = query.Get("node_id")
|
2020-03-11 16:47:14 +00:00
|
|
|
|
2019-12-18 22:20:30 +00:00
|
|
|
var out structs.CSIVolumeListResponse
|
|
|
|
if err := s.agent.RPC("CSIVolume.List", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
return out.Volumes, nil
|
|
|
|
}
|
|
|
|
|
2021-04-07 14:34:06 +00:00
|
|
|
func (s *HTTPServer) CSIExternalVolumesRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if req.Method != http.MethodGet {
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
2021-03-23 13:45:14 +00:00
|
|
|
|
|
|
|
args := structs.CSIVolumeExternalListRequest{}
|
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
query := req.URL.Query()
|
|
|
|
args.PluginID = query.Get("plugin_id")
|
|
|
|
|
|
|
|
var out structs.CSIVolumeExternalListResponse
|
|
|
|
if err := s.agent.RPC("CSIVolume.ListExternal", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
2021-04-07 14:34:06 +00:00
|
|
|
return out, nil
|
2021-03-23 13:45:14 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 22:20:30 +00:00
|
|
|
// CSIVolumeSpecificRequest dispatches GET and PUT
|
|
|
|
func (s *HTTPServer) CSIVolumeSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2020-01-28 15:28:34 +00:00
|
|
|
// Tokenize the suffix of the path to get the volume id
|
2020-03-12 18:20:40 +00:00
|
|
|
reqSuffix := strings.TrimPrefix(req.URL.Path, "/v1/volume/csi/")
|
2019-12-18 22:20:30 +00:00
|
|
|
tokens := strings.Split(reqSuffix, "/")
|
2020-08-11 14:18:54 +00:00
|
|
|
if len(tokens) < 1 {
|
2019-12-18 22:20:30 +00:00
|
|
|
return nil, CodedError(404, resourceNotFoundErr)
|
|
|
|
}
|
|
|
|
id := tokens[0]
|
|
|
|
|
2020-08-11 14:18:54 +00:00
|
|
|
if len(tokens) == 1 {
|
|
|
|
switch req.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
return s.csiVolumeGet(id, resp, req)
|
|
|
|
case http.MethodPut:
|
2021-03-23 13:45:14 +00:00
|
|
|
return s.csiVolumeRegister(resp, req)
|
2020-08-11 14:18:54 +00:00
|
|
|
case http.MethodDelete:
|
2021-03-23 13:45:14 +00:00
|
|
|
return s.csiVolumeDeregister(id, resp, req)
|
2020-08-11 14:18:54 +00:00
|
|
|
default:
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tokens) == 2 {
|
2021-03-23 13:45:14 +00:00
|
|
|
switch req.Method {
|
|
|
|
case http.MethodPut:
|
|
|
|
if tokens[1] == "create" {
|
|
|
|
return s.csiVolumeCreate(resp, req)
|
|
|
|
}
|
|
|
|
case http.MethodDelete:
|
|
|
|
if tokens[1] == "detach" {
|
|
|
|
return s.csiVolumeDetach(id, resp, req)
|
|
|
|
}
|
|
|
|
if tokens[1] == "delete" {
|
|
|
|
return s.csiVolumeDelete(id, resp, req)
|
|
|
|
}
|
|
|
|
default:
|
2020-08-11 14:18:54 +00:00
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
2019-12-18 22:20:30 +00:00
|
|
|
}
|
2020-08-11 14:18:54 +00:00
|
|
|
|
|
|
|
return nil, CodedError(404, resourceNotFoundErr)
|
2019-12-18 22:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) csiVolumeGet(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
args := structs.CSIVolumeGetRequest{
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.CSIVolumeGetResponse
|
|
|
|
if err := s.agent.RPC("CSIVolume.Get", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
if out.Volume == nil {
|
2020-01-28 15:28:34 +00:00
|
|
|
return nil, CodedError(404, "volume not found")
|
2019-12-18 22:20:30 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 18:29:34 +00:00
|
|
|
return out.Volume, nil
|
2019-12-18 22:20:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 13:45:14 +00:00
|
|
|
func (s *HTTPServer) csiVolumeRegister(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2020-08-11 16:24:41 +00:00
|
|
|
switch req.Method {
|
|
|
|
case http.MethodPost, http.MethodPut:
|
|
|
|
default:
|
2019-12-18 22:20:30 +00:00
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
2021-03-23 13:45:14 +00:00
|
|
|
args := structs.CSIVolumeRegisterRequest{}
|
|
|
|
if err := decodeBody(req, &args); err != nil {
|
2019-12-18 22:20:30 +00:00
|
|
|
return err, CodedError(400, err.Error())
|
|
|
|
}
|
|
|
|
s.parseWriteRequest(req, &args.WriteRequest)
|
|
|
|
|
|
|
|
var out structs.CSIVolumeRegisterResponse
|
|
|
|
if err := s.agent.RPC("CSIVolume.Register", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-03-23 13:45:14 +00:00
|
|
|
func (s *HTTPServer) csiVolumeCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
switch req.Method {
|
|
|
|
case http.MethodPost, http.MethodPut:
|
|
|
|
default:
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.CSIVolumeCreateRequest{}
|
|
|
|
if err := decodeBody(req, &args); err != nil {
|
|
|
|
return err, CodedError(400, err.Error())
|
|
|
|
}
|
|
|
|
s.parseWriteRequest(req, &args.WriteRequest)
|
|
|
|
|
|
|
|
var out structs.CSIVolumeCreateResponse
|
|
|
|
if err := s.agent.RPC("CSIVolume.Create", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) csiVolumeDeregister(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2020-08-11 16:24:41 +00:00
|
|
|
if req.Method != http.MethodDelete {
|
2019-12-18 22:20:30 +00:00
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
2020-07-01 16:17:51 +00:00
|
|
|
raw := req.URL.Query().Get("force")
|
|
|
|
var force bool
|
|
|
|
if raw != "" {
|
|
|
|
var err error
|
|
|
|
force, err = strconv.ParseBool(raw)
|
|
|
|
if err != nil {
|
|
|
|
return nil, CodedError(400, "invalid force value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:20:30 +00:00
|
|
|
args := structs.CSIVolumeDeregisterRequest{
|
|
|
|
VolumeIDs: []string{id},
|
2020-07-01 16:17:51 +00:00
|
|
|
Force: force,
|
2019-12-18 22:20:30 +00:00
|
|
|
}
|
|
|
|
s.parseWriteRequest(req, &args.WriteRequest)
|
|
|
|
|
|
|
|
var out structs.CSIVolumeDeregisterResponse
|
|
|
|
if err := s.agent.RPC("CSIVolume.Deregister", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-01-28 15:28:34 +00:00
|
|
|
|
2021-03-23 13:45:14 +00:00
|
|
|
func (s *HTTPServer) csiVolumeDelete(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if req.Method != http.MethodDelete {
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
2022-04-05 12:59:11 +00:00
|
|
|
secrets := parseCSISecrets(req)
|
2021-03-23 13:45:14 +00:00
|
|
|
args := structs.CSIVolumeDeleteRequest{
|
|
|
|
VolumeIDs: []string{id},
|
2022-04-05 12:59:11 +00:00
|
|
|
Secrets: secrets,
|
2021-03-23 13:45:14 +00:00
|
|
|
}
|
|
|
|
s.parseWriteRequest(req, &args.WriteRequest)
|
|
|
|
|
|
|
|
var out structs.CSIVolumeDeleteResponse
|
|
|
|
if err := s.agent.RPC("CSIVolume.Delete", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:18:54 +00:00
|
|
|
func (s *HTTPServer) csiVolumeDetach(id string, resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if req.Method != http.MethodDelete {
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeID := req.URL.Query().Get("node")
|
|
|
|
if nodeID == "" {
|
|
|
|
return nil, CodedError(400, "detach requires node ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
args := structs.CSIVolumeUnpublishRequest{
|
|
|
|
VolumeID: id,
|
|
|
|
Claim: &structs.CSIVolumeClaim{
|
|
|
|
NodeID: nodeID,
|
2020-11-11 18:06:30 +00:00
|
|
|
Mode: structs.CSIVolumeClaimGC,
|
2020-08-11 14:18:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
s.parseWriteRequest(req, &args.WriteRequest)
|
|
|
|
|
|
|
|
var out structs.CSIVolumeUnpublishResponse
|
|
|
|
if err := s.agent.RPC("CSIVolume.Unpublish", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-04-01 15:16:52 +00:00
|
|
|
func (s *HTTPServer) CSISnapshotsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
switch req.Method {
|
|
|
|
case http.MethodPut, http.MethodPost:
|
|
|
|
return s.csiSnapshotCreate(resp, req)
|
|
|
|
case http.MethodDelete:
|
|
|
|
return s.csiSnapshotDelete(resp, req)
|
|
|
|
case http.MethodGet:
|
|
|
|
return s.csiSnapshotList(resp, req)
|
|
|
|
}
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) csiSnapshotCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
|
|
|
|
args := structs.CSISnapshotCreateRequest{}
|
|
|
|
if err := decodeBody(req, &args); err != nil {
|
|
|
|
return err, CodedError(400, err.Error())
|
|
|
|
}
|
|
|
|
s.parseWriteRequest(req, &args.WriteRequest)
|
|
|
|
|
|
|
|
var out structs.CSISnapshotCreateResponse
|
|
|
|
if err := s.agent.RPC("CSIVolume.CreateSnapshot", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
2021-04-07 12:44:12 +00:00
|
|
|
return out, nil
|
2021-04-01 15:16:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) csiSnapshotDelete(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
|
|
|
|
args := structs.CSISnapshotDeleteRequest{}
|
|
|
|
s.parseWriteRequest(req, &args.WriteRequest)
|
|
|
|
|
2021-04-07 13:25:09 +00:00
|
|
|
snap := &structs.CSISnapshot{Secrets: structs.CSISecrets{}}
|
|
|
|
|
|
|
|
query := req.URL.Query()
|
|
|
|
snap.PluginID = query.Get("plugin_id")
|
|
|
|
snap.ID = query.Get("snapshot_id")
|
2022-03-01 13:47:01 +00:00
|
|
|
|
|
|
|
secrets := parseCSISecrets(req)
|
|
|
|
snap.Secrets = secrets
|
2021-04-07 13:25:09 +00:00
|
|
|
|
|
|
|
args.Snapshots = []*structs.CSISnapshot{snap}
|
|
|
|
|
2021-04-01 15:16:52 +00:00
|
|
|
var out structs.CSISnapshotDeleteResponse
|
|
|
|
if err := s.agent.RPC("CSIVolume.DeleteSnapshot", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) csiSnapshotList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
|
|
|
|
args := structs.CSISnapshotListRequest{}
|
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
query := req.URL.Query()
|
|
|
|
args.PluginID = query.Get("plugin_id")
|
2022-03-01 13:47:01 +00:00
|
|
|
secrets := parseCSISecrets(req)
|
|
|
|
args.Secrets = secrets
|
2021-04-01 15:16:52 +00:00
|
|
|
var out structs.CSISnapshotListResponse
|
2021-04-07 12:45:48 +00:00
|
|
|
if err := s.agent.RPC("CSIVolume.ListSnapshots", &args, &out); err != nil {
|
2021-04-01 15:16:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
2021-04-07 13:46:38 +00:00
|
|
|
return out, nil
|
2021-04-01 15:16:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
// CSIPluginsRequest lists CSI plugins
|
|
|
|
func (s *HTTPServer) CSIPluginsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2020-08-11 16:24:41 +00:00
|
|
|
if req.Method != http.MethodGet {
|
2020-01-28 15:28:34 +00:00
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:20:40 +00:00
|
|
|
// Type filters plugin lists to a specific type. When support for non-CSI plugins is
|
|
|
|
// introduced, we'll need to dispatch here
|
|
|
|
query := req.URL.Query()
|
|
|
|
qtype, ok := query["type"]
|
|
|
|
if !ok {
|
2020-03-24 18:28:28 +00:00
|
|
|
return []*structs.CSIPluginListStub{}, nil
|
2020-03-12 18:20:40 +00:00
|
|
|
}
|
|
|
|
if qtype[0] != "csi" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-01-28 15:28:34 +00:00
|
|
|
args := structs.CSIPluginListRequest{}
|
|
|
|
|
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.CSIPluginListResponse
|
|
|
|
if err := s.agent.RPC("CSIPlugin.List", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
return out.Plugins, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSIPluginSpecificRequest list the job with CSIInfo
|
|
|
|
func (s *HTTPServer) CSIPluginSpecificRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2020-08-11 16:24:41 +00:00
|
|
|
if req.Method != http.MethodGet {
|
2020-01-28 15:28:34 +00:00
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tokenize the suffix of the path to get the plugin id
|
2020-03-12 18:20:40 +00:00
|
|
|
reqSuffix := strings.TrimPrefix(req.URL.Path, "/v1/plugin/csi/")
|
2020-01-28 15:28:34 +00:00
|
|
|
tokens := strings.Split(reqSuffix, "/")
|
|
|
|
if len(tokens) > 2 || len(tokens) < 1 {
|
|
|
|
return nil, CodedError(404, resourceNotFoundErr)
|
|
|
|
}
|
|
|
|
id := tokens[0]
|
|
|
|
|
|
|
|
args := structs.CSIPluginGetRequest{ID: id}
|
|
|
|
if s.parse(resp, req, &args.Region, &args.QueryOptions) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.CSIPluginGetResponse
|
|
|
|
if err := s.agent.RPC("CSIPlugin.Get", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
setMeta(resp, &out.QueryMeta)
|
|
|
|
if out.Plugin == nil {
|
|
|
|
return nil, CodedError(404, "plugin not found")
|
|
|
|
}
|
|
|
|
|
2022-04-15 18:29:34 +00:00
|
|
|
return out.Plugin, nil
|
2020-08-11 16:24:41 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 13:47:01 +00:00
|
|
|
// parseCSISecrets extracts a map of k/v pairs from the CSI secrets
|
|
|
|
// header. Silently ignores invalid secrets
|
|
|
|
func parseCSISecrets(req *http.Request) structs.CSISecrets {
|
|
|
|
secretsHeader := req.Header.Get("X-Nomad-CSI-Secrets")
|
|
|
|
if secretsHeader == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
secrets := map[string]string{}
|
|
|
|
secretkvs := strings.Split(secretsHeader, ",")
|
|
|
|
for _, secretkv := range secretkvs {
|
2023-01-03 21:28:38 +00:00
|
|
|
if key, value, found := strings.Cut(secretkv, "="); found {
|
|
|
|
secrets[key] = value
|
2022-03-01 13:47:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(secrets) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return structs.CSISecrets(secrets)
|
|
|
|
}
|