rm bind && go fmt

This commit is contained in:
Alex 2022-11-29 09:51:00 +01:00
parent 153b8f1b9d
commit d9912eb940
Signed by: lx
GPG key ID: 0E496D15096376BE
2 changed files with 57 additions and 83 deletions

View file

@ -8,12 +8,12 @@ import (
"sync" "sync"
"time" "time"
"github.com/Alexis211/nomad-driver-exec2/executor"
"github.com/hashicorp/consul-template/signals" "github.com/hashicorp/consul-template/signals"
hclog "github.com/hashicorp/go-hclog" hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/client/lib/cgutil" "github.com/hashicorp/nomad/client/lib/cgutil"
"github.com/hashicorp/nomad/drivers/shared/capabilities" "github.com/hashicorp/nomad/drivers/shared/capabilities"
"github.com/hashicorp/nomad/drivers/shared/eventer" "github.com/hashicorp/nomad/drivers/shared/eventer"
"github.com/Alexis211/nomad-driver-exec2/executor"
"github.com/hashicorp/nomad/drivers/shared/resolvconf" "github.com/hashicorp/nomad/drivers/shared/resolvconf"
"github.com/hashicorp/nomad/helper/pluginutils/hclutils" "github.com/hashicorp/nomad/helper/pluginutils/hclutils"
"github.com/hashicorp/nomad/helper/pluginutils/loader" "github.com/hashicorp/nomad/helper/pluginutils/loader"
@ -71,14 +71,9 @@ var (
hclspec.NewAttr("allow_caps", "list(string)", false), hclspec.NewAttr("allow_caps", "list(string)", false),
hclspec.NewLiteral(capabilities.HCLSpecLiteral), hclspec.NewLiteral(capabilities.HCLSpecLiteral),
), ),
// Default host directories to bind in tasks "allow_bind": hclspec.NewDefault(
"bind": hclspec.NewDefault( hclspec.NewAttr("allow_bind", "bool", false),
hclspec.NewAttr("bind", "list(map(string))", false), hclspec.NewLiteral("true"),
hclspec.NewLiteral("{}"),
),
"bind_read_only": hclspec.NewDefault(
hclspec.NewAttr("bind_read_only", "list(map(string))", false),
hclspec.NewLiteral("{}"),
), ),
}) })
@ -157,11 +152,8 @@ type Config struct {
// running on this node. // running on this node.
AllowCaps []string `codec:"allow_caps"` AllowCaps []string `codec:"allow_caps"`
// Paths to bind for read-write acess in all jobs // AllowBind defines whether users may bind host directories
Bind hclutils.MapStrStr `codec:"bind"` AllowBind bool `codec:"allow_bind"`
// Paths to bind for read-only acess in all jobs
BindReadOnly hclutils.MapStrStr `codec:"bind_read_only"`
} }
func (c *Config) validate() error { func (c *Config) validate() error {
@ -244,9 +236,9 @@ 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 {
TaskConfig *drivers.TaskConfig TaskConfig *drivers.TaskConfig
Pid int Pid int
StartedAt time.Time StartedAt time.Time
} }
// NewPlugin returns a new DrivePlugin implementation // NewPlugin returns a new DrivePlugin implementation
@ -409,16 +401,16 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
// Create new executor // Create new executor
exec := executor.NewExecutorWithIsolation( exec := executor.NewExecutorWithIsolation(
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))
h := &taskHandle{ h := &taskHandle{
exec: exec, exec: exec,
pid: taskState.Pid, pid: taskState.Pid,
taskConfig: taskState.TaskConfig, taskConfig: taskState.TaskConfig,
procState: drivers.TaskStateRunning, procState: drivers.TaskStateRunning,
startedAt: taskState.StartedAt, startedAt: taskState.StartedAt,
exitResult: &drivers.ExitResult{}, exitResult: &drivers.ExitResult{},
logger: d.logger, logger: d.logger,
} }
d.tasks.Set(taskState.TaskConfig.ID, h) d.tasks.Set(taskState.TaskConfig.ID, h)
@ -446,7 +438,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
handle.Config = cfg handle.Config = cfg
exec := executor.NewExecutorWithIsolation( exec := executor.NewExecutorWithIsolation(
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))
user := cfg.User user := cfg.User
if user == "" { if user == "" {
@ -462,54 +454,36 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
} }
// Bind mounts specified in driver config // Bind mounts specified in driver config
if d.config.Bind != nil {
for host, task := range d.config.Bind {
mount_config := drivers.MountConfig{
TaskPath: task,
HostPath: host,
Readonly: false,
PropagationMode: "private",
}
d.logger.Info("adding RW mount from driver config", "mount_config", hclog.Fmt("%+v", mount_config))
cfg.Mounts = append(cfg.Mounts, &mount_config)
}
}
if d.config.BindReadOnly != nil {
for host, task := range d.config.BindReadOnly {
mount_config := drivers.MountConfig{
TaskPath: task,
HostPath: host,
Readonly: true,
PropagationMode: "private",
}
d.logger.Info("adding RO mount from driver config", "mount_config", hclog.Fmt("%+v", mount_config))
cfg.Mounts = append(cfg.Mounts, &mount_config)
}
}
// Bind mounts specified in task config // Bind mounts specified in task config
if driverConfig.Bind != nil { if d.config.AllowBind {
for host, task := range driverConfig.Bind { if driverConfig.Bind != nil {
mount_config := drivers.MountConfig{ for host, task := range driverConfig.Bind {
TaskPath: task, mount_config := drivers.MountConfig{
HostPath: host, TaskPath: task,
Readonly: false, HostPath: host,
PropagationMode: "private", Readonly: false,
PropagationMode: "private",
}
d.logger.Info("adding RW mount from task spec", "mount_config", hclog.Fmt("%+v", mount_config))
cfg.Mounts = append(cfg.Mounts, &mount_config)
} }
d.logger.Info("adding RW mount from task spec", "mount_config", hclog.Fmt("%+v", mount_config))
cfg.Mounts = append(cfg.Mounts, &mount_config)
} }
} if driverConfig.BindReadOnly != nil {
if driverConfig.BindReadOnly != nil { for host, task := range driverConfig.BindReadOnly {
for host, task := range driverConfig.BindReadOnly { mount_config := drivers.MountConfig{
mount_config := drivers.MountConfig{ TaskPath: task,
TaskPath: task, HostPath: host,
HostPath: host, Readonly: true,
Readonly: true, PropagationMode: "private",
PropagationMode: "private", }
d.logger.Info("adding RO mount from task spec", "mount_config", hclog.Fmt("%+v", mount_config))
cfg.Mounts = append(cfg.Mounts, &mount_config)
} }
d.logger.Info("adding RO mount from task spec", "mount_config", hclog.Fmt("%+v", mount_config)) }
cfg.Mounts = append(cfg.Mounts, &mount_config) } else {
if len(driverConfig.Bind) > 0 || len(driverConfig.BindReadOnly) > 0 {
return nil, nil, fmt.Errorf("bind and bind_read_only are deactivated for the %s driver", pluginName)
} }
} }
@ -539,7 +513,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
ModeIPC: executor.IsolationMode(d.config.DefaultModeIPC, driverConfig.ModeIPC), ModeIPC: executor.IsolationMode(d.config.DefaultModeIPC, driverConfig.ModeIPC),
Capabilities: caps, Capabilities: caps,
} }
d.logger.Info("launching with", "exec_cmd", hclog.Fmt("%+v", execCmd)) d.logger.Info("launching with", "exec_cmd", hclog.Fmt("%+v", execCmd))
ps, err := exec.Launch(execCmd) ps, err := exec.Launch(execCmd)
@ -548,18 +522,18 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
} }
h := &taskHandle{ h := &taskHandle{
exec: exec, exec: exec,
pid: ps.Pid, pid: ps.Pid,
taskConfig: cfg, taskConfig: cfg,
procState: drivers.TaskStateRunning, procState: drivers.TaskStateRunning,
startedAt: time.Now().Round(time.Millisecond), startedAt: time.Now().Round(time.Millisecond),
logger: d.logger, logger: d.logger,
} }
driverState := TaskState{ driverState := TaskState{
Pid: ps.Pid, Pid: ps.Pid,
TaskConfig: cfg, TaskConfig: cfg,
StartedAt: h.startedAt, StartedAt: h.startedAt,
} }
if err := handle.SetDriverState(&driverState); err != nil { if err := handle.SetDriverState(&driverState); err != nil {

View file

@ -6,15 +6,15 @@ import (
"sync" "sync"
"time" "time"
hclog "github.com/hashicorp/go-hclog"
"github.com/Alexis211/nomad-driver-exec2/executor" "github.com/Alexis211/nomad-driver-exec2/executor"
hclog "github.com/hashicorp/go-hclog"
"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
logger hclog.Logger logger hclog.Logger
// stateLock syncs access to all fields below // stateLock syncs access to all fields below
stateLock sync.RWMutex stateLock sync.RWMutex