Skip to content

Commit 8ad5fd2

Browse files
committed
Merge pull request #610 from achanda/isolators
Add support for CPU and memory isolators
2 parents 50e2fbd + 84acb25 commit 8ad5fd2

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

client/driver/rkt.go

+42-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"syscall"
1515
"time"
1616

17+
"github.com/hashicorp/go-version"
1718
"github.com/hashicorp/nomad/client/allocdir"
1819
"github.com/hashicorp/nomad/client/config"
1920
cstructs "github.com/hashicorp/nomad/client/driver/structs"
@@ -28,6 +29,13 @@ var (
2829
reAppcVersion = regexp.MustCompile(`appc version (\d[.\d]+)`)
2930
)
3031

32+
const (
33+
// rkt added support for CPU and memory isolators in 0.14.0. We cannot support
34+
// an earlier version to maintain an uniform interface across all drivers
35+
minRktVersion = "0.14.0"
36+
conversionFactor = 1024 * 1024
37+
)
38+
3139
// RktDriver is a driver for running images via Rkt
3240
// We attempt to chose sane defaults for now, with more configuration available
3341
// planned in the future
@@ -85,6 +93,13 @@ func (d *RktDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool, e
8593
node.Attributes["driver.rkt.version"] = rktMatches[1]
8694
node.Attributes["driver.rkt.appc.version"] = appcMatches[1]
8795

96+
minVersion, _ := version.NewVersion(minRktVersion)
97+
currentVersion, _ := version.NewVersion(node.Attributes["driver.rkt.version"])
98+
if currentVersion.LessThan(minVersion) {
99+
// Do not allow rkt < 0.14.0
100+
d.logger.Printf("[WARN] driver.rkt: please upgrade rkt to a version >= %s", minVersion)
101+
node.Attributes["driver.rkt"] = "0"
102+
}
88103
return true, nil
89104
}
90105

@@ -109,21 +124,21 @@ func (d *RktDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, e
109124
taskLocal := filepath.Join(taskDir, allocdir.TaskLocal)
110125

111126
// Add the given trust prefix
112-
trust_prefix, trust_cmd := task.Config["trust_prefix"]
113-
if trust_cmd {
127+
trustPrefix, trustCmd := task.Config["trust_prefix"]
128+
if trustCmd {
114129
var outBuf, errBuf bytes.Buffer
115-
cmd := exec.Command("rkt", "trust", fmt.Sprintf("--prefix=%s", trust_prefix))
130+
cmd := exec.Command("rkt", "trust", fmt.Sprintf("--prefix=%s", trustPrefix))
116131
cmd.Stdout = &outBuf
117132
cmd.Stderr = &errBuf
118133
if err := cmd.Run(); err != nil {
119134
return nil, fmt.Errorf("Error running rkt trust: %s\n\nOutput: %s\n\nError: %s",
120135
err, outBuf.String(), errBuf.String())
121136
}
122-
d.logger.Printf("[DEBUG] driver.rkt: added trust prefix: %q", trust_prefix)
137+
d.logger.Printf("[DEBUG] driver.rkt: added trust prefix: %q", trustPrefix)
123138
}
124139

125140
// Build the command.
126-
var cmd_args []string
141+
var cmdArgs []string
127142

128143
// Inject the environment variables.
129144
envVars := TaskEnvironmentVariables(ctx, task)
@@ -133,33 +148,46 @@ func (d *RktDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, e
133148
envVars.ClearAllocDir()
134149

135150
for k, v := range envVars.Map() {
136-
cmd_args = append(cmd_args, fmt.Sprintf("--set-env=%v=%v", k, v))
151+
cmdArgs = append(cmdArgs, fmt.Sprintf("--set-env=%v=%v", k, v))
137152
}
138153

139154
// Disble signature verification if the trust command was not run.
140-
if !trust_cmd {
141-
cmd_args = append(cmd_args, "--insecure-skip-verify")
155+
if !trustCmd {
156+
cmdArgs = append(cmdArgs, "--insecure-skip-verify")
142157
}
143158

144159
// Append the run command.
145-
cmd_args = append(cmd_args, "run", "--mds-register=false", img)
160+
cmdArgs = append(cmdArgs, "run", "--mds-register=false", img)
146161

147162
// Check if the user has overriden the exec command.
148-
if exec_cmd, ok := task.Config["command"]; ok {
149-
cmd_args = append(cmd_args, fmt.Sprintf("--exec=%v", exec_cmd))
163+
if execCmd, ok := task.Config["command"]; ok {
164+
cmdArgs = append(cmdArgs, fmt.Sprintf("--exec=%v", execCmd))
165+
}
166+
167+
if task.Resources.MemoryMB == 0 {
168+
return nil, fmt.Errorf("Memory limit cannot be zero")
169+
}
170+
if task.Resources.CPU == 0 {
171+
return nil, fmt.Errorf("CPU limit cannot be zero")
150172
}
151173

174+
// Add memory isolator
175+
cmdArgs = append(cmdArgs, fmt.Sprintf("--memory=%vM", int64(task.Resources.MemoryMB)*conversionFactor))
176+
177+
// Add CPU isolator
178+
cmdArgs = append(cmdArgs, fmt.Sprintf("--cpu=%vm", int64(task.Resources.CPU)))
179+
152180
// Add user passed arguments.
153181
if len(driverConfig.Args) != 0 {
154182
parsed := args.ParseAndReplace(driverConfig.Args, envVars.Map())
155183

156184
// Need to start arguments with "--"
157185
if len(parsed) > 0 {
158-
cmd_args = append(cmd_args, "--")
186+
cmdArgs = append(cmdArgs, "--")
159187
}
160188

161189
for _, arg := range parsed {
162-
cmd_args = append(cmd_args, fmt.Sprintf("%v", arg))
190+
cmdArgs = append(cmdArgs, fmt.Sprintf("%v", arg))
163191
}
164192
}
165193

@@ -177,7 +205,7 @@ func (d *RktDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, e
177205
return nil, fmt.Errorf("Error opening file to redirect stderr: %v", err)
178206
}
179207

180-
cmd := exec.Command("rkt", cmd_args...)
208+
cmd := exec.Command("rkt", cmdArgs...)
181209
cmd.Stdout = stdo
182210
cmd.Stderr = stde
183211

client/driver/rkt_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func TestRktDriver_Start(t *testing.T) {
8181
"image": "coreos.com/etcd:v2.0.4",
8282
"command": "/etcd",
8383
},
84+
Resources: &structs.Resources{
85+
MemoryMB: 256,
86+
CPU: 512,
87+
},
8488
}
8589

8690
driverCtx := testDriverContext(task.Name)
@@ -121,6 +125,10 @@ func TestRktDriver_Start_Wait(t *testing.T) {
121125
"command": "/etcd",
122126
"args": []string{"--version"},
123127
},
128+
Resources: &structs.Resources{
129+
MemoryMB: 256,
130+
CPU: 512,
131+
},
124132
}
125133

126134
driverCtx := testDriverContext(task.Name)
@@ -162,6 +170,10 @@ func TestRktDriver_Start_Wait_Skip_Trust(t *testing.T) {
162170
"command": "/etcd",
163171
"args": []string{"--version"},
164172
},
173+
Resources: &structs.Resources{
174+
MemoryMB: 256,
175+
CPU: 512,
176+
},
165177
}
166178

167179
driverCtx := testDriverContext(task.Name)
@@ -204,6 +216,10 @@ func TestRktDriver_Start_Wait_Logs(t *testing.T) {
204216
"command": "/etcd",
205217
"args": []string{"--version"},
206218
},
219+
Resources: &structs.Resources{
220+
MemoryMB: 256,
221+
CPU: 512,
222+
},
207223
}
208224

209225
driverCtx := testDriverContext(task.Name)

0 commit comments

Comments
 (0)