2018-09-26 17:33:37 +00:00
|
|
|
package drivers
|
|
|
|
|
|
|
|
import (
|
2018-11-13 01:09:27 +00:00
|
|
|
"context"
|
2018-09-26 17:33:37 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
"github.com/LK4D4/joincontext"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2018-12-11 20:27:50 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2019-01-23 14:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/grpcutils"
|
2018-11-09 04:37:50 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers/proto"
|
|
|
|
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
2018-11-21 00:30:39 +00:00
|
|
|
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
2018-11-14 11:20:35 +00:00
|
|
|
sproto "github.com/hashicorp/nomad/plugins/shared/structs/proto"
|
2018-11-09 04:37:50 +00:00
|
|
|
"google.golang.org/grpc/status"
|
2018-09-26 17:33:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ DriverPlugin = &driverPluginClient{}
|
|
|
|
|
|
|
|
type driverPluginClient struct {
|
2018-09-26 05:18:03 +00:00
|
|
|
*base.BasePluginClient
|
2018-09-26 17:33:37 +00:00
|
|
|
|
|
|
|
client proto.DriverClient
|
2018-12-11 20:27:50 +00:00
|
|
|
logger hclog.Logger
|
2018-11-13 01:09:27 +00:00
|
|
|
|
|
|
|
// doneCtx is closed when the plugin exits
|
|
|
|
doneCtx context.Context
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driverPluginClient) TaskConfigSchema() (*hclspec.Spec, error) {
|
|
|
|
req := &proto.TaskConfigSchemaRequest{}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
resp, err := d.client.TaskConfigSchema(d.doneCtx, req)
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-12-21 19:23:21 +00:00
|
|
|
return nil, grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Spec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driverPluginClient) Capabilities() (*Capabilities, error) {
|
|
|
|
req := &proto.CapabilitiesRequest{}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
resp, err := d.client.Capabilities(d.doneCtx, req)
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-12-21 19:23:21 +00:00
|
|
|
return nil, grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
caps := &Capabilities{}
|
|
|
|
if resp.Capabilities != nil {
|
|
|
|
caps.SendSignals = resp.Capabilities.SendSignals
|
|
|
|
caps.Exec = resp.Capabilities.Exec
|
2019-08-14 13:10:10 +00:00
|
|
|
caps.MustInitiateNetwork = resp.Capabilities.MustCreateNetwork
|
|
|
|
|
|
|
|
for _, mode := range resp.Capabilities.NetworkIsolationModes {
|
|
|
|
caps.NetIsolationModes = append(caps.NetIsolationModes, netIsolationModeFromProto(mode))
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
|
|
|
|
switch resp.Capabilities.FsIsolation {
|
|
|
|
case proto.DriverCapabilities_NONE:
|
2019-01-04 21:11:25 +00:00
|
|
|
caps.FSIsolation = FSIsolationNone
|
2018-09-26 17:33:37 +00:00
|
|
|
case proto.DriverCapabilities_CHROOT:
|
2019-01-04 21:11:25 +00:00
|
|
|
caps.FSIsolation = FSIsolationChroot
|
2018-09-26 17:33:37 +00:00
|
|
|
case proto.DriverCapabilities_IMAGE:
|
2019-01-04 21:11:25 +00:00
|
|
|
caps.FSIsolation = FSIsolationImage
|
2018-09-26 17:33:37 +00:00
|
|
|
default:
|
2019-01-04 21:11:25 +00:00
|
|
|
caps.FSIsolation = FSIsolationNone
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
2020-05-21 13:18:02 +00:00
|
|
|
|
|
|
|
caps.MountConfigs = MountConfigSupport(resp.Capabilities.MountConfigs)
|
2020-12-17 23:21:46 +00:00
|
|
|
caps.RemoteTasks = resp.Capabilities.RemoteTasks
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return caps, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fingerprint the driver, return a chan that will be pushed to periodically and on changes to health
|
|
|
|
func (d *driverPluginClient) Fingerprint(ctx context.Context) (<-chan *Fingerprint, error) {
|
|
|
|
req := &proto.FingerprintRequest{}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
// Join the passed context and the shutdown context
|
2018-12-21 19:23:21 +00:00
|
|
|
joinedCtx, _ := joincontext.Join(ctx, d.doneCtx)
|
2018-11-13 01:09:27 +00:00
|
|
|
|
2018-12-21 19:23:21 +00:00
|
|
|
stream, err := d.client.Fingerprint(joinedCtx, req)
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-12-21 19:23:21 +00:00
|
|
|
return nil, grpcutils.HandleReqCtxGrpcErr(err, ctx, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
ch := make(chan *Fingerprint, 1)
|
2018-10-11 19:42:08 +00:00
|
|
|
go d.handleFingerprint(ctx, ch, stream)
|
2018-09-26 17:33:37 +00:00
|
|
|
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2018-12-21 19:23:21 +00:00
|
|
|
func (d *driverPluginClient) handleFingerprint(reqCtx context.Context, ch chan *Fingerprint, stream proto.Driver_FingerprintClient) {
|
2018-09-26 17:33:37 +00:00
|
|
|
defer close(ch)
|
|
|
|
for {
|
|
|
|
pb, err := stream.Recv()
|
|
|
|
if err != nil {
|
2018-11-13 01:09:27 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
ch <- &Fingerprint{
|
2018-12-21 19:23:21 +00:00
|
|
|
Err: grpcutils.HandleReqCtxGrpcErr(err, reqCtx, d.doneCtx),
|
2018-11-13 01:09:27 +00:00
|
|
|
}
|
2018-10-11 19:42:08 +00:00
|
|
|
}
|
2018-11-13 01:09:27 +00:00
|
|
|
|
|
|
|
// End the stream
|
2018-10-11 19:42:08 +00:00
|
|
|
return
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
2018-11-13 01:09:27 +00:00
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
f := &Fingerprint{
|
2018-11-21 00:30:39 +00:00
|
|
|
Attributes: pstructs.ConvertProtoAttributeMap(pb.Attributes),
|
2018-09-26 17:33:37 +00:00
|
|
|
Health: healthStateFromProto(pb.Health),
|
|
|
|
HealthDescription: pb.HealthDescription,
|
|
|
|
}
|
|
|
|
|
2018-10-11 19:42:08 +00:00
|
|
|
select {
|
2018-12-21 19:23:21 +00:00
|
|
|
case <-reqCtx.Done():
|
2018-10-11 19:42:08 +00:00
|
|
|
return
|
|
|
|
case ch <- f:
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RecoverTask does internal state recovery to be able to control the task of
|
|
|
|
// the given TaskHandle
|
|
|
|
func (d *driverPluginClient) RecoverTask(h *TaskHandle) error {
|
|
|
|
req := &proto.RecoverTaskRequest{Handle: taskHandleToProto(h)}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
_, err := d.client.RecoverTask(d.doneCtx, req)
|
2018-12-21 19:23:21 +00:00
|
|
|
return grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartTask starts execution of a task with the given TaskConfig. A TaskHandle
|
|
|
|
// is returned to the caller that can be used to recover state of the task,
|
|
|
|
// should the driver crash or exit prematurely.
|
2019-01-04 23:01:35 +00:00
|
|
|
func (d *driverPluginClient) StartTask(c *TaskConfig) (*TaskHandle, *DriverNetwork, error) {
|
2018-09-26 17:33:37 +00:00
|
|
|
req := &proto.StartTaskRequest{
|
|
|
|
Task: taskConfigToProto(c),
|
|
|
|
}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
resp, err := d.client.StartTask(d.doneCtx, req)
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-11-09 04:37:50 +00:00
|
|
|
st := status.Convert(err)
|
|
|
|
if len(st.Details()) > 0 {
|
2018-11-14 11:20:35 +00:00
|
|
|
if rec, ok := st.Details()[0].(*sproto.RecoverableError); ok {
|
2018-11-09 04:37:50 +00:00
|
|
|
return nil, nil, structs.NewRecoverableError(err, rec.Recoverable)
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 19:23:21 +00:00
|
|
|
return nil, nil, grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2018-10-04 19:08:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 23:01:35 +00:00
|
|
|
var net *DriverNetwork
|
2018-10-04 19:08:20 +00:00
|
|
|
if resp.NetworkOverride != nil {
|
2019-01-04 23:01:35 +00:00
|
|
|
net = &DriverNetwork{
|
2018-10-04 19:08:20 +00:00
|
|
|
PortMap: map[string]int{},
|
|
|
|
IP: resp.NetworkOverride.Addr,
|
|
|
|
AutoAdvertise: resp.NetworkOverride.AutoAdvertise,
|
|
|
|
}
|
|
|
|
for k, v := range resp.NetworkOverride.PortMap {
|
|
|
|
net.PortMap[k] = int(v)
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 19:08:20 +00:00
|
|
|
return taskHandleFromProto(resp.Handle), net, nil
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitTask returns a channel that will have an ExitResult pushed to it once when the task
|
|
|
|
// exits on its own or is killed. If WaitTask is called after the task has exited, the channel
|
|
|
|
// will immedialy return the ExitResult. WaitTask can be called multiple times for
|
|
|
|
// the same task without issue.
|
|
|
|
func (d *driverPluginClient) WaitTask(ctx context.Context, id string) (<-chan *ExitResult, error) {
|
|
|
|
ch := make(chan *ExitResult)
|
|
|
|
go d.handleWaitTask(ctx, id, ch)
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driverPluginClient) handleWaitTask(ctx context.Context, id string, ch chan *ExitResult) {
|
|
|
|
defer close(ch)
|
|
|
|
var result ExitResult
|
|
|
|
req := &proto.WaitTaskRequest{
|
|
|
|
TaskId: id,
|
|
|
|
}
|
|
|
|
|
2018-12-21 19:23:21 +00:00
|
|
|
// Join the passed context and the shutdown context
|
|
|
|
joinedCtx, _ := joincontext.Join(ctx, d.doneCtx)
|
|
|
|
|
|
|
|
resp, err := d.client.WaitTask(joinedCtx, req)
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-12-21 19:23:21 +00:00
|
|
|
result.Err = grpcutils.HandleReqCtxGrpcErr(err, ctx, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
} else {
|
|
|
|
result.ExitCode = int(resp.Result.ExitCode)
|
|
|
|
result.Signal = int(resp.Result.Signal)
|
|
|
|
result.OOMKilled = resp.Result.OomKilled
|
|
|
|
if len(resp.Err) > 0 {
|
|
|
|
result.Err = errors.New(resp.Err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ch <- &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopTask stops the task with the given taskID. A timeout and signal can be
|
|
|
|
// given to control a graceful termination of the task. The driver will send the
|
|
|
|
// given signal to the task and wait for the given timeout for it to exit. If the
|
|
|
|
// task does not exit within the timeout it will be forcefully killed.
|
|
|
|
func (d *driverPluginClient) StopTask(taskID string, timeout time.Duration, signal string) error {
|
|
|
|
req := &proto.StopTaskRequest{
|
|
|
|
TaskId: taskID,
|
|
|
|
Timeout: ptypes.DurationProto(timeout),
|
|
|
|
Signal: signal,
|
|
|
|
}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
_, err := d.client.StopTask(d.doneCtx, req)
|
2018-12-21 19:23:21 +00:00
|
|
|
return grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DestroyTask removes the task from the driver's in memory state. The task
|
|
|
|
// cannot be running unless force is set to true. If force is set to true the
|
|
|
|
// driver will forcefully terminate the task before removing it.
|
|
|
|
func (d *driverPluginClient) DestroyTask(taskID string, force bool) error {
|
|
|
|
req := &proto.DestroyTaskRequest{
|
|
|
|
TaskId: taskID,
|
|
|
|
Force: force,
|
|
|
|
}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
_, err := d.client.DestroyTask(d.doneCtx, req)
|
2018-12-21 19:23:21 +00:00
|
|
|
return grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InspectTask returns status information for a task
|
|
|
|
func (d *driverPluginClient) InspectTask(taskID string) (*TaskStatus, error) {
|
|
|
|
req := &proto.InspectTaskRequest{TaskId: taskID}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
resp, err := d.client.InspectTask(d.doneCtx, req)
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-12-21 19:23:21 +00:00
|
|
|
return nil, grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status, err := taskStatusFromProto(resp.Task)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Driver != nil {
|
|
|
|
status.DriverAttributes = resp.Driver.Attributes
|
|
|
|
}
|
|
|
|
if resp.NetworkOverride != nil {
|
2019-01-04 23:01:35 +00:00
|
|
|
status.NetworkOverride = &DriverNetwork{
|
2018-10-04 19:08:20 +00:00
|
|
|
PortMap: map[string]int{},
|
|
|
|
IP: resp.NetworkOverride.Addr,
|
2018-09-26 17:33:37 +00:00
|
|
|
AutoAdvertise: resp.NetworkOverride.AutoAdvertise,
|
|
|
|
}
|
2018-10-04 19:08:20 +00:00
|
|
|
for k, v := range resp.NetworkOverride.PortMap {
|
|
|
|
status.NetworkOverride.PortMap[k] = int(v)
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TaskStats returns resource usage statistics for the task
|
2018-12-11 20:27:50 +00:00
|
|
|
func (d *driverPluginClient) TaskStats(ctx context.Context, taskID string, interval time.Duration) (<-chan *cstructs.TaskResourceUsage, error) {
|
|
|
|
req := &proto.TaskStatsRequest{
|
2019-01-25 03:19:18 +00:00
|
|
|
TaskId: taskID,
|
|
|
|
CollectionInterval: ptypes.DurationProto(interval),
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
2018-12-11 20:27:50 +00:00
|
|
|
ctx, _ = joincontext.Join(ctx, d.doneCtx)
|
|
|
|
stream, err := d.client.TaskStats(ctx, req)
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-12-11 20:27:50 +00:00
|
|
|
st := status.Convert(err)
|
|
|
|
if len(st.Details()) > 0 {
|
|
|
|
if rec, ok := st.Details()[0].(*sproto.RecoverableError); ok {
|
|
|
|
return nil, structs.NewRecoverableError(err, rec.Recoverable)
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 05:27:37 +00:00
|
|
|
return nil, grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
ch := make(chan *cstructs.TaskResourceUsage, 1)
|
2019-01-10 18:31:43 +00:00
|
|
|
go d.handleStats(ctx, ch, stream)
|
2018-12-11 20:27:50 +00:00
|
|
|
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driverPluginClient) handleStats(ctx context.Context, ch chan<- *cstructs.TaskResourceUsage, stream proto.Driver_TaskStatsClient) {
|
|
|
|
defer close(ch)
|
|
|
|
for {
|
|
|
|
resp, err := stream.Recv()
|
2019-02-21 19:55:01 +00:00
|
|
|
if ctx.Err() != nil {
|
|
|
|
// Context canceled; exit gracefully
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:27:50 +00:00
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
2019-01-10 19:20:18 +00:00
|
|
|
d.logger.Error("error receiving stream from TaskStats driver RPC, closing stream", "error", err)
|
2018-12-11 20:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// End of stream
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stats, err := TaskStatsFromProto(resp.Stats)
|
|
|
|
if err != nil {
|
2019-01-10 19:20:18 +00:00
|
|
|
d.logger.Error("failed to decode stats from RPC", "error", err, "stats", resp.Stats)
|
|
|
|
continue
|
2018-12-11 20:27:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ch <- stats:
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TaskEvents returns a channel that will receive events from the driver about all
|
|
|
|
// tasks such as lifecycle events, terminal errors, etc.
|
|
|
|
func (d *driverPluginClient) TaskEvents(ctx context.Context) (<-chan *TaskEvent, error) {
|
|
|
|
req := &proto.TaskEventsRequest{}
|
2018-11-13 01:09:27 +00:00
|
|
|
|
|
|
|
// Join the passed context and the shutdown context
|
2018-12-21 19:23:21 +00:00
|
|
|
joinedCtx, _ := joincontext.Join(ctx, d.doneCtx)
|
2018-11-13 01:09:27 +00:00
|
|
|
|
2018-12-21 19:23:21 +00:00
|
|
|
stream, err := d.client.TaskEvents(joinedCtx, req)
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-12-21 19:23:21 +00:00
|
|
|
return nil, grpcutils.HandleReqCtxGrpcErr(err, ctx, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
ch := make(chan *TaskEvent, 1)
|
|
|
|
go d.handleTaskEvents(ctx, ch, stream)
|
2018-09-26 17:33:37 +00:00
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2018-12-21 19:23:21 +00:00
|
|
|
func (d *driverPluginClient) handleTaskEvents(reqCtx context.Context, ch chan *TaskEvent, stream proto.Driver_TaskEventsClient) {
|
2018-09-26 17:33:37 +00:00
|
|
|
defer close(ch)
|
|
|
|
for {
|
|
|
|
ev, err := stream.Recv()
|
|
|
|
if err != nil {
|
2018-11-13 01:09:27 +00:00
|
|
|
if err != io.EOF {
|
|
|
|
ch <- &TaskEvent{
|
2018-12-21 19:23:21 +00:00
|
|
|
Err: grpcutils.HandleReqCtxGrpcErr(err, reqCtx, d.doneCtx),
|
2018-11-13 01:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End the stream
|
|
|
|
return
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
2018-11-13 01:09:27 +00:00
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
timestamp, _ := ptypes.Timestamp(ev.Timestamp)
|
|
|
|
event := &TaskEvent{
|
|
|
|
TaskID: ev.TaskId,
|
2018-12-18 03:36:06 +00:00
|
|
|
AllocID: ev.AllocId,
|
|
|
|
TaskName: ev.TaskName,
|
2018-09-26 17:33:37 +00:00
|
|
|
Annotations: ev.Annotations,
|
|
|
|
Message: ev.Message,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
}
|
2018-11-13 01:09:27 +00:00
|
|
|
select {
|
2018-12-21 19:23:21 +00:00
|
|
|
case <-reqCtx.Done():
|
2018-11-13 01:09:27 +00:00
|
|
|
return
|
|
|
|
case ch <- event:
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignalTask will send the given signal to the specified task
|
|
|
|
func (d *driverPluginClient) SignalTask(taskID string, signal string) error {
|
|
|
|
req := &proto.SignalTaskRequest{
|
|
|
|
TaskId: taskID,
|
|
|
|
Signal: signal,
|
|
|
|
}
|
2018-11-13 01:09:27 +00:00
|
|
|
_, err := d.client.SignalTask(d.doneCtx, req)
|
2018-12-21 19:23:21 +00:00
|
|
|
return grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExecTask will run the given command within the execution context of the task.
|
|
|
|
// The driver will wait for the given timeout for the command to complete before
|
|
|
|
// terminating it. The stdout and stderr of the command will be return to the caller,
|
|
|
|
// along with other exit information such as exit code.
|
|
|
|
func (d *driverPluginClient) ExecTask(taskID string, cmd []string, timeout time.Duration) (*ExecTaskResult, error) {
|
|
|
|
req := &proto.ExecTaskRequest{
|
|
|
|
TaskId: taskID,
|
|
|
|
Command: cmd,
|
|
|
|
Timeout: ptypes.DurationProto(timeout),
|
|
|
|
}
|
|
|
|
|
2018-11-13 01:09:27 +00:00
|
|
|
resp, err := d.client.ExecTask(d.doneCtx, req)
|
2018-09-26 17:33:37 +00:00
|
|
|
if err != nil {
|
2018-12-21 19:23:21 +00:00
|
|
|
return nil, grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := &ExecTaskResult{
|
|
|
|
Stdout: resp.Stdout,
|
|
|
|
Stderr: resp.Stderr,
|
|
|
|
ExitResult: exitResultFromProto(resp.Result),
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2019-04-28 21:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ ExecTaskStreamingRawDriver = (*driverPluginClient)(nil)
|
2018-09-26 17:33:37 +00:00
|
|
|
|
2019-04-28 21:09:12 +00:00
|
|
|
func (d *driverPluginClient) ExecTaskStreamingRaw(ctx context.Context,
|
|
|
|
taskID string,
|
|
|
|
command []string,
|
|
|
|
tty bool,
|
|
|
|
execStream ExecTaskStream) error {
|
|
|
|
|
|
|
|
stream, err := d.client.ExecTaskStreaming(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return grpcutils.HandleGrpcErr(err, d.doneCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = stream.Send(&proto.ExecTaskStreamingRequest{
|
|
|
|
Setup: &proto.ExecTaskStreamingRequest_Setup{
|
|
|
|
TaskId: taskID,
|
|
|
|
Command: command,
|
|
|
|
Tty: tty,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return grpcutils.HandleGrpcErr(err, d.doneCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
errCh := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
m, err := execStream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := stream.Send(m); err != nil {
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := stream.Recv()
|
|
|
|
if err == io.EOF {
|
|
|
|
// Once we get to the end of stream successfully, we can ignore errCh:
|
|
|
|
// e.g. input write failures after process terminates shouldn't cause method to fail
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := execStream.Send(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
2019-05-03 16:05:30 +00:00
|
|
|
|
2022-03-11 23:33:08 +00:00
|
|
|
var _ DriverNetworkManager = (*driverPluginClient)(nil)
|
|
|
|
|
|
|
|
func (d *driverPluginClient) CreateNetwork(allocID string, _ *NetworkCreateRequest) (*NetworkIsolationSpec, bool, error) {
|
2019-05-03 16:05:30 +00:00
|
|
|
req := &proto.CreateNetworkRequest{
|
|
|
|
AllocId: allocID,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.client.CreateNetwork(d.doneCtx, req)
|
|
|
|
if err != nil {
|
2019-09-18 20:34:57 +00:00
|
|
|
return nil, false, grpcutils.HandleGrpcErr(err, d.doneCtx)
|
2019-05-03 16:05:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-18 20:34:57 +00:00
|
|
|
return NetworkIsolationSpecFromProto(resp.IsolationSpec), resp.Created, nil
|
2019-05-03 16:05:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *driverPluginClient) DestroyNetwork(allocID string, spec *NetworkIsolationSpec) error {
|
|
|
|
req := &proto.DestroyNetworkRequest{
|
|
|
|
AllocId: allocID,
|
|
|
|
IsolationSpec: NetworkIsolationSpecToProto(spec),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := d.client.DestroyNetwork(d.doneCtx, req)
|
|
|
|
if err != nil {
|
|
|
|
return grpcutils.HandleGrpcErr(err, d.doneCtx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|