Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract LXC from nomad #5162

Merged
merged 9 commits into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ ifeq (,$(findstring $(THIS_OS),Darwin Linux FreeBSD))
$(error Building Nomad is currently only supported on Darwin and Linux.)
endif

# On Linux we build for Linux, Windows, and potentially Linux+LXC
# On Linux we build for Linux and Windows
ifeq (Linux,$(THIS_OS))

# Detect if we have LXC on the path
ifeq (0,$(shell pkg-config --exists lxc; echo $$?))
HAS_LXC="true"
endif

ifeq ($(TRAVIS),true)
$(info Running in Travis, verbose mode is disabled)
else
Expand All @@ -38,9 +33,6 @@ ALL_TARGETS += linux_386 \
windows_386 \
windows_amd64

ifeq ("true",$(HAS_LXC))
ALL_TARGETS += linux_amd64-lxc
endif
endif

# On MacOS, we only build for MacOS
Expand Down Expand Up @@ -122,14 +114,6 @@ pkg/windows_amd64/nomad: $(SOURCE_FILES) ## Build Nomad for windows/amd64
-tags "$(GO_TAGS)" \
-o "$@.exe"

pkg/linux_amd64-lxc/nomad: $(SOURCE_FILES) ## Build Nomad+LXC for linux/amd64
@echo "==> Building $@ with tags $(GO_TAGS)..."
@CGO_ENABLED=1 GOOS=linux GOARCH=amd64 \
go build \
-ldflags $(GO_LDFLAGS) \
-tags "$(GO_TAGS) lxc" \
-o "$@"

# Define package targets for each of the build targets we actually have on this system
define makePackageTarget

Expand Down Expand Up @@ -222,7 +206,7 @@ changelogfmt:
dev: GOOS=$(shell go env GOOS)
dev: GOARCH=$(shell go env GOARCH)
dev: GOPATH=$(shell go env GOPATH)
dev: DEV_TARGET=pkg/$(GOOS)_$(GOARCH)$(if $(HAS_LXC),-lxc)/nomad
dev: DEV_TARGET=pkg/$(GOOS)_$(GOARCH)/nomad
dev: vendorfmt changelogfmt ## Build for the current development platform
@echo "==> Removing old development build..."
@rm -f $(PROJECT_ROOT)/$(DEV_TARGET)
Expand Down Expand Up @@ -268,7 +252,7 @@ test-nomad: dev ## Run Nomad test suites
$(if $(ENABLE_RACE),-race) $(if $(VERBOSE),-v) \
-cover \
-timeout=15m \
-tags="$(if $(HAS_LXC),lxc)" ./... $(if $(VERBOSE), >test.log ; echo $$? > exit-code)
./... $(if $(VERBOSE), >test.log ; echo $$? > exit-code)
@if [ $(VERBOSE) ] ; then \
bash -C "$(PROJECT_ROOT)/scripts/test_check.sh" ; \
fi
Expand Down
13 changes: 6 additions & 7 deletions client/allocdir/alloc_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"syscall"
"testing"

cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -112,11 +111,11 @@ func TestAllocDir_MountSharedAlloc(t *testing.T) {

// Build 2 task dirs
td1 := d.NewTaskDir(t1.Name)
if err := td1.Build(false, nil, cstructs.FSIsolationChroot); err != nil {
if err := td1.Build(true, nil); err != nil {
t.Fatalf("error build task=%q dir: %v", t1.Name, err)
}
td2 := d.NewTaskDir(t2.Name)
if err := td2.Build(false, nil, cstructs.FSIsolationChroot); err != nil {
if err := td2.Build(true, nil); err != nil {
t.Fatalf("error build task=%q dir: %v", t2.Name, err)
}

Expand Down Expand Up @@ -157,11 +156,11 @@ func TestAllocDir_Snapshot(t *testing.T) {

// Build 2 task dirs
td1 := d.NewTaskDir(t1.Name)
if err := td1.Build(false, nil, cstructs.FSIsolationImage); err != nil {
if err := td1.Build(false, nil); err != nil {
t.Fatalf("error build task=%q dir: %v", t1.Name, err)
}
td2 := d.NewTaskDir(t2.Name)
if err := td2.Build(false, nil, cstructs.FSIsolationImage); err != nil {
if err := td2.Build(false, nil); err != nil {
t.Fatalf("error build task=%q dir: %v", t2.Name, err)
}

Expand Down Expand Up @@ -249,7 +248,7 @@ func TestAllocDir_Move(t *testing.T) {
defer d2.Destroy()

td1 := d1.NewTaskDir(t1.Name)
if err := td1.Build(false, nil, cstructs.FSIsolationImage); err != nil {
if err := td1.Build(false, nil); err != nil {
t.Fatalf("TaskDir.Build() faild: %v", err)
}

Expand Down Expand Up @@ -345,7 +344,7 @@ func TestAllocDir_ReadAt_SecretDir(t *testing.T) {
defer d.Destroy()

td := d.NewTaskDir(t1.Name)
if err := td.Build(false, nil, cstructs.FSIsolationImage); err != nil {
if err := td.Build(false, nil); err != nil {
t.Fatalf("TaskDir.Build() failed: %v", err)
}

Expand Down
21 changes: 7 additions & 14 deletions client/allocdir/task_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"path/filepath"

hclog "github.com/hashicorp/go-hclog"
cstructs "github.com/hashicorp/nomad/client/structs"
)

// TaskDir contains all of the paths relevant to a task. All paths are on the
Expand Down Expand Up @@ -75,7 +74,7 @@ func (t *TaskDir) Copy() *TaskDir {
// Build default directories and permissions in a task directory. chrootCreated
// allows skipping chroot creation if the caller knows it has already been
// done.
func (t *TaskDir) Build(chrootCreated bool, chroot map[string]string, fsi cstructs.FSIsolation) error {
func (t *TaskDir) Build(createChroot bool, chroot map[string]string) error {
if err := os.MkdirAll(t.Dir, 0777); err != nil {
return err
}
Expand Down Expand Up @@ -110,7 +109,7 @@ func (t *TaskDir) Build(chrootCreated bool, chroot map[string]string, fsi cstruc
// Image based isolation will bind the shared alloc dir in the driver.
// If there's no isolation the task will use the host path to the
// shared alloc dir.
if fsi == cstructs.FSIsolationChroot {
if createChroot {
// If the path doesn't exist OR it exists and is empty, link it
empty, _ := pathEmpty(t.SharedTaskDir)
if !pathExists(t.SharedTaskDir) || empty {
Expand All @@ -130,8 +129,8 @@ func (t *TaskDir) Build(chrootCreated bool, chroot map[string]string, fsi cstruc
}

// Build chroot if chroot filesystem isolation is going to be used
if fsi == cstructs.FSIsolationChroot {
if err := t.buildChroot(chrootCreated, chroot); err != nil {
if createChroot {
if err := t.buildChroot(chroot); err != nil {
return err
}
}
Expand All @@ -142,15 +141,9 @@ func (t *TaskDir) Build(chrootCreated bool, chroot map[string]string, fsi cstruc
// buildChroot takes a mapping of absolute directory or file paths on the host
// to their intended, relative location within the task directory. This
// attempts hardlink and then defaults to copying. If the path exists on the
// host and can't be embedded an error is returned. If chrootCreated is true
// skip expensive embedding operations and only ephemeral operations (eg
// mounting /dev) are done.
func (t *TaskDir) buildChroot(chrootCreated bool, entries map[string]string) error {
if !chrootCreated {
// Link/copy chroot entries
return t.embedDirs(entries)
}
return nil
// host and can't be embedded an error is returned.
func (t *TaskDir) buildChroot(entries map[string]string) error {
return t.embedDirs(entries)
}

func (t *TaskDir) embedDirs(entries map[string]string) error {
Expand Down
5 changes: 2 additions & 3 deletions client/allocdir/task_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"path/filepath"
"testing"

cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/helper/testlog"
)

Expand Down Expand Up @@ -104,7 +103,7 @@ func TestTaskDir_NonRoot_Image(t *testing.T) {
t.Fatalf("Build() failed: %v", err)
}

if err := td.Build(false, nil, cstructs.FSIsolationImage); err != nil {
if err := td.Build(false, nil); err != nil {
t.Fatalf("TaskDir.Build failed: %v", err)
}
}
Expand All @@ -127,7 +126,7 @@ func TestTaskDir_NonRoot(t *testing.T) {
t.Fatalf("Build() failed: %v", err)
}

if err := td.Build(false, nil, cstructs.FSIsolationNone); err != nil {
if err := td.Build(false, nil); err != nil {
t.Fatalf("TaskDir.Build failed: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion client/allocrunner/interfaces/task_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type TaskPoststartRequest struct {
DriverExec interfaces.ScriptExecutor

// Network info (may be nil)
DriverNetwork *cstructs.DriverNetwork
DriverNetwork *drivers.DriverNetwork

// TaskEnv is the task's environment
TaskEnv *taskenv.TaskEnv
Expand Down
7 changes: 3 additions & 4 deletions client/allocrunner/taskrunner/dispatch_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/golang/snappy"
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
Expand All @@ -34,7 +33,7 @@ func TestTaskRunner_DispatchHook_NoPayload(t *testing.T) {
alloc := mock.BatchAlloc()
task := alloc.Job.TaskGroups[0].Tasks[0]
taskDir := allocDir.NewTaskDir(task.Name)
require.NoError(taskDir.Build(false, nil, cstructs.FSIsolationNone))
require.NoError(taskDir.Build(false, nil))

h := newDispatchHook(alloc, logger)

Expand Down Expand Up @@ -79,7 +78,7 @@ func TestTaskRunner_DispatchHook_Ok(t *testing.T) {
File: "out",
}
taskDir := allocDir.NewTaskDir(task.Name)
require.NoError(taskDir.Build(false, nil, cstructs.FSIsolationNone))
require.NoError(taskDir.Build(false, nil))

h := newDispatchHook(alloc, logger)

Expand Down Expand Up @@ -123,7 +122,7 @@ func TestTaskRunner_DispatchHook_Error(t *testing.T) {
File: "out",
}
taskDir := allocDir.NewTaskDir(task.Name)
require.NoError(taskDir.Build(false, nil, cstructs.FSIsolationNone))
require.NoError(taskDir.Build(false, nil))

h := newDispatchHook(alloc, logger)

Expand Down
6 changes: 3 additions & 3 deletions client/allocrunner/taskrunner/driver_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// NewDriverHandle returns a handle for task operations on a specific task
func NewDriverHandle(driver drivers.DriverPlugin, taskID string, task *structs.Task, net *cstructs.DriverNetwork) *DriverHandle {
func NewDriverHandle(driver drivers.DriverPlugin, taskID string, task *structs.Task, net *drivers.DriverNetwork) *DriverHandle {
return &DriverHandle{
driver: driver,
net: net,
Expand All @@ -23,7 +23,7 @@ func NewDriverHandle(driver drivers.DriverPlugin, taskID string, task *structs.T
// an api to perform driver operations on the task
type DriverHandle struct {
driver drivers.DriverPlugin
net *cstructs.DriverNetwork
net *drivers.DriverNetwork
task *structs.Task
taskID string
}
Expand Down Expand Up @@ -61,6 +61,6 @@ func (h *DriverHandle) Exec(timeout time.Duration, cmd string, args []string) ([
return res.Stdout, res.ExitResult.ExitCode, res.ExitResult.Err
}

func (h *DriverHandle) Network() *cstructs.DriverNetwork {
func (h *DriverHandle) Network() *drivers.DriverNetwork {
return h.net
}
4 changes: 2 additions & 2 deletions client/allocrunner/taskrunner/service_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
tinterfaces "github.com/hashicorp/nomad/client/allocrunner/taskrunner/interfaces"
"github.com/hashicorp/nomad/client/consul"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/client/taskenv"
agentconsul "github.com/hashicorp/nomad/command/agent/consul"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
)

type serviceHookConfig struct {
Expand All @@ -37,7 +37,7 @@ type serviceHook struct {
// The following fields may be updated
delay time.Duration
driverExec tinterfaces.ScriptExecutor
driverNet *cstructs.DriverNetwork
driverNet *drivers.DriverNetwork
canary bool
services []*structs.Service
networks structs.Networks
Expand Down
3 changes: 1 addition & 2 deletions client/allocrunner/taskrunner/state/state.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package state

import (
"github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/plugins/drivers"
)
Expand All @@ -13,7 +12,7 @@ type LocalState struct {

// DriverNetwork is the network information returned by the task
// driver's Start method
DriverNetwork *structs.DriverNetwork
DriverNetwork *drivers.DriverNetwork

// TaskHandle is the handle used to reattach to the task during recovery
TaskHandle *drivers.TaskHandle
Expand Down
10 changes: 5 additions & 5 deletions client/allocrunner/taskrunner/task_dir_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
cconfig "github.com/hashicorp/nomad/client/config"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/client/taskenv"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
)

type taskDirHook struct {
Expand Down Expand Up @@ -44,7 +44,7 @@ func (h *taskDirHook) Prestart(ctx context.Context, req *interfaces.TaskPrestart

// Build the task directory structure
fsi := h.runner.driverCapabilities.FSIsolation
err := h.runner.taskDir.Build(false, chroot, fsi)
err := h.runner.taskDir.Build(fsi == drivers.FSIsolationChroot, chroot)
if err != nil {
return err
}
Expand All @@ -56,10 +56,10 @@ func (h *taskDirHook) Prestart(ctx context.Context, req *interfaces.TaskPrestart
}

// setEnvvars sets path and host env vars depending on the FS isolation used.
func setEnvvars(envBuilder *taskenv.Builder, fsi cstructs.FSIsolation, taskDir *allocdir.TaskDir, conf *cconfig.Config) {
func setEnvvars(envBuilder *taskenv.Builder, fsi drivers.FSIsolation, taskDir *allocdir.TaskDir, conf *cconfig.Config) {
// Set driver-specific environment variables
switch fsi {
case cstructs.FSIsolationNone:
case drivers.FSIsolationNone:
// Use host paths
envBuilder.SetAllocDir(taskDir.SharedAllocDir)
envBuilder.SetTaskLocalDir(taskDir.LocalDir)
Expand All @@ -72,7 +72,7 @@ func setEnvvars(envBuilder *taskenv.Builder, fsi cstructs.FSIsolation, taskDir *
}

// Set the host environment variables for non-image based drivers
if fsi != cstructs.FSIsolationImage {
if fsi != drivers.FSIsolationImage {
filter := strings.Split(conf.ReadDefault("env.blacklist", cconfig.DefaultEnvBlacklist), ",")
envBuilder.SetHostEnvvars(filter)
}
Expand Down
2 changes: 1 addition & 1 deletion client/allocrunner/taskrunner/task_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ func (tr *TaskRunner) Restore() error {
// restoreHandle ensures a TaskHandle is valid by calling Driver.RecoverTask
// and sets the driver handle. If the TaskHandle is not valid, DestroyTask is
// called.
func (tr *TaskRunner) restoreHandle(taskHandle *drivers.TaskHandle, net *cstructs.DriverNetwork) (success bool) {
func (tr *TaskRunner) restoreHandle(taskHandle *drivers.TaskHandle, net *drivers.DriverNetwork) (success bool) {
// Ensure handle is well-formed
if taskHandle.Config == nil {
return true
Expand Down
Loading