2018-09-26 17:33:37 +00:00
|
|
|
package drivers
|
|
|
|
|
|
|
|
import (
|
2018-11-13 01:09:27 +00:00
|
|
|
"context"
|
2019-01-04 23:01:35 +00:00
|
|
|
"crypto/md5"
|
2018-09-26 17:33:37 +00:00
|
|
|
"fmt"
|
2019-01-04 23:01:35 +00:00
|
|
|
"io"
|
2018-09-26 17:33:37 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2019-01-04 23:01:35 +00:00
|
|
|
"strconv"
|
2018-09-26 17:33:37 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2018-12-11 20:27:50 +00:00
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2018-10-11 00:07:52 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-10-04 19:08:20 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
2019-04-28 20:58:56 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers/proto"
|
2018-09-26 17:33:37 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/shared/hclspec"
|
2018-11-21 00:30:39 +00:00
|
|
|
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
2018-10-04 19:08:20 +00:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/zclconf/go-cty/cty/msgpack"
|
2018-09-26 17:33:37 +00:00
|
|
|
)
|
|
|
|
|
2019-01-17 02:52:31 +00:00
|
|
|
const (
|
|
|
|
// DriverHealthy is the default health description that should be used
|
|
|
|
// if the driver is nominal
|
|
|
|
DriverHealthy = "Healthy"
|
|
|
|
|
|
|
|
// Pre09TaskHandleVersion is the version used to identify that the task
|
|
|
|
// handle is from a driver that existed before driver plugins (v0.9). The
|
|
|
|
// driver should take appropriate action to handle the old driver state.
|
|
|
|
Pre09TaskHandleVersion = 0
|
|
|
|
)
|
2019-01-07 04:04:15 +00:00
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
// DriverPlugin is the interface with drivers will implement. It is also
|
|
|
|
// implemented by a plugin client which proxies the calls to go-plugin. See
|
|
|
|
// the proto/driver.proto file for detailed information about each RPC and
|
|
|
|
// message structure.
|
|
|
|
type DriverPlugin interface {
|
|
|
|
base.BasePlugin
|
|
|
|
|
|
|
|
TaskConfigSchema() (*hclspec.Spec, error)
|
|
|
|
Capabilities() (*Capabilities, error)
|
|
|
|
Fingerprint(context.Context) (<-chan *Fingerprint, error)
|
|
|
|
|
|
|
|
RecoverTask(*TaskHandle) error
|
2019-01-04 23:01:35 +00:00
|
|
|
StartTask(*TaskConfig) (*TaskHandle, *DriverNetwork, error)
|
2018-09-26 17:33:37 +00:00
|
|
|
WaitTask(ctx context.Context, taskID string) (<-chan *ExitResult, error)
|
|
|
|
StopTask(taskID string, timeout time.Duration, signal string) error
|
|
|
|
DestroyTask(taskID string, force bool) error
|
|
|
|
InspectTask(taskID string) (*TaskStatus, error)
|
2018-12-11 20:27:50 +00:00
|
|
|
TaskStats(ctx context.Context, taskID string, interval time.Duration) (<-chan *cstructs.TaskResourceUsage, error)
|
2018-09-26 17:33:37 +00:00
|
|
|
TaskEvents(context.Context) (<-chan *TaskEvent, error)
|
|
|
|
|
|
|
|
SignalTask(taskID string, signal string) error
|
|
|
|
ExecTask(taskID string, cmd []string, timeout time.Duration) (*ExecTaskResult, error)
|
|
|
|
}
|
|
|
|
|
2019-04-28 20:58:56 +00:00
|
|
|
// ExecTaskStreamingDriver marks that a driver supports streaming exec task. This represents a user friendly
|
|
|
|
// interface to implement, as an alternative to the ExecTaskStreamingRawDriver, the low level interface.
|
|
|
|
type ExecTaskStreamingDriver interface {
|
|
|
|
ExecTaskStreaming(ctx context.Context, taskID string, execOptions *ExecOptions) (*ExitResult, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecOptions struct {
|
|
|
|
// Command is command to run
|
|
|
|
Command []string
|
|
|
|
|
|
|
|
// Tty indicates whether pseudo-terminal is to be allocated
|
|
|
|
Tty bool
|
|
|
|
|
|
|
|
// streams
|
|
|
|
Stdin io.ReadCloser
|
|
|
|
Stdout io.WriteCloser
|
|
|
|
Stderr io.WriteCloser
|
|
|
|
|
|
|
|
// terminal size channel
|
|
|
|
ResizeCh <-chan TerminalSize
|
|
|
|
}
|
|
|
|
|
2019-04-29 17:35:15 +00:00
|
|
|
// DriverNetworkManager is the interface with exposes function for creating a
|
|
|
|
// network namespace for which tasks can join. This only needs to be implemented
|
|
|
|
// if the driver MUST create the network namespace
|
|
|
|
type DriverNetworkManager interface {
|
2019-09-18 20:34:57 +00:00
|
|
|
CreateNetwork(allocID string) (*NetworkIsolationSpec, bool, error)
|
2019-04-29 17:35:15 +00:00
|
|
|
DestroyNetwork(allocID string, spec *NetworkIsolationSpec) error
|
|
|
|
}
|
|
|
|
|
2018-12-20 12:21:34 +00:00
|
|
|
// InternalDriverPlugin is an interface that exposes functions that are only
|
|
|
|
// implemented by internal driver plugins.
|
|
|
|
type InternalDriverPlugin interface {
|
|
|
|
// Shutdown allows the plugin to cleanup any running state to avoid leaking
|
|
|
|
// resources. It should not block.
|
|
|
|
Shutdown()
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
// DriverSignalTaskNotSupported can be embedded by drivers which don't support
|
|
|
|
// the SignalTask RPC. This satisfies the SignalTask func requirement for the
|
|
|
|
// DriverPlugin interface.
|
|
|
|
type DriverSignalTaskNotSupported struct{}
|
|
|
|
|
2018-10-18 20:39:02 +00:00
|
|
|
func (DriverSignalTaskNotSupported) SignalTask(taskID, signal string) error {
|
2018-09-26 17:33:37 +00:00
|
|
|
return fmt.Errorf("SignalTask is not supported by this driver")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DriverExecTaskNotSupported can be embedded by drivers which don't support
|
|
|
|
// the ExecTask RPC. This satisfies the ExecTask func requirement of the
|
|
|
|
// DriverPlugin interface.
|
|
|
|
type DriverExecTaskNotSupported struct{}
|
|
|
|
|
2019-08-29 15:36:29 +00:00
|
|
|
func (_ DriverExecTaskNotSupported) ExecTask(taskID string, cmd []string, timeout time.Duration) (*ExecTaskResult, error) {
|
|
|
|
return nil, fmt.Errorf("ExecTask is not supported by this driver")
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type HealthState string
|
|
|
|
|
|
|
|
var (
|
|
|
|
HealthStateUndetected = HealthState("undetected")
|
|
|
|
HealthStateUnhealthy = HealthState("unhealthy")
|
|
|
|
HealthStateHealthy = HealthState("healthy")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Fingerprint struct {
|
2018-11-21 00:30:39 +00:00
|
|
|
Attributes map[string]*pstructs.Attribute
|
2018-09-26 17:33:37 +00:00
|
|
|
Health HealthState
|
|
|
|
HealthDescription string
|
|
|
|
|
2018-10-05 02:36:40 +00:00
|
|
|
// Err is set by the plugin if an error occurred during fingerprinting
|
2018-09-26 17:33:37 +00:00
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2019-01-04 21:11:25 +00:00
|
|
|
// FSIsolation is an enumeration to describe what kind of filesystem isolation
|
|
|
|
// a driver supports.
|
2018-09-26 17:33:37 +00:00
|
|
|
type FSIsolation string
|
|
|
|
|
|
|
|
var (
|
2019-01-04 21:11:25 +00:00
|
|
|
// FSIsolationNone means no isolation. The host filesystem is used.
|
|
|
|
FSIsolationNone = FSIsolation("none")
|
|
|
|
|
|
|
|
// FSIsolationChroot means the driver will use a chroot on the host
|
|
|
|
// filesystem.
|
2018-09-26 17:33:37 +00:00
|
|
|
FSIsolationChroot = FSIsolation("chroot")
|
2019-01-04 21:11:25 +00:00
|
|
|
|
|
|
|
// FSIsolationImage means the driver uses an image.
|
|
|
|
FSIsolationImage = FSIsolation("image")
|
2018-09-26 17:33:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Capabilities struct {
|
|
|
|
// SendSignals marks the driver as being able to send signals
|
|
|
|
SendSignals bool
|
|
|
|
|
|
|
|
// Exec marks the driver as being able to execute arbitrary commands
|
|
|
|
// such as health checks. Used by the ScriptExecutor interface.
|
|
|
|
Exec bool
|
|
|
|
|
|
|
|
//FSIsolation indicates what kind of filesystem isolation the driver supports.
|
2019-01-04 21:11:25 +00:00
|
|
|
FSIsolation FSIsolation
|
2019-04-29 17:35:15 +00:00
|
|
|
|
|
|
|
//NetIsolationModes lists the set of isolation modes supported by the driver
|
|
|
|
NetIsolationModes []NetIsolationMode
|
|
|
|
|
|
|
|
// MustInitiateNetwork tells Nomad that the driver must create the network
|
|
|
|
// namespace and that the CreateNetwork and DestroyNetwork RPCs are implemented.
|
|
|
|
MustInitiateNetwork bool
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:30:10 +00:00
|
|
|
func (c *Capabilities) HasNetIsolationMode(m NetIsolationMode) bool {
|
|
|
|
for _, mode := range c.NetIsolationModes {
|
|
|
|
if mode == m {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-29 17:35:15 +00:00
|
|
|
type NetIsolationMode string
|
|
|
|
|
|
|
|
var (
|
|
|
|
// NetIsolationModeHost disables network isolation and uses the host network
|
|
|
|
NetIsolationModeHost = NetIsolationMode("host")
|
|
|
|
|
|
|
|
// NetIsolationModeGroup uses the group network namespace for isolation
|
|
|
|
NetIsolationModeGroup = NetIsolationMode("group")
|
|
|
|
|
|
|
|
// NetIsolationModeTask isolates the network to just the task
|
|
|
|
NetIsolationModeTask = NetIsolationMode("task")
|
|
|
|
|
|
|
|
// NetIsolationModeNone indicates that there is no network to isolate and is
|
|
|
|
// inteded to be used for tasks that the client manages remotely
|
|
|
|
NetIsolationModeNone = NetIsolationMode("none")
|
|
|
|
)
|
|
|
|
|
|
|
|
type NetworkIsolationSpec struct {
|
|
|
|
Mode NetIsolationMode
|
|
|
|
Path string
|
|
|
|
Labels map[string]string
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-28 20:58:56 +00:00
|
|
|
type TerminalSize struct {
|
|
|
|
Height int
|
|
|
|
Width int
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
type TaskConfig struct {
|
2019-04-29 17:35:15 +00:00
|
|
|
ID string
|
|
|
|
JobName string
|
|
|
|
TaskGroupName string
|
|
|
|
Name string
|
|
|
|
Env map[string]string
|
|
|
|
DeviceEnv map[string]string
|
|
|
|
Resources *Resources
|
|
|
|
Devices []*DeviceConfig
|
|
|
|
Mounts []*MountConfig
|
|
|
|
User string
|
|
|
|
AllocDir string
|
|
|
|
rawDriverConfig []byte
|
|
|
|
StdoutPath string
|
|
|
|
StderrPath string
|
|
|
|
AllocID string
|
|
|
|
NetworkIsolation *NetworkIsolationSpec
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 00:07:52 +00:00
|
|
|
func (tc *TaskConfig) Copy() *TaskConfig {
|
|
|
|
if tc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c := new(TaskConfig)
|
|
|
|
*c = *tc
|
|
|
|
c.Env = helper.CopyMapStringString(c.Env)
|
2018-12-19 22:23:09 +00:00
|
|
|
c.DeviceEnv = helper.CopyMapStringString(c.DeviceEnv)
|
2018-10-11 00:07:52 +00:00
|
|
|
c.Resources = tc.Resources.Copy()
|
2018-12-19 22:23:09 +00:00
|
|
|
|
|
|
|
if c.Devices != nil {
|
|
|
|
dc := make([]*DeviceConfig, len(c.Devices))
|
|
|
|
for i, c := range c.Devices {
|
|
|
|
dc[i] = c.Copy()
|
|
|
|
}
|
|
|
|
c.Devices = dc
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Mounts != nil {
|
|
|
|
mc := make([]*MountConfig, len(c.Mounts))
|
|
|
|
for i, m := range c.Mounts {
|
|
|
|
mc[i] = m.Copy()
|
|
|
|
}
|
|
|
|
c.Mounts = mc
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:07:52 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
func (tc *TaskConfig) EnvList() []string {
|
|
|
|
l := make([]string, 0, len(tc.Env))
|
|
|
|
for k, v := range tc.Env {
|
|
|
|
l = append(l, k+"="+v)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(l)
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *TaskConfig) TaskDir() *allocdir.TaskDir {
|
|
|
|
taskDir := filepath.Join(tc.AllocDir, tc.Name)
|
|
|
|
return &allocdir.TaskDir{
|
|
|
|
Dir: taskDir,
|
|
|
|
SharedAllocDir: filepath.Join(tc.AllocDir, allocdir.SharedAllocName),
|
|
|
|
LogDir: filepath.Join(tc.AllocDir, allocdir.SharedAllocName, allocdir.LogDirName),
|
|
|
|
SharedTaskDir: filepath.Join(taskDir, allocdir.SharedAllocName),
|
|
|
|
LocalDir: filepath.Join(taskDir, allocdir.TaskLocal),
|
|
|
|
SecretsDir: filepath.Join(taskDir, allocdir.TaskSecrets),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tc *TaskConfig) DecodeDriverConfig(t interface{}) error {
|
2018-10-12 17:37:28 +00:00
|
|
|
return base.MsgPackDecode(tc.rawDriverConfig, t)
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 19:08:20 +00:00
|
|
|
func (tc *TaskConfig) EncodeDriverConfig(val cty.Value) error {
|
|
|
|
data, err := msgpack.Marshal(val, val.Type())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tc.rawDriverConfig = data
|
|
|
|
return nil
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-06 05:39:48 +00:00
|
|
|
func (tc *TaskConfig) EncodeConcreteDriverConfig(t interface{}) error {
|
|
|
|
data := []byte{}
|
|
|
|
err := base.MsgPackEncode(&data, t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tc.rawDriverConfig = data
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
type Resources struct {
|
2018-12-13 23:06:48 +00:00
|
|
|
NomadResources *structs.AllocatedTaskResources
|
2018-10-04 19:08:20 +00:00
|
|
|
LinuxResources *LinuxResources
|
|
|
|
}
|
|
|
|
|
2018-10-11 00:07:52 +00:00
|
|
|
func (r *Resources) Copy() *Resources {
|
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
res := new(Resources)
|
|
|
|
if r.NomadResources != nil {
|
|
|
|
res.NomadResources = r.NomadResources.Copy()
|
|
|
|
}
|
|
|
|
if r.LinuxResources != nil {
|
|
|
|
res.LinuxResources = r.LinuxResources.Copy()
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:08:20 +00:00
|
|
|
type LinuxResources struct {
|
2018-09-26 17:33:37 +00:00
|
|
|
CPUPeriod int64
|
|
|
|
CPUQuota int64
|
|
|
|
CPUShares int64
|
|
|
|
MemoryLimitBytes int64
|
|
|
|
OOMScoreAdj int64
|
|
|
|
CpusetCPUs string
|
|
|
|
CpusetMems string
|
2018-11-16 16:08:53 +00:00
|
|
|
|
|
|
|
// PrecentTicks is used to calculate the CPUQuota, currently the docker
|
|
|
|
// driver exposes cpu period and quota through the driver configuration
|
|
|
|
// and thus the calculation for CPUQuota cannot be done on the client.
|
|
|
|
// This is a capatability and should only be used by docker until the docker
|
|
|
|
// specific options are deprecated in favor of exposes CPUPeriod and
|
|
|
|
// CPUQuota at the task resource stanza.
|
|
|
|
PercentTicks float64
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 00:07:52 +00:00
|
|
|
func (r *LinuxResources) Copy() *LinuxResources {
|
|
|
|
res := new(LinuxResources)
|
|
|
|
*res = *r
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
type DeviceConfig struct {
|
|
|
|
TaskPath string
|
|
|
|
HostPath string
|
|
|
|
Permissions string
|
|
|
|
}
|
|
|
|
|
2018-12-19 22:23:09 +00:00
|
|
|
func (d *DeviceConfig) Copy() *DeviceConfig {
|
|
|
|
if d == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dc := new(DeviceConfig)
|
|
|
|
*dc = *d
|
|
|
|
return dc
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
type MountConfig struct {
|
volumes: Add support for mount propagation
This commit introduces support for configuring mount propagation when
mounting volumes with the `volume_mount` stanza on Linux targets.
Similar to Kubernetes, we expose 3 options for configuring mount
propagation:
- private, which is equivalent to `rprivate` on Linux, which does not allow the
container to see any new nested mounts after the chroot was created.
- host-to-task, which is equivalent to `rslave` on Linux, which allows new mounts
that have been created _outside of the container_ to be visible
inside the container after the chroot is created.
- bidirectional, which is equivalent to `rshared` on Linux, which allows both
the container to see new mounts created on the host, but
importantly _allows the container to create mounts that are
visible in other containers an don the host_
private and host-to-task are safe, but bidirectional mounts can be
dangerous, as if the code inside a container creates a mount, and does
not clean it up before tearing down the container, it can cause bad
things to happen inside the kernel.
To add a layer of safety here, we require that the user has ReadWrite
permissions on the volume before allowing bidirectional mounts, as a
defense in depth / validation case, although creating mounts should also require
a priviliged execution environment inside the container.
2019-09-13 21:13:20 +00:00
|
|
|
TaskPath string
|
|
|
|
HostPath string
|
|
|
|
Readonly bool
|
|
|
|
PropagationMode string
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 14:48:28 +00:00
|
|
|
func (m *MountConfig) IsEqual(o *MountConfig) bool {
|
|
|
|
return m.TaskPath == o.TaskPath &&
|
|
|
|
m.HostPath == o.HostPath &&
|
volumes: Add support for mount propagation
This commit introduces support for configuring mount propagation when
mounting volumes with the `volume_mount` stanza on Linux targets.
Similar to Kubernetes, we expose 3 options for configuring mount
propagation:
- private, which is equivalent to `rprivate` on Linux, which does not allow the
container to see any new nested mounts after the chroot was created.
- host-to-task, which is equivalent to `rslave` on Linux, which allows new mounts
that have been created _outside of the container_ to be visible
inside the container after the chroot is created.
- bidirectional, which is equivalent to `rshared` on Linux, which allows both
the container to see new mounts created on the host, but
importantly _allows the container to create mounts that are
visible in other containers an don the host_
private and host-to-task are safe, but bidirectional mounts can be
dangerous, as if the code inside a container creates a mount, and does
not clean it up before tearing down the container, it can cause bad
things to happen inside the kernel.
To add a layer of safety here, we require that the user has ReadWrite
permissions on the volume before allowing bidirectional mounts, as a
defense in depth / validation case, although creating mounts should also require
a priviliged execution environment inside the container.
2019-09-13 21:13:20 +00:00
|
|
|
m.Readonly == o.Readonly &&
|
|
|
|
m.PropagationMode == o.PropagationMode
|
2019-07-25 14:48:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 22:23:09 +00:00
|
|
|
func (m *MountConfig) Copy() *MountConfig {
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
mc := new(MountConfig)
|
|
|
|
*mc = *m
|
|
|
|
return mc
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
const (
|
|
|
|
TaskStateUnknown TaskState = "unknown"
|
|
|
|
TaskStateRunning TaskState = "running"
|
|
|
|
TaskStateExited TaskState = "exited"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TaskState string
|
|
|
|
|
|
|
|
type ExitResult struct {
|
|
|
|
ExitCode int
|
|
|
|
Signal int
|
|
|
|
OOMKilled bool
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2018-09-26 05:18:03 +00:00
|
|
|
func (r *ExitResult) Successful() bool {
|
|
|
|
return r.ExitCode == 0 && r.Signal == 0 && r.Err == nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 01:41:32 +00:00
|
|
|
func (r *ExitResult) Copy() *ExitResult {
|
2018-11-26 21:53:15 +00:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-11-21 01:41:32 +00:00
|
|
|
res := new(ExitResult)
|
|
|
|
*res = *r
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-09-26 17:33:37 +00:00
|
|
|
type TaskStatus struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
State TaskState
|
|
|
|
StartedAt time.Time
|
|
|
|
CompletedAt time.Time
|
|
|
|
ExitResult *ExitResult
|
|
|
|
DriverAttributes map[string]string
|
2019-01-04 23:01:35 +00:00
|
|
|
NetworkOverride *DriverNetwork
|
2018-09-26 17:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TaskEvent struct {
|
|
|
|
TaskID string
|
2018-12-18 03:36:06 +00:00
|
|
|
TaskName string
|
|
|
|
AllocID string
|
2018-09-26 17:33:37 +00:00
|
|
|
Timestamp time.Time
|
|
|
|
Message string
|
|
|
|
Annotations map[string]string
|
|
|
|
|
2018-10-05 02:36:40 +00:00
|
|
|
// Err is only used if an error occurred while consuming the RPC stream
|
2018-09-26 17:33:37 +00:00
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecTaskResult struct {
|
|
|
|
Stdout []byte
|
|
|
|
Stderr []byte
|
|
|
|
ExitResult *ExitResult
|
|
|
|
}
|
2019-01-04 23:01:35 +00:00
|
|
|
|
|
|
|
// DriverNetwork is the network created by driver's (eg Docker's bridge
|
|
|
|
// network) during Prestart.
|
|
|
|
type DriverNetwork struct {
|
|
|
|
// PortMap can be set by drivers to replace ports in environment
|
|
|
|
// variables with driver-specific mappings.
|
|
|
|
PortMap map[string]int
|
|
|
|
|
|
|
|
// IP is the IP address for the task created by the driver.
|
|
|
|
IP string
|
|
|
|
|
|
|
|
// AutoAdvertise indicates whether the driver thinks services that
|
|
|
|
// choose to auto-advertise-addresses should use this IP instead of the
|
|
|
|
// host's. eg If a Docker network plugin is used
|
|
|
|
AutoAdvertise bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advertise returns true if the driver suggests using the IP set. May be
|
|
|
|
// called on a nil Network in which case it returns false.
|
|
|
|
func (d *DriverNetwork) Advertise() bool {
|
|
|
|
return d != nil && d.AutoAdvertise
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy a DriverNetwork struct. If it is nil, nil is returned.
|
|
|
|
func (d *DriverNetwork) Copy() *DriverNetwork {
|
|
|
|
if d == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pm := make(map[string]int, len(d.PortMap))
|
|
|
|
for k, v := range d.PortMap {
|
|
|
|
pm[k] = v
|
|
|
|
}
|
|
|
|
return &DriverNetwork{
|
|
|
|
PortMap: pm,
|
|
|
|
IP: d.IP,
|
|
|
|
AutoAdvertise: d.AutoAdvertise,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash the contents of a DriverNetwork struct to detect changes. If it is nil,
|
|
|
|
// an empty slice is returned.
|
|
|
|
func (d *DriverNetwork) Hash() []byte {
|
|
|
|
if d == nil {
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
h := md5.New()
|
|
|
|
io.WriteString(h, d.IP)
|
|
|
|
io.WriteString(h, strconv.FormatBool(d.AutoAdvertise))
|
|
|
|
for k, v := range d.PortMap {
|
|
|
|
io.WriteString(h, k)
|
|
|
|
io.WriteString(h, strconv.Itoa(v))
|
|
|
|
}
|
|
|
|
return h.Sum(nil)
|
|
|
|
}
|
2019-04-28 20:58:56 +00:00
|
|
|
|
|
|
|
//// helper types for operating on raw exec operation
|
|
|
|
// we alias proto instances as much as possible to avoid conversion overhead
|
|
|
|
|
|
|
|
// ExecTaskStreamingRawDriver represents a low-level interface for executing a streaming exec
|
|
|
|
// call, and is intended to be used when driver instance is to delegate exec handling to another
|
|
|
|
// backend, e.g. to a executor or a driver behind a grpc/rpc protocol
|
|
|
|
//
|
|
|
|
// Nomad client would prefer this interface method over `ExecTaskStreaming` if driver implements it.
|
|
|
|
type ExecTaskStreamingRawDriver interface {
|
|
|
|
ExecTaskStreamingRaw(
|
|
|
|
ctx context.Context,
|
|
|
|
taskID string,
|
|
|
|
command []string,
|
|
|
|
tty bool,
|
|
|
|
stream ExecTaskStream) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecTaskStream represents a stream of exec streaming messages,
|
|
|
|
// and is a handle to get stdin and tty size and send back
|
|
|
|
// stdout/stderr and exit operations.
|
|
|
|
//
|
|
|
|
// The methods are not concurrent safe; callers must ensure that methods are called
|
|
|
|
// from at most one goroutine.
|
|
|
|
type ExecTaskStream interface {
|
|
|
|
// Send relays response message back to API.
|
|
|
|
//
|
|
|
|
// The call is synchronous and no references to message is held: once
|
|
|
|
// method call completes, the message reference can be reused or freed.
|
|
|
|
Send(*ExecTaskStreamingResponseMsg) error
|
|
|
|
|
|
|
|
// Receive exec streaming messages from API. Returns `io.EOF` on completion of stream.
|
|
|
|
Recv() (*ExecTaskStreamingRequestMsg, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecTaskStreamingRequestMsg = proto.ExecTaskStreamingRequest
|
|
|
|
type ExecTaskStreamingResponseMsg = proto.ExecTaskStreamingResponse
|
2019-12-07 03:11:41 +00:00
|
|
|
|
|
|
|
// InternalCapabilitiesDriver is an experimental interface enabling a driver
|
|
|
|
// to disable some nomad functionality (e.g. logs or metrics).
|
|
|
|
//
|
|
|
|
// Intended for internal drivers only while the interface is stabalized.
|
|
|
|
type InternalCapabilitiesDriver interface {
|
|
|
|
InternalCapabilities() InternalCapabilities
|
|
|
|
}
|
|
|
|
|
|
|
|
// InternalCapabilities flags disabled functionality.
|
|
|
|
// Zero value means all is supported.
|
|
|
|
type InternalCapabilities struct {
|
|
|
|
DisableLogCollection bool
|
|
|
|
DisableMetricsCollection bool
|
|
|
|
}
|