This commit is contained in:
Alex Dadgar 2018-12-13 16:21:41 -08:00
parent b653ae2af7
commit 327b551b39
13 changed files with 186 additions and 99 deletions

View file

@ -697,15 +697,14 @@ func (tr *TaskRunner) buildTaskConfig() *drivers.TaskConfig {
task := tr.Task()
alloc := tr.Alloc()
invocationid := uuid.Generate()[:8]
// TODO This shouldn't use the task resources
taskResources := tr.taskResources
return &drivers.TaskConfig{
ID: fmt.Sprintf("%s/%s/%s", alloc.ID, task.Name, invocationid),
Name: task.Name,
JobName: alloc.Job.Name,
Resources: &drivers.Resources{
NomadResources: task.Resources,
NomadResources: taskResources,
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: int64(task.Resources.MemoryMB) * 1024 * 1024,
CPUShares: int64(task.Resources.CPU),

View file

@ -17,8 +17,6 @@ import (
"testing"
"time"
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
docker "github.com/fsouza/go-dockerclient"
"github.com/hashicorp/consul/lib/freeport"
hclog "github.com/hashicorp/go-hclog"
@ -30,6 +28,7 @@ import (
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/base"
"github.com/hashicorp/nomad/plugins/drivers"
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
"github.com/hashicorp/nomad/plugins/shared/loader"
tu "github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/assert"
@ -38,10 +37,13 @@ import (
var (
basicResources = &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 256,
CPU: 512,
DiskMB: 20,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 256,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 250,
},
},
LinuxResources: &drivers.LinuxResources{
CPUShares: 512,
@ -96,9 +98,13 @@ func dockerTask(t *testing.T) (*drivers.TaskConfig, *TaskConfig, []int) {
ID: uuid.Generate(),
Name: "redis-demo",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 256,
CPU: 512,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 256,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 512,
},
Networks: []*structs.NetworkResource{
{
IP: "127.0.0.1",
@ -2259,7 +2265,7 @@ func TestDockerDriver_OOMKilled(t *testing.T) {
Resources: basicResources,
}
task.Resources.LinuxResources.MemoryLimitBytes = 10 * 1024 * 1024
task.Resources.NomadResources.MemoryMB = 10
task.Resources.NomadResources.Memory.MemoryMB = 10
require.NoError(t, task.EncodeConcreteDriverConfig(&taskCfg))

View file

@ -35,9 +35,13 @@ func TestMain(m *testing.M) {
}
var testResources = &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,

View file

@ -249,9 +249,13 @@ func basicTask(t *testing.T, name string, taskConfig map[string]interface{}) *dr
ID: uuid.Generate(),
Name: name,
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,

View file

@ -102,9 +102,13 @@ func TestLXCDriver_Start_Wait(t *testing.T) {
ID: uuid.Generate(),
Name: "test",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
CPU: 1,
MemoryMB: 2,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 2,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 1024,
},
},
LinuxResources: &drivers.LinuxResources{
CPUShares: 1024,
@ -196,9 +200,13 @@ func TestLXCDriver_Start_Stop(t *testing.T) {
ID: uuid.Generate(),
Name: "test",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
CPU: 1,
MemoryMB: 2,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 2,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 1024,
},
},
LinuxResources: &drivers.LinuxResources{
CPUShares: 1024,

View file

@ -209,7 +209,7 @@ func (d *Driver) formatMount(hostPath, taskPath string, readOnly bool) string {
}
func (d *Driver) setResourceLimits(c *lxc.Container, cfg *drivers.TaskConfig) error {
if err := c.SetMemoryLimit(lxc.ByteSize(cfg.Resources.NomadResources.MemoryMB) * lxc.MB); err != nil {
if err := c.SetMemoryLimit(lxc.ByteSize(cfg.Resources.NomadResources.Memory.MemoryMB) * lxc.MB); err != nil {
return fmt.Errorf("unable to set memory limits: %v", err)
}

View file

@ -19,9 +19,13 @@ func TestLXCDriver_Mounts(t *testing.T) {
ID: uuid.Generate(),
Name: "test",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
CPU: 1,
MemoryMB: 2,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 2,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 1024,
},
},
LinuxResources: &drivers.LinuxResources{
CPUShares: 1024,

View file

@ -318,10 +318,11 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *cstru
accelerator = driverConfig.Accelerator
}
if cfg.Resources.NomadResources.MemoryMB < 128 || cfg.Resources.NomadResources.MemoryMB > 4000000 {
mb := cfg.Resources.NomadResources.Memory.MemoryMB
if mb < 128 || mb > 4000000 {
return nil, nil, fmt.Errorf("Qemu memory assignment out of bounds")
}
mem := fmt.Sprintf("%dM", cfg.Resources.NomadResources.MemoryMB)
mem := fmt.Sprintf("%dM", mb)
absPath, err := GetAbsolutePath("qemu-system-x86_64")
if err != nil {

View file

@ -1,24 +1,22 @@
package qemu
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"context"
"fmt"
"time"
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
"github.com/hashicorp/hcl2/hcl"
ctestutil "github.com/hashicorp/nomad/client/testutil"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/plugins/drivers"
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
"github.com/hashicorp/nomad/plugins/shared"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
@ -44,9 +42,13 @@ func TestQemuDriver_Start_Wait_Stop(t *testing.T) {
ID: uuid.Generate(),
Name: "linux",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 512,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 512,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
Networks: []*structs.NetworkResource{
{
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
@ -102,9 +104,13 @@ func TestQemuDriver_GetMonitorPathOldQemu(t *testing.T) {
ID: uuid.Generate(),
Name: "linux",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 512,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 512,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
Networks: []*structs.NetworkResource{
{
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
@ -153,9 +159,13 @@ func TestQemuDriver_GetMonitorPathNewQemu(t *testing.T) {
ID: uuid.Generate(),
Name: "linux",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 512,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 512,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
Networks: []*structs.NetworkResource{
{
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
@ -243,9 +253,13 @@ func TestQemuDriver_User(t *testing.T) {
Name: "linux",
User: "alice",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 512,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 512,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
Networks: []*structs.NetworkResource{
{
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},
@ -295,9 +309,13 @@ func TestQemuDriver_Stats(t *testing.T) {
ID: uuid.Generate(),
Name: "linux",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 512,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 512,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
Networks: []*structs.NetworkResource{
{
ReservedPorts: []structs.Port{{Label: "main", Value: 22000}, {Label: "web", Value: 80}},

View file

@ -13,9 +13,6 @@ import (
"testing"
"time"
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
"golang.org/x/sys/unix"
"github.com/hashicorp/hcl2/hcl"
ctestutil "github.com/hashicorp/nomad/client/testutil"
"github.com/hashicorp/nomad/helper/testlog"
@ -24,10 +21,12 @@ import (
"github.com/hashicorp/nomad/nomad/structs"
basePlug "github.com/hashicorp/nomad/plugins/base"
"github.com/hashicorp/nomad/plugins/drivers"
dtestutil "github.com/hashicorp/nomad/plugins/drivers/testutils"
"github.com/hashicorp/nomad/plugins/shared"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)
var _ drivers.DriverPlugin = (*Driver)(nil)
@ -93,9 +92,13 @@ func TestRktDriver_Start_Wait_Stop_DNS(t *testing.T) {
AllocID: uuid.Generate(),
Name: "etcd",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,
@ -173,9 +176,13 @@ func TestRktDriver_Start_Wait_Stop(t *testing.T) {
AllocID: uuid.Generate(),
Name: "etcd",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,
@ -228,9 +235,13 @@ func TestRktDriver_Start_Wait_Skip_Trust(t *testing.T) {
AllocID: uuid.Generate(),
Name: "etcd",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,
@ -283,9 +294,13 @@ func TestRktDriver_InvalidTrustPrefix(t *testing.T) {
AllocID: uuid.Generate(),
Name: "etcd",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,
@ -334,9 +349,13 @@ func TestRktDriver_StartWaitRecoverWaitStop(t *testing.T) {
AllocID: uuid.Generate(),
Name: "etcd",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,
@ -434,9 +453,13 @@ func TestRktDriver_Start_Wait_Volume(t *testing.T) {
AllocID: uuid.Generate(),
Name: "rkttest_alpine",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,
@ -518,9 +541,13 @@ func TestRktDriver_Start_Wait_TaskMounts(t *testing.T) {
AllocID: uuid.Generate(),
Name: "rkttest_alpine",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,
@ -586,9 +613,13 @@ func TestRktDriver_PortMapping(t *testing.T) {
AllocID: uuid.Generate(),
Name: "redis",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
Networks: []*structs.NetworkResource{
{
IP: "127.0.0.1",
@ -640,9 +671,13 @@ func TestRktDriver_UserGroup(t *testing.T) {
User: "nobody",
Name: "rkttest_alpine",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,
@ -702,9 +737,13 @@ func TestRktDriver_Exec(t *testing.T) {
AllocID: uuid.Generate(),
Name: "etcd",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,
@ -780,9 +819,13 @@ func TestRktDriver_Stats(t *testing.T) {
AllocID: uuid.Generate(),
Name: "etcd",
Resources: &drivers.Resources{
NomadResources: &structs.Resources{
MemoryMB: 128,
CPU: 100,
NomadResources: &structs.AllocatedTaskResources{
Memory: structs.AllocatedMemoryResources{
MemoryMB: 128,
},
Cpu: structs.AllocatedCpuResources{
CpuShares: 100,
},
},
LinuxResources: &drivers.LinuxResources{
MemoryLimitBytes: 134217728,

View file

@ -105,7 +105,7 @@ func (l *LibcontainerExecutor) Launch(command *ExecCommand) (*ProcessState, erro
if command.Resources == nil {
command.Resources = &drivers.Resources{
NomadResources: &structs.Resources{},
NomadResources: &structs.AllocatedTaskResources{},
}
}
@ -578,20 +578,21 @@ func configureCgroups(cfg *lconfigs.Config, command *ExecCommand) error {
return nil
}
if command.Resources.NomadResources.MemoryMB > 0 {
if mb := command.Resources.NomadResources.Memory.MemoryMB; mb > 0 {
// Total amount of memory allowed to consume
cfg.Cgroups.Resources.Memory = int64(command.Resources.NomadResources.MemoryMB * 1024 * 1024)
cfg.Cgroups.Resources.Memory = int64(mb * 1024 * 1024)
// Disable swap to avoid issues on the machine
var memSwappiness uint64
cfg.Cgroups.Resources.MemorySwappiness = &memSwappiness
}
if command.Resources.NomadResources.CPU < 2 {
return fmt.Errorf("resources.CPU must be equal to or greater than 2: %v", command.Resources.NomadResources.CPU)
cpuShares := command.Resources.NomadResources.Cpu.CpuShares
if cpuShares < 2 {
return fmt.Errorf("resources.Cpu.CpuShares must be equal to or greater than 2: %v", cpuShares)
}
// Set the relative CPU shares for this cgroup.
cfg.Cgroups.Resources.CpuShares = uint64(command.Resources.NomadResources.CPU)
cfg.Cgroups.Resources.CpuShares = uint64(cpuShares)
return nil
}

View file

@ -69,7 +69,7 @@ func testExecutorCommandWithChroot(t *testing.T) (*ExecCommand, *allocdir.AllocD
Env: taskEnv.List(),
TaskDir: td.Dir,
Resources: &drivers.Resources{
NomadResources: task.Resources,
NomadResources: alloc.AllocatedResources.Tasks[task.Name],
},
}
configureTLogging(cmd)
@ -109,7 +109,7 @@ func TestExecutor_IsolationAndConstraints(t *testing.T) {
data, err := ioutil.ReadFile(memLimits)
require.NoError(err)
expectedMemLim := strconv.Itoa(execCmd.Resources.NomadResources.MemoryMB * 1024 * 1024)
expectedMemLim := strconv.Itoa(int(execCmd.Resources.NomadResources.Memory.MemoryMB * 1024 * 1024))
actualMemLim := strings.TrimSpace(string(data))
require.Equal(actualMemLim, expectedMemLim)
require.NoError(executor.Shutdown("", 0))

View file

@ -13,15 +13,14 @@ import (
"testing"
"time"
"github.com/hashicorp/nomad/plugins/drivers"
tu "github.com/hashicorp/nomad/testutil"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/client/allocdir"
cstructs "github.com/hashicorp/nomad/client/structs"
"github.com/hashicorp/nomad/client/taskenv"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/plugins/drivers"
tu "github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/require"
)
@ -55,7 +54,7 @@ func testExecutorCommand(t *testing.T) (*ExecCommand, *allocdir.AllocDir) {
Env: taskEnv.List(),
TaskDir: td.Dir,
Resources: &drivers.Resources{
NomadResources: task.Resources,
NomadResources: alloc.AllocatedResources.Tasks[task.Name],
},
}