Revert some wrong changes to make this closer to upstream exec driver

This commit is contained in:
Alex 2022-11-29 10:05:29 +01:00
parent b0c7108d0d
commit dc9af4e04f
Signed by: lx
GPG key ID: 0E496D15096376BE
2 changed files with 71 additions and 28 deletions

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"path/filepath"
"runtime" "runtime"
"sync" "sync"
"time" "time"
@ -119,6 +120,9 @@ type Driver struct {
// config is the driver configuration set by the SetConfig RPC // config is the driver configuration set by the SetConfig RPC
config Config config Config
// nomadConfig is the client config from nomad
nomadConfig *base.ClientDriverConfig
// tasks is the in memory datastore mapping taskIDs to driverHandles // tasks is the in memory datastore mapping taskIDs to driverHandles
tasks *taskStore tasks *taskStore
@ -244,6 +248,7 @@ func (tc *TaskConfig) validate() error {
// StartTask. This information is needed to rebuild the task state and handler // StartTask. This information is needed to rebuild the task state and handler
// during recovery. // during recovery.
type TaskState struct { type TaskState struct {
ReattachConfig *pstructs.ReattachConfig
TaskConfig *drivers.TaskConfig TaskConfig *drivers.TaskConfig
Pid int Pid int
StartedAt time.Time StartedAt time.Time
@ -306,6 +311,9 @@ func (d *Driver) SetConfig(cfg *base.Config) error {
d.logger.Info("Got config", "driver_config", hclog.Fmt("%+v", config)) d.logger.Info("Got config", "driver_config", hclog.Fmt("%+v", config))
d.config = config d.config = config
if cfg != nil && cfg.AgentConfig != nil {
d.nomadConfig = cfg.AgentConfig.Driver
}
return nil return nil
} }
@ -407,13 +415,24 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
return fmt.Errorf("failed to decode task state from handle: %v", err) return fmt.Errorf("failed to decode task state from handle: %v", err)
} }
// Create new executor // Create client for reattached executor
exec := executor.NewExecutorWithIsolation( plugRC, err := pstructs.ReattachConfigToGoPlugin(taskState.ReattachConfig)
if err != nil {
d.logger.Error("failed to build ReattachConfig from task state", "error", err, "task_id", handle.Config.ID)
return fmt.Errorf("failed to build ReattachConfig from task state: %v", err)
}
exec, pluginClient, err := executor.ReattachToExecutor(plugRC,
d.logger.With("task_name", handle.Config.Name, "alloc_id", handle.Config.AllocID)) d.logger.With("task_name", handle.Config.Name, "alloc_id", handle.Config.AllocID))
if err != nil {
d.logger.Error("failed to reattach to executor", "error", err, "task_id", handle.Config.ID)
return fmt.Errorf("failed to reattach to executor: %v", err)
}
h := &taskHandle{ h := &taskHandle{
exec: exec, exec: exec,
pid: taskState.Pid, pid: taskState.Pid,
pluginClient: pluginClient,
taskConfig: taskState.TaskConfig, taskConfig: taskState.TaskConfig,
procState: drivers.TaskStateRunning, procState: drivers.TaskStateRunning,
startedAt: taskState.StartedAt, startedAt: taskState.StartedAt,
@ -445,8 +464,19 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
handle := drivers.NewTaskHandle(taskHandleVersion) handle := drivers.NewTaskHandle(taskHandleVersion)
handle.Config = cfg handle.Config = cfg
exec := executor.NewExecutorWithIsolation( pluginLogFile := filepath.Join(cfg.TaskDir().Dir, "executor.out")
d.logger.With("task_name", handle.Config.Name, "alloc_id", handle.Config.AllocID)) executorConfig := &executor.ExecutorConfig{
LogFile: pluginLogFile,
LogLevel: "debug",
FSIsolation: true,
}
exec, pluginClient, err := executor.CreateExecutor(
d.logger.With("task_name", handle.Config.Name, "alloc_id", handle.Config.AllocID),
d.nomadConfig, executorConfig)
if err != nil {
return nil, nil, fmt.Errorf("failed to create executor: %v", err)
}
user := cfg.User user := cfg.User
if user == "" { if user == "" {
@ -544,12 +574,14 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
ps, err := exec.Launch(execCmd) ps, err := exec.Launch(execCmd)
if err != nil { if err != nil {
pluginClient.Kill()
return nil, nil, fmt.Errorf("failed to launch command with executor: %v", err) return nil, nil, fmt.Errorf("failed to launch command with executor: %v", err)
} }
h := &taskHandle{ h := &taskHandle{
exec: exec, exec: exec,
pid: ps.Pid, pid: ps.Pid,
pluginClient: pluginClient,
taskConfig: cfg, taskConfig: cfg,
procState: drivers.TaskStateRunning, procState: drivers.TaskStateRunning,
startedAt: time.Now().Round(time.Millisecond), startedAt: time.Now().Round(time.Millisecond),
@ -557,6 +589,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
} }
driverState := TaskState{ driverState := TaskState{
ReattachConfig: pstructs.ReattachConfigFromGoPlugin(pluginClient.ReattachConfig()),
Pid: ps.Pid, Pid: ps.Pid,
TaskConfig: cfg, TaskConfig: cfg,
StartedAt: h.startedAt, StartedAt: h.startedAt,
@ -565,6 +598,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
if err := handle.SetDriverState(&driverState); err != nil { if err := handle.SetDriverState(&driverState); err != nil {
d.logger.Error("failed to start task, error setting driver state", "error", err) d.logger.Error("failed to start task, error setting driver state", "error", err)
_ = exec.Shutdown("", 0) _ = exec.Shutdown("", 0)
pluginClient.Kill()
return nil, nil, fmt.Errorf("failed to set driver state: %v", err) return nil, nil, fmt.Errorf("failed to set driver state: %v", err)
} }
@ -616,6 +650,9 @@ func (d *Driver) StopTask(taskID string, timeout time.Duration, signal string) e
} }
if err := handle.exec.Shutdown(signal, timeout); err != nil { if err := handle.exec.Shutdown(signal, timeout); err != nil {
if handle.pluginClient.Exited() {
return nil
}
return fmt.Errorf("executor Shutdown failed: %v", err) return fmt.Errorf("executor Shutdown failed: %v", err)
} }
@ -651,10 +688,14 @@ func (d *Driver) DestroyTask(taskID string, force bool) error {
return fmt.Errorf("cannot destroy running task") return fmt.Errorf("cannot destroy running task")
} }
if !handle.pluginClient.Exited() {
if err := handle.exec.Shutdown("", 0); err != nil { if err := handle.exec.Shutdown("", 0); err != nil {
handle.logger.Error("destroying executor failed", "error", err) handle.logger.Error("destroying executor failed", "error", err)
} }
handle.pluginClient.Kill()
}
// workaround for the case where DestroyTask was issued on task restart // workaround for the case where DestroyTask was issued on task restart
d.resetCgroup(handle) d.resetCgroup(handle)

View file

@ -8,12 +8,14 @@ import (
"github.com/Alexis211/nomad-driver-exec2/executor" "github.com/Alexis211/nomad-driver-exec2/executor"
hclog "github.com/hashicorp/go-hclog" hclog "github.com/hashicorp/go-hclog"
plugin "github.com/hashicorp/go-plugin"
"github.com/hashicorp/nomad/plugins/drivers" "github.com/hashicorp/nomad/plugins/drivers"
) )
type taskHandle struct { type taskHandle struct {
exec executor.Executor exec executor.Executor
pid int pid int
pluginClient *plugin.Client
logger hclog.Logger logger hclog.Logger
// stateLock syncs access to all fields below // stateLock syncs access to all fields below