2017-10-10 06:01:55 +00:00
|
|
|
// +build linux
|
2017-09-27 21:21:45 +00:00
|
|
|
|
2015-09-29 21:48:10 +00:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
2015-10-07 22:24:16 +00:00
|
|
|
"bytes"
|
2017-02-01 00:43:57 +00:00
|
|
|
"context"
|
2015-10-07 22:24:16 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-02-01 00:43:57 +00:00
|
|
|
"io/ioutil"
|
2015-10-07 22:24:16 +00:00
|
|
|
"log"
|
2016-03-08 05:28:24 +00:00
|
|
|
"net"
|
2016-10-07 19:37:52 +00:00
|
|
|
"os"
|
2015-10-07 22:24:16 +00:00
|
|
|
"os/exec"
|
2015-10-09 19:14:56 +00:00
|
|
|
"path/filepath"
|
2015-10-07 22:24:16 +00:00
|
|
|
"regexp"
|
|
|
|
"runtime"
|
2016-08-16 01:49:06 +00:00
|
|
|
"strconv"
|
2015-10-07 22:24:16 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2017-09-20 15:09:08 +00:00
|
|
|
appcschema "github.com/appc/spec/schema"
|
|
|
|
rktv1 "github.com/rkt/rkt/api/v1"
|
|
|
|
|
2016-02-17 00:10:45 +00:00
|
|
|
"github.com/hashicorp/go-plugin"
|
2015-12-21 17:48:21 +00:00
|
|
|
"github.com/hashicorp/go-version"
|
2015-10-09 19:14:56 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocdir"
|
2015-10-07 22:24:16 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
2017-04-21 23:50:20 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/env"
|
2016-02-17 00:10:45 +00:00
|
|
|
"github.com/hashicorp/nomad/client/driver/executor"
|
2016-06-12 03:15:50 +00:00
|
|
|
dstructs "github.com/hashicorp/nomad/client/driver/structs"
|
|
|
|
cstructs "github.com/hashicorp/nomad/client/structs"
|
2017-02-21 03:35:51 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2016-04-09 22:38:42 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/fields"
|
2015-10-07 22:24:16 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-11-14 02:09:42 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2015-09-29 21:48:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-02-02 23:39:45 +00:00
|
|
|
reRktVersion = regexp.MustCompile(`rkt [vV]ersion[:]? (\d[.\d]+)`)
|
|
|
|
reAppcVersion = regexp.MustCompile(`appc [vV]ersion[:]? (\d[.\d]+)`)
|
2015-09-29 21:48:10 +00:00
|
|
|
)
|
|
|
|
|
2015-12-22 05:15:37 +00:00
|
|
|
const (
|
2015-12-22 18:11:22 +00:00
|
|
|
// minRktVersion is the earliest supported version of rkt. rkt added support
|
|
|
|
// for CPU and memory isolators in 0.14.0. We cannot support an earlier
|
|
|
|
// version to maintain an uniform interface across all drivers
|
2017-09-26 16:43:50 +00:00
|
|
|
minRktVersion = "1.27.0"
|
2015-12-22 18:11:22 +00:00
|
|
|
|
2016-04-01 01:11:27 +00:00
|
|
|
// The key populated in the Node Attributes to indicate the presence of the
|
|
|
|
// Rkt driver
|
|
|
|
rktDriverAttr = "driver.rkt"
|
2016-10-13 23:18:18 +00:00
|
|
|
|
|
|
|
// rktVolumesConfigOption is the key for enabling the use of custom
|
|
|
|
// bind volumes.
|
2016-10-20 00:13:45 +00:00
|
|
|
rktVolumesConfigOption = "rkt.volumes.enabled"
|
|
|
|
rktVolumesConfigDefault = true
|
2016-10-19 00:36:19 +00:00
|
|
|
|
|
|
|
// rktCmd is the command rkt is installed as.
|
|
|
|
rktCmd = "rkt"
|
2017-02-01 00:43:57 +00:00
|
|
|
|
2017-09-20 15:09:08 +00:00
|
|
|
// rktNetworkDeadline is how long to wait for container network to start
|
|
|
|
rktNetworkDeadline = 5 * time.Second
|
2015-12-22 05:15:37 +00:00
|
|
|
)
|
|
|
|
|
2015-09-29 21:48:10 +00:00
|
|
|
// RktDriver is a driver for running images via Rkt
|
|
|
|
// We attempt to chose sane defaults for now, with more configuration available
|
|
|
|
// planned in the future
|
|
|
|
type RktDriver struct {
|
2015-10-07 22:24:16 +00:00
|
|
|
DriverContext
|
2017-02-21 03:35:51 +00:00
|
|
|
|
|
|
|
// A tri-state boolean to know if the fingerprinting has happened and
|
|
|
|
// whether it has been successful
|
|
|
|
fingerprintSuccess *bool
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 04:22:49 +00:00
|
|
|
type RktDriverConfig struct {
|
2016-08-16 01:49:06 +00:00
|
|
|
ImageName string `mapstructure:"image"`
|
|
|
|
Command string `mapstructure:"command"`
|
|
|
|
Args []string `mapstructure:"args"`
|
|
|
|
TrustPrefix string `mapstructure:"trust_prefix"`
|
|
|
|
DNSServers []string `mapstructure:"dns_servers"` // DNS Server for containers
|
|
|
|
DNSSearchDomains []string `mapstructure:"dns_search_domains"` // DNS Search domains for containers
|
|
|
|
Net []string `mapstructure:"net"` // Networks for the containers
|
|
|
|
PortMapRaw []map[string]string `mapstructure:"port_map"` //
|
|
|
|
PortMap map[string]string `mapstructure:"-"` // A map of host port and the port name defined in the image manifest file
|
2017-07-21 11:05:15 +00:00
|
|
|
Volumes []string `mapstructure:"volumes"` // Host-Volumes to mount in, syntax: /path/to/host/directory:/destination/path/in/container[:readOnly]
|
2017-06-07 07:58:42 +00:00
|
|
|
InsecureOptions []string `mapstructure:"insecure_options"` // list of args for --insecure-options
|
2016-08-16 01:49:06 +00:00
|
|
|
|
2017-06-09 17:40:47 +00:00
|
|
|
NoOverlay bool `mapstructure:"no_overlay"` // disable overlayfs for rkt run
|
|
|
|
Debug bool `mapstructure:"debug"` // Enable debug option for rkt command
|
2015-11-14 02:09:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 21:48:10 +00:00
|
|
|
// rktHandle is returned from Start/Open as a handle to the PID
|
|
|
|
type rktHandle struct {
|
2017-02-01 00:43:57 +00:00
|
|
|
uuid string
|
2017-05-17 00:33:50 +00:00
|
|
|
env *env.TaskEnv
|
2017-04-21 23:50:20 +00:00
|
|
|
taskDir *allocdir.TaskDir
|
2016-03-03 17:21:21 +00:00
|
|
|
pluginClient *plugin.Client
|
|
|
|
executorPid int
|
|
|
|
executor executor.Executor
|
|
|
|
logger *log.Logger
|
|
|
|
killTimeout time.Duration
|
|
|
|
maxKillTimeout time.Duration
|
2016-06-12 03:15:50 +00:00
|
|
|
waitCh chan *dstructs.WaitResult
|
2016-03-03 17:21:21 +00:00
|
|
|
doneCh chan struct{}
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// rktPID is a struct to map the pid running the process to the vm image on
|
|
|
|
// disk
|
|
|
|
type rktPID struct {
|
2017-02-01 00:43:57 +00:00
|
|
|
UUID string
|
2016-03-03 17:21:21 +00:00
|
|
|
PluginConfig *PluginReattachConfig
|
|
|
|
ExecutorPid int
|
|
|
|
KillTimeout time.Duration
|
|
|
|
MaxKillTimeout time.Duration
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 15:09:08 +00:00
|
|
|
// Retrieve pod status for the pod with the given UUID.
|
|
|
|
func rktGetStatus(uuid string) (*rktv1.Pod, error) {
|
|
|
|
statusArgs := []string{
|
|
|
|
"status",
|
|
|
|
"--format=json",
|
|
|
|
uuid,
|
|
|
|
}
|
2017-09-26 07:00:35 +00:00
|
|
|
var outBuf bytes.Buffer
|
2017-09-20 15:09:08 +00:00
|
|
|
cmd := exec.Command(rktCmd, statusArgs...)
|
|
|
|
cmd.Stdout = &outBuf
|
2017-09-26 07:00:35 +00:00
|
|
|
cmd.Stderr = ioutil.Discard
|
2017-09-20 15:09:08 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var status rktv1.Pod
|
|
|
|
if err := json.Unmarshal(outBuf.Bytes(), &status); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &status, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieves a pod manifest
|
|
|
|
func rktGetManifest(uuid string) (*appcschema.PodManifest, error) {
|
|
|
|
statusArgs := []string{
|
|
|
|
"cat-manifest",
|
|
|
|
uuid,
|
|
|
|
}
|
2017-09-26 07:00:35 +00:00
|
|
|
var outBuf bytes.Buffer
|
2017-09-20 15:09:08 +00:00
|
|
|
cmd := exec.Command(rktCmd, statusArgs...)
|
|
|
|
cmd.Stdout = &outBuf
|
2017-09-26 07:00:35 +00:00
|
|
|
cmd.Stderr = ioutil.Discard
|
2017-09-20 15:09:08 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var manifest appcschema.PodManifest
|
|
|
|
if err := json.Unmarshal(outBuf.Bytes(), &manifest); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &manifest, nil
|
|
|
|
}
|
|
|
|
|
2017-09-26 22:15:27 +00:00
|
|
|
func rktGetDriverNetwork(uuid string, driverConfigPortMap map[string]string) (*cstructs.DriverNetwork, error) {
|
|
|
|
deadline := time.Now().Add(rktNetworkDeadline)
|
|
|
|
var lastErr error
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
if status, err := rktGetStatus(uuid); err == nil {
|
|
|
|
for _, net := range status.Networks {
|
|
|
|
if !net.IP.IsGlobalUnicast() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the pod manifest so we can figure out which ports are exposed
|
|
|
|
var portmap map[string]int
|
|
|
|
manifest, err := rktGetManifest(uuid)
|
|
|
|
if err == nil {
|
|
|
|
portmap, err = rktManifestMakePortMap(manifest, driverConfigPortMap)
|
|
|
|
if err != nil {
|
|
|
|
lastErr = fmt.Errorf("could not create manifest-based portmap: %v", err)
|
|
|
|
return nil, lastErr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lastErr = fmt.Errorf("could not get pod manifest: %v", err)
|
|
|
|
return nil, lastErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cstructs.DriverNetwork{
|
|
|
|
PortMap: portmap,
|
|
|
|
IP: status.Networks[0].IP.String(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(status.Networks) == 0 {
|
|
|
|
lastErr = fmt.Errorf("no networks found")
|
|
|
|
} else {
|
|
|
|
lastErr = fmt.Errorf("no good driver networks out of %d returned", len(status.Networks))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lastErr = fmt.Errorf("getting status failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(400 * time.Millisecond)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("timed out, last error: %v", lastErr)
|
|
|
|
}
|
|
|
|
|
2017-09-20 15:09:08 +00:00
|
|
|
// Given a rkt/appc pod manifest and driver portmap configuration, create
|
|
|
|
// a driver portmap.
|
|
|
|
func rktManifestMakePortMap(manifest *appcschema.PodManifest, configPortMap map[string]string) (map[string]int, error) {
|
|
|
|
if len(manifest.Apps) == 0 {
|
|
|
|
return nil, fmt.Errorf("manifest has no apps")
|
|
|
|
}
|
|
|
|
if len(manifest.Apps) != 1 {
|
|
|
|
return nil, fmt.Errorf("manifest has multiple apps!")
|
|
|
|
}
|
|
|
|
app := manifest.Apps[0]
|
|
|
|
if app.App == nil {
|
|
|
|
return nil, fmt.Errorf("specified app has no App object")
|
|
|
|
}
|
|
|
|
|
|
|
|
portMap := make(map[string]int)
|
|
|
|
for svc, name := range configPortMap {
|
|
|
|
for _, port := range app.App.Ports {
|
|
|
|
if port.Name.String() == name {
|
|
|
|
portMap[svc] = int(port.Port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return portMap, nil
|
|
|
|
}
|
|
|
|
|
2017-11-16 22:26:35 +00:00
|
|
|
// rktRemove pod after it has exited.
|
|
|
|
func rktRemove(uuid string) error {
|
2017-11-16 23:45:14 +00:00
|
|
|
errBuf := &bytes.Buffer{}
|
2017-11-16 22:26:35 +00:00
|
|
|
cmd := exec.Command(rktCmd, "rm", uuid)
|
|
|
|
cmd.Stdout = ioutil.Discard
|
2017-11-16 23:45:14 +00:00
|
|
|
cmd.Stderr = errBuf
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
if msg := errBuf.String(); len(msg) > 0 {
|
|
|
|
return fmt.Errorf("error removing pod %q: %s", uuid, msg)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-11-16 22:26:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-27 21:21:45 +00:00
|
|
|
// NewRktDriver is used to create a new rkt driver
|
2015-09-29 21:48:10 +00:00
|
|
|
func NewRktDriver(ctx *DriverContext) Driver {
|
2015-11-05 21:46:02 +00:00
|
|
|
return &RktDriver{DriverContext: *ctx}
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
func (d *RktDriver) FSIsolation() cstructs.FSIsolation {
|
|
|
|
return cstructs.FSIsolationImage
|
|
|
|
}
|
|
|
|
|
2016-04-09 23:15:09 +00:00
|
|
|
// Validate is used to validate the driver configuration
|
2016-04-08 20:19:43 +00:00
|
|
|
func (d *RktDriver) Validate(config map[string]interface{}) error {
|
2016-04-09 22:38:42 +00:00
|
|
|
fd := &fields.FieldData{
|
|
|
|
Raw: config,
|
|
|
|
Schema: map[string]*fields.FieldSchema{
|
2017-09-26 22:26:33 +00:00
|
|
|
"image": {
|
2016-04-09 22:38:42 +00:00
|
|
|
Type: fields.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"command": {
|
2016-08-01 13:58:35 +00:00
|
|
|
Type: fields.TypeString,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"args": {
|
2016-04-09 22:38:42 +00:00
|
|
|
Type: fields.TypeArray,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"trust_prefix": {
|
2016-08-01 13:58:35 +00:00
|
|
|
Type: fields.TypeString,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"dns_servers": {
|
2016-04-09 22:38:42 +00:00
|
|
|
Type: fields.TypeArray,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"dns_search_domains": {
|
2016-04-09 22:38:42 +00:00
|
|
|
Type: fields.TypeArray,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"net": {
|
2016-08-16 01:49:06 +00:00
|
|
|
Type: fields.TypeArray,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"port_map": {
|
2016-08-16 01:49:06 +00:00
|
|
|
Type: fields.TypeArray,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"debug": {
|
2016-08-09 00:17:12 +00:00
|
|
|
Type: fields.TypeBool,
|
2016-08-08 10:30:47 +00:00
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"volumes": {
|
2016-11-23 22:37:09 +00:00
|
|
|
Type: fields.TypeArray,
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"no_overlay": {
|
2017-06-06 22:17:33 +00:00
|
|
|
Type: fields.TypeBool,
|
2017-06-09 17:40:47 +00:00
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
"insecure_options": {
|
2017-06-07 07:58:42 +00:00
|
|
|
Type: fields.TypeArray,
|
2017-06-06 22:17:33 +00:00
|
|
|
},
|
2016-04-09 22:38:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fd.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-08 20:19:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-19 22:06:23 +00:00
|
|
|
func (d *RktDriver) Abilities() DriverAbilities {
|
|
|
|
return DriverAbilities{
|
|
|
|
SendSignals: false,
|
2017-04-13 16:52:16 +00:00
|
|
|
Exec: true,
|
2016-10-19 22:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 14:09:53 +00:00
|
|
|
func (d *RktDriver) Fingerprint(req *cstructs.FingerprintRequest, resp *cstructs.FingerprintResponse) error {
|
2015-10-07 22:24:16 +00:00
|
|
|
// Only enable if we are root when running on non-windows systems.
|
|
|
|
if runtime.GOOS != "windows" && syscall.Geteuid() != 0 {
|
2017-02-21 03:35:51 +00:00
|
|
|
if d.fingerprintSuccess == nil || *d.fingerprintSuccess {
|
2016-04-01 09:22:17 +00:00
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: must run as root user, disabling")
|
|
|
|
}
|
2017-02-21 03:35:51 +00:00
|
|
|
d.fingerprintSuccess = helper.BoolToPtr(false)
|
2018-01-26 19:31:37 +00:00
|
|
|
resp.RemoveAttribute(rktDriverAttr)
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 00:36:19 +00:00
|
|
|
outBytes, err := exec.Command(rktCmd, "version").Output()
|
2015-10-07 22:24:16 +00:00
|
|
|
if err != nil {
|
2017-02-21 03:35:51 +00:00
|
|
|
d.fingerprintSuccess = helper.BoolToPtr(false)
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
out := strings.TrimSpace(string(outBytes))
|
|
|
|
|
|
|
|
rktMatches := reRktVersion.FindStringSubmatch(out)
|
2015-10-21 01:08:46 +00:00
|
|
|
appcMatches := reAppcVersion.FindStringSubmatch(out)
|
2015-10-07 22:24:16 +00:00
|
|
|
if len(rktMatches) != 2 || len(appcMatches) != 2 {
|
2017-02-21 03:35:51 +00:00
|
|
|
d.fingerprintSuccess = helper.BoolToPtr(false)
|
2018-01-26 19:31:37 +00:00
|
|
|
resp.RemoveAttribute(rktDriverAttr)
|
2018-01-24 14:09:53 +00:00
|
|
|
return fmt.Errorf("Unable to parse Rkt version string: %#v", rktMatches)
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2015-12-22 05:15:37 +00:00
|
|
|
minVersion, _ := version.NewVersion(minRktVersion)
|
2017-10-16 20:58:58 +00:00
|
|
|
currentVersion, _ := version.NewVersion(rktMatches[1])
|
|
|
|
if currentVersion.LessThan(minVersion) {
|
2016-10-19 00:36:19 +00:00
|
|
|
// Do not allow ancient rkt versions
|
2017-10-16 20:58:58 +00:00
|
|
|
if d.fingerprintSuccess == nil {
|
|
|
|
// Only log on first failure
|
|
|
|
d.logger.Printf("[WARN] driver.rkt: unsupported rkt version %s; please upgrade to >= %s",
|
|
|
|
currentVersion, minVersion)
|
|
|
|
}
|
|
|
|
d.fingerprintSuccess = helper.BoolToPtr(false)
|
2018-01-26 19:31:37 +00:00
|
|
|
resp.RemoveAttribute(rktDriverAttr)
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2015-12-21 17:48:21 +00:00
|
|
|
}
|
2016-10-24 23:00:19 +00:00
|
|
|
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute(rktDriverAttr, "1")
|
|
|
|
resp.AddAttribute("driver.rkt.version", rktMatches[1])
|
|
|
|
resp.AddAttribute("driver.rkt.appc.version", appcMatches[1])
|
2018-01-30 17:57:37 +00:00
|
|
|
resp.Applicable = true
|
2017-10-16 20:58:58 +00:00
|
|
|
|
2016-10-24 23:00:19 +00:00
|
|
|
// Advertise if this node supports rkt volumes
|
|
|
|
if d.config.ReadBoolDefault(rktVolumesConfigOption, rktVolumesConfigDefault) {
|
2018-01-26 16:21:07 +00:00
|
|
|
resp.AddAttribute("driver."+rktVolumesConfigOption, "1")
|
2016-10-24 23:00:19 +00:00
|
|
|
}
|
2017-02-21 03:35:51 +00:00
|
|
|
d.fingerprintSuccess = helper.BoolToPtr(true)
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 23:00:19 +00:00
|
|
|
func (d *RktDriver) Periodic() (bool, time.Duration) {
|
|
|
|
return true, 15 * time.Second
|
|
|
|
}
|
|
|
|
|
2017-05-19 18:08:49 +00:00
|
|
|
func (d *RktDriver) Prestart(ctx *ExecContext, task *structs.Task) (*PrestartResponse, error) {
|
2017-01-10 21:24:45 +00:00
|
|
|
return nil, nil
|
2016-11-30 00:39:36 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 21:48:10 +00:00
|
|
|
// Run an existing Rkt image.
|
2017-06-09 17:29:41 +00:00
|
|
|
func (d *RktDriver) Start(ctx *ExecContext, task *structs.Task) (*StartResponse, error) {
|
2015-11-14 04:22:49 +00:00
|
|
|
var driverConfig RktDriverConfig
|
2015-11-14 02:09:42 +00:00
|
|
|
if err := mapstructure.WeakDecode(task.Config, &driverConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-16 14:30:08 +00:00
|
|
|
|
2016-08-16 01:49:06 +00:00
|
|
|
driverConfig.PortMap = mapMergeStrStr(driverConfig.PortMapRaw...)
|
|
|
|
|
2016-08-16 14:30:08 +00:00
|
|
|
// ACI image
|
2015-11-14 02:09:42 +00:00
|
|
|
img := driverConfig.ImageName
|
2015-10-09 17:52:44 +00:00
|
|
|
|
2017-09-20 13:02:42 +00:00
|
|
|
// Global arguments given to both prepare and run-prepared
|
|
|
|
globalArgs := make([]string, 0, 50)
|
2017-05-23 00:46:40 +00:00
|
|
|
|
2016-08-08 10:30:47 +00:00
|
|
|
// Add debug option to rkt command.
|
2016-08-09 00:17:12 +00:00
|
|
|
debug := driverConfig.Debug
|
2016-08-08 10:30:47 +00:00
|
|
|
|
2015-10-07 22:24:16 +00:00
|
|
|
// Add the given trust prefix
|
2016-08-05 17:55:20 +00:00
|
|
|
trustPrefix := driverConfig.TrustPrefix
|
2016-02-05 21:28:41 +00:00
|
|
|
insecure := false
|
2016-08-05 17:55:20 +00:00
|
|
|
if trustPrefix != "" {
|
2015-10-12 21:37:56 +00:00
|
|
|
var outBuf, errBuf bytes.Buffer
|
2016-10-19 00:36:19 +00:00
|
|
|
cmd := exec.Command(rktCmd, "trust", "--skip-fingerprint-review=true", fmt.Sprintf("--prefix=%s", trustPrefix), fmt.Sprintf("--debug=%t", debug))
|
2015-10-12 21:37:56 +00:00
|
|
|
cmd.Stdout = &outBuf
|
|
|
|
cmd.Stderr = &errBuf
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error running rkt trust: %s\n\nOutput: %s\n\nError: %s",
|
|
|
|
err, outBuf.String(), errBuf.String())
|
|
|
|
}
|
2015-12-21 10:10:37 +00:00
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: added trust prefix: %q", trustPrefix)
|
2015-12-22 18:23:29 +00:00
|
|
|
} else {
|
|
|
|
// Disble signature verification if the trust command was not run.
|
2016-02-05 21:28:41 +00:00
|
|
|
insecure = true
|
|
|
|
}
|
2017-06-07 07:58:42 +00:00
|
|
|
|
|
|
|
// if we have a selective insecure_options, prefer them
|
|
|
|
// insecure options are rkt's global argument, so we do this before the actual "run"
|
|
|
|
if len(driverConfig.InsecureOptions) > 0 {
|
2017-09-20 13:02:42 +00:00
|
|
|
globalArgs = append(globalArgs, fmt.Sprintf("--insecure-options=%s", strings.Join(driverConfig.InsecureOptions, ",")))
|
2017-06-07 07:58:42 +00:00
|
|
|
} else if insecure {
|
2017-09-20 13:02:42 +00:00
|
|
|
globalArgs = append(globalArgs, "--insecure-options=all")
|
2017-06-07 07:58:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 19:11:22 +00:00
|
|
|
// debug is rkt's global argument, so add it before the actual "run"
|
2017-09-20 13:02:42 +00:00
|
|
|
globalArgs = append(globalArgs, fmt.Sprintf("--debug=%t", debug))
|
2017-06-07 07:58:42 +00:00
|
|
|
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs := make([]string, 0, 50)
|
|
|
|
runArgs := make([]string, 0, 50)
|
2017-06-07 07:58:42 +00:00
|
|
|
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, globalArgs...)
|
|
|
|
prepareArgs = append(prepareArgs, "prepare")
|
|
|
|
runArgs = append(runArgs, globalArgs...)
|
|
|
|
runArgs = append(runArgs, "run-prepared")
|
2016-10-13 23:18:18 +00:00
|
|
|
|
2017-06-06 22:17:33 +00:00
|
|
|
// disable overlayfs
|
|
|
|
if driverConfig.NoOverlay {
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, "--no-overlay=true")
|
2017-06-06 22:17:33 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 22:34:08 +00:00
|
|
|
// Convert underscores to dashes in task names for use in volume names #2358
|
|
|
|
sanitizedName := strings.Replace(task.Name, "_", "-", -1)
|
|
|
|
|
2016-10-13 23:18:18 +00:00
|
|
|
// Mount /alloc
|
2017-03-26 00:05:53 +00:00
|
|
|
allocVolName := fmt.Sprintf("%s-%s-alloc", d.DriverContext.allocID, sanitizedName)
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--volume=%s,kind=host,source=%s", allocVolName, ctx.TaskDir.SharedAllocDir))
|
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--mount=volume=%s,target=%s", allocVolName, allocdir.SharedAllocContainerPath))
|
2016-10-13 23:18:18 +00:00
|
|
|
|
|
|
|
// Mount /local
|
2017-03-26 00:05:53 +00:00
|
|
|
localVolName := fmt.Sprintf("%s-%s-local", d.DriverContext.allocID, sanitizedName)
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--volume=%s,kind=host,source=%s", localVolName, ctx.TaskDir.LocalDir))
|
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--mount=volume=%s,target=%s", localVolName, allocdir.TaskLocalContainerPath))
|
2016-10-13 23:18:18 +00:00
|
|
|
|
|
|
|
// Mount /secrets
|
2017-03-26 00:05:53 +00:00
|
|
|
secretsVolName := fmt.Sprintf("%s-%s-secrets", d.DriverContext.allocID, sanitizedName)
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--volume=%s,kind=host,source=%s", secretsVolName, ctx.TaskDir.SecretsDir))
|
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--mount=volume=%s,target=%s", secretsVolName, allocdir.TaskSecretsContainerPath))
|
2016-10-13 23:34:47 +00:00
|
|
|
|
|
|
|
// Mount arbitrary volumes if enabled
|
|
|
|
if len(driverConfig.Volumes) > 0 {
|
2016-10-20 00:13:45 +00:00
|
|
|
if enabled := d.config.ReadBoolDefault(rktVolumesConfigOption, rktVolumesConfigDefault); !enabled {
|
2016-10-13 23:34:47 +00:00
|
|
|
return nil, fmt.Errorf("%s is false; cannot use rkt volumes: %+q", rktVolumesConfigOption, driverConfig.Volumes)
|
|
|
|
}
|
|
|
|
for i, rawvol := range driverConfig.Volumes {
|
|
|
|
parts := strings.Split(rawvol, ":")
|
2017-07-21 11:05:15 +00:00
|
|
|
readOnly := "false"
|
|
|
|
// job spec:
|
|
|
|
// volumes = ["/host/path:/container/path[:readOnly]"]
|
|
|
|
// the third parameter is optional, mount is read-write by default
|
|
|
|
if len(parts) == 3 {
|
|
|
|
if parts[2] == "readOnly" {
|
|
|
|
d.logger.Printf("[DEBUG] Mounting %s:%s as readOnly", parts[0], parts[1])
|
|
|
|
readOnly = "true"
|
|
|
|
} else {
|
|
|
|
d.logger.Printf("[WARN] Unknown volume parameter '%s' ignored for mount %s", parts[2], parts[0])
|
|
|
|
}
|
|
|
|
} else if len(parts) != 2 {
|
2016-10-13 23:34:47 +00:00
|
|
|
return nil, fmt.Errorf("invalid rkt volume: %q", rawvol)
|
|
|
|
}
|
2017-03-26 00:05:53 +00:00
|
|
|
volName := fmt.Sprintf("%s-%s-%d", d.DriverContext.allocID, sanitizedName, i)
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--volume=%s,kind=host,source=%s,readOnly=%s", volName, parts[0], readOnly))
|
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--mount=volume=%s,target=%s", volName, parts[1]))
|
2016-10-13 23:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-13 23:18:18 +00:00
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// Inject environment variables
|
2017-05-23 00:46:40 +00:00
|
|
|
for k, v := range ctx.TaskEnv.Map() {
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--set-env=%s=%s", k, v))
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 13:02:42 +00:00
|
|
|
// Image is set here, because the commands that follow apply to it
|
|
|
|
prepareArgs = append(prepareArgs, img)
|
|
|
|
|
2016-05-15 16:41:34 +00:00
|
|
|
// Check if the user has overridden the exec command.
|
2016-08-05 17:55:20 +00:00
|
|
|
if driverConfig.Command != "" {
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--exec=%v", driverConfig.Command))
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2015-10-09 17:52:44 +00:00
|
|
|
|
2015-12-21 06:06:45 +00:00
|
|
|
// Add memory isolator
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--memory=%vM", int64(task.Resources.MemoryMB)))
|
2015-12-21 06:06:45 +00:00
|
|
|
|
|
|
|
// Add CPU isolator
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--cpu=%vm", int64(task.Resources.CPU)))
|
2015-12-21 06:06:45 +00:00
|
|
|
|
2016-03-08 05:28:24 +00:00
|
|
|
// Add DNS servers
|
2016-11-23 22:38:47 +00:00
|
|
|
if len(driverConfig.DNSServers) == 1 && (driverConfig.DNSServers[0] == "host" || driverConfig.DNSServers[0] == "none") {
|
|
|
|
// Special case single item lists with the special values "host" or "none"
|
2017-09-20 13:02:42 +00:00
|
|
|
runArgs = append(runArgs, fmt.Sprintf("--dns=%s", driverConfig.DNSServers[0]))
|
2016-11-23 22:38:47 +00:00
|
|
|
} else {
|
|
|
|
for _, ip := range driverConfig.DNSServers {
|
|
|
|
if err := net.ParseIP(ip); err == nil {
|
|
|
|
msg := fmt.Errorf("invalid ip address for container dns server %q", ip)
|
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: %v", msg)
|
|
|
|
return nil, msg
|
|
|
|
} else {
|
2017-09-20 13:02:42 +00:00
|
|
|
runArgs = append(runArgs, fmt.Sprintf("--dns=%s", ip))
|
2016-11-23 22:38:47 +00:00
|
|
|
}
|
2016-03-08 05:28:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set DNS search domains
|
|
|
|
for _, domain := range driverConfig.DNSSearchDomains {
|
2017-09-20 13:02:42 +00:00
|
|
|
runArgs = append(runArgs, fmt.Sprintf("--dns-search=%s", domain))
|
2016-03-08 05:28:24 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 01:49:06 +00:00
|
|
|
// set network
|
|
|
|
network := strings.Join(driverConfig.Net, ",")
|
|
|
|
if network != "" {
|
2017-09-20 13:02:42 +00:00
|
|
|
runArgs = append(runArgs, fmt.Sprintf("--net=%s", network))
|
2016-08-16 01:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup port mapping and exposed ports
|
|
|
|
if len(task.Resources.Networks) == 0 {
|
|
|
|
d.logger.Println("[DEBUG] driver.rkt: No network interfaces are available")
|
|
|
|
if len(driverConfig.PortMap) > 0 {
|
|
|
|
return nil, fmt.Errorf("Trying to map ports but no network interface is available")
|
|
|
|
}
|
2017-12-02 01:20:43 +00:00
|
|
|
} else if network == "host" {
|
|
|
|
// Port mapping is skipped when host networking is used.
|
|
|
|
d.logger.Println("[DEBUG] driver.rkt: Ignoring port_map when using --net=host")
|
2016-08-16 01:49:06 +00:00
|
|
|
} else {
|
|
|
|
// TODO add support for more than one network
|
|
|
|
network := task.Resources.Networks[0]
|
|
|
|
for _, port := range network.ReservedPorts {
|
|
|
|
var containerPort string
|
|
|
|
|
2016-10-25 23:27:35 +00:00
|
|
|
mapped, ok := driverConfig.PortMap[port.Label]
|
|
|
|
if !ok {
|
|
|
|
// If the user doesn't have a mapped port using port_map, driver stops running container.
|
2016-08-16 01:49:06 +00:00
|
|
|
return nil, fmt.Errorf("port_map is not set. When you defined port in the resources, you need to configure port_map.")
|
|
|
|
}
|
2016-10-25 23:27:35 +00:00
|
|
|
containerPort = mapped
|
2016-08-16 01:49:06 +00:00
|
|
|
|
|
|
|
hostPortStr := strconv.Itoa(port.Value)
|
|
|
|
|
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: exposed port %s", containerPort)
|
|
|
|
// Add port option to rkt run arguments. rkt allows multiple port args
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--port=%s:%s", containerPort, hostPortStr))
|
2016-08-16 01:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range network.DynamicPorts {
|
|
|
|
// By default we will map the allocated port 1:1 to the container
|
|
|
|
var containerPort string
|
|
|
|
|
|
|
|
if mapped, ok := driverConfig.PortMap[port.Label]; ok {
|
|
|
|
containerPort = mapped
|
|
|
|
} else {
|
|
|
|
// If the user doesn't have mapped a port using port_map, driver stops running container.
|
|
|
|
return nil, fmt.Errorf("port_map is not set. When you defined port in the resources, you need to configure port_map.")
|
|
|
|
}
|
|
|
|
|
|
|
|
hostPortStr := strconv.Itoa(port.Value)
|
|
|
|
|
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: exposed port %s", containerPort)
|
|
|
|
// Add port option to rkt run arguments. rkt allows multiple port args
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--port=%s:%s", containerPort, hostPortStr))
|
2016-08-16 01:49:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-04 19:12:30 +00:00
|
|
|
// If a user has been specified for the task, pass it through to the user
|
|
|
|
if task.User != "" {
|
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("--user=%s", task.User))
|
|
|
|
}
|
|
|
|
|
2015-10-09 17:52:44 +00:00
|
|
|
// Add user passed arguments.
|
2015-11-18 23:16:42 +00:00
|
|
|
if len(driverConfig.Args) != 0 {
|
2017-05-23 00:46:40 +00:00
|
|
|
parsed := ctx.TaskEnv.ParseAndReplace(driverConfig.Args)
|
2015-10-09 17:52:44 +00:00
|
|
|
|
|
|
|
// Need to start arguments with "--"
|
|
|
|
if len(parsed) > 0 {
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, "--")
|
2015-10-09 17:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, arg := range parsed {
|
2017-09-20 13:02:42 +00:00
|
|
|
prepareArgs = append(prepareArgs, fmt.Sprintf("%v", arg))
|
2015-10-09 17:52:44 +00:00
|
|
|
}
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2015-10-09 17:52:44 +00:00
|
|
|
|
2016-12-03 01:04:07 +00:00
|
|
|
pluginLogFile := filepath.Join(ctx.TaskDir.Dir, fmt.Sprintf("%s-executor.out", task.Name))
|
2017-01-12 19:50:49 +00:00
|
|
|
executorConfig := &dstructs.ExecutorConfig{
|
|
|
|
LogFile: pluginLogFile,
|
|
|
|
LogLevel: d.config.LogLevel,
|
2015-10-09 19:14:56 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 19:50:49 +00:00
|
|
|
execIntf, pluginClient, err := createExecutor(d.config.LogOutput, d.config, executorConfig)
|
2016-02-17 00:10:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-26 22:24:14 +00:00
|
|
|
|
2017-09-20 13:02:42 +00:00
|
|
|
absPath, err := GetAbsolutePath(rktCmd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var outBuf, errBuf bytes.Buffer
|
|
|
|
cmd := exec.Command(rktCmd, prepareArgs...)
|
|
|
|
cmd.Stdout = &outBuf
|
|
|
|
cmd.Stderr = &errBuf
|
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: preparing pod %q for task %q with: %v", img, d.taskName, prepareArgs)
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Error preparing rkt pod: %s\n\nOutput: %s\n\nError: %s",
|
|
|
|
err, outBuf.String(), errBuf.String())
|
|
|
|
}
|
|
|
|
uuid := strings.TrimSpace(outBuf.String())
|
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: pod %q for task %q prepared, UUID is: %s", img, d.taskName, uuid)
|
|
|
|
runArgs = append(runArgs, uuid)
|
|
|
|
|
2017-05-26 22:24:14 +00:00
|
|
|
// The task's environment is set via --set-env flags above, but the rkt
|
|
|
|
// command itself needs an evironment with PATH set to find iptables.
|
|
|
|
eb := env.NewEmptyBuilder()
|
|
|
|
filter := strings.Split(d.config.ReadDefault("env.blacklist", config.DefaultEnvBlacklist), ",")
|
2017-05-26 22:41:26 +00:00
|
|
|
rktEnv := eb.SetHostEnvvars(filter).Build()
|
2016-02-17 00:10:45 +00:00
|
|
|
executorCtx := &executor.ExecutorContext{
|
2017-05-26 22:41:26 +00:00
|
|
|
TaskEnv: rktEnv,
|
2016-12-03 01:04:07 +00:00
|
|
|
Driver: "rkt",
|
|
|
|
Task: task,
|
|
|
|
TaskDir: ctx.TaskDir.Dir,
|
|
|
|
LogDir: ctx.TaskDir.LogDir,
|
2016-02-17 00:10:45 +00:00
|
|
|
}
|
2016-10-12 18:35:29 +00:00
|
|
|
if err := execIntf.SetContext(executorCtx); err != nil {
|
|
|
|
pluginClient.Kill()
|
|
|
|
return nil, fmt.Errorf("failed to set executor context: %v", err)
|
|
|
|
}
|
2015-10-09 19:14:56 +00:00
|
|
|
|
2016-10-12 18:35:29 +00:00
|
|
|
execCmd := &executor.ExecCommand{
|
2017-05-26 22:24:14 +00:00
|
|
|
Cmd: absPath,
|
2017-09-20 13:02:42 +00:00
|
|
|
Args: runArgs,
|
2016-10-12 18:35:29 +00:00
|
|
|
}
|
|
|
|
ps, err := execIntf.LaunchCmd(execCmd)
|
2016-02-17 00:10:45 +00:00
|
|
|
if err != nil {
|
|
|
|
pluginClient.Kill()
|
2016-03-19 19:18:10 +00:00
|
|
|
return nil, err
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2015-10-09 17:52:44 +00:00
|
|
|
|
2017-09-20 13:02:42 +00:00
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: started ACI %q (UUID: %s) for task %q with: %v", img, uuid, d.taskName, runArgs)
|
2016-03-03 17:21:21 +00:00
|
|
|
maxKill := d.DriverContext.config.MaxKillTimeout
|
2015-10-07 22:24:16 +00:00
|
|
|
h := &rktHandle{
|
2017-02-01 00:43:57 +00:00
|
|
|
uuid: uuid,
|
2017-05-26 22:41:26 +00:00
|
|
|
env: rktEnv,
|
2017-04-21 23:50:20 +00:00
|
|
|
taskDir: ctx.TaskDir,
|
2016-03-03 17:21:21 +00:00
|
|
|
pluginClient: pluginClient,
|
2016-03-16 03:21:52 +00:00
|
|
|
executor: execIntf,
|
2016-03-03 17:21:21 +00:00
|
|
|
executorPid: ps.Pid,
|
|
|
|
logger: d.logger,
|
|
|
|
killTimeout: GetKillTimeout(task.KillTimeout, maxKill),
|
|
|
|
maxKillTimeout: maxKill,
|
|
|
|
doneCh: make(chan struct{}),
|
2016-06-12 03:15:50 +00:00
|
|
|
waitCh: make(chan *dstructs.WaitResult, 1),
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
go h.run()
|
2017-09-20 15:09:08 +00:00
|
|
|
|
2017-12-02 01:20:43 +00:00
|
|
|
// Only return a driver network if *not* using host networking
|
|
|
|
var driverNetwork *cstructs.DriverNetwork
|
|
|
|
if network != "host" {
|
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: retrieving network information for pod %q (UUID %s) for task %q", img, uuid, d.taskName)
|
|
|
|
driverNetwork, err = rktGetDriverNetwork(uuid, driverConfig.PortMap)
|
|
|
|
if err != nil && !pluginClient.Exited() {
|
|
|
|
d.logger.Printf("[WARN] driver.rkt: network status retrieval for pod %q (UUID %s) for task %q failed. Last error: %v", img, uuid, d.taskName, err)
|
|
|
|
|
|
|
|
// If a portmap was given, this turns into a fatal error
|
|
|
|
if len(driverConfig.PortMap) != 0 {
|
|
|
|
pluginClient.Kill()
|
|
|
|
return nil, fmt.Errorf("Trying to map ports but driver could not determine network information")
|
|
|
|
}
|
2017-09-26 08:20:57 +00:00
|
|
|
}
|
2017-09-20 15:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &StartResponse{Handle: h, Network: driverNetwork}, nil
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 00:46:08 +00:00
|
|
|
func (d *RktDriver) Cleanup(*ExecContext, *CreatedResources) error { return nil }
|
2017-01-10 21:24:45 +00:00
|
|
|
|
2015-09-29 21:48:10 +00:00
|
|
|
func (d *RktDriver) Open(ctx *ExecContext, handleID string) (DriverHandle, error) {
|
2015-10-07 22:24:16 +00:00
|
|
|
// Parse the handle
|
|
|
|
pidBytes := []byte(strings.TrimPrefix(handleID, "Rkt:"))
|
2016-03-03 17:21:21 +00:00
|
|
|
id := &rktPID{}
|
|
|
|
if err := json.Unmarshal(pidBytes, id); err != nil {
|
2015-10-07 22:24:16 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse Rkt handle '%s': %v", handleID, err)
|
|
|
|
}
|
|
|
|
|
2016-02-17 00:10:45 +00:00
|
|
|
pluginConfig := &plugin.ClientConfig{
|
2016-03-03 17:21:21 +00:00
|
|
|
Reattach: id.PluginConfig.PluginConfig(),
|
2016-02-17 00:10:45 +00:00
|
|
|
}
|
2017-01-12 19:50:49 +00:00
|
|
|
exec, pluginClient, err := createExecutorWithConfig(pluginConfig, d.config.LogOutput)
|
2016-02-17 00:10:45 +00:00
|
|
|
if err != nil {
|
2017-11-16 22:26:35 +00:00
|
|
|
d.logger.Println("[ERR] driver.rkt: error connecting to plugin so destroying plugin pid and user pid")
|
2016-03-03 17:21:21 +00:00
|
|
|
if e := destroyPlugin(id.PluginConfig.Pid, id.ExecutorPid); e != nil {
|
2017-11-16 22:26:35 +00:00
|
|
|
d.logger.Printf("[ERR] driver.rkt: error destroying plugin and executor pid: %v", e)
|
2016-02-17 00:10:45 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("error connecting to plugin: %v", err)
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 22:41:26 +00:00
|
|
|
// The task's environment is set via --set-env flags in Start, but the rkt
|
|
|
|
// command itself needs an evironment with PATH set to find iptables.
|
|
|
|
eb := env.NewEmptyBuilder()
|
|
|
|
filter := strings.Split(d.config.ReadDefault("env.blacklist", config.DefaultEnvBlacklist), ",")
|
|
|
|
rktEnv := eb.SetHostEnvvars(filter).Build()
|
|
|
|
|
2016-03-30 05:05:02 +00:00
|
|
|
ver, _ := exec.Version()
|
|
|
|
d.logger.Printf("[DEBUG] driver.rkt: version of executor: %v", ver.Version)
|
2015-10-07 22:24:16 +00:00
|
|
|
// Return a driver handle
|
|
|
|
h := &rktHandle{
|
2017-02-01 00:43:57 +00:00
|
|
|
uuid: id.UUID,
|
2017-05-26 22:41:26 +00:00
|
|
|
env: rktEnv,
|
2017-04-21 23:50:20 +00:00
|
|
|
taskDir: ctx.TaskDir,
|
2016-03-03 17:21:21 +00:00
|
|
|
pluginClient: pluginClient,
|
|
|
|
executorPid: id.ExecutorPid,
|
2016-03-29 23:27:31 +00:00
|
|
|
executor: exec,
|
2016-03-03 17:21:21 +00:00
|
|
|
logger: d.logger,
|
|
|
|
killTimeout: id.KillTimeout,
|
|
|
|
maxKillTimeout: id.MaxKillTimeout,
|
|
|
|
doneCh: make(chan struct{}),
|
2016-06-12 03:15:50 +00:00
|
|
|
waitCh: make(chan *dstructs.WaitResult, 1),
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
go h.run()
|
|
|
|
return h, nil
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *rktHandle) ID() string {
|
2015-10-07 22:24:16 +00:00
|
|
|
// Return a handle to the PID
|
|
|
|
pid := &rktPID{
|
2017-02-01 00:43:57 +00:00
|
|
|
UUID: h.uuid,
|
2016-03-03 17:21:21 +00:00
|
|
|
PluginConfig: NewPluginReattachConfig(h.pluginClient.ReattachConfig()),
|
|
|
|
KillTimeout: h.killTimeout,
|
|
|
|
MaxKillTimeout: h.maxKillTimeout,
|
|
|
|
ExecutorPid: h.executorPid,
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
|
|
|
data, err := json.Marshal(pid)
|
|
|
|
if err != nil {
|
|
|
|
h.logger.Printf("[ERR] driver.rkt: failed to marshal rkt PID to JSON: %s", err)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Rkt:%s", string(data))
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-12 03:15:50 +00:00
|
|
|
func (h *rktHandle) WaitCh() chan *dstructs.WaitResult {
|
2015-10-07 22:24:16 +00:00
|
|
|
return h.waitCh
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *rktHandle) Update(task *structs.Task) error {
|
2016-02-04 03:43:44 +00:00
|
|
|
// Store the updated kill timeout.
|
2016-03-03 17:21:21 +00:00
|
|
|
h.killTimeout = GetKillTimeout(task.KillTimeout, h.maxKillTimeout)
|
2016-03-17 09:53:31 +00:00
|
|
|
h.executor.UpdateTask(task)
|
2016-02-04 03:43:44 +00:00
|
|
|
|
2015-10-07 22:24:16 +00:00
|
|
|
// Update is not possible
|
|
|
|
return nil
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 00:43:57 +00:00
|
|
|
func (h *rktHandle) Exec(ctx context.Context, cmd string, args []string) ([]byte, int, error) {
|
|
|
|
if h.uuid == "" {
|
|
|
|
return nil, 0, fmt.Errorf("unable to find rkt pod UUID")
|
|
|
|
}
|
|
|
|
// enter + UUID + cmd + args...
|
|
|
|
enterArgs := make([]string, 3+len(args))
|
|
|
|
enterArgs[0] = "enter"
|
|
|
|
enterArgs[1] = h.uuid
|
|
|
|
enterArgs[2] = cmd
|
|
|
|
copy(enterArgs[3:], args)
|
2017-05-04 23:21:40 +00:00
|
|
|
return executor.ExecScript(ctx, h.taskDir.Dir, h.env, nil, rktCmd, enterArgs)
|
2017-02-01 00:43:57 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 19:37:52 +00:00
|
|
|
func (h *rktHandle) Signal(s os.Signal) error {
|
2016-10-07 23:49:00 +00:00
|
|
|
return fmt.Errorf("Rkt does not support signals")
|
2016-10-07 19:37:52 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 21:48:10 +00:00
|
|
|
// Kill is used to terminate the task. We send an Interrupt
|
|
|
|
// and then provide a 5 second grace period before doing a Kill.
|
|
|
|
func (h *rktHandle) Kill() error {
|
2016-02-17 00:10:45 +00:00
|
|
|
h.executor.ShutDown()
|
2015-10-07 22:24:16 +00:00
|
|
|
select {
|
|
|
|
case <-h.doneCh:
|
|
|
|
return nil
|
2015-12-23 00:10:30 +00:00
|
|
|
case <-time.After(h.killTimeout):
|
2016-02-17 00:10:45 +00:00
|
|
|
return h.executor.Exit()
|
2015-10-07 22:24:16 +00:00
|
|
|
}
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 23:06:01 +00:00
|
|
|
func (h *rktHandle) Stats() (*cstructs.TaskResourceUsage, error) {
|
2017-01-09 21:47:06 +00:00
|
|
|
return nil, DriverStatsNotImplemented
|
2016-04-28 23:06:01 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 21:48:10 +00:00
|
|
|
func (h *rktHandle) run() {
|
2016-11-04 21:58:55 +00:00
|
|
|
ps, werr := h.executor.Wait()
|
2015-10-07 22:24:16 +00:00
|
|
|
close(h.doneCh)
|
2016-11-04 21:58:55 +00:00
|
|
|
if ps.ExitCode == 0 && werr != nil {
|
2016-02-19 02:57:13 +00:00
|
|
|
if e := killProcess(h.executorPid); e != nil {
|
2017-11-16 22:26:35 +00:00
|
|
|
h.logger.Printf("[ERR] driver.rkt: error killing user process: %v", e)
|
2016-02-19 02:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-23 20:19:45 +00:00
|
|
|
|
2016-11-04 21:58:55 +00:00
|
|
|
// Exit the executor
|
2016-03-18 19:04:11 +00:00
|
|
|
if err := h.executor.Exit(); err != nil {
|
|
|
|
h.logger.Printf("[ERR] driver.rkt: error killing executor: %v", err)
|
|
|
|
}
|
2016-02-17 00:10:45 +00:00
|
|
|
h.pluginClient.Kill()
|
2016-11-04 21:58:55 +00:00
|
|
|
|
2017-11-16 22:26:35 +00:00
|
|
|
// Remove the pod
|
|
|
|
if err := rktRemove(h.uuid); err != nil {
|
2017-11-16 23:45:14 +00:00
|
|
|
h.logger.Printf("[ERR] driver.rkt: error removing pod %q - must gc manually: %v", h.uuid, err)
|
2017-11-16 22:26:35 +00:00
|
|
|
} else {
|
|
|
|
h.logger.Printf("[DEBUG] driver.rkt: removed pod %q", h.uuid)
|
|
|
|
}
|
|
|
|
|
2016-11-04 21:58:55 +00:00
|
|
|
// Send the results
|
|
|
|
h.waitCh <- dstructs.NewWaitResult(ps.ExitCode, 0, werr)
|
|
|
|
close(h.waitCh)
|
2015-09-29 21:48:10 +00:00
|
|
|
}
|