2020-01-08 12:47:07 +00:00
|
|
|
package allocrunner
|
|
|
|
|
|
|
|
import (
|
2020-02-11 13:45:16 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2020-01-08 12:47:07 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2020-08-06 18:51:46 +00:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2020-02-11 13:45:16 +00:00
|
|
|
"github.com/hashicorp/nomad/client/pluginmanager/csimanager"
|
2020-01-08 12:47:07 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2020-05-21 13:18:02 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2020-01-08 12:47:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// csiHook will wait for remote csi volumes to be attached to the host before
|
|
|
|
// continuing.
|
|
|
|
//
|
|
|
|
// It is a noop for allocs that do not depend on CSI Volumes.
|
|
|
|
type csiHook struct {
|
2020-08-06 18:51:46 +00:00
|
|
|
ar *allocRunner
|
|
|
|
alloc *structs.Allocation
|
|
|
|
logger hclog.Logger
|
|
|
|
csimanager csimanager.Manager
|
|
|
|
rpcClient RPCer
|
|
|
|
updater hookResourceSetter
|
|
|
|
volumeRequests map[string]*volumeAndRequest
|
2020-01-08 12:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *csiHook) Name() string {
|
|
|
|
return "csi_hook"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *csiHook) Prerun() error {
|
|
|
|
if !c.shouldRun() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-09 14:39:06 +00:00
|
|
|
// We use this context only to attach hclog to the gRPC context. The
|
|
|
|
// lifetime is the lifetime of the gRPC stream, not specific RPC timeouts,
|
|
|
|
// but we manage the stream lifetime via Close in the pluginmanager.
|
|
|
|
ctx := context.Background()
|
2020-03-30 20:46:43 +00:00
|
|
|
|
2020-02-17 12:57:25 +00:00
|
|
|
volumes, err := c.claimVolumesFromAlloc()
|
2020-02-11 13:45:16 +00:00
|
|
|
if err != nil {
|
2020-03-10 14:22:42 +00:00
|
|
|
return fmt.Errorf("claim volumes: %v", err)
|
2020-02-11 13:45:16 +00:00
|
|
|
}
|
2020-08-06 18:51:46 +00:00
|
|
|
c.volumeRequests = volumes
|
2020-02-11 13:45:16 +00:00
|
|
|
|
|
|
|
mounts := make(map[string]*csimanager.MountInfo, len(volumes))
|
2020-02-17 11:10:12 +00:00
|
|
|
for alias, pair := range volumes {
|
2020-04-02 20:04:56 +00:00
|
|
|
mounter, err := c.csimanager.MounterForPlugin(ctx, pair.volume.PluginID)
|
2020-02-11 13:45:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:10:12 +00:00
|
|
|
usageOpts := &csimanager.UsageOptions{
|
|
|
|
ReadOnly: pair.request.ReadOnly,
|
|
|
|
AttachmentMode: string(pair.volume.AttachmentMode),
|
|
|
|
AccessMode: string(pair.volume.AccessMode),
|
2020-03-23 17:55:26 +00:00
|
|
|
MountOptions: pair.request.MountOptions,
|
2020-02-17 11:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 12:57:25 +00:00
|
|
|
mountInfo, err := mounter.MountVolume(ctx, pair.volume, c.alloc, usageOpts, pair.publishContext)
|
2020-02-11 13:45:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mounts[alias] = mountInfo
|
|
|
|
}
|
|
|
|
|
2020-02-11 16:39:16 +00:00
|
|
|
res := c.updater.GetAllocHookResources()
|
|
|
|
res.CSIMounts = mounts
|
|
|
|
c.updater.SetAllocHookResources(res)
|
2020-02-11 13:45:16 +00:00
|
|
|
|
2020-01-08 12:47:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-06 18:51:46 +00:00
|
|
|
// Postrun sends an RPC to the server to unpublish the volume. This may
|
|
|
|
// forward client RPCs to the node plugins or to the controller plugins,
|
|
|
|
// depending on whether other allocations on this node have claims on this
|
|
|
|
// volume.
|
|
|
|
func (c *csiHook) Postrun() error {
|
|
|
|
if !c.shouldRun() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var mErr *multierror.Error
|
|
|
|
|
|
|
|
for _, pair := range c.volumeRequests {
|
|
|
|
req := &structs.CSIVolumeUnpublishRequest{
|
|
|
|
VolumeID: pair.request.Source,
|
|
|
|
Claim: &structs.CSIVolumeClaim{
|
|
|
|
AllocationID: c.alloc.ID,
|
|
|
|
NodeID: c.alloc.NodeID,
|
|
|
|
Mode: structs.CSIVolumeClaimRelease,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{
|
2020-08-11 17:08:39 +00:00
|
|
|
Region: c.alloc.Job.Region,
|
|
|
|
Namespace: c.alloc.Job.Namespace,
|
|
|
|
AuthToken: c.ar.clientConfig.Node.SecretID,
|
|
|
|
},
|
2020-08-06 18:51:46 +00:00
|
|
|
}
|
|
|
|
err := c.rpcClient.RPC("CSIVolume.Unpublish",
|
|
|
|
req, &structs.CSIVolumeUnpublishResponse{})
|
|
|
|
if err != nil {
|
|
|
|
mErr = multierror.Append(mErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mErr.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:10:12 +00:00
|
|
|
type volumeAndRequest struct {
|
|
|
|
volume *structs.CSIVolume
|
|
|
|
request *structs.VolumeRequest
|
2020-02-17 12:57:25 +00:00
|
|
|
|
|
|
|
// When volumeAndRequest was returned from a volume claim, this field will be
|
|
|
|
// populated for plugins that require it.
|
|
|
|
publishContext map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// claimVolumesFromAlloc is used by the pre-run hook to fetch all of the volume
|
|
|
|
// metadata and claim it for use by this alloc/node at the same time.
|
|
|
|
func (c *csiHook) claimVolumesFromAlloc() (map[string]*volumeAndRequest, error) {
|
|
|
|
result := make(map[string]*volumeAndRequest)
|
|
|
|
tg := c.alloc.Job.LookupTaskGroup(c.alloc.TaskGroup)
|
|
|
|
|
|
|
|
// Initially, populate the result map with all of the requests
|
|
|
|
for alias, volumeRequest := range tg.Volumes {
|
2020-05-21 13:18:02 +00:00
|
|
|
|
2020-02-17 12:57:25 +00:00
|
|
|
if volumeRequest.Type == structs.VolumeTypeCSI {
|
2020-05-21 13:18:02 +00:00
|
|
|
|
|
|
|
for _, task := range tg.Tasks {
|
|
|
|
caps, err := c.ar.GetTaskDriverCapabilities(task.Name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not validate task driver capabilities: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if caps.MountConfigs == drivers.MountConfigSupportNone {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"task driver %q for %q does not support CSI", task.Driver, task.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 12:57:25 +00:00
|
|
|
result[alias] = &volumeAndRequest{request: volumeRequest}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the result map and upsert the volume field as each volume gets
|
|
|
|
// claimed by the server.
|
|
|
|
for alias, pair := range result {
|
|
|
|
claimType := structs.CSIVolumeClaimWrite
|
|
|
|
if pair.request.ReadOnly {
|
|
|
|
claimType = structs.CSIVolumeClaimRead
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &structs.CSIVolumeClaimRequest{
|
|
|
|
VolumeID: pair.request.Source,
|
|
|
|
AllocationID: c.alloc.ID,
|
2020-04-23 15:06:23 +00:00
|
|
|
NodeID: c.alloc.NodeID,
|
2020-02-17 12:57:25 +00:00
|
|
|
Claim: claimType,
|
2020-08-11 17:08:39 +00:00
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: c.alloc.Job.Region,
|
|
|
|
Namespace: c.alloc.Job.Namespace,
|
|
|
|
AuthToken: c.ar.clientConfig.Node.SecretID,
|
|
|
|
},
|
2020-02-17 12:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var resp structs.CSIVolumeClaimResponse
|
|
|
|
if err := c.rpcClient.RPC("CSIVolume.Claim", req, &resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Volume == nil {
|
|
|
|
return nil, fmt.Errorf("Unexpected nil volume returned for ID: %v", pair.request.Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
result[alias].volume = resp.Volume
|
|
|
|
result[alias].publishContext = resp.PublishContext
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2020-02-17 11:10:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 13:18:02 +00:00
|
|
|
func newCSIHook(ar *allocRunner, logger hclog.Logger, alloc *structs.Allocation, rpcClient RPCer, csi csimanager.Manager, updater hookResourceSetter) *csiHook {
|
2020-01-08 12:47:07 +00:00
|
|
|
return &csiHook{
|
2020-05-21 13:18:02 +00:00
|
|
|
ar: ar,
|
2020-02-11 13:45:16 +00:00
|
|
|
alloc: alloc,
|
|
|
|
logger: logger.Named("csi_hook"),
|
|
|
|
rpcClient: rpcClient,
|
|
|
|
csimanager: csi,
|
2020-02-11 16:39:16 +00:00
|
|
|
updater: updater,
|
2020-01-08 12:47:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *csiHook) shouldRun() bool {
|
|
|
|
tg := h.alloc.Job.LookupTaskGroup(h.alloc.TaskGroup)
|
|
|
|
for _, vol := range tg.Volumes {
|
|
|
|
if vol.Type == structs.VolumeTypeCSI {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|