Skip to content

Commit

Permalink
CSI ListSnapshots secrets implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Griffiths <ggriffiths@purestorage.com>
  • Loading branch information
Grant Griffiths committed Jul 3, 2021
1 parent 4e9ed5f commit 6f77d54
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 3 deletions.
6 changes: 5 additions & 1 deletion api/csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (v *CSIVolumes) DeleteSnapshot(snap *CSISnapshot, w *WriteOptions) error {
}

// ListSnapshots lists external storage volume snapshots.
func (v *CSIVolumes) ListSnapshots(pluginID string, q *QueryOptions) (*CSISnapshotListResponse, *QueryMeta, error) {
func (v *CSIVolumes) ListSnapshots(pluginID string, secrets string, q *QueryOptions) (*CSISnapshotListResponse, *QueryMeta, error) {
var resp *CSISnapshotListResponse

qp := url.Values{}
Expand All @@ -150,6 +150,9 @@ func (v *CSIVolumes) ListSnapshots(pluginID string, q *QueryOptions) (*CSISnapsh
if q.PerPage != 0 {
qp.Set("per_page", fmt.Sprint(q.PerPage))
}
if secrets != "" {
qp.Set("secrets", secrets)
}

qm, err := v.client.query("/v1/volumes/snapshot?"+qp.Encode(), &resp, q)
if err != nil {
Expand Down Expand Up @@ -406,6 +409,7 @@ type CSISnapshotCreateResponse struct {
// fields
type CSISnapshotListRequest struct {
PluginID string
Secrets CSISecrets
QueryOptions
}

Expand Down
1 change: 1 addition & 0 deletions client/csi_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ func (c *CSI) ControllerListSnapshots(req *structs.ClientCSIControllerListSnapsh
defer plugin.Close()

csiReq := req.ToCSIRequest()
fmt.Println("CSI REQ", *csiReq)

ctx, cancelFn := c.requestContext()
defer cancelFn()
Expand Down
2 changes: 2 additions & 0 deletions client/structs/csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ type ClientCSIControllerListSnapshotsRequest struct {
// not Nomad's own fields, for clarity when mapping between the two RPCs
MaxEntries int32
StartingToken string
Secrets structs.CSISecrets

CSIControllerQuery
}
Expand All @@ -370,6 +371,7 @@ func (req *ClientCSIControllerListSnapshotsRequest) ToCSIRequest() *csi.Controll
return &csi.ControllerListSnapshotsRequest{
MaxEntries: req.MaxEntries,
StartingToken: req.StartingToken,
Secrets: req.Secrets,
}
}

Expand Down
10 changes: 10 additions & 0 deletions command/agent/csi_endpoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package agent

import (
"fmt"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -333,6 +334,15 @@ func (s *HTTPServer) csiSnapshotList(resp http.ResponseWriter, req *http.Request

query := req.URL.Query()
args.PluginID = query.Get("plugin_id")
secrets := query["secrets"]
args.Secrets = make(structs.CSISecrets)
for _, raw := range secrets {
fmt.Println("**RAW SEC", raw)
secret := strings.Split(raw, "=")
if len(secret) == 2 {
args.Secrets[secret[0]] = secret[1]
}
}

var out structs.CSISnapshotListResponse
if err := s.agent.RPC("CSIVolume.ListSnapshots", &args, &out); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion command/volume_snapshot_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ List Options:
-plugin: Display only snapshots managed by a particular plugin. By default
this command will query all plugins for their snapshots.
-secrets: A set of key/value secrets to be used when listing snapshots.
`
return strings.TrimSpace(helpText)
}
Expand Down Expand Up @@ -68,11 +69,13 @@ func (c *VolumeSnapshotListCommand) Name() string { return "volume snapshot list
func (c *VolumeSnapshotListCommand) Run(args []string) int {
var pluginID string
var verbose bool
var secrets string

flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.StringVar(&pluginID, "plugin", "", "")
flags.BoolVar(&verbose, "verbose", false, "")
flags.StringVar(&secrets, "secrets", "", "")

if err := flags.Parse(args); err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing arguments %s", err))
Expand Down Expand Up @@ -121,7 +124,7 @@ func (c *VolumeSnapshotListCommand) Run(args []string) int {
q := &api.QueryOptions{PerPage: 30} // TODO: tune page size

for {
resp, _, err := client.CSIVolumes().ListSnapshots(pluginID, q)
resp, _, err := client.CSIVolumes().ListSnapshots(pluginID, secrets, q)
if err != nil && !errors.Is(err, io.EOF) {
c.Ui.Error(fmt.Sprintf(
"Error querying CSI external snapshots for plugin %q: %s", pluginID, err))
Expand Down
1 change: 1 addition & 0 deletions nomad/structs/csi.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ type CSISnapshotDeleteResponse struct {
// fields
type CSISnapshotListRequest struct {
PluginID string
Secrets CSISecrets
QueryOptions
}

Expand Down
2 changes: 2 additions & 0 deletions plugins/csi/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,12 +756,14 @@ func (r *ControllerDeleteSnapshotRequest) Validate() error {
type ControllerListSnapshotsRequest struct {
MaxEntries int32
StartingToken string
Secrets structs.CSISecrets
}

func (r *ControllerListSnapshotsRequest) ToCSIRepresentation() *csipbv1.ListSnapshotsRequest {
return &csipbv1.ListSnapshotsRequest{
MaxEntries: r.MaxEntries,
StartingToken: r.StartingToken,
Secrets: r.Secrets,
}
}

Expand Down
6 changes: 5 additions & 1 deletion vendor/github.com/hashicorp/nomad/api/csi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6f77d54

Please sign in to comment.