Skip to content

Commit 56b2b36

Browse files
authored
Merge pull request #1678 from hashicorp/revert-1671-f-secret-dir2
Revert "Introduce a Secret/ directory"
2 parents e2c1798 + 335caba commit 56b2b36

26 files changed

+80
-529
lines changed

client/alloc_runner.go

+9-18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/hashicorp/nomad/client/allocdir"
1313
"github.com/hashicorp/nomad/client/config"
1414
"github.com/hashicorp/nomad/client/driver"
15-
"github.com/hashicorp/nomad/client/secretdir"
1615
"github.com/hashicorp/nomad/nomad/structs"
1716

1817
cstructs "github.com/hashicorp/nomad/client/structs"
@@ -52,20 +51,17 @@ type AllocRunner struct {
5251

5352
dirtyCh chan struct{}
5453

55-
ctx *driver.ExecContext
56-
ctxLock sync.Mutex
54+
ctx *driver.ExecContext
55+
ctxLock sync.Mutex
56+
tasks map[string]*TaskRunner
57+
taskStates map[string]*structs.TaskState
58+
restored map[string]struct{}
59+
taskLock sync.RWMutex
5760

58-
tasks map[string]*TaskRunner
59-
restored map[string]struct{}
60-
taskLock sync.RWMutex
61-
62-
taskStates map[string]*structs.TaskState
6361
taskStatusLock sync.RWMutex
6462

6563
updateCh chan *structs.Allocation
6664

67-
secretDir secretdir.SecretDirectory
68-
6965
destroy bool
7066
destroyCh chan struct{}
7167
destroyLock sync.Mutex
@@ -84,13 +80,12 @@ type allocRunnerState struct {
8480

8581
// NewAllocRunner is used to create a new allocation context
8682
func NewAllocRunner(logger *log.Logger, config *config.Config, updater AllocStateUpdater,
87-
alloc *structs.Allocation, secretDir secretdir.SecretDirectory) *AllocRunner {
83+
alloc *structs.Allocation) *AllocRunner {
8884
ar := &AllocRunner{
8985
config: config,
9086
updater: updater,
9187
logger: logger,
9288
alloc: alloc,
93-
secretDir: secretDir,
9489
dirtyCh: make(chan struct{}, 1),
9590
tasks: make(map[string]*TaskRunner),
9691
taskStates: copyTaskStates(alloc.TaskStates),
@@ -131,8 +126,6 @@ func (r *AllocRunner) RestoreState() error {
131126
}
132127
if r.ctx == nil {
133128
snapshotErrors.Errors = append(snapshotErrors.Errors, fmt.Errorf("alloc_runner snapshot includes a nil context"))
134-
} else {
135-
r.ctx.AllocDir.SetSecretDirFn(r.secretDir.CreateFor)
136129
}
137130
if e := snapshotErrors.ErrorOrNil(); e != nil {
138131
return e
@@ -393,11 +386,9 @@ func (r *AllocRunner) Run() {
393386
// Create the execution context
394387
r.ctxLock.Lock()
395388
if r.ctx == nil {
396-
path := filepath.Join(r.config.AllocDir, r.alloc.ID)
397-
size := r.Alloc().Resources.DiskMB
398-
allocDir := allocdir.NewAllocDir(r.alloc.ID, path, size, r.secretDir.CreateFor)
389+
allocDir := allocdir.NewAllocDir(filepath.Join(r.config.AllocDir, r.alloc.ID), r.Alloc().Resources.DiskMB)
399390
if err := allocDir.Build(tg.Tasks); err != nil {
400-
r.logger.Printf("[ERR] client: failed to build task directories: %v", err)
391+
r.logger.Printf("[WARN] client: failed to build task directories: %v", err)
401392
r.setStatus(structs.AllocClientStatusFailed, fmt.Sprintf("failed to build task dirs for '%s'", alloc.TaskGroup))
402393
r.ctxLock.Unlock()
403394
return

client/alloc_runner_test.go

+15-16
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/hashicorp/nomad/testutil"
1313

1414
"github.com/hashicorp/nomad/client/config"
15-
"github.com/hashicorp/nomad/client/secretdir"
1615
ctestutil "github.com/hashicorp/nomad/client/testutil"
1716
)
1817

@@ -26,7 +25,7 @@ func (m *MockAllocStateUpdater) Update(alloc *structs.Allocation) {
2625
m.Allocs = append(m.Allocs, alloc)
2726
}
2827

29-
func testAllocRunnerFromAlloc(t *testing.T, alloc *structs.Allocation, restarts bool) (*MockAllocStateUpdater, *AllocRunner) {
28+
func testAllocRunnerFromAlloc(alloc *structs.Allocation, restarts bool) (*MockAllocStateUpdater, *AllocRunner) {
3029
logger := testLogger()
3130
conf := config.DefaultConfig()
3231
conf.StateDir = os.TempDir()
@@ -36,17 +35,17 @@ func testAllocRunnerFromAlloc(t *testing.T, alloc *structs.Allocation, restarts
3635
*alloc.Job.LookupTaskGroup(alloc.TaskGroup).RestartPolicy = structs.RestartPolicy{Attempts: 0}
3736
alloc.Job.Type = structs.JobTypeBatch
3837
}
39-
ar := NewAllocRunner(logger, conf, upd.Update, alloc, secretdir.NewTestSecretDir(t))
38+
ar := NewAllocRunner(logger, conf, upd.Update, alloc)
4039
return upd, ar
4140
}
4241

43-
func testAllocRunner(t *testing.T, restarts bool) (*MockAllocStateUpdater, *AllocRunner) {
44-
return testAllocRunnerFromAlloc(t, mock.Alloc(), restarts)
42+
func testAllocRunner(restarts bool) (*MockAllocStateUpdater, *AllocRunner) {
43+
return testAllocRunnerFromAlloc(mock.Alloc(), restarts)
4544
}
4645

4746
func TestAllocRunner_SimpleRun(t *testing.T) {
4847
ctestutil.ExecCompatible(t)
49-
upd, ar := testAllocRunner(t, false)
48+
upd, ar := testAllocRunner(false)
5049
go ar.Run()
5150
defer ar.Destroy()
5251

@@ -83,7 +82,7 @@ func TestAllocRunner_RetryArtifact(t *testing.T) {
8382
}
8483

8584
alloc.Job.TaskGroups[0].Tasks = append(alloc.Job.TaskGroups[0].Tasks, badtask)
86-
upd, ar := testAllocRunnerFromAlloc(t, alloc, true)
85+
upd, ar := testAllocRunnerFromAlloc(alloc, true)
8786
go ar.Run()
8887
defer ar.Destroy()
8988

@@ -119,7 +118,7 @@ func TestAllocRunner_RetryArtifact(t *testing.T) {
119118

120119
func TestAllocRunner_TerminalUpdate_Destroy(t *testing.T) {
121120
ctestutil.ExecCompatible(t)
122-
upd, ar := testAllocRunner(t, false)
121+
upd, ar := testAllocRunner(false)
123122

124123
// Ensure task takes some time
125124
task := ar.alloc.Job.TaskGroups[0].Tasks[0]
@@ -208,7 +207,7 @@ func TestAllocRunner_TerminalUpdate_Destroy(t *testing.T) {
208207

209208
func TestAllocRunner_DiskExceeded_Destroy(t *testing.T) {
210209
ctestutil.ExecCompatible(t)
211-
upd, ar := testAllocRunner(t, false)
210+
upd, ar := testAllocRunner(false)
212211

213212
// Ensure task takes some time
214213
task := ar.alloc.Job.TaskGroups[0].Tasks[0]
@@ -314,7 +313,7 @@ func TestAllocRunner_DiskExceeded_Destroy(t *testing.T) {
314313
}
315314
func TestAllocRunner_Destroy(t *testing.T) {
316315
ctestutil.ExecCompatible(t)
317-
upd, ar := testAllocRunner(t, false)
316+
upd, ar := testAllocRunner(false)
318317

319318
// Ensure task takes some time
320319
task := ar.alloc.Job.TaskGroups[0].Tasks[0]
@@ -366,7 +365,7 @@ func TestAllocRunner_Destroy(t *testing.T) {
366365

367366
func TestAllocRunner_Update(t *testing.T) {
368367
ctestutil.ExecCompatible(t)
369-
_, ar := testAllocRunner(t, false)
368+
_, ar := testAllocRunner(false)
370369

371370
// Ensure task takes some time
372371
task := ar.alloc.Job.TaskGroups[0].Tasks[0]
@@ -392,7 +391,7 @@ func TestAllocRunner_Update(t *testing.T) {
392391

393392
func TestAllocRunner_SaveRestoreState(t *testing.T) {
394393
ctestutil.ExecCompatible(t)
395-
upd, ar := testAllocRunner(t, false)
394+
upd, ar := testAllocRunner(false)
396395

397396
// Ensure task takes some time
398397
task := ar.alloc.Job.TaskGroups[0].Tasks[0]
@@ -414,7 +413,7 @@ func TestAllocRunner_SaveRestoreState(t *testing.T) {
414413

415414
// Create a new alloc runner
416415
ar2 := NewAllocRunner(ar.logger, ar.config, upd.Update,
417-
&structs.Allocation{ID: ar.alloc.ID}, ar.secretDir)
416+
&structs.Allocation{ID: ar.alloc.ID})
418417
err = ar2.RestoreState()
419418
if err != nil {
420419
t.Fatalf("err: %v", err)
@@ -442,7 +441,7 @@ func TestAllocRunner_SaveRestoreState(t *testing.T) {
442441

443442
func TestAllocRunner_SaveRestoreState_TerminalAlloc(t *testing.T) {
444443
ctestutil.ExecCompatible(t)
445-
upd, ar := testAllocRunner(t, false)
444+
upd, ar := testAllocRunner(false)
446445
ar.logger = prefixedTestLogger("ar1: ")
447446

448447
// Ensure task takes some time
@@ -486,7 +485,7 @@ func TestAllocRunner_SaveRestoreState_TerminalAlloc(t *testing.T) {
486485

487486
// Create a new alloc runner
488487
ar2 := NewAllocRunner(ar.logger, ar.config, upd.Update,
489-
&structs.Allocation{ID: ar.alloc.ID}, ar.secretDir)
488+
&structs.Allocation{ID: ar.alloc.ID})
490489
ar2.logger = prefixedTestLogger("ar2: ")
491490
err = ar2.RestoreState()
492491
if err != nil {
@@ -548,7 +547,7 @@ func TestAllocRunner_SaveRestoreState_TerminalAlloc(t *testing.T) {
548547

549548
func TestAllocRunner_TaskFailed_KillTG(t *testing.T) {
550549
ctestutil.ExecCompatible(t)
551-
upd, ar := testAllocRunner(t, false)
550+
upd, ar := testAllocRunner(false)
552551

553552
// Create two tasks in the task group
554553
task := ar.alloc.Job.TaskGroups[0].Tasks[0]

client/allocdir/alloc_dir.go

+4-57
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,11 @@ var (
4646
// regardless of driver.
4747
TaskLocal = "local"
4848

49-
// TaskSecrets is the the name of the secret directory inside each task
50-
// directory
51-
TaskSecrets = "secrets"
52-
5349
// TaskDirs is the set of directories created in each tasks directory.
5450
TaskDirs = []string{"tmp"}
5551
)
5652

57-
type CreateSecretDirFn func(allocID, task string) (path string, err error)
58-
5953
type AllocDir struct {
60-
// AllocID is the allocation ID for this directory
61-
AllocID string
62-
63-
// createSecretDirFn is used to create a secret directory and retrieve the
64-
// path to it so that it can be mounted in the task directory.
65-
createSecretDirFn CreateSecretDirFn
66-
6754
// AllocDir is the directory used for storing any state
6855
// of this allocation. It will be purged on alloc destroy.
6956
AllocDir string
@@ -122,30 +109,20 @@ type AllocDirFS interface {
122109
}
123110

124111
// NewAllocDir initializes the AllocDir struct with allocDir as base path for
125-
// the allocation directory and maxSize as the maximum allowed size in
126-
// megabytes. The secretDirFn is used to create secret directories and get their
127-
// path which will then be mounted into the task directory
128-
func NewAllocDir(allocID, allocDir string, maxSize int, secretDirFn CreateSecretDirFn) *AllocDir {
112+
// the allocation directory and maxSize as the maximum allowed size in megabytes.
113+
func NewAllocDir(allocDir string, maxSize int) *AllocDir {
129114
d := &AllocDir{
130-
AllocID: allocID,
131115
AllocDir: allocDir,
132116
MaxCheckDiskInterval: maxCheckDiskInterval,
133117
MinCheckDiskInterval: minCheckDiskInterval,
134118
CheckDiskMaxEnforcePeriod: checkDiskMaxEnforcePeriod,
135119
TaskDirs: make(map[string]string),
136120
MaxSize: maxSize,
137-
createSecretDirFn: secretDirFn,
138121
}
139122
d.SharedDir = filepath.Join(d.AllocDir, SharedAllocName)
140123
return d
141124
}
142125

143-
// SetSecretDirFn is used to set the function used to create secret
144-
// directories.
145-
func (d *AllocDir) SetSecretDirFn(fn CreateSecretDirFn) {
146-
d.createSecretDirFn = fn
147-
}
148-
149126
// Tears down previously build directory structure.
150127
func (d *AllocDir) Destroy() error {
151128

@@ -168,7 +145,7 @@ func (d *AllocDir) UnmountAll() error {
168145
// Check if the directory has the shared alloc mounted.
169146
taskAlloc := filepath.Join(dir, SharedAllocName)
170147
if d.pathExists(taskAlloc) {
171-
if err := d.unmount(taskAlloc); err != nil {
148+
if err := d.unmountSharedDir(taskAlloc); err != nil {
172149
mErr.Errors = append(mErr.Errors,
173150
fmt.Errorf("failed to unmount shared alloc dir %q: %v", taskAlloc, err))
174151
} else if err := os.RemoveAll(taskAlloc); err != nil {
@@ -177,18 +154,6 @@ func (d *AllocDir) UnmountAll() error {
177154
}
178155
}
179156

180-
// Remove the secrets dir
181-
taskSecrets := filepath.Join(dir, TaskSecrets)
182-
if d.pathExists(taskSecrets) {
183-
if err := d.unmount(taskSecrets); err != nil {
184-
mErr.Errors = append(mErr.Errors,
185-
fmt.Errorf("failed to unmount secrets dir %q: %v", taskSecrets, err))
186-
} else if err := os.RemoveAll(taskSecrets); err != nil {
187-
mErr.Errors = append(mErr.Errors,
188-
fmt.Errorf("failed to delete secrets dir %q: %v", taskSecrets, err))
189-
}
190-
}
191-
192157
// Unmount dev/ and proc/ have been mounted.
193158
d.unmountSpecialDirs(dir)
194159
}
@@ -258,24 +223,6 @@ func (d *AllocDir) Build(tasks []*structs.Task) error {
258223
return err
259224
}
260225
}
261-
262-
// Get the secret directory
263-
if d.createSecretDirFn != nil {
264-
sdir, err := d.createSecretDirFn(d.AllocID, t.Name)
265-
if err != nil {
266-
return fmt.Errorf("Creating secret directory for task %q failed: %v", t.Name, err)
267-
}
268-
269-
// Mount the secret directory
270-
taskSecret := filepath.Join(taskDir, TaskSecrets)
271-
if err := d.mount(sdir, taskSecret); err != nil {
272-
return fmt.Errorf("failed to mount secret directory for task %q: %v", t.Name, err)
273-
}
274-
275-
if err := d.dropDirPermissions(taskSecret); err != nil {
276-
return err
277-
}
278-
}
279226
}
280227

281228
return nil
@@ -385,7 +332,7 @@ func (d *AllocDir) MountSharedDir(task string) error {
385332
}
386333

387334
taskLoc := filepath.Join(taskDir, SharedAllocName)
388-
if err := d.mount(d.SharedDir, taskLoc); err != nil {
335+
if err := d.mountSharedDir(taskLoc); err != nil {
389336
return fmt.Errorf("Failed to mount shared directory for task %v: %v", task, err)
390337
}
391338

client/allocdir/alloc_dir_darwin.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"syscall"
55
)
66

7-
// Hardlinks the shared directory. As a side-effect the src and dest directory
8-
// must be on the same filesystem.
9-
func (d *AllocDir) mount(src, dest string) error {
10-
return syscall.Link(src, dest)
7+
// Hardlinks the shared directory. As a side-effect the shared directory and
8+
// task directory must be on the same filesystem.
9+
func (d *AllocDir) mountSharedDir(dir string) error {
10+
return syscall.Link(d.SharedDir, dir)
1111
}
1212

13-
func (d *AllocDir) unmount(dir string) error {
13+
func (d *AllocDir) unmountSharedDir(dir string) error {
1414
return syscall.Unlink(dir)
1515
}
1616

client/allocdir/alloc_dir_freebsd.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"syscall"
55
)
66

7-
// Hardlinks the shared directory. As a side-effect the src and dest directory
8-
// must be on the same filesystem.
9-
func (d *AllocDir) mount(src, dest string) error {
10-
return syscall.Link(src, dest)
7+
// Hardlinks the shared directory. As a side-effect the shared directory and
8+
// task directory must be on the same filesystem.
9+
func (d *AllocDir) mountSharedDir(dir string) error {
10+
return syscall.Link(d.SharedDir, dir)
1111
}
1212

13-
func (d *AllocDir) unmount(dir string) error {
13+
func (d *AllocDir) unmountSharedDir(dir string) error {
1414
return syscall.Unlink(dir)
1515
}
1616

client/allocdir/alloc_dir_linux.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ import (
99
"github.com/hashicorp/go-multierror"
1010
)
1111

12-
func (d *AllocDir) mount(src, dest string) error {
13-
if err := os.MkdirAll(dest, 0777); err != nil {
12+
// Bind mounts the shared directory into the task directory. Must be root to
13+
// run.
14+
func (d *AllocDir) mountSharedDir(taskDir string) error {
15+
if err := os.MkdirAll(taskDir, 0777); err != nil {
1416
return err
1517
}
1618

17-
return syscall.Mount(src, dest, "", syscall.MS_BIND, "")
19+
return syscall.Mount(d.SharedDir, taskDir, "", syscall.MS_BIND, "")
1820
}
1921

20-
func (d *AllocDir) unmount(dir string) error {
22+
func (d *AllocDir) unmountSharedDir(dir string) error {
2123
return syscall.Unmount(dir, 0)
2224
}
2325

0 commit comments

Comments
 (0)